mirror of
https://git.launchpad.net/~ubuntu-release/britney/+git/britney2-ubuntu
synced 2025-07-04 13:01:29 +00:00
Migrate output_write to use a logger
With a bit of code we can replace the manual file-handling for "upgrade_output" with a logger. This will enable us to refactor other bits that currently depend on "output_write" by making those bits use a logger instead. This also migrates "do_hint" to use the new output logger. This is due to "do_hint" being the only method relying on writing of partial lines and maintaining support for that in "output_write" would have been non-trivial. To ensure "pretty" formatting to stdout, the messages in "output_write" are now chopped into multiple lines. The only visible change is that the output to stdout from "output_write" now also includes the prefix with a timestamp. However, then contents of "upgrade_output" remain unchanged deliberately. Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
parent
9e6fcc0ed3
commit
ef8c56f189
53
britney.py
53
britney.py
@ -284,6 +284,11 @@ class Britney(object):
|
|||||||
|
|
||||||
self.logger = logging.getLogger()
|
self.logger = logging.getLogger()
|
||||||
|
|
||||||
|
# Logger for "upgrade_output"; the file handler will be attached later when
|
||||||
|
# we are ready to open the file.
|
||||||
|
self.output_logger = logging.getLogger('britney2.output.upgrade_output')
|
||||||
|
self.output_logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
# parse the command line arguments
|
# parse the command line arguments
|
||||||
self.policies = []
|
self.policies = []
|
||||||
self._hint_parser = HintParser()
|
self._hint_parser = HintParser()
|
||||||
@ -2626,15 +2631,18 @@ class Britney(object):
|
|||||||
requested version is not in unstable, then the hint is skipped.
|
requested version is not in unstable, then the hint is skipped.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
output_logger = self.output_logger
|
||||||
if isinstance(pkgvers[0], tuple) or isinstance(pkgvers[0], list):
|
if isinstance(pkgvers[0], tuple) or isinstance(pkgvers[0], list):
|
||||||
_pkgvers = [ MigrationItem('%s/%s' % (p, v)) for (p,v) in pkgvers ]
|
_pkgvers = [ MigrationItem('%s/%s' % (p, v)) for (p,v) in pkgvers ]
|
||||||
else:
|
else:
|
||||||
_pkgvers = pkgvers
|
_pkgvers = pkgvers
|
||||||
|
|
||||||
self.logger.info("> Processing '%s' hint from %s", hinttype, who)
|
self.logger.info("> Processing '%s' hint from %s", hinttype, who)
|
||||||
self.output_write("Trying %s from %s: %s\n" % (hinttype, who, " ".join("%s/%s" % (x.uvname, x.version) for x in _pkgvers)))
|
output_logger.info("Trying %s from %s: %s", hinttype, who,
|
||||||
|
" ".join("%s/%s" % (x.uvname, x.version) for x in _pkgvers)
|
||||||
|
)
|
||||||
|
|
||||||
ok = True
|
issues = []
|
||||||
# loop on the requested packages and versions
|
# loop on the requested packages and versions
|
||||||
for idx in range(len(_pkgvers)):
|
for idx in range(len(_pkgvers)):
|
||||||
pkg = _pkgvers[idx]
|
pkg = _pkgvers[idx]
|
||||||
@ -2653,19 +2661,19 @@ class Britney(object):
|
|||||||
|
|
||||||
# handle *-proposed-updates
|
# handle *-proposed-updates
|
||||||
if pkg.suite in ['pu', 'tpu']:
|
if pkg.suite in ['pu', 'tpu']:
|
||||||
if pkg.package not in self.sources[pkg.suite]: continue
|
if pkg.package not in self.sources[pkg.suite]:
|
||||||
|
continue
|
||||||
if apt_pkg.version_compare(self.sources[pkg.suite][pkg.package].version, pkg.version) != 0:
|
if apt_pkg.version_compare(self.sources[pkg.suite][pkg.package].version, pkg.version) != 0:
|
||||||
self.output_write(" Version mismatch, %s %s != %s\n" % (pkg.package, pkg.version, self.sources[pkg.suite][pkg.package].version))
|
issues.append("Version mismatch, %s %s != %s" % (pkg.package, pkg.version,
|
||||||
ok = False
|
self.sources[pkg.suite][pkg.package].version))
|
||||||
# does the package exist in unstable?
|
# does the package exist in unstable?
|
||||||
elif not inunstable:
|
elif not inunstable:
|
||||||
self.output_write(" Source %s has no version in unstable\n" % pkg.package)
|
issues.append("Source %s has no version in unstable" % pkg.package)
|
||||||
ok = False
|
|
||||||
elif not rightversion:
|
elif not rightversion:
|
||||||
self.output_write(" Version mismatch, %s %s != %s\n" % (pkg.package, pkg.version, self.sources['unstable'][pkg.package].version))
|
issues.append("Version mismatch, %s %s != %s" % (pkg.package, pkg.version,
|
||||||
ok = False
|
self.sources['unstable'][pkg.package].version))
|
||||||
if not ok:
|
if issues:
|
||||||
self.output_write("Not using hint\n")
|
output_logger.warning("%s: Not using hint", ", ".join(issues))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.do_all(hinttype, _pkgvers)
|
self.do_all(hinttype, _pkgvers)
|
||||||
@ -2776,8 +2784,20 @@ class Britney(object):
|
|||||||
|
|
||||||
def output_write(self, msg):
|
def output_write(self, msg):
|
||||||
"""Simple wrapper for output writing"""
|
"""Simple wrapper for output writing"""
|
||||||
print(msg, end='')
|
# This method used to take full lines and write them directly to a file.
|
||||||
self.__output.write(msg)
|
# As such msg is expected to contain a character to be written plus
|
||||||
|
# explicit newlines.
|
||||||
|
if not msg:
|
||||||
|
# Empty string -> nothing is written (emulating fd.write(''))
|
||||||
|
return
|
||||||
|
# Remove trailing \n as the logger will add one.
|
||||||
|
# - Note that we cannot emulate partial lines (but nothing uses that
|
||||||
|
# so we do not bother trying either).
|
||||||
|
if msg and msg[-1] == '\n':
|
||||||
|
msg = msg[:-1]
|
||||||
|
|
||||||
|
for line in msg.split('\n'):
|
||||||
|
self.output_logger.info(line)
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
"""Main method
|
"""Main method
|
||||||
@ -2796,8 +2816,11 @@ class Britney(object):
|
|||||||
self.upgrade_me = self.options.actions.split()
|
self.upgrade_me = self.options.actions.split()
|
||||||
|
|
||||||
if self.options.compute_migrations or self.options.hint_tester:
|
if self.options.compute_migrations or self.options.hint_tester:
|
||||||
with open(self.options.upgrade_output, 'w', encoding='utf-8') as f:
|
file_handler = logging.FileHandler(self.options.upgrade_output, mode='w', encoding='utf-8')
|
||||||
self.__output = f
|
output_formatter = logging.Formatter('%(message)s')
|
||||||
|
file_handler.setFormatter(output_formatter)
|
||||||
|
self.output_logger.addHandler(file_handler)
|
||||||
|
self.logger.info("Logging upgrade output to %s", self.options.upgrade_output)
|
||||||
|
|
||||||
# run the hint tester
|
# run the hint tester
|
||||||
if self.options.hint_tester:
|
if self.options.hint_tester:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user