diff --git a/britney.py b/britney.py index dff1680..d274089 100755 --- a/britney.py +++ b/britney.py @@ -186,14 +186,12 @@ import string import time import optparse import urllib -import yaml import apt_pkg from functools import reduce, partial from itertools import chain, ifilter, product from operator import attrgetter -from datetime import datetime if __name__ == '__main__': # Check if there is a python-search dir for this version of @@ -217,7 +215,8 @@ from hints import HintCollection from britney_util import (old_libraries_format, same_source, undo_changes, register_reverses, compute_reverse_tree, read_nuninst, write_nuninst, write_heidi, - eval_uninst, newly_uninst, make_migrationitem) + eval_uninst, newly_uninst, make_migrationitem, + write_excuses) from consts import (VERSION, SECTION, BINARIES, MAINTAINER, FAKESRC, SOURCE, SOURCEVER, ARCHITECTURE, DEPENDS, CONFLICTS, PROVIDES, RDEPENDS, RCONFLICTS, MULTIARCH, ESSENTIAL) @@ -1668,28 +1667,12 @@ class Britney(object): # write excuses to the output file if not self.options.dry_run: self.__log("> Writing Excuses to %s" % self.options.excuses_output, type="I") - f = open(self.options.excuses_output, 'w') - f.write("\n") - f.write("excuses...") - f.write("\n") - f.write("

Generated: " + time.strftime("%Y.%m.%d %H:%M:%S %z", time.gmtime(time.time())) + "

\n") - f.write("\n") - f.close() - + write_excuses(self.excuses, self.options.excuses_output, + output_format="legacy-html") if hasattr(self.options, 'excuses_yaml_output'): self.__log("> Writing YAML Excuses to %s" % self.options.excuses_yaml_output, type="I") - f = open(self.options.excuses_yaml_output, 'w') - excuselist = [] - for e in self.excuses: - excuselist.append(e.excusedata()) - excusesdata = {} - excusesdata["sources"] = excuselist - excusesdata["generated"] = datetime.utcnow() - f.write(yaml.dump(excusesdata, default_flow_style=False, allow_unicode=True)) - f.close() + write_excuses(self.excuses, self.options.excuses_yaml_output, + output_format="yaml") self.__log("Update Excuses generation completed", type="I") diff --git a/britney_util.py b/britney_util.py index be87e13..826ab53 100644 --- a/britney_util.py +++ b/britney_util.py @@ -23,9 +23,12 @@ import apt_pkg from functools import partial +from datetime import datetime from itertools import chain, ifilter, ifilterfalse, izip, repeat import re import time +import yaml + from migrationitem import MigrationItem, UnversionnedMigrationItem from consts import (VERSION, BINARIES, PROVIDES, DEPENDS, CONFLICTS, @@ -413,3 +416,34 @@ def make_migrationitem(package, sources, VERSION=VERSION): item = UnversionnedMigrationItem(package) return MigrationItem("%s/%s" % (item.uvname, sources[item.suite][item.package][VERSION])) + + +def write_excuses(excuses, dest_file, output_format="yaml"): + """Write the excuses to dest_file + + Writes a list of excuses in a specified output_format to the + path denoted by dest_file. The output_format can either be "yaml" + or "legacy-html". + """ + if output_format == "yaml": + with open(dest_file, 'w') as f: + excuselist = [] + for e in excuses: + excuselist.append(e.excusedata()) + excusesdata = {} + excusesdata["sources"] = excuselist + excusesdata["generated"] = datetime.utcnow() + f.write(yaml.dump(excusesdata, default_flow_style=False, allow_unicode=True)) + elif output_format == "legacy-html": + with open(dest_file, 'w') as f: + f.write("\n") + f.write("excuses...") + f.write("\n") + f.write("

Generated: " + time.strftime("%Y.%m.%d %H:%M:%S %z", time.gmtime(time.time())) + "

\n") + f.write("\n") + else: + raise ValueError('Output format must be either "yaml or "legacy-html"') +