aboutsummaryrefslogtreecommitdiff
path: root/gitosis/gitdaemon.py
blob: 78ca9eadf1b4f6e9e1bbad1047e87fce44ff99c6 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import errno
import logging
import os
 
from ConfigParser import NoSectionErrorNoOptionError
 
log = logging.getLogger('gitosis.gitdaemon')
 
from gitosis import util
 
def export_ok_path(repopath):
    p = os.path.join(repopath'git-daemon-export-ok')
    return p
 
def allow_export(repopath):
    p = export_ok_path(repopath)
    file(p'a').close()
 
def deny_export(repopath):
    p = export_ok_path(repopath)
    try:
        os.unlink(p)
    except OSErrore:
        if e.errno == errno.ENOENT:
            pass
        else:
            raise
 
def _extract_reldir(topdir, dirpath):
    if topdir == dirpath:
        return '.'
    prefix = topdir + '/'
    assert dirpath.startswith(prefix)
    reldir = dirpath[len(prefix):]
    return reldir
 
def set_export_ok(config):
    repositories = util.getRepositoryDir(config)
 
    try:
        global_enable = config.getboolean('gitosis''daemon')
    except (NoSectionErrorNoOptionError):
        global_enable = False
    log.debug(
        'Global default is %r',
        {True: 'allow', False: 'deny'}.get(global_enable),
        )
 
    def _error(e):
        if e.errno == errno.ENOENT:
            pass
        else:
            raise e
 
    for (dirpathdirnamesfilenames) \ 
            in os.walk(repositoriesonerror=_error):
        # oh how many times i have wished for os.walk to report 
        # topdir and reldir separately, instead of dirpath 
        reldir = _extract_reldir(
            topdir=repositories,
            dirpath=dirpath,
            )
 
        log.debug('Walking %r, seeing %r'reldirdirnames)
 
        to_recurse = []
        repos = []
        for dirname in dirnames:
            if dirname.endswith('.git'):
                repos.append(dirname)
            else:
                to_recurse.append(dirname)
        dirnames[:] = to_recurse
 
        for repo in repos:
            nameext = os.path.splitext(repo)
            if reldir != '.':
                name = os.path.join(reldirname)
            assert ext == '.git'
            try:
                enable = config.getboolean('repo %s' % name'daemon')
            except (NoSectionErrorNoOptionError):
                enable = global_enable
 
            if enable:
                log.debug('Allow %r'name)
                allow_export(os.path.join(dirpathrepo))
            else:
                log.debug('Deny %r'name)
                deny_export(os.path.join(dirpathrepo))