summaryrefslogtreecommitdiff
path: root/backend/repo_service.py
blob: 037d3240fec3292c01ee2a8d2d2900829a7b2929 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import subprocess
import syslog
import sys
import re
import time
import os

syslog.openlog('repo_service')

repo_name_re = re.compile(r'^[a-z][a-z0-9_-]{2,}$')
repo_desc_re = re.compile(r'^[\w\s]{5,}$')

repo_dir = os.path.dirname(os.path.realpath(__file__))
repo_dir = os.path.join(repo_dir, 'gitolite')
repo_git = 'git@127.0.0.1:gitolite-admin.git'
cgit_conf = '/etc/cgitrc.repo_service'

class GitException(Exception):
    pass

class RepoExistsException(Exception):
    pass

def git_call(args, **kwargs):
    try:
        subprocess.check_call(args, **kwargs)
    except subprocess.CalledProcessError as e:
        syslog.syslog('%s failed.' % ' '.join(args[:1]))
        raise GitException()

def gitolite_append(repo_owner, repo_name, repo_desc):
    if not os.path.isdir(repo_dir):
        git_call(['git', 'clone', repo_git, repo_dir])
    else:
        git_call(['git', 'fetch', repo_git, 'master'], cwd=repo_dir)
        git_call(['git', 'reset', '--hard', 'FETCH_HEAD'], cwd=repo_dir)

    grep = subprocess.call(
            ['grep', '-r' , '^\s*repo\s\s*%s\s*$' % repo_name, '.'],
            cwd=os.path.join(repo_dir, 'conf'))
    if grep != 1:
        raise RepoExistsException

    filename = os.path.join(repo_dir, 'conf', 'repo_service.conf')

    repo_date = time.strftime('%a, %d %b %Y %T %z')
    entry = '''
    # Automatically created by repo_service
    # at %(repo_date)s
    # for %(repo_owner)s
        repo     %(repo_name)s
                 R    = daemon
                 RW   = @members
''' % locals()

    try:
        with open(filename, 'a') as gitolite_conf:
            gitolite_conf.write(entry)
    except EnvironmentError as e:
        syslog.syslog('could not append to repo_service.conf: %s' % repr(e))
        raise GitException

    git_call(['git', 'add', filename], cwd=repo_dir)
    git_call(['git', 'commit',
            '--author=Repo Service <repo_service@localhost>',
            '--message=repo_service: create %s for %s' % (repo_name, repo_owner)],
            cwd=repo_dir)
    git_call(['git', 'push', repo_git, 'master:master'], cwd=repo_dir)

def cgit_append(repo_owner, repo_name, repo_desc):
    repo_date = time.strftime('%a, %d %b %Y %T %z')
    cgit_entry = '''
# Automatically created by repo_service
# at %(repo_date)s
# for %(repo_owner)s
repo.url=%(repo_name)s
repo.path=/var/lib/git/repositories/%(repo_name)s.git
repo.desc=%(repo_desc)s
repo.owner=repo-service
''' % locals()

    with open(cgit_conf, 'a') as cgit_config:
        cgit_config.write(cgit_entry)

def create_repo(repo_owner, repo_name, repo_desc):
    if repo_name_re.match(repo_name) is None:
        syslog.syslog('Declined repo_name %s to %s' % (
            repr(repo_name), repo_owner))
        return 'ERROR_NAME'

    if repo_desc_re.match(repo_desc) is None:
        syslog.syslog('Declined repo_desc %s to %s' % (
            repr(repo_desc), repo_owner))
        return 'ERROR_DESC'

    syslog.syslog('Handling create_repo for %s ' % (repo_owner) +
            'repo_name=%s, repo_desc=%s' % (repo_name, repo_desc))

    try:
        gitolite_append(repo_owner, repo_name, repo_desc)
    except RepoExistsException:
        return 'ERROR_EXISTS'
    except GitException:
        syslog.syslog('gitolite_append failed')
        return 'ERROR_GITOLITE'

    try:
        cgit_append(repo_owner, repo_name, repo_desc)
    except EnvironmentError:
        syslog.syslog('cgit_append failed')
        return 'ERROR_CGIT'

    syslog.syslog('created repo %s' % repo_name)
    return 'SUCCESS'

if __name__ == '__main__':
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    import errno
    import select

    server = SimpleXMLRPCServer(("127.0.0.1", 8023))
    server.register_function(create_repo)

    while True:
        try:
            server.serve_forever()
        except select.error as e:
            if e.args[0] == errno.EINTR:
                continue
            raise