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")
87 username = utils.getusername()
89 print "Non-dak user: %s" % username
92 # Initialise DB connection
95 ################################################################################
97 def usage (exit_code=0):
98 print """Usage: transitions [OPTION]...
99 Update and check the release managers transition file.
103 -h, --help show this help and exit.
104 -e, --edit edit the transitions file
105 -i, --import <file> check and import transitions from file
106 -c, --check check the transitions file, remove outdated entries
107 -S, --sudo use sudo to update transitions file
108 -a, --automatic don't prompt (only affects check).
109 -n, --no-action don't do anything (only affects check)"""
113 ################################################################################
115 #####################################
116 #### This may run within sudo !! ####
117 #####################################
118 def load_transitions(trans_file):
120 Parse a transition yaml file and check it for validity.
122 @attention: This function may run B{within sudo}
124 @type trans_file: string
125 @param trans_file: filename to parse
128 @return: validated dictionary of transition entries or None
129 if validation fails, empty string if reading C{trans_file}
130 returned something else than a dict
133 # Parse the yaml file
134 sourcefile = file(trans_file, 'r')
135 sourcecontent = sourcefile.read()
138 trans = yaml.load(sourcecontent)
139 except yaml.YAMLError, exc:
140 # Someone fucked it up
141 print "ERROR: %s" % (exc)
144 # lets do further validation here
145 checkkeys = ["source", "reason", "packages", "new", "rm"]
147 # If we get an empty definition - we just have nothing to check, no transitions defined
148 if type(trans) != dict:
149 # This can be anything. We could have no transitions defined. Or someone totally fucked up the
150 # file, adding stuff in a way we dont know or want. Then we set it empty - and simply have no
151 # transitions anymore. User will see it in the information display after he quit the editor and
160 # First check if we know all the keys for the transition and if they have
161 # the right type (and for the packages also if the list has the right types
162 # included, ie. not a list in list, but only str in the list)
164 if key not in checkkeys:
165 print "ERROR: Unknown key %s in transition %s" % (key, test)
168 if key == "packages":
169 if type(t[key]) != list:
170 print "ERROR: Unknown type %s for packages in transition %s." % (type(t[key]), test)
173 for package in t["packages"]:
174 if type(package) != str:
175 print "ERROR: Packages list contains invalid type %s (as %s) in transition %s" % (type(package), package, test)
177 if re_broken_package.match(package):
178 # Someone had a space too much (or not enough), we have something looking like
179 # "package1 - package2" now.
180 print "ERROR: Invalid indentation of package list in transition %s, around package(s): %s" % (test, package)
183 # In case someone has an empty packages list
184 print "ERROR: No packages defined in transition %s" % (test)
188 elif type(t[key]) != str:
189 if key == "new" and type(t[key]) == int:
190 # Ok, debian native version
193 print "ERROR: Unknown type %s for key %s in transition %s" % (type(t[key]), key, test)
196 # And now the other way round - are all our keys defined?
197 for key in checkkeys:
199 print "ERROR: Missing key %s in transition %s" % (key, test)
202 # In case someone defined very broken things
203 print "ERROR: Unable to parse the file"
212 ################################################################################
214 #####################################
215 #### This may run within sudo !! ####
216 #####################################
221 @attention: This function may run B{within sudo}
224 for retry in range(10):
225 lock_fd = os.open(f, os.O_RDWR | os.O_CREAT)
227 fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
230 if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EEXIST':
231 print "Unable to get lock for %s (try %d of 10)" % \
237 utils.fubar("Couldn't obtain lock for %s." % (f))
239 ################################################################################
241 #####################################
242 #### This may run within sudo !! ####
243 #####################################
244 def write_transitions(from_trans):
246 Update the active transitions file safely.
247 This function takes a parsed input file (which avoids invalid
248 files or files that may be be modified while the function is
249 active) and ensure the transitions file is updated atomically
252 @attention: This function may run B{within sudo}
254 @type from_trans: dict
255 @param from_trans: transitions dictionary, as returned by L{load_transitions}
259 trans_file = Cnf["Dinstall::Reject::ReleaseTransitions"]
260 trans_temp = trans_file + ".tmp"
262 trans_lock = lock_file(trans_file)
263 temp_lock = lock_file(trans_temp)
265 destfile = file(trans_temp, 'w')
266 yaml.dump(from_trans, destfile, default_flow_style=False)
269 os.rename(trans_temp, trans_file)
273 ################################################################################
275 ##########################################
276 #### This usually runs within sudo !! ####
277 ##########################################
278 def write_transitions_from_file(from_file):
280 We have a file we think is valid; if we're using sudo, we invoke it
281 here, otherwise we just parse the file and call write_transitions
283 @attention: This function usually runs B{within sudo}
285 @type from_file: filename
286 @param from_file: filename of a transitions file
290 # Lets check if from_file is in the directory we expect it to be in
291 if not os.path.abspath(from_file).startswith(Cnf["Transitions::TempPath"]):
292 print "Will not accept transitions file outside of %s" % (Cnf["Transitions::TempPath"])
296 os.spawnl(os.P_WAIT, "/usr/bin/sudo", "/usr/bin/sudo", "-u", "dak", "-H",
297 "/usr/local/bin/dak", "transitions", "--import", from_file)
299 trans = load_transitions(from_file)
301 raise TransitionsError, "Unparsable transitions file %s" % (file)
302 write_transitions(trans)
304 ################################################################################
306 def temp_transitions_file(transitions):
308 Open a temporary file and dump the current transitions into it, so users
311 @type transitions: dict
312 @param transitions: current defined transitions
315 @return: path of newly created tempfile
317 @note: NB: file is unlinked by caller, but fd is never actually closed.
318 We need the chmod, as the file is (most possibly) copied from a
319 sudo-ed script and would be unreadable if it has default mkstemp mode
322 (fd, path) = tempfile.mkstemp("", "transitions", Cnf["Transitions::TempPath"])
325 yaml.dump(transitions, f, default_flow_style=False)
328 ################################################################################
330 def edit_transitions():
331 """ Edit the defined transitions. """
332 trans_file = Cnf["Dinstall::Reject::ReleaseTransitions"]
333 edit_file = temp_transitions_file(load_transitions(trans_file))
335 editor = os.environ.get("EDITOR", "vi")
338 result = os.system("%s %s" % (editor, edit_file))
341 utils.fubar("%s invocation failed for %s, not removing tempfile." % (editor, edit_file))
343 # Now try to load the new file
344 test = load_transitions(edit_file)
348 print "Edit was unparsable."
349 prompt = "[E]dit again, Drop changes?"
352 print "Edit looks okay.\n"
353 print "The following transitions are defined:"
354 print "------------------------------------------------------------------------"
355 transition_info(test)
357 prompt = "[S]ave, Edit again, Drop changes?"
361 while prompt.find(answer) == -1:
362 answer = utils.our_raw_input(prompt)
365 answer = answer[:1].upper()
371 print "OK, discarding changes"
377 print "You pressed something you shouldn't have :("
380 # We seem to be done and also have a working file. Copy over.
381 write_transitions_from_file(edit_file)
384 print "Transitions file updated."
386 ################################################################################
388 def check_transitions(transitions):
390 Check if the defined transitions still apply and remove those that no longer do.
391 @note: Asks the user for confirmation first unless -a has been set.
400 session = DBConn().session()
402 # Now look through all defined transitions
403 for trans in transitions:
404 t = transitions[trans]
408 # Will be an empty list if nothing is in testing.
409 sources = get_source_in_suite(source, "testing", session)
411 info[trans] = get_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
415 # No package in testing
416 print "Transition source %s not in testing, transition still ongoing." % (source)
418 current = sources[0].version
419 compare = apt_pkg.VersionCompare(current, expected)
421 # This is still valid, the current version in database is older than
422 # the new version we wait for
423 print "This transition is still ongoing, we currently have version %s" % (current)
425 print "REMOVE: This transition is over, the target package reached testing. REMOVE"
426 print "%s wanted version: %s, has %s" % (source, expected, current)
427 to_remove.append(trans)
429 print "-------------------------------------------------------------------------"
432 prompt = "Removing: "
433 for remove in to_remove:
437 prompt += " Commit Changes? (y/N)"
440 if Options["no-action"]:
442 elif Options["automatic"]:
445 answer = utils.our_raw_input(prompt).lower()
451 print "Not committing changes"
456 subst['__SUBJECT__'] = "Transitions completed: " + ", ".join(sorted(to_remove))
457 subst['__TRANSITION_MESSAGE__'] = "The following transitions were removed:\n"
458 for remove in sorted(to_remove):
459 subst['__TRANSITION_MESSAGE__'] += info[remove] + '\n'
460 del transitions[remove]
462 # If we have a mail address configured for transitions,
463 # send a notification
464 subst['__TRANSITION_EMAIL__'] = Cnf.get("Transitions::Notifications", "")
465 if subst['__TRANSITION_EMAIL__'] != "":
466 print "Sending notification to %s" % subst['__TRANSITION_EMAIL__']
467 subst['__DAK_ADDRESS__'] = Cnf["Dinstall::MyEmailAddress"]
468 subst['__BCC__'] = 'X-DAK: dak transitions'
469 if Cnf.has_key("Dinstall::Bcc"):
470 subst["__BCC__"] += '\nBcc: %s' % Cnf["Dinstall::Bcc"]
471 message = utils.TemplateSubst(subst,
472 os.path.join(Cnf["Dir::Templates"], 'transition.removed'))
473 utils.send_mail(message)
475 edit_file = temp_transitions_file(transitions)
476 write_transitions_from_file(edit_file)
480 print "WTF are you typing?"
483 ################################################################################
485 def get_info(trans, source, expected, rm, reason, packages):
487 Print information about a single transition.
490 @param trans: Transition name
493 @param source: Source package
495 @type expected: string
496 @param expected: Expected version in testing
499 @param rm: Responsible RM
502 @param reason: Reason
505 @param packages: list of blocked packages
508 return """Looking at transition: %s
513 Blocked Packages (total: %d): %s
514 """ % (trans, source, expected, rm, reason, len(packages), ", ".join(packages))
516 ################################################################################
518 def transition_info(transitions):
520 Print information about all defined transitions.
521 Calls L{get_info} for every transition and then tells user if the transition is
522 still ongoing or if the expected version already hit testing.
524 @type transitions: dict
525 @param transitions: defined transitions
527 for trans in transitions:
528 t = transitions[trans]
532 # Will be empty list if nothing is in testing.
533 sources = get_suite_version(source, "testing")
535 print get_info(trans, source, expected, t["rm"], t["reason"], t["packages"])
538 # No package in testing
539 print "Transition source %s not in testing, transition still ongoing." % (source)
541 current = sources[0].version
542 compare = apt_pkg.VersionCompare(current, expected)
543 print "Apt compare says: %s" % (compare)
545 # This is still valid, the current version in database is older than
546 # the new version we wait for
547 print "This transition is still ongoing, we currently have version %s" % (current)
549 print "This transition is over, the target package reached testing, should be removed"
550 print "%s wanted version: %s, has %s" % (source, expected, current)
551 print "-------------------------------------------------------------------------"
553 ################################################################################
557 Prepare the work to be done, do basic checks.
559 @attention: This function may run B{within sudo}
564 #####################################
565 #### This can run within sudo !! ####
566 #####################################
569 # Check if there is a file defined (and existant)
570 transpath = Cnf.get("Dinstall::Reject::ReleaseTransitions", "")
572 utils.warn("Dinstall::Reject::ReleaseTransitions not defined")
574 if not os.path.exists(transpath):
575 utils.warn("ReleaseTransitions file, %s, not found." %
576 (Cnf["Dinstall::Reject::ReleaseTransitions"]))
578 # Also check if our temp directory is defined and existant
579 temppath = Cnf.get("Transitions::TempPath", "")
581 utils.warn("Transitions::TempPath not defined")
583 if not os.path.exists(temppath):
584 utils.warn("Temporary path %s not found." %
585 (Cnf["Transitions::TempPath"]))
588 if Options["import"]:
590 write_transitions_from_file(Options["import"])
591 except TransitionsError, m:
595 ##############################################
596 #### Up to here it can run within sudo !! ####
597 ##############################################
599 # Parse the yaml file
600 transitions = load_transitions(transpath)
601 if transitions == None:
602 # Something very broken with the transitions, exit
603 utils.warn("Could not parse existing transitions file. Aborting.")
607 # Let's edit the transitions file
609 elif Options["check"]:
610 # Check and remove outdated transitions
611 check_transitions(transitions)
613 # Output information about the currently defined transitions.
614 print "Currently defined transitions:"
615 transition_info(transitions)
619 ################################################################################
621 if __name__ == '__main__':