]> git.decadent.org.uk Git - dak.git/blobdiff - dak/transitions.py
merge from Joerg
[dak.git] / dak / transitions.py
index a38ab26a9a206b7b36784947269c9e2af6bfcff6..d8845a0bfe153b9aea560f681a48613b75e8e68d 100755 (executable)
@@ -23,7 +23,7 @@
 
 ################################################################################
 
-import os, pg, sys, time, errno, fcntl, tempfile
+import os, pg, sys, time, errno, fcntl, tempfile, pwd
 import apt_pkg
 import daklib.database
 import daklib.utils
@@ -47,7 +47,7 @@ def init():
                  ('e',"edit","Edit-Transitions::Options::Edit"),
                  ('i',"import","Edit-Transitions::Options::Import", "HasArg"),
                  ('c',"check","Edit-Transitions::Options::Check"),
-                 ('S',"use-sudo","Edit-Transitions::Options::Sudo"),
+                 ('s',"sudo","Edit-Transitions::Options::Sudo"),
                  ('n',"no-action","Edit-Transitions::Options::No-Action")]
 
     for i in ["help", "no-action", "edit", "import", "check", "sudo"]:
@@ -58,18 +58,24 @@ def init():
 
     Options = Cnf.SubTree("Edit-Transitions::Options")
 
-    projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
-    daklib.database.init(Cnf, projectB)
-    
     if Options["help"]:
         usage()
 
+    whoami = os.getuid()
+    whoamifull = pwd.getpwuid(whoami)
+    username = whoamifull[0]
+    if username != "dak":
+        print "Non-dak user: %s" % username
+        Options["sudo"] = "y"
+
+    projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
+    daklib.database.init(Cnf, projectB)
+    
 ################################################################################
 
 def usage (exit_code=0):
-    print """Usage: edit_transitions [OPTION]...
+    print """Usage: transitions [OPTION]...
 Update and check the release managers transition file.
-transitions.
 
 Options:
 
@@ -78,7 +84,7 @@ Options:
   -i, --import <file>       check and import transitions from file
   -c, --check               check the transitions file, remove outdated entries
   -S, --sudo                use sudo to update transitions file
-  -n, --no-action           don't do anything"""
+  -n, --no-action           don't do anything (only affects check)"""
 
     sys.exit(exit_code)
 
@@ -149,7 +155,7 @@ def write_transitions_from_file(from_file):
 
     if Options["sudo"]:
         os.spawnl(os.P_WAIT, "/usr/bin/sudo", "/usr/bin/sudo", "-u", "dak", "-H", 
-              "/usr/local/bin/dak", "edit-transitions", "--import", from_file)
+              "/usr/local/bin/dak", "transitions", "--import", from_file)
     else:
         trans = load_transitions(from_file)
         if trans is None:
@@ -160,8 +166,11 @@ def write_transitions_from_file(from_file):
 
 def temp_transitions_file(transitions):
     # NB: file is unlinked by caller, but fd is never actually closed.
-
+    # We need the chmod, as the file is (most possibly) copied from a
+    # sudo-ed script and would be unreadable if it has default mkstemp mode
+    
     (fd, path) = tempfile.mkstemp("","transitions")
+    os.chmod(path, 0644)
     f = open(path, "w")
     syck.dump(transitions, f)
     return path
@@ -185,33 +194,43 @@ def edit_transitions():
 
         if test == None:
             # Edit is broken
-            answer = "XXX"
-            prompt = "Broken edit: [E]dit again, Drop changes?"
-
-            while prompt.find(answer) == -1:
-                answer = daklib.utils.our_raw_input(prompt)
-                if answer == "":
-                    answer = "E"
-                answer = answer[:1].upper()
-
-            if answer == 'E':
-                continue
-            elif answer == 'D':
-                os.unlink(edit_file)
-                print "OK, discarding changes"
-                sys.exit(0)
+            print "Edit was unparsable."
+            prompt = "[E]dit again, Drop changes?"
+            default = "E"
         else:
-            # No problems in loading the new file, jump out of the while loop
+            print "Edit looks okay.\n"
+            print "The following transitions are defined:"
+            print "------------------------------------------------------------------------"
+            transition_info(test)
+
+           prompt = "[S]ave, Edit again, Drop changes?"
+           default = "S"
+
+        answer = "XXX"
+        while prompt.find(answer) == -1:
+            answer = daklib.utils.our_raw_input(prompt)
+            if answer == "":
+                answer = default
+            answer = answer[:1].upper()
+
+        if answer == 'E':
+            continue
+        elif answer == 'D':
+            os.unlink(edit_file)
+            print "OK, discarding changes"
+            sys.exit(0)
+        elif answer == 'S':
+            # Ready to save
             break
+        else:
+            print "You pressed something you shouldn't have :("
+            sys.exit(1)
 
     # We seem to be done and also have a working file. Copy over.
     write_transitions_from_file(edit_file)
     os.unlink(edit_file)
 
-    # Before we finish print out transition info again
-    print "\n\n------------------------------------------------------------------------"
-    print "Edit done, file saved, currently defined transitions:\n"
-    transition_info(load_transitions(trans_file))
+    print "Transitions file updated."
 
 ################################################################################
 
@@ -281,8 +300,7 @@ def check_transitions(transitions):
 ################################################################################
 
 def print_info(trans, source, expected, rm, reason, packages):
-        print """
-Looking at transition: %s
+        print """Looking at transition: %s
  Source:      %s
  New Version: %s
  Responsible: %s