Read paths from the same configfile as the scripts
authorMagnus Hagander <magnus@hagander.net>
Tue, 30 Dec 2008 16:28:40 +0000 (17:28 +0100)
committerMagnus Hagander <magnus@hagander.net>
Tue, 30 Dec 2008 16:32:54 +0000 (17:32 +0100)
pggit.py
pggit.settings.sample

index 4ad0932a827aa3d0216e8929e53d62645720ef7e..6626f6cf4dc39913f4d844928db368460e91c807 100755 (executable)
--- a/pggit.py
+++ b/pggit.py
@@ -15,20 +15,20 @@ controlled by the client side git command.
 
 import sys
 import os
+import os.path
 import psycopg2
-
-# MUST have trailing slash
-REPOPREFIX="/home/gitlab/"
+import ConfigParser
 
 ALLOWED_COMMANDS = ('git-upload-pack', 'git-receive-pack')
 WRITE_COMMANDS = ('git-receive-pack')
 
 class Logger:
-       def __init__(self):
+       def __init__(self, cfg):
                self.user = "Unknown"
+               self.logfile = cfg.get('paths','logfile')
 
        def log(self, message):
-               f = open("/home/gitlab/pggit.log","a")
+               f = open(self.logfile,"a")
                f.write("(%s): %s" % (self.user, message))
                f.write("\n")
                f.close()
@@ -43,8 +43,10 @@ class PgGit:
        path = None
        subpath = None
 
-       def __init__(self):
-               self.logger = Logger()
+       def __init__(self, cfg):
+               self.cfg = cfg
+               self.logger = Logger(cfg)
+               self.repoprefix = "%s/repos/" % cfg.get('paths','githome')
                pass
 
        def parse_commandline(self):
@@ -73,16 +75,16 @@ class PgGit:
                # FIXME: what about that single quote? Make sure it's there?
 
                # use os.path.normpath to make sure the user does not attempt to break out of the repository root
-               self.path = os.path.normpath(("%s%s" % (REPOPREFIX, args[2:].rstrip("'"))))
-               if not self.path.startswith(REPOPREFIX):
+               self.path = os.path.normpath(("%s%s" % (self.repoprefix, args[2:].rstrip("'"))))
+               if not self.path.startswith(self.repoprefix):
                        raise Exception("Escaping the root directory is of course not permitted")
                if not os.path.exists(self.path):
                        raise Exception('git repository "%s" does not exist' % args)
-               self.subpath = self.path[len(REPOPREFIX):]
+               self.subpath = self.path[len(self.repoprefix):]
 
        def check_permissions(self):
                writeperm = False
-               db = psycopg2.connect("dbname=gitlab host=/tmp/ user=mha")
+               db = psycopg2.connect(self.cfg.get('database','db'))
                curs = db.cursor()
                curs.execute("SELECT write FROM repository_permissions INNER JOIN repositories ON repoid=repository WHERE userid=%s AND name=%s",
                        (self.user, self.subpath))
@@ -119,5 +121,7 @@ class PgGit:
                        raise e
 
 if __name__ == "__main__":
-       PgGit().run()
+       c = ConfigParser.ConfigParser()
+       c.read("%s/pggit.settings" % os.path.abspath(sys.path[0]))
+       PgGit(c).run()
 
index bd31a0f75b4d0ef996b9548b263972737974d02c..a8a4f03e402fd0b1f605e01d1298e40accef5ff4 100644 (file)
@@ -11,3 +11,4 @@ group=pggit
 githome=/home/gitlab
 pggit=/opt/pgsql/pggit/pggit.py
 gitweblist=/opt/pgsql/pggit/__temp__gitweb.list
+logfile=/home/gitlab/pggit.log