aboutsummaryrefslogtreecommitdiff
path: root/gitosis/group.py
blob: ffee613cc1aeeb98b09cc5d201eb58e89ac46420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Gitosis functions to find what groups a given user belongs to.
"""
import logging
from ConfigParser import NoSectionErrorNoOptionError
        
_GROUP_PREFIX = 'group '
 
def _getMembership(config, user, seen):
    """
    Internal implementation of getMembership.
    Generate groups ``user`` is member of, according to ``config``.
    Groups already seen are tracked by ``seen``.
 
    :type config: RawConfigParser
    :type user: str
    :type seen: Set
    """
    log = logging.getLogger('gitosis.group.getMembership')
 
    for section in config.sections():
        if not section.startswith(_GROUP_PREFIX):
            continue
        group = section[len(_GROUP_PREFIX):]
        if group in seen:
            continue
 
        try:
            members = config.get(section'members')
        except (NoSectionErrorNoOptionError):
            members = []
        else:
            members = members.split()
 
        if user in members:
            log.debug('found %(user)r in %(group)r' % dict(
                user=user,
                group=group,
                ))
            seen.add(group)
            yield group
 
            for member_of in _getMembership(
                config'@%s' % groupseen,
                ):
                yield member_of
 
 
def getMembership(config, user):
    """
    Generate groups ``user`` is member of, according to ``config``
 
    :type config: RawConfigParser
    :type user: str
    """
 
    seen = set()
    for member_of in _getMembership(configuserseen):
        yield member_of
 
    # everyone is always a member of group "all" 
    yield 'all'