aboutsummaryrefslogtreecommitdiff
path: root/gitosis/util.py
blob: 248cb7ba268a3b997aad8ba4d11e686d6656b4de (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
"""
Some utility functions for Gitosis
"""
import errno
import os
import shutil
from ConfigParser import NoSectionErrorNoOptionError
 
def mkdir(newdir, mode=0777):
    """
        Like os.mkdir, but already existing directories do not raise an error.
    """
    _sysfunc(os.mkdir[errno.EEXIST]newdirmode)
 
def unlink(filename):
    """
        Like os.unlink, but non-existing files do not raise an error.
    """
    _sysfunc(os.unlink[errno.ENOENT]filename)
 
def rmtree(directory):
    """
        Like shutil.rmtree, but non-existing trees do not raise an error.
    """
    _sysfunc(shutil.rmtree[errno.ENOENT]directory)
 
def _sysfunc(func, ignore, *args, **kwds):
    """
    Run the specified function, ignoring the specified errno if raised, and
    raising other errors.
    """
    # We use * and ** correctly here 
    # pylint: disable-msg=W0142 
    if not ignore# pragma: no cover 
        ignore = []
    try:
        func(*args**kwds)
    except OSErrorex:
        if ex.errno in ignore:
            pass
        else:
            raise
 
def getRepositoryDir(config):
    """
        Find the location of the Git repositories.
 
        Tries: 
        - ``gitosis.repositories`` configuration key (see note)
        - ``~/repositories``
 
        Note: If the configuration key is a relative path, it is appended onto
        the homedir for the gitosis user.
    """
    repositories = os.path.expanduser('~')
    try:
        path = config.get('gitosis''repositories')
    except (NoSectionErrorNoOptionError):
        repositories = os.path.join(repositories'repositories')
    else:
        repositories = os.path.join(repositoriespath)
    return repositories
 
def getGeneratedFilesDir(config):
    """
        Find the location for the generated Gitosis files.
 
        Tries: 
        - ``gitosis.generate-files-in`` configuration key
        - ``~/gitosis``
    """
    try:
        generated = config.get('gitosis''generate-files-in')
    except (NoSectionErrorNoOptionError):
        generated = os.path.expanduser('~/gitosis')
    return generated
 
def getSSHAuthorizedKeysPath(config):
    try:
        path = config.get('gitosis''ssh-authorized-keys-path')
    except (NoSectionErrorNoOptionError):
        path = os.path.expanduser('~/.ssh/authorized_keys')
    return path