summaryrefslogtreecommitdiffstats
path: root/gitsymlinks.py
blob: 3551d244d796993115eb6c0fb61403f00a8d9725 (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
#!/usr/bin/python
# -*- mode: python; coding: utf-8 -*-

# Copyright © 2008 Jeffrey C. Ollie

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

"""Make convenience symlinks for public git repositories.

This will make urls to get repositories on fedorapeople.org more convenient:

git://fedorapeople.org/git/$user/$repo.git

instead of:

git://fedorapeople.org/home/fedora/$user/public_git/$repo.git

For safety this should be run as a non-root user.
"""

import os
import pwd
import sys

if os.getuid() == 0:
    sys.stderr.write('Do not run this as root!')
    sys.exit(1)

symlink_dir = '/git'

existing_symlinks = {}
blocked_symlinks = set()

for filename in os.listdir(symlink_dir):
    dst = os.path.join(symlink_dir, filename)
    if not os.path.islink(dst):
        print "oops: %s is not a symlink" % dst
        blocked_symlinks.add(dst)
    else:
        existing_symlinks[dst] = os.readlink(dst)

unneeded_symlinks = existing_symlinks.copy()
needed_symlinks = {}

for (username, uid, gid, passwd, gecos, homedir, shell) in pwd.getpwall():
    if uid < 100000:
        continue

    src = os.path.join(homedir, 'public_git')

    if not os.path.exists(src):
        continue

    dst = os.path.join(symlink_dir, username)

    needed_symlinks[dst] = src

    try:
        del unneeded_symlinks[dst]
    except KeyError:
        pass

for dst, src in needed_symlinks.iteritems():
    if dst in blocked_symlinks:
        sys.stderr.write("oops: %s is blocked!\n" % dst)
        continue

    if dst in existing_symlinks:
        # make sure that symlink points to right place
        if src != existing_symlinks[dst]:
            sys.stderr.write("updating symlink %s: %s -> %s\n" % (dst, existing_symlinks[dst], src))
            os.unlink(dst)
            os.symlink(src, dst)
        
    else:
        # create symlinks that don't already exist
        sys.stderr.write("new symlink: %s -> %s\n" % (dst, src))
        os.symlink(src, dst)

# get rid of any symlinks we no longer need
for dst, src in unneeded_symlinks.iteritems():
    sys.stdout.write("deleting symlink %s -> %s\n" % (dst, src))
    os.unlink(dst)