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.dbconn import *
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
50 ################################################################################
52 #####################################
53 #### This may run within sudo !! ####
54 #####################################
57 Initialize. Sets up database connection, parses commandline arguments.
59 @attention: This function may run B{within sudo}
66 Cnf = utils.get_conf()
68 Arguments = [('a',"automatic","Edit-Transitions::Options::Automatic"),
69 ('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 ["automatic", "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 # Initialise DB connection
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 -a, --automatic don't prompt (only affects check).
111 -n, --no-action don't do anything (only affects check)"""
115 ################################################################################
117 #####################################
118 #### This may run within sudo !! ####
119 #####################################
120 def load_transitions(trans_file):
122 Parse a transition yaml file and check it for validity.
124 @attention: This function may run B{within sudo}
126 @type trans_file: string
127 @param trans_file: filename to parse
130 @return: validated dictionary of transition entries or None
131 if validation fails, empty string if reading C{trans_file}
132 returned something else than a dict
135 # Parse the yaml file
136 sourcefile = file(trans_file, 'r')
137 sourcecontent = sourcefile.read()
140 trans = yaml.load(sourcecontent)
141 except yaml.YAMLError, exc:
142 # Someone fucked it up
143 print "ERROR: %s" % (exc)
146 # lets do further validation here
147 checkkeys = ["source", "reason", "packages", "new", "rm"]
149 # If we get an empty definition - we just have nothing to check, no transitions defined
150 if type(trans) != dict:
151 # This can be anything. We could have no transitions defined. Or someone totally fucked up the
152 # file, adding stuff in a way we dont know or want. Then we set it empty - and simply have no
153 # transitions anymore. User will see it in the information display after he quit the editor and
162 # First check if we know all the keys for the transition and if they have
163 # the right type (and for the packages also if the list has the right types
164 # included, ie. not a list in list, but only str in the list)
166 if key not in checkkeys:
167 print "ERROR: Unknown key %s in transition %s" % (key, test)
170 if key == "packages":
171 if type(t[key]) != list:
172 print "ERROR: Unknown type %s for packages in transition %s." % (type(t[key]), test)
175 for package in t["packages"]:
176 if type(package) != str:
177 print "ERROR: Packages list contains invalid type %s (as %s) in transition %s" % (type(package), package, test)
179 if re_broken_package.match(package):
180 # Someone had a space too much (or not enough), we have something looking like
181 # "package1 - package2" now.
182 print "ERROR: Invalid indentation of package list in transition %s, around package(s): %s" % (test, package)
185 # In case someone has an empty packages list
186 print "ERROR: No packages defined in transition %s" % (test)
190 elif type(t[key]) != str:
191 if key == "new" and type(t[key]) == int:
192 # Ok, debian native version
195 print "ERROR: Unknown type %s for key %s in transition %s" % (type(t[key]), key, test)
198 # And now the other way round - are all our keys defined?
199 for key in checkkeys:
201 print "ERROR: Missing key %s in transition %s" % (key, test)
204 # In case someone defined very broken things
205 print "ERROR: Unable to parse the file"
214 ################################################################################
216 #####################################
217 #### This may run within sudo !! ####
218 #####################################
223 @attention: This function may run B{within sudo}
226 for retry in range(10):
227 lock_fd = os.open(f, os.O_RDWR | os.O_CREAT)
229 fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
232 if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EEXIST':
233 print "Unable to get lock for %s (try %d of 10)" % \
239 utils.fubar("Couldn't obtain lock for %s." % (f))
241 ################################################################################
243 #####################################
244 #### This may run within sudo !! ####
245 #####################################
246 def write_transitions(from_trans):
248 Update the active transitions file safely.
249 This function takes a parsed input file (which avoids invalid
250 files or files that may be be modified while the function is
251 active) and ensure the transitions file is updated atomically
254 @attention: This function may run B{within sudo}
256 @type from_trans: dict
257 @param from_trans: transitions dictionary, as returned by L{load_transitions}
261 trans_file = Cnf["Dinstall::Reject::ReleaseTransitions"]
262 trans_temp = trans_file + ".tmp"
264 trans_lock = lock_file(trans_file)
265 temp_lock = lock_file(trans_temp)
267 destfile = file(trans_temp, 'w')
268 yaml.dump(from_trans, destfile, default_flow_style=False)
271 os.rename(trans_temp, trans_file)
275 ################################################################################
277 ##########################################
278 #### This usually runs within sudo !! ####
279 ##########################################
280 def write_transitions_from_file(from_file):
282 We have a file we think is valid; if we're using sudo, we invoke it
283 here, otherwise we just parse the file and call write_transitions
285 @attention: This function usually runs B{within sudo}
287 @type from_file: filename
288 @param from_file: filename of a transitions file
292 # Lets check if from_file is in the directory we expect it to be in
293 if not os.path.abspath(from_file).startswith(Cnf["Transitions::TempPath"]):
294 print "Will not accept transitions file outside of %s" % (Cnf["Transitions::TempPath"])
298 os.spawnl(os.P_WAIT, "/usr/bin/sudo", "/usr/bin/sudo", "-u", "dak", "-H",
299 "/usr/local/bin/dak", "transitions", "--import", from_file)
301 trans = load_transitions(from_file)
303 raise TransitionsError, "Unparsable transitions file %s" % (file)
304 write_transitions(trans)
306 ################################################################################
308 def temp_transitions_file(transitions):
310 Open a temporary file and dump the current transitions into it, so users
313 @type transitions: dict
314 @param transitions: current defined transitions
317 @return: path of newly created tempfile
319 @note: NB: file is unlinked by caller, but fd is never actually closed.
320 We need the chmod, as the file is (most possibly) copied from a
321 sudo-ed script and would be unreadable if it has default mkstemp mode
324 (fd, path) = tempfile.mkstemp("", "transitions", Cnf["Transitions::TempPath"])
327 yaml.dump(transitions, f, default_flow_style=False)
330 ################################################################################
332 def edit_transitions():
333 """ Edit the defined transitions. """
334 trans_file = Cnf["Dinstall::Reject::ReleaseTransitions"]
335 edit_file = temp_transitions_file(load_transitions(trans_file))
337 editor = os.environ.get("EDITOR", "vi")
340 result = os.system("%s %s" % (editor, edit_file))
343 utils.fubar("%s invocation failed for %s, not removing tempfile." % (editor, edit_file))
345 # Now try to load the new file
346 test = load_transitions(edit_file)
350 print "Edit was unparsable."
351 prompt = "[E]dit again, Drop changes?"
354 print "Edit looks okay.\n"
355 print "The following transitions are defined:"
356 print "------------------------------------------------------------------------"
357 transition_info(test)
359 prompt = "[S]ave, Edit again, Drop changes?"
363 while prompt.find(answer) == -1:
364 answer = utils.our_raw_input(prompt)
367 answer = answer[:1].upper()
373 print "OK, discarding changes"
379 print "You pressed something you shouldn't have :("
382 # We seem to be done and also have a working file. Copy over.
383 write_transitions_from_file(edit_file)
386 print "Transitions file updated."
388 ################################################################################
390 def check_transitions(transitions):
392 Check if the defined transitions still apply and remove those that no longer do.
393 @note: Asks the user for confirmation first unless -a has been set.
402 session = DBConn().session()
404 # Now look through all defined transitions
405 for trans in transitions:
406 t = transitions[trans]
410 # Will be an empty list if nothing is in testing.
411 sources = get_source_in_suite(source, "testing", session)
413 info[trans] = get_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
417 # No package in testing
418 print "Transition source %s not in testing, transition still ongoing." % (source)
420 current = sources[0].version
421 compare = apt_pkg.VersionCompare(current, expected)
423 # This is still valid, the current version in database is older than
424 # the new version we wait for
425 print "This transition is still ongoing, we currently have version %s" % (current)
427 print "REMOVE: This transition is over, the target package reached testing. REMOVE"
428 print "%s wanted version: %s, has %s" % (source, expected, current)
429 to_remove.append(trans)
431 print "-------------------------------------------------------------------------"
434 prompt = "Removing: "
435 for remove in to_remove:
439 prompt += " Commit Changes? (y/N)"
442 if Options["no-action"]:
444 elif Options["automatic"]:
447 answer = utils.our_raw_input(prompt).lower()
453 print "Not committing changes"
458 subst['__SUBJECT__'] = "Transitions completed: " + ", ".join(sorted(to_remove))
459 subst['__TRANSITION_MESSAGE__'] = "The following transitions were removed:\n"
460 for remove in sorted(to_remove):
461 subst['__TRANSITION_MESSAGE__'] += info[remove] + '\n'
462 del transitions[remove]
464 # If we have a mail address configured for transitions,
465 # send a notification
466 subst['__TRANSITION_EMAIL__'] = Cnf.get("Transitions::Notifications", "")
467 if subst['__TRANSITION_EMAIL__'] != "":
468 print "Sending notification to %s" % subst['__TRANSITION_EMAIL__']
469 subst['__DAK_ADDRESS__'] = Cnf["Dinstall::MyEmailAddress"]
470 subst['__BCC__'] = 'X-DAK: dak transitions'
471 if Cnf.has_key("Dinstall::Bcc"):
472 subst["__BCC__"] += '\nBcc: %s' % Cnf["Dinstall::Bcc"]
473 message = utils.TemplateSubst(subst,
474 os.path.join(Cnf["Dir::Templates"], 'transition.removed'))
475 utils.send_mail(message)
477 edit_file = temp_transitions_file(transitions)
478 write_transitions_from_file(edit_file)
482 print "WTF are you typing?"
485 ################################################################################
487 def get_info(trans, source, expected, rm, reason, packages):
489 Print information about a single transition.
492 @param trans: Transition name
495 @param source: Source package
497 @type expected: string
498 @param expected: Expected version in testing
501 @param rm: Responsible RM
504 @param reason: Reason
507 @param packages: list of blocked packages
510 return """Looking at transition: %s
515 Blocked Packages (total: %d): %s
516 """ % (trans, source, expected, rm, reason, len(packages), ", ".join(packages))
518 ################################################################################
520 def transition_info(transitions):
522 Print information about all defined transitions.
523 Calls L{get_info} for every transition and then tells user if the transition is
524 still ongoing or if the expected version already hit testing.
526 @type transitions: dict
527 @param transitions: defined transitions
529 for trans in transitions:
530 t = transitions[trans]
534 # Will be empty list if nothing is in testing.
535 sources = get_suite_version(source, "testing")
537 print get_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
540 # No package in testing
541 print "Transition source %s not in testing, transition still ongoing." % (source)
543 current = sources[0].version
544 compare = apt_pkg.VersionCompare(current, expected)
545 print "Apt compare says: %s" % (compare)
547 # This is still valid, the current version in database is older than
548 # the new version we wait for
549 print "This transition is still ongoing, we currently have version %s" % (current)
551 print "This transition is over, the target package reached testing, should be removed"
552 print "%s wanted version: %s, has %s" % (source, expected, current)
553 print "-------------------------------------------------------------------------"
555 ################################################################################
559 Prepare the work to be done, do basic checks.
561 @attention: This function may run B{within sudo}
566 #####################################
567 #### This can run within sudo !! ####
568 #####################################
571 # Check if there is a file defined (and existant)
572 transpath = Cnf.get("Dinstall::Reject::ReleaseTransitions", "")
574 utils.warn("Dinstall::Reject::ReleaseTransitions not defined")
576 if not os.path.exists(transpath):
577 utils.warn("ReleaseTransitions file, %s, not found." %
578 (Cnf["Dinstall::Reject::ReleaseTransitions"]))
580 # Also check if our temp directory is defined and existant
581 temppath = Cnf.get("Transitions::TempPath", "")
583 utils.warn("Transitions::TempPath not defined")
585 if not os.path.exists(temppath):
586 utils.warn("Temporary path %s not found." %
587 (Cnf["Transitions::TempPath"]))
590 if Options["import"]:
592 write_transitions_from_file(Options["import"])
593 except TransitionsError, m:
597 ##############################################
598 #### Up to here it can run within sudo !! ####
599 ##############################################
601 # Parse the yaml file
602 transitions = load_transitions(transpath)
603 if transitions == None:
604 # Something very broken with the transitions, exit
605 utils.warn("Could not parse existing transitions file. Aborting.")
609 # Let's edit the transitions file
611 elif Options["check"]:
612 # Check and remove outdated transitions
613 check_transitions(transitions)
615 # Output information about the currently defined transitions.
616 print "Currently defined transitions:"
617 transition_info(transitions)
621 ################################################################################
623 if __name__ == '__main__':