]> git.decadent.org.uk Git - dak.git/blobdiff - dak/update_db.py
Modified update_db.py to handle cases with port/host were non-standard,
[dak.git] / dak / update_db.py
index cda7aeb37baec6507b4050a48abb833c856ae622..e59a558c5344418cf3f4b554152ed8672878ae8e 100755 (executable)
@@ -26,7 +26,7 @@
 
 ################################################################################
 
-import psycopg2, sys
+import psycopg2, sys, fcntl, os
 import apt_pkg
 import time
 from daklib import database
@@ -50,6 +50,7 @@ Updates dak's database schema to the lastest version. You should disable crontab
 
 
 ################################################################################
+
     def update_db_to_zero(self):
         # This function will attempt to update a pre-zero database schema to zero
 
@@ -99,7 +100,12 @@ Updates dak's database schema to the lastest version. You should disable crontab
         print "Determining dak database revision ..."
 
         try:
-            self.db = psycopg2.connect("dbname='" + Cnf["DB::Name"] + "' host='" + Cnf["DB::Host"] + "' port='" + str(Cnf["DB::Port"]) + "'")
+            # Build a connect string
+            connect_str = "dbname=%s"% (Cnf["DB::Name"])
+            if Cnf["DB::Host"] != '': connect_str += " host=%s" % (Cnf["DB::Host"])
+            if Cnf["DB::Port"] != '-1': connect_str += " port=%d" % (int(Cnf["DB::Port"]))
+
+            self.db = psycopg2.connect(connect_str)
 
         except:
             print "FATAL: Failed connect to database"
@@ -114,7 +120,7 @@ Updates dak's database schema to the lastest version. You should disable crontab
             print "Please make sure you have a database backup handy. If you don't, press Ctrl-C now!"
             print ""
             print "Continuing in five seconds ..."
-            #time.sleep(5)
+            time.sleep(5)
             print ""
             print "Attempting to upgrade pre-zero database to zero"
 
@@ -133,7 +139,7 @@ Updates dak's database schema to the lastest version. You should disable crontab
             dakdb = __import__("dakdb", globals(), locals(), ['update'+str(i+1)])
             update_module = getattr(dakdb, "update"+str(i+1))
             update_module.do_update(self)
-            database_revision /+ 1
+            database_revision += 1
 
 ################################################################################
 
@@ -150,13 +156,22 @@ Updates dak's database schema to the lastest version. You should disable crontab
 
         options = Cnf.SubTree("Update-DB::Options")
         if options["Help"]:
-            usage()
+            self.usage()
         elif arguments:
             utils.warn("dak update-db takes no arguments.")
-            usage(exit_code=1)
+            self.usage(exit_code=1)
+
 
         self.update_db()
 
+        try:
+            lock_fd = os.open(Cnf["Dinstall::LockFile"], os.O_RDWR | os.O_CREAT)
+            fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+        except IOError, e:
+            if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EAGAIN':
+                utils.fubar("Couldn't obtain lock; assuming another 'dak process-unchecked' is already running.")
+
+
 ################################################################################
 
 if __name__ == '__main__':