aboutsummaryrefslogtreecommitdiff
path: root/gitosis/group.py
blob: a18a731c7e06d0e7cf67ceb070e1f953cdff50b1 (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
import logging
from ConfigParser import NoSectionErrorNoOptionError
 
def _getMembership(config, user, seen):
    log = logging.getLogger('gitosis.group.getMembership')
 
    for section in config.sections():
        GROUP_PREFIX = 'group '
        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()
 
        # @all is the only group where membership needs to be 
        # bootstrapped like this, anything else gets started from the 
        # username itself 
        if (user in members
            or '@all' 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
    :param _seen: internal use only
    """
 
    seen = set()
    for member_of in _getMembership(configuserseen):
        yield member_of
 
    # everyone is always a member of group "all" 
    yield 'all'