1 #!/usr/bin/env python |
|
2 |
|
3 import sys |
|
4 import shutil |
|
5 import os |
|
6 import pwd |
|
7 import subprocess |
|
8 import optparse |
|
9 |
|
10 oparser = optparse.OptionParser() |
|
11 |
|
12 oparser.add_option("--prefix") |
|
13 oparser.add_option("--root") |
|
14 oparser.set_defaults(root="", prefix="/usr/local") |
|
15 (options, args) = oparser.parse_args() |
|
16 |
|
17 if len(args) > 0: |
|
18 oparser.print_help() |
|
19 sys.exit(-1) |
|
20 |
|
21 # This must be run as root, because it must create an hg user. |
|
22 # Normally the clean thing to do is let it fail with a permission error |
|
23 # if a non-root user tries to run it, but I don't want anyone thinking |
|
24 # that they can make it work as non-root by changing install paths. |
|
25 # Patches for doing this more cleanly welcome of course. |
|
26 |
|
27 if os.getgid() != 0: |
|
28 print >>sys.stderr, "Install must be run as root user" |
|
29 sys.exit(-1) |
|
30 |
|
31 def installFiles(d, *sources): |
|
32 d = options.root + d |
|
33 os.makedirs(d) |
|
34 for f in sources: |
|
35 shutil.copy(f, d) |
|
36 |
|
37 installFiles(options.prefix + '/share/mercurial-server', |
|
38 'src/hg-ssh', |
|
39 'src/refresh-auth') |
|
40 installFiles(options.prefix + '/share/mercurial-server/mercurialserver', |
|
41 'src/mercurialserver/__init__.py', |
|
42 'src/mercurialserver/paths.py', |
|
43 'src/mercurialserver/changes.py', |
|
44 'src/mercurialserver/access.py', |
|
45 'src/mercurialserver/servelog.py', |
|
46 'src/mercurialserver/refreshauth.py', |
|
47 'src/mercurialserver/ruleset.py') |
|
48 installFiles(options.prefix + '/share/mercurial-server/init', |
|
49 'src/init/hginit', |
|
50 'src/init/dot-mercurial-server', |
|
51 'src/init/hgadmin-hgrc') |
|
52 installFiles(options.prefix + '/share/doc/mercurial-server', |
|
53 'README', |
|
54 'build/html/index.html') |
|
55 installFiles('/etc/mercurial-server', |
|
56 'src/init/conf/remote-hgrc', |
|
57 'src/init/conf/access.conf') |
|
58 installFiles('/etc/mercurial-server/keys/root') |
|
59 installFiles('/etc/mercurial-server/keys/users') |
|
60 |
|
61 def becomeFunc(u): |
|
62 p = pwd.getpwnam(u) |
|
63 def become(): |
|
64 os.setgid(p.pw_gid) |
|
65 os.setegid(p.pw_gid) |
|
66 os.setuid(p.pw_uid) |
|
67 os.seteuid(p.pw_uid) |
|
68 os.chdir(p.pw_dir) |
|
69 return become |
|
70 |
|
71 if options.root == '': |
|
72 try: |
|
73 pwd.getpwnam('hg') |
|
74 except KeyError: |
|
75 subprocess.check_call( |
|
76 "adduser --system --shell /bin/sh --group --disabled-password".split() + |
|
77 ["--gecos", "Mercurial repositories", "hg"]) |
|
78 subprocess.check_call([options.prefix + '/share/mercurial-server/init/hginit', |
|
79 options.prefix + '/share/mercurial-server'], |
|
80 preexec_fn = becomeFunc('hg')) |
|
81 |
|