install
branchdebian
changeset 172 5dd3698fad54
parent 118 107906bfe2c6
parent 171 7b69d1d86254
child 174 f141a4b9d5a9
equal deleted inserted replaced
118:107906bfe2c6 172:5dd3698fad54
     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 
       
    32 def installFiles(d, *sources):
       
    33     d = options.root + d
       
    34     os.makedirs(d)
       
    35     for f in sources:
       
    36         shutil.copy(f, d)
       
    37 
       
    38 installFiles(options.prefix + '/share/mercurial-server',
       
    39     'src/hg-ssh',
       
    40     'src/refresh-auth')
       
    41 installFiles(options.prefix + '/share/mercurial-server/mercurialserver',
       
    42     'src/mercurialserver/__init__.py',
       
    43     'src/mercurialserver/paths.py',
       
    44     'src/mercurialserver/changes.py',
       
    45     'src/mercurialserver/access.py',
       
    46     'src/mercurialserver/servelog.py',
       
    47     'src/mercurialserver/refreshauth.py',
       
    48     'src/mercurialserver/ruleset.py')
       
    49 installFiles(options.prefix + '/share/mercurial-server/init',
       
    50     'src/init/hginit',
       
    51     'src/init/hgadmin-hgrc')
       
    52 installFiles(options.prefix + '/share/doc/mercurial-server',
       
    53     'README',
       
    54     'doc/configuring-access',
       
    55     'doc/file-conditions',
       
    56     'doc/how-it-works',
       
    57     'doc/security')
       
    58 installFiles('/etc/mercurial-server',
       
    59     'src/init/conf/remote-hgrc',
       
    60     'src/init/conf/access.conf')
       
    61 installFiles('/etc/mercurial-server/keys/root')
       
    62 installFiles('/etc/mercurial-server/keys/users')
       
    63 
       
    64 def becomeFunc(u):
       
    65     p = pwd.getpwnam(u)
       
    66     def become():
       
    67         os.setgid(p.pw_gid)
       
    68         os.setegid(p.pw_gid)
       
    69         os.setuid(p.pw_uid)
       
    70         os.seteuid(p.pw_uid)
       
    71     return become
       
    72 
       
    73 if options.root == '':
       
    74     try:
       
    75        pwd.getpwnam('hg')
       
    76     except KeyError:
       
    77         subprocess.check_call(
       
    78             "adduser --system --shell /bin/sh --group --disabled-password".split() + 
       
    79             ["--gecos", "Mercurial repositories", "hg"])
       
    80         subprocess.check_call([options.prefix + '/share/mercurial-server/init/hginit',
       
    81             options.prefix + '/share/mercurial-server'],
       
    82             preexec_fn = becomeFunc('hg'))
       
    83