From 90e8fe81e1b2610e352c82c0301076ffc7da5ac0 Mon Sep 17 00:00:00 2001 From: Dan Streetman Date: Fri, 12 Oct 2018 18:54:07 -0400 Subject: [PATCH] replace ubuntutools.logger with standard python logging --- backportpackage | 8 +- bitesize | 4 +- grep-merges | 11 ++- hugdaylist | 25 +++--- import-bug-from-debian | 6 +- merge-changelog | 7 +- pbuilder-dist | 19 +++-- pull-debian-debdiff | 12 +-- requestbackport | 31 ++++---- requestsync | 76 +++++++++--------- reverse-depends | 50 ++++++------ seeded-in-ubuntu | 24 +++--- sponsor-patch | 10 ++- submittodebian | 17 ++-- syncpackage | 86 +++++++++++---------- ubuntu-build | 68 ++++++++-------- ubuntu-iso | 8 +- ubuntu-upload-permission | 56 +++++++------- ubuntutools/__init__.py | 17 ++++ ubuntutools/archive.py | 49 ++++++------ ubuntutools/builder.py | 21 ++--- ubuntutools/config.py | 13 ++-- ubuntutools/logger.py | 76 ------------------ ubuntutools/lp/lpapicache.py | 19 +++-- ubuntutools/pullpkg.py | 42 ++++++---- ubuntutools/requestsync/lp.py | 6 +- ubuntutools/requestsync/mail.py | 16 ++-- ubuntutools/sponsor_patch/bugtask.py | 6 +- ubuntutools/sponsor_patch/patch.py | 14 ++-- ubuntutools/sponsor_patch/source_package.py | 42 +++++----- ubuntutools/sponsor_patch/sponsor_patch.py | 50 ++++++------ ubuntutools/test/test_archive.py | 12 --- ubuntutools/test/test_config.py | 35 ++++----- ubuntutools/test/test_logger.py | 54 ------------- ubuntutools/test/test_update_maintainer.py | 25 +++--- ubuntutools/update_maintainer.py | 8 +- 36 files changed, 478 insertions(+), 545 deletions(-) delete mode 100644 ubuntutools/logger.py delete mode 100644 ubuntutools/test/test_logger.py diff --git a/backportpackage b/backportpackage index c8f1ef0..1df7129 100755 --- a/backportpackage +++ b/backportpackage @@ -36,11 +36,13 @@ from ubuntutools.builder import get_builder from ubuntutools.lp.lpapicache import (Launchpad, Distribution, SeriesNotFoundException, PackageNotFoundException) -from ubuntutools.logger import Logger from ubuntutools.misc import (system_distribution, vendor_to_distroinfo, codename_to_distribution) from ubuntutools.question import YesNoQuestion +from ubuntutools import getLogger +Logger = getLogger(__name__) + def error(msg): Logger.error(msg) @@ -48,7 +50,7 @@ def error(msg): def check_call(cmd, *args, **kwargs): - Logger.command(cmd) + Logger.debug(' '.join(cmd)) ret = subprocess.call(cmd, *args, **kwargs) if ret != 0: error('%s returned %d.' % (cmd[0], ret)) @@ -302,7 +304,7 @@ def orig_needed(upload, workdir, pkg): not headers['content-location'].startswith('https://launchpadlibrarian.net')): return True except HttpLib2Error as e: - Logger.info(e) + Logger.debug(e) return True return False diff --git a/bitesize b/bitesize index 6fd1b47..5a2c132 100755 --- a/bitesize +++ b/bitesize @@ -28,7 +28,9 @@ from launchpadlib.launchpad import Launchpad from launchpadlib.errors import HTTPError from ubuntutools.config import UDTConfig -from ubuntutools.logger import Logger + +from ubuntutools import getLogger +Logger = getLogger(__name__) def error_out(msg): diff --git a/grep-merges b/grep-merges index 3658a1b..7c75cd4 100755 --- a/grep-merges +++ b/grep-merges @@ -27,6 +27,9 @@ from httplib2 import Http, HttpLib2Error import ubuntutools.misc +from ubuntutools import getLogger +Logger = getLogger(__name__) + def main(): parser = optparse.OptionParser( @@ -52,11 +55,11 @@ def main(): try: headers, page = Http().request(url) except HttpLib2Error as e: - print(str(e), file=sys.stderr) + Logger.exception(e) sys.exit(1) if headers.status != 200: - print("%s: %s %s" % (url, headers.status, headers.reason), - file=sys.stderr) + Logger.error("%s: %s %s" % (url, headers.status, + headers.reason)) sys.exit(1) for merge in json.loads(page): @@ -71,7 +74,7 @@ def main(): pretty_uploader = '{} {}'.format(author, uploader) if (match is None or match in package or match in author or match in uploader or match in teams): - print('%s\t%s' % (package, pretty_uploader)) + Logger.info('%s\t%s' % (package, pretty_uploader)) if __name__ == '__main__': diff --git a/hugdaylist b/hugdaylist index 37f531f..5140ae1 100755 --- a/hugdaylist +++ b/hugdaylist @@ -36,6 +36,9 @@ from launchpadlib.launchpad import Launchpad from ubuntutools.lp.libsupport import translate_web_api +from ubuntutools import getLogger +Logger = getLogger(__name__) + def check_args(): howmany = -1 @@ -57,8 +60,7 @@ def check_args(): # Check that we have an URL. if not args: - print("An URL pointing to a Launchpad bug list is required.", - file=sys.stderr) + Logger.error("An URL pointing to a Launchpad bug list is required.") opt_parser.print_help() sys.exit(1) else: @@ -87,15 +89,14 @@ def main(): if len(url.split("?", 1)) == 2: # search options not supported, because there is no mapping web ui # options <-> API options - print("Options in url are not supported, url: %s" % url, - file=sys.stderr) + Logger.error("Options in url are not supported, url: %s" % url) sys.exit(1) launchpad = None try: launchpad = Launchpad.login_with("ubuntu-dev-tools", 'production') except IOError as error: - print(error) + Logger.exception(error) sys.exit(1) api_url = translate_web_api(url, launchpad) @@ -104,8 +105,8 @@ def main(): except Exception as error: response = getattr(error, "response", {}) if response.get("status", None) == "404": - print(("The URL at '%s' does not appear to be a valid url to a " - "product") % url, file=sys.stderr) + Logger.error("The URL at '%s' does not appear to be a " + "valid url to a product" % url) sys.exit(1) else: raise @@ -113,12 +114,12 @@ def main(): bug_list = [b for b in product.searchTasks() if filter_unsolved(b)] if not bug_list: - print("Bug list of %s is empty." % url) + Logger.info("Bug list of %s is empty." % url) sys.exit(0) if howmany == -1: howmany = len(bug_list) - print(""" + Logger.info(""" ## || This task is done || somebody || || ## || This task is assigned || somebody || || ## || This task isn't || ... || || @@ -128,13 +129,13 @@ def main(): for i in list(bug_list)[:howmany]: bug = i.bug - print('|| [%s %s] || %s || ||' - % (bug.web_link, bug.id, bug.title)) + Logger.info('|| [%s %s] || %s || ||' % + (bug.web_link, bug.id, bug.title)) if __name__ == '__main__': try: main() except KeyboardInterrupt: - print("Aborted.", file=sys.stderr) + Logger.error("Aborted.") sys.exit(1) diff --git a/import-bug-from-debian b/import-bug-from-debian index df3d21d..079ce3c 100755 --- a/import-bug-from-debian +++ b/import-bug-from-debian @@ -30,7 +30,9 @@ import webbrowser from launchpadlib.launchpad import Launchpad from ubuntutools.config import UDTConfig -from ubuntutools.logger import Logger + +from ubuntutools import getLogger +Logger = getLogger(__name__) try: import debianbts @@ -134,7 +136,7 @@ def main(): d_watch = u_bug.addWatch(remote_bug=bug_num, bug_tracker=lp_debbugs) d_task.bug_watch = d_watch d_task.lp_save() - Logger.normal("Opened %s", u_bug.web_link) + Logger.info("Opened %s", u_bug.web_link) if not options.browserless: webbrowser.open(u_bug.web_link) diff --git a/merge-changelog b/merge-changelog index 9d0b67e..f14d4db 100755 --- a/merge-changelog +++ b/merge-changelog @@ -22,9 +22,12 @@ import sys from debian.changelog import Changelog +from ubuntutools import getLogger +Logger = getLogger(__name__) + def usage(exit_code=1): - print('''Usage: merge-changelog + Logger.info('''Usage: merge-changelog merge-changelog takes two changelogs that once shared a common source, merges them back together, and prints the merged result to stdout. This @@ -61,7 +64,7 @@ def merge_changelog(left_changelog, right_changelog): assert block.version == version - print(str(block).strip(), end='\n\n') + Logger.info(str(block).strip() + '\n\n') def main(): diff --git a/pbuilder-dist b/pbuilder-dist index 4fff772..0971b0a 100755 --- a/pbuilder-dist +++ b/pbuilder-dist @@ -39,9 +39,11 @@ from distro_info import DebianDistroInfo, UbuntuDistroInfo, DistroDataOutdated import ubuntutools.misc import ubuntutools.version from ubuntutools.config import UDTConfig -from ubuntutools.logger import Logger from ubuntutools.question import YesNoQuestion +from ubuntutools import getLogger +Logger = getLogger(__name__) + class PbuilderDist(object): def __init__(self, builder): @@ -103,9 +105,10 @@ class PbuilderDist(object): '~/pbuilder/')) if 'SUDO_USER' in os.environ: - Logger.warn('Running under sudo. ' - 'This is probably not what you want. ' - 'pbuilder-dist will use sudo itself, when necessary.') + Logger.warning('Running under sudo. ' + 'This is probably not what you want. ' + 'pbuilder-dist will use sudo itself, ' + 'when necessary.') if os.stat(os.environ['HOME']).st_uid != os.getuid(): Logger.error("You don't own $HOME") sys.exit(1) @@ -280,7 +283,7 @@ class PbuilderDist(object): codename = debian_info.codename(self.target_distro, default=self.target_distro) except DistroDataOutdated as error: - Logger.warn(error) + Logger.warning(error) if codename in (debian_info.devel(), 'experimental'): self.enable_security = False self.enable_updates = False @@ -307,7 +310,7 @@ class PbuilderDist(object): try: dev_release = self.target_distro == UbuntuDistroInfo().devel() except DistroDataOutdated as error: - Logger.warn(error) + Logger.warning(error) dev_release = True if dev_release: @@ -396,7 +399,7 @@ def show_help(exit_code=0): Print a help message for pbuilder-dist, and exit with the given code. """ - print('See man pbuilder-dist for more information.') + Logger.info('See man pbuilder-dist for more information.') sys.exit(exit_code) @@ -498,7 +501,7 @@ def main(): if '--debug-echo' not in args: sys.exit(subprocess.call(app.get_command(args))) else: - print(app.get_command([arg for arg in args if arg != '--debug-echo'])) + Logger.info(app.get_command([arg for arg in args if arg != '--debug-echo'])) if __name__ == '__main__': diff --git a/pull-debian-debdiff b/pull-debian-debdiff index 9685c80..50f15ce 100755 --- a/pull-debian-debdiff +++ b/pull-debian-debdiff @@ -24,9 +24,11 @@ import debian.changelog from ubuntutools.archive import DebianSourcePackage, DownloadError from ubuntutools.config import UDTConfig -from ubuntutools.logger import Logger from ubuntutools.version import Version +from ubuntutools import getLogger +Logger = getLogger(__name__) + def previous_version(package, version, distance): "Given an (extracted) package, determine the version distance versions ago" @@ -79,7 +81,7 @@ def main(): opts.debsec_mirror = config.get_value('DEBSEC_MIRROR') mirrors = [opts.debsec_mirror, opts.debian_mirror] - Logger.normal('Downloading %s %s', package, version) + Logger.info('Downloading %s %s', package, version) newpkg = DebianSourcePackage(package, version, mirrors=mirrors) try: @@ -96,7 +98,7 @@ def main(): if not oldversion: Logger.error('No previous version could be found') sys.exit(1) - Logger.normal('Downloading %s %s', package, oldversion) + Logger.info('Downloading %s %s', package, oldversion) oldpkg = DebianSourcePackage(package, oldversion, mirrors=mirrors) try: @@ -104,11 +106,11 @@ def main(): except DownloadError as e: Logger.error('Failed to download: %s', str(e)) sys.exit(1) - print('file://' + oldpkg.debdiff(newpkg, diffstat=True)) + Logger.info('file://' + oldpkg.debdiff(newpkg, diffstat=True)) if __name__ == '__main__': try: main() except KeyboardInterrupt: - Logger.normal('User abort.') + Logger.info('User abort.') diff --git a/requestbackport b/requestbackport index e660f51..a6e30f3 100755 --- a/requestbackport +++ b/requestbackport @@ -25,11 +25,13 @@ from distro_info import UbuntuDistroInfo from ubuntutools.config import UDTConfig from ubuntutools.lp.lpapicache import Launchpad, Distribution from ubuntutools.lp.udtexceptions import PackageNotFoundException -from ubuntutools.logger import Logger from ubuntutools.question import (YesNoQuestion, EditBugReport, confirmation_prompt) from ubuntutools.rdepends import query_rdepends, RDependsException +from ubuntutools import getLogger +Logger = getLogger(__name__) + class DestinationException(Exception): pass @@ -106,16 +108,11 @@ def check_existing(package, destinations): if not bugs: return - Logger.normal("There are existing bug reports that look similar to your " - "request. Please check before continuing:") + Logger.info("There are existing bug reports that look similar to your " + "request. Please check before continuing:") - by_id = {} - for bug_task in bugs: - bug = bug_task.bug - by_id[bug.id] = bug - - for id_, bug in sorted(by_id.items()): - Logger.normal(" * LP: #%-7i: %s %s", bug.id, bug.title, bug.web_link) + for bug in sorted(set(bug_task.bug for bug_task in bugs)): + Logger.info(" * LP: #%-7i: %s %s", bug.id, bug.title, bug.web_link) confirmation_prompt() @@ -190,8 +187,8 @@ def locate_package(package, distribution): except KeyError: continue package = apt_pkg.candidate.source_name - Logger.normal("Binary package specified, considering its source " - "package instead: %s", package) + Logger.info("Binary package specified, considering its source " + "package instead: %s", package) def request_backport(package_spph, source, destinations): @@ -204,8 +201,8 @@ def request_backport(package_spph, source, destinations): Logger.error("%s (%s) has no published binaries in %s. ", package_spph.getPackageName(), package_spph.getVersion(), source) - Logger.normal("Is it stuck in bin-NEW? It can't be backported until " - "the binaries have been accepted.") + Logger.info("Is it stuck in bin-NEW? It can't be backported until " + "the binaries have been accepted.") sys.exit(1) testing = [] @@ -256,8 +253,8 @@ def request_backport(package_spph, source, destinations): editor.edit() subject, body = editor.get_report() - Logger.normal('The final report is:\nSummary: %s\nDescription:\n%s\n', - subject, body) + Logger.info('The final report is:\nSummary: %s\nDescription:\n%s\n', + subject, body) if YesNoQuestion().ask("Request this backport", "yes") == "no": sys.exit(1) @@ -268,7 +265,7 @@ def request_backport(package_spph, source, destinations): for target in targets[1:]: bug.addTask(target=target) - Logger.normal("Backport request filed as %s", bug.web_link) + Logger.info("Backport request filed as %s", bug.web_link) def main(): diff --git a/requestsync b/requestsync index 6d77bb8..a12a669 100755 --- a/requestsync +++ b/requestsync @@ -38,6 +38,9 @@ from ubuntutools.misc import require_utf8 from ubuntutools.question import confirmation_prompt, EditBugReport from ubuntutools.version import Version +from ubuntutools import getLogger +Logger = getLogger(__name__) + # # entry point # @@ -97,7 +100,7 @@ def main(): config = UDTConfig(options.no_conf) if options.deprecated_lp_flag: - print("The --lp flag is now default, ignored.") + Logger.info("The --lp flag is now default, ignored.") if options.email: options.lpapi = False else: @@ -115,8 +118,8 @@ def main(): elif options.lpinstance == 'staging': bug_mail_domain = 'bugs.staging.launchpad.net' else: - print('Error: Unknown launchpad instance: %s' % options.lpinstance, - file=sys.stderr) + Logger.error('Error: Unknown launchpad instance: %s' + % options.lpinstance) sys.exit(1) mailserver_host = config.get_value('SMTP_SERVER', @@ -130,8 +133,8 @@ def main(): firstmx = mxlist[0] mailserver_host = firstmx[1] except ImportError: - print('Please install python3-dns to support Launchpad mail ' - 'server lookup.', file=sys.stderr) + Logger.error('Please install python-dns to support ' + 'Launchpad mail server lookup.') sys.exit(1) mailserver_port = config.get_value('SMTP_PORT', default=25, @@ -167,9 +170,8 @@ def main(): get_ubuntu_delta_changelog, mail_bug, need_sponsorship) if not any(x in os.environ for x in ('UBUMAIL', 'DEBEMAIL', 'EMAIL')): - print('E: The environment variable UBUMAIL, DEBEMAIL or EMAIL ' - 'needs to be set to let this script mail the sync request.', - file=sys.stderr) + Logger.error('The environment variable UBUMAIL, DEBEMAIL or EMAIL needs ' + 'to be set to let this script mail the sync request.') sys.exit(1) newsource = options.newpkg @@ -187,15 +189,14 @@ def main(): else: ubu_info = UbuntuDistroInfo() release = ubu_info.devel() - print('W: Target release missing - assuming %s' % release, - file=sys.stderr) + Logger.warning('Target release missing - assuming %s' % release) elif len(args) == 2: release = args[1] elif len(args) == 3: release = args[1] force_base_version = Version(args[2]) else: - print('E: Too many arguments.', file=sys.stderr) + Logger.error('Too many arguments.') parser.print_help() sys.exit(1) @@ -210,13 +211,12 @@ def main(): ubuntu_version = Version('~') ubuntu_component = None # Set after getting the Debian info if not newsource: - print(("'%s' doesn't exist in 'Ubuntu %s'.\n" - "Do you want to sync a new package?") - % (srcpkg, release)) + Logger.info("'%s' doesn't exist in 'Ubuntu %s'." % (srcpkg, release)) + Logger.info("Do you want to sync a new package?") confirmation_prompt() newsource = True except udtexceptions.SeriesNotFoundException as error: - print("E: %s" % error, file=sys.stderr) + Logger.error(error) sys.exit(1) # Get the requested Debian source package @@ -225,10 +225,10 @@ def main(): debian_version = Version(debian_srcpkg.getVersion()) debian_component = debian_srcpkg.getComponent() except udtexceptions.PackageNotFoundException as error: - print("E: %s" % error, file=sys.stderr) + Logger.error(error) sys.exit(1) except udtexceptions.SeriesNotFoundException as error: - print("E: %s" % error, file=sys.stderr) + Logger.error(error) sys.exit(1) if ubuntu_component is None: @@ -246,17 +246,17 @@ def main(): debian_version = Version(debian_srcpkg.getVersion()) debian_component = debian_srcpkg.getComponent() except udtexceptions.PackageNotFoundException as error: - print("E: %s" % error, file=sys.stderr) + Logger.error(error) sys.exit(1) if ubuntu_version == debian_version: - print('E: The versions in Debian and Ubuntu are the same already ' - '(%s). Aborting.' % ubuntu_version, file=sys.stderr) + Logger.error('The versions in Debian and Ubuntu are the ' + 'same already (%s). Aborting.' % ubuntu_version) sys.exit(1) if ubuntu_version > debian_version: - print(('E: The version in Ubuntu (%s) is newer than the version in ' - 'Debian (%s). Aborting.') - % (ubuntu_version, debian_version), file=sys.stderr) + Logger.error('The version in Ubuntu (%s) is newer than ' + 'the version in Debian (%s). Aborting.' + % (ubuntu_version, debian_version)) sys.exit(1) # -s flag not specified - check if we do need sponsorship @@ -264,8 +264,8 @@ def main(): sponsorship = need_sponsorship(srcpkg, ubuntu_component, release) if not sponsorship and not ffe: - print('Consider using syncpackage(1) for syncs that do not require ' - 'feature freeze exceptions.', file=sys.stderr) + Logger.error('Consider using syncpackage(1) for syncs that ' + 'do not require feature freeze exceptions.') # Check for existing package reports if not newsource: @@ -283,9 +283,9 @@ def main(): if 'ubuntu' in str(ubuntu_version): need_interaction = True - print('Changes have been made to the package in Ubuntu.\n' - 'Please edit the report and give an explanation.\n' - 'Not saving the report file will abort the request.') + Logger.info('Changes have been made to the package in Ubuntu.') + Logger.info('Please edit the report and give an explanation.') + Logger.info('Not saving the report file will abort the request.') report += ('Explanation of the Ubuntu delta and why it can be ' 'dropped:\n%s\n>>> ENTER_EXPLANATION_HERE <<<\n\n' % get_ubuntu_delta_changelog(ubuntu_srcpkg)) @@ -293,9 +293,9 @@ def main(): if ffe: need_interaction = True - print('To approve FeatureFreeze exception, you need to state\n' - 'the reason why you feel it is necessary.\n' - 'Not saving the report file will abort the request.') + Logger.info('To approve FeatureFreeze exception, you need to state') + Logger.info('the reason why you feel it is necessary.') + Logger.info('Not saving the report file will abort the request.') report += ('Explanation of FeatureFreeze exception:\n' '>>> ENTER_EXPLANATION_HERE <<<\n\n') @@ -312,10 +312,10 @@ def main(): changelog = debian_srcpkg.getChangelog(since_version=base_version) if not changelog: if not options.missing_changelog_ok: - print("E: Did not retrieve any changelog entries. " - "Do you need to specify '-C'? " - "Was the package recently uploaded? (check " - "http://packages.debian.org/changelogs/)", file=sys.stderr) + Logger.error("Did not retrieve any changelog entries. " + "Do you need to specify '-C'? " + "Was the package recently uploaded? (check " + "http://packages.debian.org/changelogs/)") sys.exit(1) else: need_interaction = True @@ -327,8 +327,8 @@ def main(): title, report = editor.get_report() if 'XXX FIXME' in report: - print("E: changelog boilerplate found in report, please manually add " - "changelog when using '-C'", file=sys.stderr) + Logger.error("changelog boilerplate found in report, " + "please manually add changelog when using '-C'") sys.exit(1) # bug status and bug subscriber @@ -359,5 +359,5 @@ if __name__ == '__main__': try: main() except KeyboardInterrupt: - print("\nUser abort.") + Logger.error("User abort.") sys.exit(2) diff --git a/reverse-depends b/reverse-depends index e8d06f9..d5ca6b8 100755 --- a/reverse-depends +++ b/reverse-depends @@ -19,11 +19,13 @@ import sys from distro_info import DistroDataOutdated -from ubuntutools.logger import Logger from ubuntutools.misc import (system_distribution, vendor_to_distroinfo, codename_to_distribution) from ubuntutools.rdepends import query_rdepends, RDependsException +from ubuntutools import getLogger +Logger = getLogger(__name__) + DEFAULT_MAX_DEPTH = 10 # We want avoid any infinite loop... @@ -32,7 +34,7 @@ def main(): try: default_release = system_distro_info.devel() except DistroDataOutdated as e: - Logger.warn(e) + Logger.warning(e) default_release = 'unstable' description = ("List reverse-dependencies of package. " @@ -85,7 +87,7 @@ def main(): options.release = distro_info.codename(options.release, default=options.release) except DistroDataOutdated: - # We already printed a warning + # We already logged a warning pass if options.build_depends: @@ -143,14 +145,14 @@ def main(): def display_verbose(package, values): if not values: - print("No reverse dependencies found") + Logger.info("No reverse dependencies found") return - def print_field(field): - print(field) - print('=' * len(field)) + def log_field(field): + Logger.info(field) + Logger.info('=' * len(field)) - def print_package(values, package, arch, dependency, offset=0): + def log_package(values, package, arch, dependency, offset=0): line = ' ' * offset + '* %s' % package if all_archs and set(arch) != all_archs: line += ' [%s]' % ' '.join(sorted(arch)) @@ -158,17 +160,17 @@ def display_verbose(package, values): if len(line) < 30: line += ' ' * (30 - len(line)) line += ' (for %s)' % dependency - print(line) + Logger.info(line) data = values.get(package) if data: offset = offset + 1 for rdeps in data.values(): for rdep in rdeps: - print_package(values, - rdep['Package'], - rdep.get('Architectures', all_archs), - rdep.get('Dependency'), - offset) + log_package(values, + rdep['Package'], + rdep.get('Architectures', all_archs), + rdep.get('Dependency'), + offset) all_archs = set() # This isn't accurate, but we make up for it by displaying what we found @@ -179,19 +181,19 @@ def display_verbose(package, values): all_archs.update(rdep['Architectures']) for field, rdeps in values[package].items(): - print_field(field) + Logger.info(field) rdeps.sort(key=lambda x: x['Package']) for rdep in rdeps: - print_package(values, - rdep['Package'], - rdep.get('Architectures', all_archs), - rdep.get('Dependency')) - print() + log_package(values, + rdep['Package'], + rdep.get('Architectures', all_archs), + rdep.get('Dependency')) + Logger.info("") if all_archs: - print("Packages without architectures listed are " - "reverse-dependencies in: %s" - % ', '.join(sorted(list(all_archs)))) + Logger.info("Packages without architectures listed are " + "reverse-dependencies in: %s" + % ', '.join(sorted(list(all_archs)))) def display_consise(values): @@ -201,7 +203,7 @@ def display_consise(values): for rdep in rdeps: result.add(rdep['Package']) - print('\n'.join(sorted(list(result)))) + Logger.info('\n'.join(sorted(list(result)))) if __name__ == '__main__': diff --git a/seeded-in-ubuntu b/seeded-in-ubuntu index 7c3b849..846f92e 100755 --- a/seeded-in-ubuntu +++ b/seeded-in-ubuntu @@ -24,7 +24,9 @@ import urllib.request from ubuntutools.lp.lpapicache import (Distribution, Launchpad, PackageNotFoundException) -from ubuntutools.logger import Logger + +from ubuntutools import getLogger +Logger = getLogger(__name__) DATA_URL = 'http://qa.ubuntuwire.org/ubuntu-seeded-packages/seeded.json.gz' @@ -88,28 +90,28 @@ def output_binaries(index, binaries): '''Print binaries found in index''' for binary in binaries: if binary in index: - print("%s is seeded in:" % binary) - print(present_on(index[binary])) + Logger.info("%s is seeded in:" % binary) + Logger.info(present_on(index[binary])) else: - print("%s is not seeded (and may not exist)." % binary) + Logger.info("%s is not seeded (and may not exist)." % binary) def output_by_source(index, by_source): - '''Print binaries found in index. Grouped by source''' + '''Logger.Info(binaries found in index. Grouped by source''' for source, binaries in by_source.items(): seen = False if not binaries: - print("Status unknown: No binary packages built by the latest " - "%s.\nTry again using -b and the expected binary packages." - % source) + Logger.info("Status unknown: No binary packages built by the latest " + "%s.\nTry again using -b and the expected binary packages." + % source) continue for binary in binaries: if binary in index: seen = True - print("%s (from %s) is seeded in:" % (binary, source)) - print(present_on(index[binary])) + Logger.info("%s (from %s) is seeded in:" % (binary, source)) + Logger.info(present_on(index[binary])) if not seen: - print("%s's binaries are not seeded." % source) + Logger.info("%s's binaries are not seeded." % source) def main(): diff --git a/sponsor-patch b/sponsor-patch index fe50146..1e77852 100755 --- a/sponsor-patch +++ b/sponsor-patch @@ -19,12 +19,15 @@ import os import shutil import sys import tempfile +import logging from ubuntutools.builder import get_builder from ubuntutools.config import UDTConfig -from ubuntutools.logger import Logger from ubuntutools.sponsor_patch.sponsor_patch import sponsor_patch, check_dependencies +from ubuntutools import getLogger +Logger = getLogger(__name__) + def parse(script_name): """Parse the command line parameters.""" @@ -64,7 +67,8 @@ def parse(script_name): "temporary directory, deleted afterwards).") (options, args) = parser.parse_args() - Logger.set_verbosity(options.verbose) + if options.verbose: + Logger.setLevel(logging.DEBUG) check_dependencies() if len(args) == 0: @@ -123,7 +127,7 @@ def main(): options.keyid, options.lpinstance, options.update, options.upload, workdir) except KeyboardInterrupt: - print("\nUser abort.") + Logger.error("User abort.") sys.exit(2) finally: if options.workdir is None: diff --git a/submittodebian b/submittodebian index 2599ff4..2238521 100755 --- a/submittodebian +++ b/submittodebian @@ -39,6 +39,9 @@ from ubuntutools.config import ubu_email from ubuntutools.question import YesNoQuestion, EditFile from ubuntutools.update_maintainer import update_maintainer, restore_maintainer +from ubuntutools import getLogger +Logger = getLogger(__name__) + def get_most_recent_debian_version(changelog): for block in changelog: @@ -89,7 +92,7 @@ def gen_debdiff(tmpdir, changelog): diff_cmd = ['bzr', 'diff', '-r', 'tag:' + str(oldver)] if call(diff_cmd, stdout=DEVNULL, stderr=DEVNULL) == 1: - print("Extracting bzr diff between %s and %s" % (oldver, newver)) + Logger.info("Extracting bzr diff between %s and %s" % (oldver, newver)) else: if oldver.epoch is not None: oldver = str(oldver)[str(oldver).index(":") + 1:] @@ -102,7 +105,7 @@ def gen_debdiff(tmpdir, changelog): check_file(olddsc) check_file(newdsc) - print("Generating debdiff between %s and %s" % (oldver, newver)) + Logger.info("Generating debdiff between %s and %s" % (oldver, newver)) diff_cmd = ['debdiff', olddsc, newdsc] with Popen(diff_cmd, stdout=PIPE, encoding='utf-8') as diff: @@ -119,7 +122,7 @@ def check_file(fname, critical=True): else: if not critical: return False - print("Couldn't find «%s».\n" % fname) + Logger.info("Couldn't find «%s».\n" % fname) sys.exit(1) @@ -127,7 +130,7 @@ def submit_bugreport(body, debdiff, deb_version, changelog): try: devel = UbuntuDistroInfo().devel() except DistroDataOutdated as e: - print(str(e)) + Logger.info(str(e)) devel = '' if os.path.dirname(sys.argv[0]).startswith('/usr/bin'): @@ -197,7 +200,7 @@ no-cc with open(fn, 'w') as f: f.write(reportbugrc) - print("""\ + Logger.info("""\ You have not configured reportbug. Assuming this is the first time you have used it. Writing a ~/.reportbugrc that will use Debian's mail server, and CC the bug to you at <%s> @@ -221,8 +224,8 @@ def main(): parser.parse_args() if not os.path.exists('/usr/bin/reportbug'): - print("This utility requires the «reportbug» package, which isn't " - "currently installed.") + Logger.error("This utility requires the «reportbug» package, which isn't " + "currently installed.") sys.exit(1) check_reportbug_config() diff --git a/syncpackage b/syncpackage index 8dc296d..1268e1a 100755 --- a/syncpackage +++ b/syncpackage @@ -21,6 +21,7 @@ # ################################################################## import fnmatch +import logging import optparse import os import shutil @@ -37,7 +38,6 @@ from ubuntutools.config import UDTConfig, ubu_email from ubuntutools.lp import udtexceptions from ubuntutools.lp.lpapicache import (Distribution, Launchpad, PersonTeam, SourcePackagePublishingHistory) -from ubuntutools.logger import Logger from ubuntutools.misc import split_release_pocket from ubuntutools.question import YesNoQuestion from ubuntutools.requestsync.mail import ( @@ -45,6 +45,9 @@ from ubuntutools.requestsync.mail import ( from ubuntutools.requestsync.lp import get_debian_srcpkg, get_ubuntu_srcpkg from ubuntutools.version import Version +from ubuntutools import getLogger +Logger = getLogger(__name__) + def remove_signature(dscname): '''Removes the signature from a .dsc file if the .dsc file is signed.''' @@ -116,7 +119,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, ubuntu_ver = Version('~') ubu_pkg = None need_orig = True - Logger.normal('%s does not exist in Ubuntu.', name) + Logger.info('%s does not exist in Ubuntu.', name) Logger.debug('Source %s: current version %s, new version %s', src_pkg.source, ubuntu_ver, new_ver) @@ -128,9 +131,9 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, Logger.error('--force is required to discard Ubuntu changes.') sys.exit(1) - Logger.warn('Overwriting modified Ubuntu version %s, ' - 'setting current version to %s', - ubuntu_ver.full_version, cur_ver.full_version) + Logger.warning('Overwriting modified Ubuntu version %s, ' + 'setting current version to %s', + ubuntu_ver.full_version, cur_ver.full_version) if simulate: return @@ -144,7 +147,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, needs_fakesync = not (need_orig or ubu_pkg.verify_orig()) if needs_fakesync and fakesync: - Logger.warn('Performing a fakesync') + Logger.warning('Performing a fakesync') elif not needs_fakesync and fakesync: Logger.error('Fakesync not required, aborting.') sys.exit(1) @@ -163,7 +166,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, # change into package directory directory = src_pkg.source + '-' + new_ver.upstream_version - Logger.command(('cd', directory)) + Logger.debug('cd' + directory) os.chdir(directory) # read Debian distribution from debian/changelog if not specified @@ -183,9 +186,9 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, cmd.append("-sa") else: cmd.append("-sd") - if not Logger.verbose: + if not Logger.isEnabledFor(logging.DEBUG): cmd += ["-q"] - Logger.command(cmd + ['>', '../' + changes_filename]) + Logger.debug(' '.join(cmd) + '> ../' + changes_filename) changes = subprocess.check_output(cmd, encoding='utf-8') # Add additional bug numbers @@ -193,7 +196,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, changes = add_fixed_bugs(changes, bugs) # remove extracted (temporary) files - Logger.command(('cd', '..')) + Logger.debug('cd ..') os.chdir('..') shutil.rmtree(directory, True) @@ -208,7 +211,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, cmd = ["debsign", changes_filename] if keyid is not None: cmd.insert(1, "-k" + keyid) - Logger.command(cmd) + Logger.debug(' '.join(cmd)) subprocess.check_call(cmd) else: # Create fakesync changelog entry @@ -223,14 +226,14 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, cmd = ['dch', '-v', new_ver.full_version, '--force-distribution', '-D', release, message] env = {'DEBFULLNAME': name, 'DEBEMAIL': email} - Logger.command(cmd) + Logger.debug(' '.join(cmd)) subprocess.check_call(cmd, env=env) # update the Maintainer field cmd = ["update-maintainer"] - if not Logger.verbose: + if not Logger.isEnabledFor(logging.DEBUG): cmd.append("-q") - Logger.command(cmd) + Logger.debug(' '.join(cmd)) subprocess.check_call(cmd) # Build source package @@ -240,7 +243,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, cmd += ['-sa'] if keyid: cmd += ["-k" + keyid] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) returncode = subprocess.call(cmd) if returncode != 0: Logger.error('Source-only build with debuild failed. ' @@ -339,9 +342,9 @@ def copy(src_pkg, release, bugs, sponsoree=None, simulate=False, force=False): ubuntu_spph.getComponent(), mirrors=[]) - Logger.normal('Source %s -> %s/%s: current version %s, new version %s', - src_pkg.source, ubuntu_series, ubuntu_pocket, - ubuntu_pkg.version, src_pkg.version) + Logger.info('Source %s -> %s/%s: current version %s, new version %s', + src_pkg.source, ubuntu_series, ubuntu_pocket, + ubuntu_pkg.version, src_pkg.version) ubuntu_version = Version(ubuntu_pkg.version.full_version) base_version = ubuntu_version.get_related_debian_version() @@ -358,21 +361,21 @@ def copy(src_pkg, release, bugs, sponsoree=None, simulate=False, force=False): sys.exit(1) except udtexceptions.PackageNotFoundException: base_version = Version('~') - Logger.normal('Source %s -> %s/%s: not in Ubuntu, new version %s', - src_pkg.source, ubuntu_series, ubuntu_pocket, - src_pkg.version) + Logger.info('Source %s -> %s/%s: not in Ubuntu, new version %s', + src_pkg.source, ubuntu_series, ubuntu_pocket, + src_pkg.version) changes = debian_spph.getChangelog(since_version=base_version) if changes: changes = changes.strip() - Logger.normal("New changes:\n%s", changes) + Logger.info("New changes:\n%s", changes) if simulate: return if sponsoree: - Logger.normal("Sponsoring this sync for %s (%s)", - sponsoree.display_name, sponsoree.name) + Logger.info("Sponsoring this sync for %s (%s)", + sponsoree.display_name, sponsoree.name) answer = YesNoQuestion().ask("Sync this package", "no") if answer != "yes": return @@ -392,14 +395,14 @@ def copy(src_pkg, release, bugs, sponsoree=None, simulate=False, force=False): Logger.error(error.content) sys.exit(1) - Logger.normal('Request succeeded; you should get an e-mail once it is ' - 'processed.') + Logger.info('Request succeeded; you should get an e-mail once it is ' + 'processed.') bugs = sorted(set(bugs)) if bugs: - Logger.normal("Launchpad bugs to be closed: %s", - ', '.join(str(bug) for bug in bugs)) - Logger.normal('Please wait for the sync to be successful before ' - 'closing bugs.') + Logger.info("Launchpad bugs to be closed: %s", + ', '.join(str(bug) for bug in bugs)) + Logger.info('Please wait for the sync to be successful before ' + 'closing bugs.') answer = YesNoQuestion().ask("Close bugs", "yes") if answer == "yes": close_bugs(bugs, src_pkg.source, src_pkg.version.full_version, @@ -468,7 +471,7 @@ def close_bugs(bugs, package, version, changes, sponsoree): if target == ubuntu or (target.name == package and getattr(target, 'distribution', None) == ubuntu): if task.status != 'Fix Released': - Logger.normal("Closed bug %s", task.web_link) + Logger.info("Closed bug %s", task.web_link) task.status = 'Fix Released' task.lp_save() bug.newMessage(content=message) @@ -597,7 +600,8 @@ def main(): '''Handle parameters and get the ball rolling''' (options, package) = parse() - Logger.verbose = options.verbose + if options.verbose: + Logger.setLevel('DEBUG') config = UDTConfig(options.no_conf) if options.debian_mirror is None: options.debian_mirror = config.get_value('DEBIAN_MIRROR') @@ -626,10 +630,10 @@ def main(): options.release = "%s-proposed" % ubuntu.current_series.name if not options.fakesync and not options.lp: - Logger.warn("The use of --no-lp is not recommended for uploads " - "targeted at Ubuntu. " - "The archive-admins discourage its use, except for " - "fakesyncs.") + Logger.warning("The use of --no-lp is not recommended for uploads " + "targeted at Ubuntu. " + "The archive-admins discourage its use, except for " + "fakesyncs.") sponsoree = None if options.sponsoree: @@ -687,17 +691,17 @@ def main(): if blacklist_fail: Logger.error("Source package %s is blacklisted.", src_pkg.source) elif blacklisted == 'ALWAYS': - Logger.normal("Source package %s is blacklisted.", src_pkg.source) + Logger.info(u"Source package %s is blacklisted.", src_pkg.source) if messages: for message in messages: for line in textwrap.wrap(message): - Logger.normal(line) + Logger.info(line) if comments: - Logger.normal("Blacklist Comments:") + Logger.info("Blacklist Comments:") for comment in comments: for line in textwrap.wrap(comment): - Logger.normal(" " + line) + Logger.info(" " + line) if blacklist_fail: sys.exit(1) @@ -717,4 +721,4 @@ if __name__ == "__main__": try: main() except KeyboardInterrupt: - Logger.normal('User abort.') + Logger.info('User abort.') diff --git a/ubuntu-build b/ubuntu-build index 660f895..2083366 100755 --- a/ubuntu-build +++ b/ubuntu-build @@ -32,6 +32,9 @@ from ubuntutools.lp.udtexceptions import (SeriesNotFoundException, from ubuntutools.lp.lpapicache import Distribution, PersonTeam from ubuntutools.misc import split_release_pocket +from ubuntutools import getLogger +Logger = getLogger(__name__) + def main(): # Usage. @@ -108,15 +111,15 @@ def main(): # Check our operation. if op not in ("rescore", "retry", "status"): - print("Invalid operation: %s." % op, file=sys.stderr) + Logger.error("Invalid operation: %s." % op) sys.exit(1) # If the user has specified an architecture to build, we only wish to # rebuild it and nothing else. if options.architecture: if options.architecture[0] not in valid_archs: - print("Invalid architecture specified: %s." - % options.architecture[0], file=sys.stderr) + Logger.error("Invalid architecture specified: %s." + % options.architecture[0]) sys.exit(1) else: one_arch = True @@ -127,7 +130,7 @@ def main(): try: (release, pocket) = split_release_pocket(release) except PocketDoesNotExistError as error: - print('E: %s' % error) + Logger.error(error) sys.exit(1) # Get the ubuntu archive @@ -141,7 +144,7 @@ def main(): sources = ubuntu_archive.getSourcePackage(package, release, pocket) distroseries = Distribution('ubuntu').getSeries(release) except (SeriesNotFoundException, PackageNotFoundException) as error: - print(error) + Logger.error(error) sys.exit(1) # Get list of builds for that package. builds = sources.getBuilds() @@ -163,16 +166,16 @@ def main(): pocket=pocket) if op in ('rescore', 'retry') and not necessary_privs: - print(("You cannot perform the %s operation on a %s package as " - "you do not have the permissions to do this action.") - % (op, component), file=sys.stderr) + Logger.error("You cannot perform the %s operation on a %s " + "package as you do not have the permissions " + "to do this action." % (op, component)) sys.exit(1) # Output details. - print("The source version for '%s' in %s (%s) is at %s." - % (package, release.capitalize(), component, version)) + Logger.info("The source version for '%s' in %s (%s) is at %s." % + (package, release.capitalize(), component, version)) - print("Current build status for this package:") + Logger.info("Current build status for this package:") # Output list of arches for package and their status. done = False @@ -182,29 +185,28 @@ def main(): continue done = True - print("%s: %s." % (build.arch_tag, build.buildstate)) + Logger.info("%s: %s." % (build.arch_tag, build.buildstate)) if op == 'rescore': if build.can_be_rescored: # FIXME: make priority an option priority = 5000 - print('Rescoring build %s to %d...' - % (build.arch_tag, priority)) + Logger.info('Rescoring build %s to %d...' % (build.arch_tag, priority)) build.rescore(score=priority) else: - print('Cannot rescore build on %s.' % build.arch_tag) + Logger.info('Cannot rescore build on %s.' % build.arch_tag) if op == 'retry': if build.can_be_retried: - print('Retrying build on %s...' % build.arch_tag) + Logger.info('Retrying build on %s...' % build.arch_tag) build.retry() else: - print('Cannot retry build on %s.' % build.arch_tag) + Logger.info('Cannot retry build on %s.' % build.arch_tag) # We are done if done: sys.exit(0) - print(("No builds for '%s' found in the %s release - it may have been " - "built in a former release.") % (package, release.capitalize())) + Logger.info("No builds for '%s' found in the %s release" % (package, release.capitalize())) + Logger.info("It may have been built in a former release.") sys.exit(0) # Batch mode @@ -225,14 +227,14 @@ def main(): try: (release, pocket) = split_release_pocket(release) except PocketDoesNotExistError as error: - print('E: %s' % error) + Logger.error(error) sys.exit(1) ubuntu_archive = Distribution('ubuntu').getArchive() try: distroseries = Distribution('ubuntu').getSeries(release) except SeriesNotFoundException as error: - print(error) + Logger.error(error) sys.exit(1) me = PersonTeam.me @@ -241,14 +243,14 @@ def main(): and me.isLpTeamMember('launchpad-buildd-admins')) or False) if options.priority and not can_rescore: - print("You don't have the permissions to rescore builds. " - "Ignoring your rescore request.", file=sys.stderr) + Logger.error("You don't have the permissions to rescore " + "builds. Ignoring your rescore request.") for pkg in args: try: pkg = ubuntu_archive.getSourcePackage(pkg, release, pocket) except PackageNotFoundException as error: - print(error) + Logger.error(error) continue # Check permissions (part 2): check upload permissions for the source @@ -258,20 +260,20 @@ def main(): pkg.getPackageName(), pkg.getComponent()) if options.retry and not can_retry: - print(("You don't have the permissions to retry the build of " - "'%s'. Ignoring your request.") - % pkg.getPackageName(), file=sys.stderr) + Logger.error("You don't have the permissions to retry the " + "build of '%s'. Ignoring your request." + % pkg.getPackageName()) - print("The source version for '%s' in '%s' (%s) is: %s" - % (pkg.getPackageName(), release, pocket, pkg.getVersion())) + Logger.info("The source version for '%s' in '%s' (%s) is: %s" % + (pkg.getPackageName(), release, pocket, pkg.getVersion())) - print(pkg.getBuildStates(archs)) + Logger.info(pkg.getBuildStates(archs)) if can_retry: - print(pkg.retryBuilds(archs)) + Logger.info(pkg.retryBuilds(archs)) if options.priority and can_rescore: - print(pkg.rescoreBuilds(archs, options.priority)) + Logger.info(pkg.rescoreBuilds(archs, options.priority)) - print() + Logger.info('') if __name__ == '__main__': diff --git a/ubuntu-iso b/ubuntu-iso index fd56a66..c07b420 100755 --- a/ubuntu-iso +++ b/ubuntu-iso @@ -24,6 +24,9 @@ import optparse import subprocess import sys +from ubuntutools import getLogger +Logger = getLogger(__name__) + def extract(iso, path): command = ['isoinfo', '-R', '-i', iso, '-x', path] @@ -54,12 +57,11 @@ def main(): version = extract(iso, '/.disk/info') if len(version) == 0: - print('%s does not appear to be an Ubuntu ISO' % iso, - file=sys.stderr) + Logger.error('%s does not appear to be an Ubuntu ISO' % iso) err = True continue - print(prefix + version) + Logger.info(prefix + version) if err: sys.exit(1) diff --git a/ubuntu-upload-permission b/ubuntu-upload-permission index 2099134..1debf86 100755 --- a/ubuntu-upload-permission +++ b/ubuntu-upload-permission @@ -20,9 +20,11 @@ import sys from ubuntutools.lp.lpapicache import (Launchpad, Distribution, PersonTeam, Packageset, PackageNotFoundException, SeriesNotFoundException) -from ubuntutools.logger import Logger from ubuntutools.misc import split_release_pocket +from ubuntutools import getLogger +Logger = getLogger(__name__) + def parse_arguments(): '''Parse arguments and return (options, package)''' @@ -77,52 +79,50 @@ def main(): component_uploader = archive.getUploadersForComponent( component_name=component)[0] - print("All upload permissions for %s:" % package) - print() - print("Component (%s)" % component) - print("============" + ("=" * len(component))) + Logger.info("All upload permissions for %s:" % package) + Logger.info("") + Logger.info("Component (%s)" % component) + Logger.info("============" + ("=" * len(component))) print_uploaders([component_uploader], options.list_team_members) packagesets = sorted(Packageset.setsIncludingSource( distroseries=series, sourcepackagename=package)) if packagesets: - print() - print("Packagesets") - print("===========") + Logger.info("") + Logger.info("Packagesets") + Logger.info("===========") for packageset in packagesets: - print() - print("%s:" % packageset.name) + Logger.info("") + Logger.info("%s:" % packageset.name) print_uploaders(archive.getUploadersForPackageset( packageset=packageset), options.list_team_members) ppu_uploaders = archive.getUploadersForPackage( source_package_name=package) if ppu_uploaders: - print() - print("Per-Package-Uploaders") - print("=====================") - print() + Logger.info("") + Logger.info("Per-Package-Uploaders") + Logger.info("=====================") + Logger.info("") print_uploaders(ppu_uploaders, options.list_team_members) - print() + Logger.info("") if PersonTeam.me.canUploadPackage(archive, series, package, component, pocket): - print("You can upload %s to %s." % (package, options.release)) + Logger.info("You can upload %s to %s." % (package, options.release)) else: - print("You can not upload %s to %s, yourself." - % (package, options.release)) + Logger.info("You can not upload %s to %s, yourself." % (package, options.release)) if (series.status in ('Current Stable Release', 'Supported', 'Obsolete') and pocket == 'Release'): - print(("%s is in the '%s' state. You may want to query the " - "%s-proposed pocket.") - % (release, series.status, release)) + Logger.info("%s is in the '%s' state. You may want to query the %s-proposed pocket." % + (release, series.status, release)) else: - print("But you can still contribute to it via the sponsorship " - "process: https://wiki.ubuntu.com/SponsorshipProcess") + Logger.info("But you can still contribute to it via the sponsorship " + "process: https://wiki.ubuntu.com/SponsorshipProcess") if not options.list_uploaders: - print("To see who has the necessary upload rights, " - "use the --list-uploaders option.") + Logger.info("To see who has the necessary upload rights, " + "use the --list-uploaders option.") sys.exit(1) @@ -133,9 +133,9 @@ def print_uploaders(uploaders, expand_teams=False, prefix=''): recursion. """ for uploader in sorted(uploaders, key=lambda p: p.display_name): - print(("%s* %s (%s)%s" % - (prefix, uploader.display_name, uploader.name, - ' [team]' if uploader.is_team else ''))) + Logger.info("%s* %s (%s)%s" % + (prefix, uploader.display_name, uploader.name, + ' [team]' if uploader.is_team else '')) if expand_teams and uploader.is_team: print_uploaders(uploader.participants, True, prefix=prefix + ' ') diff --git a/ubuntutools/__init__.py b/ubuntutools/__init__.py index 40a7c42..f886333 100644 --- a/ubuntutools/__init__.py +++ b/ubuntutools/__init__.py @@ -2,3 +2,20 @@ # # Ubuntu Development Tools # https://launchpad.net/ubuntu-dev-tools + +import logging + + +def _loggingBasicConfig(**kwargs): + '''Set log level to INFO and define log format to use.''' + if 'level' not in kwargs: + kwargs['level'] = logging.INFO + if 'format' not in kwargs: + kwargs['format'] = '%(message)s' + logging.basicConfig(**kwargs) + + +def getLogger(name=None): + '''Get standard Python logging.Logger with some ubuntutools defaults.''' + _loggingBasicConfig() + return logging.getLogger(name) diff --git a/ubuntutools/archive.py b/ubuntutools/archive.py index 96ed316..43f4034 100644 --- a/ubuntutools/archive.py +++ b/ubuntutools/archive.py @@ -35,6 +35,7 @@ import hashlib import json import os.path import re +import shutil import subprocess import sys @@ -50,9 +51,11 @@ from ubuntutools.lp.lpapicache import (Launchpad, Distribution, PersonTeam, from ubuntutools.lp.udtexceptions import (PackageNotFoundException, SeriesNotFoundException, InvalidDistroValueError) -from ubuntutools.logger import Logger from ubuntutools.version import Version +import logging +Logger = logging.getLogger(__name__) + class DownloadError(Exception): "Unable to pull a source package" @@ -139,7 +142,6 @@ class SourcePackage(object): lp = kwargs.get('lp') mirrors = kwargs.get('mirrors', ()) workdir = kwargs.get('workdir', '.') - quiet = kwargs.get('quiet', False) series = kwargs.get('series') pocket = kwargs.get('pocket') status = kwargs.get('status') @@ -152,7 +154,6 @@ class SourcePackage(object): self.binary = None self.try_binary = True self.workdir = workdir - self.quiet = quiet self._series = series self._pocket = pocket self._status = status @@ -216,21 +217,21 @@ class SourcePackage(object): # or we've already tried raise pnfe - Logger.normal('Source package lookup failed, ' - 'trying lookup of binary package %s' % self.source) + Logger.info('Source package lookup failed, ' + 'trying lookup of binary package %s' % self.source) try: bpph = archive.getBinaryPackage(self.source, **params) except PackageNotFoundException as bpnfe: # log binary lookup failure, in case it provides hints - Logger.normal(str(bpnfe)) + Logger.info(str(bpnfe)) # raise the original exception for the source lookup raise pnfe self.binary = self.source self.source = bpph.getSourcePackageName() - Logger.normal("Using source package '{}' for binary package '{}'" - .format(self.source, self.binary)) + Logger.info("Using source package '{}' for binary package '{}'" + .format(self.source, self.binary)) spph = bpph.getBuild().getSourcePackagePublishingHistory() if spph: @@ -394,14 +395,14 @@ class SourcePackage(object): message = 'Public key not found, could not verify signature' if self._verify_signature: if valid: - Logger.normal(message) + Logger.info(message) elif no_pub_key: - Logger.warn(message) + Logger.warning(message) else: Logger.error(message) raise DownloadError(message) else: - Logger.info(message) + Logger.debug(message) def _write_dsc(self): "Write dsc file to workdir" @@ -500,9 +501,9 @@ class SourcePackage(object): if self._download_file(url, name): break except HTTPError as e: - Logger.normal('HTTP Error %i: %s', e.code, str(e)) + Logger.info('HTTP Error %i: %s', e.code, str(e)) except URLError as e: - Logger.normal('URL Error: %s', e.reason) + Logger.info('URL Error: %s', e.reason) else: raise DownloadError('File %s could not be found' % name) @@ -525,13 +526,13 @@ class SourcePackage(object): found = True break except HTTPError as e: - Logger.normal('HTTP Error %i: %s', e.code, str(e)) + Logger.info('HTTP Error %i: %s', e.code, str(e)) except URLError as e: - Logger.normal('URL Error: %s', e.reason) + Logger.info('URL Error: %s', e.reason) if found: total += 1 else: - Logger.normal("Could not download from any location: %s", fname) + Logger.info("Could not download from any location: %s", fname) return total def verify(self): @@ -557,7 +558,7 @@ class SourcePackage(object): cmd = ['dpkg-source', '-x', self.dsc_name] if destdir: cmd.append(destdir) - Logger.command(cmd) + Logger.debug(' '.join(cmd)) if subprocess.call(cmd, cwd=self.workdir): Logger.error('Source unpack failed.') sys.exit(1) @@ -569,14 +570,14 @@ class SourcePackage(object): """ cmd = ['debdiff', self.dsc_name, newpkg.dsc_name] difffn = newpkg.dsc_name[:-3] + 'debdiff' - Logger.command(cmd + ['> %s' % difffn]) + Logger.debug(' '.join(cmd) + ('> %s' % difffn)) with open(difffn, 'w') as f: if subprocess.call(cmd, stdout=f, cwd=self.workdir) > 2: Logger.error('Debdiff failed.') sys.exit(1) if diffstat: cmd = ('diffstat', '-p1', difffn) - Logger.command(cmd) + Logger.debug(' '.join(cmd)) if subprocess.call(cmd): Logger.error('diffstat failed.') sys.exit(1) @@ -590,7 +591,7 @@ class DebianSPPH(SourcePackagePublishingHistory): resource_type = 'source_package_publishing_history' def getBinaries(self, arch, name=None): - Logger.normal('Using Snapshot to find binary packages') + Logger.info('Using Snapshot to find binary packages') srcpkg = Snapshot.getSourcePackage(self.getPackageName(), version=self.getVersion()) return srcpkg.getSPPH().getBinaries(arch=arch, name=name) @@ -631,7 +632,7 @@ class DebianSourcePackage(SourcePackage): except SeriesNotFoundException: pass - Logger.normal('Package not found in Launchpad, using Snapshot') + Logger.info('Package not found in Launchpad, using Snapshot') self._spph = self.snapshot_package.getSPPH() return self._spph @@ -692,7 +693,7 @@ class DebianSourcePackage(SourcePackage): self._snapshot_package = srcpkg else: # we have neither version nor spph, so look up our version using madison - Logger.normal('Using madison to find latest version number') + Logger.info('Using madison to find latest version number') series = self._series params = {'series': series} if series else {} srcpkg = Madison(self.distribution).getSourcePackage(self.source, **params) @@ -899,10 +900,10 @@ class _Snapshot(_WebJSON): if s.startswith('pool'): found_pool = True if not component: - Logger.warn("could not determine component from path %s" % path) + Logger.warning("could not determine component from path %s" % path) return self.DEBIAN_COMPONENTS[0] if component not in self.DEBIAN_COMPONENTS: - Logger.warn("unexpected component %s" % component) + Logger.warning("unexpected component %s" % component) return component def _get_package(self, name, url, pkginit, version, sort_key): diff --git a/ubuntutools/builder.py b/ubuntutools/builder.py index 8ab478f..c082602 100644 --- a/ubuntutools/builder.py +++ b/ubuntutools/builder.py @@ -21,7 +21,8 @@ import os import subprocess -from ubuntutools.logger import Logger +import logging +Logger = logging.getLogger(__name__) def _build_preparation(result_directory): @@ -71,7 +72,7 @@ class Pbuilder(Builder): self.name, "--build", "--architecture", self.architecture, "--distribution", dist, "--buildresult", result_directory, dsc_file] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) returncode = subprocess.call(cmd) return self._build_failure(returncode, dsc_file) @@ -79,7 +80,7 @@ class Pbuilder(Builder): cmd = ["sudo", "-E", "ARCH=" + self.architecture, "DIST=" + dist, self.name, "--update", "--architecture", self.architecture, "--distribution", dist] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) returncode = subprocess.call(cmd) return self._update_failure(returncode, dist) @@ -92,13 +93,13 @@ class Pbuilderdist(Builder): _build_preparation(result_directory) cmd = [self.name, dist, self.architecture, "build", dsc_file, "--buildresult", result_directory] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) returncode = subprocess.call(cmd) return self._build_failure(returncode, dsc_file) def update(self, dist): cmd = [self.name, dist, self.architecture, "update"] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) returncode = subprocess.call(cmd) return self._update_failure(returncode, dist) @@ -110,19 +111,19 @@ class Sbuild(Builder): def build(self, dsc_file, dist, result_directory): _build_preparation(result_directory) workdir = os.getcwd() - Logger.command(["cd", result_directory]) + Logger.debug("cd " + result_directory) os.chdir(result_directory) cmd = ["sbuild", "--arch-all", "--dist=" + dist, "--arch=" + self.architecture, dsc_file] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) returncode = subprocess.call(cmd) - Logger.command(["cd", workdir]) + Logger.debug("cd " + workdir) os.chdir(workdir) return self._build_failure(returncode, dsc_file) def update(self, dist): cmd = ["schroot", "--list"] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) process = subprocess.run(cmd, stdout=subprocess.PIPE, encoding='utf-8') chroots, _ = process.stdout.strip().split() if process.returncode != 0: @@ -144,7 +145,7 @@ class Sbuild(Builder): ["sbuild-clean", "-a", "-c"]] for cmd in commands: # pylint: disable=W0631 - Logger.command(cmd + [chroot]) + Logger.debug(' '.join(cmd) + " " + chroot) ret = subprocess.call(cmd + [chroot]) # pylint: enable=W0631 if ret != 0: diff --git a/ubuntutools/config.py b/ubuntutools/config.py index dfa53a3..5cc14ce 100644 --- a/ubuntutools/config.py +++ b/ubuntutools/config.py @@ -23,7 +23,8 @@ import socket import sys import locale -from ubuntutools.logger import Logger +import logging +Logger = logging.getLogger(__name__) class UDTConfig(object): @@ -71,8 +72,8 @@ class UDTConfig(object): for line in f: parsed = shlex.split(line, comments=True) if len(parsed) > 1: - Logger.warn('Cannot parse variable assignment in %s: %s', - getattr(f, 'name', ''), line) + Logger.warning('Cannot parse variable assignment in %s: %s', + getattr(f, 'name', ''), line) if len(parsed) >= 1 and '=' in parsed[0]: key, value = parsed[0].split('=', 1) config[key] = value @@ -111,10 +112,8 @@ class UDTConfig(object): replacements = self.prefix + '_' + key if key in self.defaults: replacements += 'or UBUNTUTOOLS_' + key - Logger.warn( - 'Using deprecated configuration variable %s. ' - 'You should use %s.', - k, replacements) + Logger.warning('Using deprecated configuration variable %s. ' + 'You should use %s.', k, replacements) return value return default diff --git a/ubuntutools/logger.py b/ubuntutools/logger.py deleted file mode 100644 index 3c04ca4..0000000 --- a/ubuntutools/logger.py +++ /dev/null @@ -1,76 +0,0 @@ -# -# logger.py - A simple logging helper class -# -# Copyright (C) 2010, Benjamin Drung -# -# Permission to use, copy, modify, and/or distribute this software -# for any purpose with or without fee is hereby granted, provided -# that the above copyright notice and this permission notice appear -# in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL -# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED -# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE -# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR -# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, -# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN -# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import os -import sys - - -def escape_arg(arg): - """Shell-escpae arg, if necessary. - Fairly simplistic, doesn't escape anything except whitespace. - """ - if ' ' not in arg: - return arg - return '"%s"' % arg.replace('\\', r'\\').replace('"', r'\"') - - -class Logger(object): - script_name = os.path.basename(sys.argv[0]) - verbose = False - - stdout = sys.stdout - stderr = sys.stderr - - @classmethod - def _print(cls, format_, message, args=None, stderr=False): - if args: - message = message % args - stream = cls.stderr if stderr else cls.stdout - stream.write((format_ + "\n") % (cls.script_name, message)) - - @classmethod - def command(cls, cmd): - if cls.verbose: - cls._print("%s: I: %s", " ".join(escape_arg(arg) for arg in cmd)) - - @classmethod - def debug(cls, message, *args): - if cls.verbose: - cls._print("%s: D: %s", message, args, stderr=True) - - @classmethod - def error(cls, message, *args): - cls._print("%s: Error: %s", message, args, stderr=True) - - @classmethod - def warn(cls, message, *args): - cls._print("%s: Warning: %s", message, args, stderr=True) - - @classmethod - def info(cls, message, *args): - if cls.verbose: - cls._print("%s: I: %s", message, args) - - @classmethod - def normal(cls, message, *args): - cls._print("%s: %s", message, args) - - @classmethod - def set_verbosity(cls, verbose): - cls.verbose = verbose diff --git a/ubuntutools/lp/lpapicache.py b/ubuntutools/lp/lpapicache.py index a821ebc..de465d2 100644 --- a/ubuntutools/lp/lpapicache.py +++ b/ubuntutools/lp/lpapicache.py @@ -27,7 +27,6 @@ import collections import re -import sys from debian.changelog import Changelog from httplib2 import Http, HttpLib2Error @@ -47,6 +46,9 @@ from ubuntutools.lp.udtexceptions import (AlreadyLoggedInError, PocketDoesNotExistError, SeriesNotFoundException) +import logging +Logger = logging.getLogger(__name__) + __all__ = [ 'Archive', @@ -72,7 +74,7 @@ class _Launchpad(object): self.__lp = LP.login_with('ubuntu-dev-tools', service, version=api_version) except IOError as error: - print('E: %s' % error, file=sys.stderr) + Logger.error(str(error)) raise else: raise AlreadyLoggedInError('Already logged in to Launchpad.') @@ -153,6 +155,7 @@ class BaseWrapper(object, metaclass=MetaWrapper): cached._lpobject = data # and add it to our cache cls._cache[data.self_link] = cached + Logger.debug("%s: %s" % (cls.__name__, data.self_link)) # add additional class specific caching (if available) cache = getattr(cls, 'cache', None) if isinstance(cache, collections.Callable): @@ -765,17 +768,17 @@ class SourcePackagePublishingHistory(BaseWrapper): if self._changelog is None: url = self._lpobject.changelogUrl() if url is None: - print('E: No changelog available for %s %s' % - (self.getPackageName(), self.getVersion()), file=sys.stderr) + Logger.error('No changelog available for %s %s' % + (self.getPackageName(), self.getVersion())) return None try: response, changelog = Http().request(url) except HttpLib2Error as e: - print(str(e), file=sys.stderr) + Logger.error(str(e)) return None if response.status != 200: - print('%s: %s %s' % (url, response.status, response.reason), file=sys.stderr) + Logger.error('%s: %s %s' % (url, response.status, response.reason)) return None self._changelog = changelog @@ -824,7 +827,7 @@ class SourcePackagePublishingHistory(BaseWrapper): self._have_all_binaries = True else: # we have to go the long way :( - print("Please wait, this may take some time...") + Logger.info("Please wait, this may take some time...") archive = self.getArchive() urls = self.binaryFileUrls() for url in urls: @@ -854,7 +857,7 @@ class SourcePackagePublishingHistory(BaseWrapper): try: bpph = archive.getBinaryPackage(**params) except PackageNotFoundException: - print("Could not find pkg in archive: %s" % filename) + Logger.debug("Could not find pkg in archive: %s" % filename) continue if a not in self._binaries: self._binaries[a] = {} diff --git a/ubuntutools/pullpkg.py b/ubuntutools/pullpkg.py index 11f1bd8..2e7a4ac 100644 --- a/ubuntutools/pullpkg.py +++ b/ubuntutools/pullpkg.py @@ -39,9 +39,13 @@ from ubuntutools.lp.udtexceptions import (SeriesNotFoundException, PackageNotFoundException, PocketDoesNotExistError, InvalidDistroValueError) -from ubuntutools.logger import Logger from ubuntutools.misc import (split_release_pocket, host_architecture, STATUSES) +from ubuntutools import _loggingBasicConfig + +import logging +Logger = logging.getLogger(__name__) + PULL_SOURCE = 'source' PULL_DEBS = 'debs' PULL_DDEBS = 'ddebs' @@ -76,18 +80,22 @@ class PullPkg(object): """For use by stand-alone cmdline scripts. This will handle catching certain exceptions or kbd interrupts, - and printing out (via Logger) the error message, instead of - allowing the exception to flow up to the script. This does - not catch unexpected exceptions, such as internal errors. + setting up the root logger level to INFO, and printing out + (via Logger) a caught error message, instead of allowing the + exception to flow up to the script. This does not catch + unexpected exceptions, such as internal errors. + On (expected) error, this will call sys.exit(error); unexpected errors will flow up to the caller. On success, this simply returns. """ + _loggingBasicConfig() + try: cls(*args, **kwargs).pull() return except KeyboardInterrupt: - Logger.normal('User abort.') + Logger.info('User abort.') except (PackageNotFoundException, SeriesNotFoundException, PocketDoesNotExistError, InvalidDistroValueError) as e: Logger.error(str(e)) @@ -120,7 +128,7 @@ class PullPkg(object): parser = ArgumentParser(epilog=epilog) parser.add_argument('-v', '--verbose', action='store_true', - help="Print verbose/debug messages") + help="Print debug messages") parser.add_argument('-d', '--download-only', action='store_true', help="Do not extract the source package") parser.add_argument('-m', '--mirror', action='append', @@ -231,7 +239,7 @@ class PullPkg(object): debian_info = DebianDistroInfo() codename = debian_info.codename(release) if codename: - Logger.normal("Using release '%s' for '%s'", codename, release) + Logger.info("Using release '%s' for '%s'", codename, release) release = codename if distro == DISTRO_PPA: @@ -335,8 +343,8 @@ class PullPkg(object): options = vars(self.argparser.parse_args(args)) assert 'verbose' in options - if options['verbose'] is not None: - Logger.set_verbosity(options['verbose']) + if options['verbose']: + Logger.setLevel(logging.DEBUG) Logger.debug("pullpkg options: %s", options) @@ -349,15 +357,15 @@ class PullPkg(object): srcpkg = DISTRO_PKG_CLASS[distro](**params) spph = srcpkg.lp_spph - Logger.normal('Found %s', spph.display_name) + Logger.info('Found %s', spph.display_name) if pull == PULL_LIST: - Logger.normal("Source files:") + Logger.info("Source files:") for f in srcpkg.dsc['Files']: - Logger.normal(" %s", f['name']) - Logger.normal("Binary files:") + Logger.info(" %s", f['name']) + Logger.info("Binary files:") for f in spph.getBinaries(options['arch']): - Logger.normal(" %s", f.getFileName()) + Logger.info(" %s", f.getFileName()) elif pull == PULL_SOURCE: # allow DownloadError to flow up to caller srcpkg.pull() @@ -368,9 +376,9 @@ class PullPkg(object): else: name = '.*' if params['package'] != spph.getPackageName(): - Logger.normal("Pulling only binary package '%s'", params['package']) - Logger.normal("Use package name '%s' to pull all binary packages", - spph.getPackageName()) + Logger.info("Pulling only binary package '%s'", params['package']) + Logger.info("Use package name '%s' to pull all binary packages", + spph.getPackageName()) name = params['package'] if pull == PULL_DEBS: name = r'{}(?", self._debdiff_filename]) + Logger.debug(' '.join(cmd) + " > " + self._debdiff_filename) debdiff = subprocess.check_output(cmd, encoding='utf-8') # write debdiff file @@ -417,7 +419,7 @@ class SourcePackage(object): lintian_filename = os.path.join(self._workdir, self._package + "_" + strip_epoch(self._version) + ".lintian") - Logger.command(cmd + [">", lintian_filename]) + Logger.debug(' '.join(cmd) + " > " + lintian_filename) report = subprocess.check_output(cmd, encoding='utf-8') # write lintian report file @@ -434,7 +436,7 @@ class SourcePackage(object): cmd = ["syncpackage", self._package, "-b", str(bug_number), "-f", "-s", requester, "-V", str(self._version), "-d", series] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) if subprocess.call(cmd) != 0: Logger.error("Syncing of %s %s failed.", self._package, str(self._version)) diff --git a/ubuntutools/sponsor_patch/sponsor_patch.py b/ubuntutools/sponsor_patch/sponsor_patch.py index d1d1b62..d873a81 100644 --- a/ubuntutools/sponsor_patch/sponsor_patch.py +++ b/ubuntutools/sponsor_patch/sponsor_patch.py @@ -25,7 +25,6 @@ from distro_info import UbuntuDistroInfo from launchpadlib.launchpad import Launchpad -from ubuntutools.logger import Logger from ubuntutools.update_maintainer import (update_maintainer, MaintainerUpdateException) from ubuntutools.question import input_number @@ -35,6 +34,9 @@ from ubuntutools.sponsor_patch.patch import Patch from ubuntutools.sponsor_patch.question import ask_for_manual_fixing from ubuntutools.sponsor_patch.source_package import SourcePackage +import logging +Logger = logging.getLogger(__name__) + def is_command_available(command, check_sbin=False): "Is command in $PATH?" @@ -59,8 +61,8 @@ def check_dependencies(): missing.append('pbuilder/cowbuilder/sbuild') if missing: - Logger.warn("sponsor-patch requires %s to be installed for full " - "functionality", ', '.join(missing)) + Logger.warning("sponsor-patch requires %s to be installed for full " + "functionality", ', '.join(missing)) def get_source_package_name(bug_task): @@ -82,7 +84,7 @@ def get_user_shell(): def edit_source(): # Spawn shell to allow modifications cmd = [get_user_shell()] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) print("""An interactive shell was launched in file://%s Edit your files. When you are done, exit the shell. If you wish to abort the @@ -112,7 +114,7 @@ def ask_for_patch_or_branch(bug, attached_patches, linked_branches): patches += "es" msg = "https://launchpad.net/bugs/%i has %s linked and %s attached:" % \ (bug.id, branches, patches) - Logger.normal(msg) + Logger.info(msg) i = 0 for linked_branch in linked_branches: i += 1 @@ -160,7 +162,7 @@ def download_branch(branch): if os.path.isdir(dir_name): shutil.rmtree(dir_name) cmd = ["bzr", "branch", branch] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) if subprocess.call(cmd) != 0: Logger.error("Failed to download branch %s." % (branch)) sys.exit(1) @@ -170,7 +172,7 @@ def download_branch(branch): def merge_branch(branch): edit = False cmd = ["bzr", "merge", branch] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) if subprocess.call(cmd) != 0: Logger.error("Failed to merge branch %s." % (branch)) ask_for_manual_fixing() @@ -182,7 +184,7 @@ def extract_source(dsc_file, verbose=False): cmd = ["dpkg-source", "--no-preparation", "-x", dsc_file] if not verbose: cmd.insert(1, "-q") - Logger.command(cmd) + Logger.debug(' '.join(cmd)) if subprocess.call(cmd) != 0: Logger.error("Extraction of %s failed." % (os.path.basename(dsc_file))) sys.exit(1) @@ -219,21 +221,21 @@ def get_open_ubuntu_bug_task(launchpad, bug, branch=None): task = tasks[0] elif len(ubuntu_tasks) > 1: task_list = [t.get_short_info() for t in ubuntu_tasks] - Logger.info("%i Ubuntu tasks exist for bug #%i.\n%s", len(ubuntu_tasks), - bug_id, "\n".join(task_list)) + Logger.debug("%i Ubuntu tasks exist for bug #%i.\n%s", len(ubuntu_tasks), + bug_id, "\n".join(task_list)) open_ubuntu_tasks = [x for x in ubuntu_tasks if not x.is_complete()] if len(open_ubuntu_tasks) == 1: task = open_ubuntu_tasks[0] else: - Logger.normal("https://launchpad.net/bugs/%i has %i Ubuntu tasks:" % - (bug_id, len(ubuntu_tasks))) + Logger.info("https://launchpad.net/bugs/%i has %i Ubuntu tasks:" % + (bug_id, len(ubuntu_tasks))) for i in range(len(ubuntu_tasks)): print("%i) %s" % (i + 1, ubuntu_tasks[i].get_package_and_series())) selected = input_number("To which Ubuntu task does the patch belong", 1, len(ubuntu_tasks)) task = ubuntu_tasks[selected - 1] - Logger.info("Selected Ubuntu task: %s" % (task.get_short_info())) + Logger.debug("Selected Ubuntu task: %s" % (task.get_short_info())) return task @@ -248,15 +250,15 @@ def _create_and_change_into(workdir): (workdir, error.errno, error.strerror)) sys.exit(1) if workdir != os.getcwd(): - Logger.command(["cd", workdir]) + Logger.debug("cd " + workdir) os.chdir(workdir) def _update_maintainer_field(): """Update the Maintainer field in debian/control.""" - Logger.command(["update-maintainer"]) + Logger.debug("update-maintainer") try: - update_maintainer("debian", Logger.verbose) + update_maintainer("debian", Logger.isEnabledFor(logging.DEBUG)) except MaintainerUpdateException as e: Logger.error("update-maintainer failed: %s", str(e)) sys.exit(1) @@ -265,9 +267,9 @@ def _update_maintainer_field(): def _update_timestamp(): """Run dch to update the timestamp of debian/changelog.""" cmd = ["dch", "--maintmaint", "--release", ""] - Logger.command(cmd) + Logger.debug(' '.join(cmd)) if subprocess.call(cmd) != 0: - Logger.info("Failed to update timestamp in debian/changelog.") + Logger.debug("Failed to update timestamp in debian/changelog.") def _download_and_change_into(task, dsc_file, patch, branch): @@ -277,23 +279,23 @@ def _download_and_change_into(task, dsc_file, patch, branch): branch_dir = download_branch(task.get_branch_link()) # change directory - Logger.command(["cd", branch_dir]) + Logger.debug("cd " + branch_dir) os.chdir(branch_dir) else: if patch: patch.download() - Logger.info("Ubuntu package: %s" % (task.package)) + Logger.debug("Ubuntu package: %s" % (task.package)) if task.is_merge(): - Logger.info("The task is a merge request.") + Logger.debug("The task is a merge request.") if task.is_sync(): - Logger.info("The task is a sync request.") + Logger.debug("The task is a sync request.") - extract_source(dsc_file, Logger.verbose) + extract_source(dsc_file, Logger.isEnabledFor(logging.DEBUG)) # change directory directory = task.package + '-' + task.get_version().upstream_version - Logger.command(["cd", directory]) + Logger.debug("cd " + directory) os.chdir(directory) diff --git a/ubuntutools/test/test_archive.py b/ubuntutools/test/test_archive.py index 9483b8d..46f6c11 100644 --- a/ubuntutools/test/test_archive.py +++ b/ubuntutools/test/test_archive.py @@ -92,10 +92,6 @@ class LocalSourcePackageTestCase(unittest.TestCase): self.url_opener = mock.MagicMock(spec=OpenerDirector) self.url_opener.open.side_effect = self.urlopen_proxy - # Silence the tests a little: - self._stubout('ubuntutools.logger.Logger.stdout') - self._stubout('ubuntutools.logger.Logger.stderr') - def _stubout(self, stub): patcher = mock.patch(stub) self.addCleanup(patcher.stop) @@ -151,7 +147,6 @@ class LocalSourcePackageTestCase(unittest.TestCase): dscfile='test-data/example_1.0-1.dsc', workdir=self.workdir, verify_signature=False) - pkg.quiet = True pkg.pull() pkg.unpack() @@ -164,7 +159,6 @@ class LocalSourcePackageTestCase(unittest.TestCase): 'example_1.0-1.dsc'), workdir=self.workdir, verify_signature=False) - pkg.quiet = True pkg.pull() pkg.unpack() @@ -179,7 +173,6 @@ class LocalSourcePackageTestCase(unittest.TestCase): 'example_1.0-1.dsc'), workdir=self.workdir, verify_signature=False) - pkg.quiet = True pkg.pull() pkg.unpack() @@ -197,7 +190,6 @@ class LocalSourcePackageTestCase(unittest.TestCase): dscfile='test-data/example_1.0-1.dsc', workdir=self.workdir, verify_signature=False) - pkg.quiet = True pkg.pull() def test_pull(self): @@ -208,7 +200,6 @@ class LocalSourcePackageTestCase(unittest.TestCase): verify_signature=False) pkg.url_opener = self.url_opener - pkg.quiet = True pkg.pull() def test_mirrors(self): @@ -228,7 +219,6 @@ class LocalSourcePackageTestCase(unittest.TestCase): mirrors=[mirror], verify_signature=False) pkg.url_opener = url_opener - pkg.quiet = True pkg.pull() def test_dsc_missing(self): @@ -237,7 +227,6 @@ class LocalSourcePackageTestCase(unittest.TestCase): version='1.0-1', component='main', workdir=self.workdir) - pkg.quiet = True self.assertRaises(ubuntutools.archive.DownloadError, pkg.pull) @@ -267,7 +256,6 @@ class DebianLocalSourcePackageTestCase(LocalSourcePackageTestCase): workdir=self.workdir, mirrors=[debian_mirror, debsec_mirror], verify_signature=False) - pkg.quiet = True pkg.url_opener = url_opener pkg.pull() pkg.unpack() diff --git a/ubuntutools/test/test_config.py b/ubuntutools/test/test_config.py index 08b37fb..f8653a6 100644 --- a/ubuntutools/test/test_config.py +++ b/ubuntutools/test/test_config.py @@ -18,11 +18,10 @@ import locale import mock import os -import sys +# import sys from io import StringIO from ubuntutools.config import UDTConfig, ubu_email -from ubuntutools.logger import Logger from ubuntutools.test import unittest @@ -51,16 +50,16 @@ class ConfigTestCase(unittest.TestCase): self.addCleanup(patcher.stop) patcher.start() - Logger.stdout = StringIO() - Logger.stderr = StringIO() + # Logger.stdout = StringIO() + # Logger.stderr = StringIO() self.clean_environment() def tearDown(self): - self.assertEqual(Logger.stdout.getvalue(), '') - self.assertEqual(Logger.stderr.getvalue(), '') - Logger.stdout = sys.stdout - Logger.stderr = sys.stderr + # self.assertEqual(Logger.stdout.getvalue(), '') + # self.assertEqual(Logger.stderr.getvalue(), '') + # Logger.stdout = sys.stdout + # Logger.stderr = sys.stderr self.clean_environment() @@ -98,11 +97,11 @@ REPEAT=yes 'INHERIT': 'user', 'REPEAT': 'yes', }) - errs = Logger.stderr.getvalue().strip() - Logger.stderr = StringIO() - self.assertEqual(len(errs.splitlines()), 1) - self.assertRegex(errs, - r'Warning: Cannot parse.*\bCOMMAND_EXECUTION=a') + # errs = Logger.stderr.getvalue().strip() + # Logger.stderr = StringIO() + # self.assertEqual(len(errs.splitlines()), 1) + # self.assertRegex(errs, + # r'Warning: Cannot parse.*\bCOMMAND_EXECUTION=a') def get_value(self, *args, **kwargs): config = UDTConfig(prefix='TEST') @@ -138,11 +137,11 @@ REPEAT=yes self._config_files['user'] = 'COMPATFOOBAR=bar' self.assertEqual(self.get_value('QUX', compat_keys=['COMPATFOOBAR']), 'bar') - errs = Logger.stderr.getvalue().strip() - Logger.stderr = StringIO() - self.assertEqual(len(errs.splitlines()), 1) - self.assertRegex(errs, - r'deprecated.*\bCOMPATFOOBAR\b.*\bTEST_QUX\b') + # errs = Logger.stderr.getvalue().strip() + # Logger.stderr = StringIO() + # self.assertEqual(len(errs.splitlines()), 1) + # self.assertRegex(errs, + # r'deprecated.*\bCOMPATFOOBAR\b.*\bTEST_QUX\b') def test_boolean(self): self._config_files['user'] = "TEST_BOOLEAN=yes" diff --git a/ubuntutools/test/test_logger.py b/ubuntutools/test/test_logger.py deleted file mode 100644 index 3f7fec3..0000000 --- a/ubuntutools/test/test_logger.py +++ /dev/null @@ -1,54 +0,0 @@ -# test_logger.py - Test ubuntutools.logger.Logger. -# -# Copyright (C) 2012, Stefano Rivera -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -# PERFORMANCE OF THIS SOFTWARE. - -from io import StringIO -import sys - -from ubuntutools.logger import Logger -from ubuntutools.test import unittest - - -class LoggerTestCase(unittest.TestCase): - def setUp(self): - Logger.stdout = StringIO() - Logger.stderr = StringIO() - self._script_name = Logger.script_name - Logger.script_name = 'test' - self._verbose = Logger.verbose - - def tearDown(self): - Logger.stdout = sys.stdout - Logger.stderr = sys.stderr - Logger.script_name = self._script_name - Logger.verbose = self._verbose - - def testCommand(self): - Logger.command(('ls', 'a b')) - self.assertEqual(Logger.stdout.getvalue(), '') - Logger.set_verbosity(True) - Logger.command(('ls', 'a b')) - self.assertEqual(Logger.stdout.getvalue(), 'test: I: ls "a b"\n') - self.assertEqual(Logger.stderr.getvalue(), '') - - def testNoArgs(self): - Logger.normal('hello %s') - self.assertEqual(Logger.stdout.getvalue(), 'test: hello %s\n') - self.assertEqual(Logger.stderr.getvalue(), '') - - def testArgs(self): - Logger.normal('hello %s', 'world') - self.assertEqual(Logger.stdout.getvalue(), 'test: hello world\n') - self.assertEqual(Logger.stderr.getvalue(), '') diff --git a/ubuntutools/test/test_update_maintainer.py b/ubuntutools/test/test_update_maintainer.py index bd5d567..b4ff824 100644 --- a/ubuntutools/test/test_update_maintainer.py +++ b/ubuntutools/test/test_update_maintainer.py @@ -18,10 +18,9 @@ import mock import os -import sys +# import sys from io import StringIO -from ubuntutools.logger import Logger from ubuntutools.test import unittest from ubuntutools.update_maintainer import update_maintainer @@ -236,18 +235,18 @@ class UpdateMaintainerTestCase(unittest.TestCase): self.addCleanup(patcher.stop) patcher.start() self._files["rules"] = StringIO(_SIMPLE_RULES) - Logger.stdout = StringIO() - Logger.stderr = StringIO() + # Logger.stdout = StringIO() + # Logger.stderr = StringIO() def tearDown(self): - self.assertEqual(Logger.stdout.getvalue(), '') - self.assertEqual(Logger.stderr.getvalue(), '') + # self.assertEqual(Logger.stdout.getvalue(), '') + # self.assertEqual(Logger.stderr.getvalue(), '') self._files["changelog"] = None self._files["control"] = None self._files["control.in"] = None self._files["rules"] = None - Logger.stdout = sys.stdout - Logger.stderr = sys.stderr + # Logger.stdout = sys.stdout + # Logger.stderr = sys.stderr # pylint: enable=C0103 def test_debian_package(self): @@ -266,11 +265,11 @@ class UpdateMaintainerTestCase(unittest.TestCase): self._files["control"] = StringIO(_AXIS2C_CONTROL) update_maintainer(self._directory) self.assertEqual(self._files["control"].getvalue(), _AXIS2C_UPDATED) - warnings = Logger.stderr.getvalue().strip() - Logger.stderr = StringIO() - self.assertEqual(len(warnings.splitlines()), 1) - self.assertRegex(warnings, "Warning: Overwriting original maintainer: " - "Soren Hansen ") + # warnings = Logger.stderr.getvalue().strip() + # Logger.stderr = StringIO() + # self.assertEqual(len(warnings.splitlines()), 1) + # self.assertRegex(warnings, "Warning: Overwriting original maintainer: " + # "Soren Hansen ") def test_update_maintainer(self): """Test: Update Maintainer field.""" diff --git a/ubuntutools/update_maintainer.py b/ubuntutools/update_maintainer.py index 2c5de57..89a5e3a 100644 --- a/ubuntutools/update_maintainer.py +++ b/ubuntutools/update_maintainer.py @@ -20,7 +20,9 @@ import os import re import debian.changelog -from ubuntutools.logger import Logger + +import logging +Logger = logging.getLogger(__name__) # Prior May 2009 these Maintainers were used: _PREVIOUS_UBUNTU_MAINTAINER = ( @@ -176,8 +178,8 @@ def update_maintainer(debian_directory, verbose=False): return if control.get_original_maintainer() is not None: - Logger.warn("Overwriting original maintainer: %s", - control.get_original_maintainer()) + Logger.warning("Overwriting original maintainer: %s", + control.get_original_maintainer()) if verbose: print("The original maintainer is: %s" % original_maintainer)