Rewrite install script in Python
authorPaul Crowley <paul@lshift.net>
Fri, 06 Mar 2009 16:15:28 +0000
changeset 80 fcb20d7593e6
parent 79 3a58a95fae2f
child 81 f23736ad66bc
Rewrite install script in Python
install
--- a/install	Fri Mar 06 13:41:30 2009 +0000
+++ b/install	Fri Mar 06 16:15:28 2009 +0000
@@ -1,34 +1,52 @@
-#!/bin/sh
+#!/usr/bin/env python
+
+import shutil
+import os
+import pwd
+import subprocess
 
-set -e
+def installFiles(d, *sources):
+    os.makedirs(d)
+    for f in sources:
+        shutil.copy(f, d)
+
+dest = '/usr/local/lib/mercurial-server'
 
-install -o root -g root -d /usr/local/lib/mercurial-server
-install -o root -g root -t /usr/local/lib/mercurial-server \
-    src/hg-ssh \
-    src/refresh-auth
-install -o root -g root -d /usr/local/lib/mercurial-server/mercurialserver
-install -o root -g root -t /usr/local/lib/mercurial-server/mercurialserver -m 644 \
-    src/mercurialserver/__init__.py \
-    src/mercurialserver/paths.py \
-    src/mercurialserver/changes.py \
-    src/mercurialserver/access.py \
-    src/mercurialserver/servelog.py \
-    src/mercurialserver/refreshauth.py \
-    src/mercurialserver/ruleset.py
-install -o root -g root -d /usr/local/lib/mercurial-server/init
-install -o root -g root -t /usr/local/lib/mercurial-server/init \
-    src/init/hginit
-install -o root -g root -t /usr/local/lib/mercurial-server/init -m 644 \
-    src/init/hgadmin-hgrc
-install -o root -g root -d /etc/mercurial-server
-install -o root -g root -t /etc/mercurial-server -m 644 \
-    src/init/conf/remote-hgrc \
-    src/init/conf/access.conf
-install -o root -g root -d /etc/mercurial-server/keys/root
-install -o root -g root -d /etc/mercurial-server/keys/users
+installFiles(dest,
+    'src/hg-ssh',
+    'src/refresh-auth')
+installFiles(dest + '/mercurialserver',
+    'src/mercurialserver/__init__.py',
+    'src/mercurialserver/paths.py',
+    'src/mercurialserver/changes.py',
+    'src/mercurialserver/access.py',
+    'src/mercurialserver/servelog.py',
+    'src/mercurialserver/refreshauth.py',
+    'src/mercurialserver/ruleset.py')
+installFiles(dest + '/init',
+    'src/init/hginit',
+    'src/init/hgadmin-hgrc')
+installFiles('/etc/mercurial-server',
+    'src/init/conf/remote-hgrc',
+    'src/init/conf/access.conf')
+installFiles('/etc/mercurial-server/keys/root')
+installFiles('/etc/mercurial-server/keys/users')
 
-if [ x$(getent passwd hg | wc -c) = x0 ] ; then
-    adduser --system --shell /bin/sh --group --disabled-password \
-        --gecos "Mercurial repositories" hg
-    su -c /usr/local/lib/mercurial-server/init/hginit hg
-fi
+def becomeFunc(u):
+    p = pwd.getpwnam(u)
+    def become():
+        os.setgid(p.pw_gid)
+        os.setegid(p.pw_gid)
+        os.setuid(p.pw_uid)
+        os.seteuid(p.pw_uid)
+    return become
+
+try:
+   pwd.getpwnam('hg')
+except KeyError:
+    subprocess.check_call(
+        "adduser --system --shell /bin/sh --group --disabled-password".split() + 
+        ["--gecos", "Mercurial repositories", "hg"])
+        
+    subprocess.check_call([dest + '/init/hginit'], preexec_fn = becomeFunc('hg'))
+