4 Display, edit and check the release manager's transition file.
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2008 Joerg Jaspert <joerg@debian.org>
8 @license: GNU General Public License version 2 or later
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 ################################################################################
27 # <elmo> if klecker.d.o died, I swear to god, I'm going to migrate to gentoo.
29 ################################################################################
40 from daklib import database
41 from daklib import utils
42 from daklib.dak_exceptions import TransitionsError
43 from daklib.regexes import re_broken_package
47 Cnf = None #: Configuration, apt_pkg.Configuration
48 Options = None #: Parsed CommandLine arguments
49 projectB = None #: database connection, pgobject
51 ################################################################################
53 #####################################
54 #### This may run within sudo !! ####
55 #####################################
58 Initialize. Sets up database connection, parses commandline arguments.
60 @attention: This function may run B{within sudo}
63 global Cnf, Options, projectB
67 Cnf = utils.get_conf()
69 Arguments = [('h',"help","Edit-Transitions::Options::Help"),
70 ('e',"edit","Edit-Transitions::Options::Edit"),
71 ('i',"import","Edit-Transitions::Options::Import", "HasArg"),
72 ('c',"check","Edit-Transitions::Options::Check"),
73 ('s',"sudo","Edit-Transitions::Options::Sudo"),
74 ('n',"no-action","Edit-Transitions::Options::No-Action")]
76 for i in ["help", "no-action", "edit", "import", "check", "sudo"]:
77 if not Cnf.has_key("Edit-Transitions::Options::%s" % (i)):
78 Cnf["Edit-Transitions::Options::%s" % (i)] = ""
80 apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv)
82 Options = Cnf.SubTree("Edit-Transitions::Options")
88 whoamifull = pwd.getpwuid(whoami)
89 username = whoamifull[0]
91 print "Non-dak user: %s" % username
94 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]))
95 database.init(Cnf, projectB)
97 ################################################################################
99 def usage (exit_code=0):
100 print """Usage: transitions [OPTION]...
101 Update and check the release managers transition file.
105 -h, --help show this help and exit.
106 -e, --edit edit the transitions file
107 -i, --import <file> check and import transitions from file
108 -c, --check check the transitions file, remove outdated entries
109 -S, --sudo use sudo to update transitions file
110 -n, --no-action don't do anything (only affects check)"""
114 ################################################################################
116 #####################################
117 #### This may run within sudo !! ####
118 #####################################
119 def load_transitions(trans_file):
121 Parse a transition yaml file and check it for validity.
123 @attention: This function may run B{within sudo}
125 @type trans_file: string
126 @param trans_file: filename to parse
129 @return: validated dictionary of transition entries or None
130 if validation fails, empty string if reading C{trans_file}
131 returned something else than a dict
134 # Parse the yaml file
135 sourcefile = file(trans_file, 'r')
136 sourcecontent = sourcefile.read()
139 trans = yaml.load(sourcecontent)
140 except yaml.YAMLError, exc:
141 # Someone fucked it up
142 print "ERROR: %s" % (exc)
145 # lets do further validation here
146 checkkeys = ["source", "reason", "packages", "new", "rm"]
148 # If we get an empty definition - we just have nothing to check, no transitions defined
149 if type(trans) != dict:
150 # This can be anything. We could have no transitions defined. Or someone totally fucked up the
151 # file, adding stuff in a way we dont know or want. Then we set it empty - and simply have no
152 # transitions anymore. User will see it in the information display after he quit the editor and
161 # First check if we know all the keys for the transition and if they have
162 # the right type (and for the packages also if the list has the right types
163 # included, ie. not a list in list, but only str in the list)
165 if key not in checkkeys:
166 print "ERROR: Unknown key %s in transition %s" % (key, test)
169 if key == "packages":
170 if type(t[key]) != list:
171 print "ERROR: Unknown type %s for packages in transition %s." % (type(t[key]), test)
174 for package in t["packages"]:
175 if type(package) != str:
176 print "ERROR: Packages list contains invalid type %s (as %s) in transition %s" % (type(package), package, test)
178 if re_broken_package.match(package):
179 # Someone had a space too much (or not enough), we have something looking like
180 # "package1 - package2" now.
181 print "ERROR: Invalid indentation of package list in transition %s, around package(s): %s" % (test, package)
184 # In case someone has an empty packages list
185 print "ERROR: No packages defined in transition %s" % (test)
189 elif type(t[key]) != str:
190 if key == "new" and type(t[key]) == int:
191 # Ok, debian native version
194 print "ERROR: Unknown type %s for key %s in transition %s" % (type(t[key]), key, test)
197 # And now the other way round - are all our keys defined?
198 for key in checkkeys:
200 print "ERROR: Missing key %s in transition %s" % (key, test)
203 # In case someone defined very broken things
204 print "ERROR: Unable to parse the file"
213 ################################################################################
215 #####################################
216 #### This may run within sudo !! ####
217 #####################################
222 @attention: This function may run B{within sudo}
225 for retry in range(10):
226 lock_fd = os.open(f, os.O_RDWR | os.O_CREAT)
228 fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
231 if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EEXIST':
232 print "Unable to get lock for %s (try %d of 10)" % \
238 utils.fubar("Couldn't obtain lock for %s." % (f))
240 ################################################################################
242 #####################################
243 #### This may run within sudo !! ####
244 #####################################
245 def write_transitions(from_trans):
247 Update the active transitions file safely.
248 This function takes a parsed input file (which avoids invalid
249 files or files that may be be modified while the function is
250 active) and ensure the transitions file is updated atomically
253 @attention: This function may run B{within sudo}
255 @type from_trans: dict
256 @param from_trans: transitions dictionary, as returned by L{load_transitions}
260 trans_file = Cnf["Dinstall::Reject::ReleaseTransitions"]
261 trans_temp = trans_file + ".tmp"
263 trans_lock = lock_file(trans_file)
264 temp_lock = lock_file(trans_temp)
266 destfile = file(trans_temp, 'w')
267 yaml.dump(from_trans, destfile, default_flow_style=False)
270 os.rename(trans_temp, trans_file)
274 ################################################################################
276 ##########################################
277 #### This usually runs within sudo !! ####
278 ##########################################
279 def write_transitions_from_file(from_file):
281 We have a file we think is valid; if we're using sudo, we invoke it
282 here, otherwise we just parse the file and call write_transitions
284 @attention: This function usually runs B{within sudo}
286 @type from_file: filename
287 @param from_file: filename of a transitions file
291 # Lets check if from_file is in the directory we expect it to be in
292 if not os.path.abspath(from_file).startswith(Cnf["Transitions::TempPath"]):
293 print "Will not accept transitions file outside of %s" % (Cnf["Transitions::TempPath"])
297 os.spawnl(os.P_WAIT, "/usr/bin/sudo", "/usr/bin/sudo", "-u", "dak", "-H",
298 "/usr/local/bin/dak", "transitions", "--import", from_file)
300 trans = load_transitions(from_file)
302 raise TransitionsError, "Unparsable transitions file %s" % (file)
303 write_transitions(trans)
305 ################################################################################
307 def temp_transitions_file(transitions):
309 Open a temporary file and dump the current transitions into it, so users
312 @type transitions: dict
313 @param transitions: current defined transitions
316 @return: path of newly created tempfile
318 @note: NB: file is unlinked by caller, but fd is never actually closed.
319 We need the chmod, as the file is (most possibly) copied from a
320 sudo-ed script and would be unreadable if it has default mkstemp mode
323 (fd, path) = tempfile.mkstemp("", "transitions", Cnf["Transitions::TempPath"])
326 yaml.dump(transitions, f, default_flow_style=False)
329 ################################################################################
331 def edit_transitions():
332 """ Edit the defined transitions. """
333 trans_file = Cnf["Dinstall::Reject::ReleaseTransitions"]
334 edit_file = temp_transitions_file(load_transitions(trans_file))
336 editor = os.environ.get("EDITOR", "vi")
339 result = os.system("%s %s" % (editor, edit_file))
342 utils.fubar("%s invocation failed for %s, not removing tempfile." % (editor, edit_file))
344 # Now try to load the new file
345 test = load_transitions(edit_file)
349 print "Edit was unparsable."
350 prompt = "[E]dit again, Drop changes?"
353 print "Edit looks okay.\n"
354 print "The following transitions are defined:"
355 print "------------------------------------------------------------------------"
356 transition_info(test)
358 prompt = "[S]ave, Edit again, Drop changes?"
362 while prompt.find(answer) == -1:
363 answer = utils.our_raw_input(prompt)
366 answer = answer[:1].upper()
372 print "OK, discarding changes"
378 print "You pressed something you shouldn't have :("
381 # We seem to be done and also have a working file. Copy over.
382 write_transitions_from_file(edit_file)
385 print "Transitions file updated."
387 ################################################################################
389 def check_transitions(transitions):
391 Check if the defined transitions still apply and remove those that no longer do.
392 @note: Asks the user for confirmation first.
397 # Now look through all defined transitions
398 for trans in transitions:
399 t = transitions[trans]
403 # Will be None if nothing is in testing.
404 current = database.get_suite_version(source, "testing")
406 print_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
409 # No package in testing
410 print "Transition source %s not in testing, transition still ongoing." % (source)
412 compare = apt_pkg.VersionCompare(current, expected)
414 # This is still valid, the current version in database is older than
415 # the new version we wait for
416 print "This transition is still ongoing, we currently have version %s" % (current)
418 print "REMOVE: This transition is over, the target package reached testing. REMOVE"
419 print "%s wanted version: %s, has %s" % (source, expected, current)
420 to_remove.append(trans)
422 print "-------------------------------------------------------------------------"
425 prompt = "Removing: "
426 for remove in to_remove:
430 prompt += " Commit Changes? (y/N)"
433 if Options["no-action"]:
436 answer = utils.our_raw_input(prompt).lower()
442 print "Not committing changes"
446 for remove in to_remove:
447 del transitions[remove]
449 edit_file = temp_transitions_file(transitions)
450 write_transitions_from_file(edit_file)
454 print "WTF are you typing?"
457 ################################################################################
459 def print_info(trans, source, expected, rm, reason, packages):
461 Print information about a single transition.
464 @param trans: Transition name
467 @param source: Source package
469 @type expected: string
470 @param expected: Expected version in testing
473 @param rm: Responsible RM
476 @param reason: Reason
479 @param packages: list of blocked packages
482 print """Looking at transition: %s
487 Blocked Packages (total: %d): %s
488 """ % (trans, source, expected, rm, reason, len(packages), ", ".join(packages))
491 ################################################################################
493 def transition_info(transitions):
495 Print information about all defined transitions.
496 Calls L{print_info} for every transition and then tells user if the transition is
497 still ongoing or if the expected version already hit testing.
499 @type transitions: dict
500 @param transitions: defined transitions
502 for trans in transitions:
503 t = transitions[trans]
507 # Will be None if nothing is in testing.
508 current = database.get_suite_version(source, "testing")
510 print_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
513 # No package in testing
514 print "Transition source %s not in testing, transition still ongoing." % (source)
516 compare = apt_pkg.VersionCompare(current, expected)
517 print "Apt compare says: %s" % (compare)
519 # This is still valid, the current version in database is older than
520 # the new version we wait for
521 print "This transition is still ongoing, we currently have version %s" % (current)
523 print "This transition is over, the target package reached testing, should be removed"
524 print "%s wanted version: %s, has %s" % (source, expected, current)
525 print "-------------------------------------------------------------------------"
527 ################################################################################
531 Prepare the work to be done, do basic checks.
533 @attention: This function may run B{within sudo}
538 #####################################
539 #### This can run within sudo !! ####
540 #####################################
543 # Check if there is a file defined (and existant)
544 transpath = Cnf.get("Dinstall::Reject::ReleaseTransitions", "")
546 utils.warn("Dinstall::Reject::ReleaseTransitions not defined")
548 if not os.path.exists(transpath):
549 utils.warn("ReleaseTransitions file, %s, not found." %
550 (Cnf["Dinstall::Reject::ReleaseTransitions"]))
552 # Also check if our temp directory is defined and existant
553 temppath = Cnf.get("Transitions::TempPath", "")
555 utils.warn("Transitions::TempPath not defined")
557 if not os.path.exists(temppath):
558 utils.warn("Temporary path %s not found." %
559 (Cnf["Transitions::TempPath"]))
562 if Options["import"]:
564 write_transitions_from_file(Options["import"])
565 except TransitionsError, m:
569 ##############################################
570 #### Up to here it can run within sudo !! ####
571 ##############################################
573 # Parse the yaml file
574 transitions = load_transitions(transpath)
575 if transitions == None:
576 # Something very broken with the transitions, exit
577 utils.warn("Could not parse existing transitions file. Aborting.")
581 # Let's edit the transitions file
583 elif Options["check"]:
584 # Check and remove outdated transitions
585 check_transitions(transitions)
587 # Output information about the currently defined transitions.
588 print "Currently defined transitions:"
589 transition_info(transitions)
593 ################################################################################
595 if __name__ == '__main__':