From f5af9701946f06b83351eb089237cf6416d67bb0 Mon Sep 17 00:00:00 2001 From: "Aaron Hall, MBA" Date: Mon, 12 Nov 2018 21:02:08 -0500 Subject: [PATCH] issue #109 Replace print statements with logger (#113) * Replace `print` statements with logger (issue #109) * Use pytest>=3.4 * Configure pytest in setup.cfg --- auditwheel/main_addtag.py | 16 ++++++++++------ auditwheel/main_lddtree.py | 6 +++++- auditwheel/main_repair.py | 6 ++++-- auditwheel/policy/__init__.py | 13 ++++--------- auditwheel/repair.py | 8 ++++++-- auditwheel/tmpdirs.py | 1 - auditwheel/wheeltools.py | 19 +++++++++++-------- setup.cfg | 4 ++++ test-requirements.txt | 2 +- tests/test_manylinux.py | 21 ++++++++++----------- 10 files changed, 55 insertions(+), 41 deletions(-) diff --git a/auditwheel/main_addtag.py b/auditwheel/main_addtag.py index 3ed5c6ea..dabe2e0d 100644 --- a/auditwheel/main_addtag.py +++ b/auditwheel/main_addtag.py @@ -1,6 +1,10 @@ from os.path import basename, exists, join, abspath from .policy import (load_policies, get_policy_name, get_priority_by_name, POLICY_PRIORITY_HIGHEST) +import logging + + +logger = logging.getLogger(__name__) def configure_parser(sub_parsers): @@ -30,12 +34,12 @@ def execute(args, p): parsed_fname = WHEEL_INFO_RE(basename(args.WHEEL_FILE)) in_fname_tags = parsed_fname.groupdict()['plat'].split('.') - print('%s recieves the following tag: "%s".' % (basename(args.WHEEL_FILE), - wheel_abi.overall_tag)) - print('Use ``auditwheel show`` for more details') + logger.info('%s recieves the following tag: "%s".', + basename(args.WHEEL_FILE), wheel_abi.overall_tag) + logger.info('Use ``auditwheel show`` for more details') if wheel_abi.overall_tag in in_fname_tags: - print('No tags to be added. Exiting.') + logger.info('No tags to be added. Exiting.') return 1 # todo: move more of this logic to separate file @@ -46,12 +50,12 @@ def execute(args, p): try: out_wheel = add_platforms(ctx, [wheel_abi.overall_tag]) except WheelToolsError as e: - print('\n%s.' % str(e), file=sys.stderr) + logger.exception('\n%s.', repr(e)) return 1 if out_wheel: # tell context manager to write wheel on exit with # the proper output directory ctx.out_wheel = join(args.WHEEL_DIR, basename(out_wheel)) - print('\nUpdated wheel written to %s' % out_wheel) + logger.info('\nUpdated wheel written to %s', out_wheel) return 0 diff --git a/auditwheel/main_lddtree.py b/auditwheel/main_lddtree.py index 3035d372..fce96f76 100644 --- a/auditwheel/main_lddtree.py +++ b/auditwheel/main_lddtree.py @@ -1,3 +1,7 @@ +import logging + +logger = logging.getLogger(__name__) + def configure_subparser(sub_parsers): help = 'Analyze a single ELF file (similar to ``ldd``).' p = sub_parsers.add_parser('lddtree', help=help, description=help) @@ -9,4 +13,4 @@ def execute(args, p): import json from .lddtree import lddtree - print(json.dumps(lddtree(args.file), indent=4)) + logger.info(json.dumps(lddtree(args.file), indent=4)) diff --git a/auditwheel/main_repair.py b/auditwheel/main_repair.py index 2441fbf4..6d8f2b54 100644 --- a/auditwheel/main_repair.py +++ b/auditwheel/main_repair.py @@ -1,7 +1,9 @@ from os.path import isfile, exists, abspath, basename from .policy import (load_policies, get_policy_name, get_priority_by_name, POLICY_PRIORITY_HIGHEST) +import logging +logger = logging.getLogger(__name__) def configure_parser(sub_parsers): policy_names = [p['name'] for p in load_policies()] @@ -48,7 +50,7 @@ def execute(args, p): if find_executable('patchelf') is None: p.error('cannot find the \'patchelf\' tool, which is required') - print('Repairing %s' % basename(args.WHEEL_FILE)) + logger.info('Repairing %s', basename(args.WHEEL_FILE)) if not exists(args.WHEEL_DIR): os.makedirs(args.WHEEL_DIR) @@ -77,4 +79,4 @@ def execute(args, p): update_tags=args.UPDATE_TAGS) if out_wheel is not None: - print('\nFixed-up wheel written to %s' % out_wheel) + logger.info('\nFixed-up wheel written to %s', out_wheel) diff --git a/auditwheel/policy/__init__.py b/auditwheel/policy/__init__.py index 777192eb..ed3f186d 100644 --- a/auditwheel/policy/__init__.py +++ b/auditwheel/policy/__init__.py @@ -3,6 +3,9 @@ import platform as _platform_module from typing import Optional from os.path import join, dirname, abspath +import logging + +logger = logging.getLogger(__name__) _sys_map = {'linux2': 'linux', 'linux': 'linux', @@ -25,17 +28,9 @@ # Windows probably, but there's not much reason to inspect foreign package # that won't run on the platform. if platform != 'linux': - print('Error: This tool only supports Linux', file=sys.stderr) + logger.critical('Error: This tool only supports Linux') sys.exit(1) -# if linkage != 'ELF': -# print( -# ('Error: This tool only supports platforms that use the ELF ' -# 'executable and linker format.'), -# file=sys.stderr) -# sys.exit(1) - - def get_arch_name(): if _platform_module.machine() in non_x86_linux_machines: return _platform_module.machine() diff --git a/auditwheel/repair.py b/auditwheel/repair.py index 2b7c8e99..7e3a4bb1 100644 --- a/auditwheel/repair.py +++ b/auditwheel/repair.py @@ -8,6 +8,7 @@ from subprocess import check_call, check_output, CalledProcessError from distutils.spawn import find_executable from typing import Optional +import logging from .policy import get_replace_platforms from .wheeltools import InWheelCtx, add_platforms @@ -16,6 +17,9 @@ from .hashfile import hashfile +logger = logging.getLogger(__name__) + + @functools.lru_cache() def verify_patchelf(): """This function looks for the ``patchelf`` external binary in the PATH, @@ -129,7 +133,7 @@ def copylib(src_path, dest_dir): if os.path.exists(dest_path): return new_soname, dest_path - print('Grafting: %s -> %s' % (src_path, dest_path)) + logger.info('Grafting: %s -> %s', src_path, dest_path) rpaths = elf_read_rpaths(src_path) shutil.copy2(src_path, dest_path) @@ -147,5 +151,5 @@ def copylib(src_path, dest_dir): def patchelf_set_rpath(fn, libdir): rpath = pjoin('$ORIGIN', relpath(libdir, dirname(fn))) - print('Setting RPATH: %s to "%s"' % (fn, rpath)) + logger.info('Setting RPATH: %s to "%s"', fn, rpath) check_call(['patchelf', '--force-rpath', '--set-rpath', rpath, fn]) diff --git a/auditwheel/tmpdirs.py b/auditwheel/tmpdirs.py index 3c72998a..debb3a85 100644 --- a/auditwheel/tmpdirs.py +++ b/auditwheel/tmpdirs.py @@ -1,6 +1,5 @@ ''' Contexts for *with* statement providing temporary directories ''' -from __future__ import division, print_function, absolute_import import os import shutil from tempfile import template, mkdtemp diff --git a/auditwheel/wheeltools.py b/auditwheel/wheeltools.py index 1038ae90..1ef799c6 100644 --- a/auditwheel/wheeltools.py +++ b/auditwheel/wheeltools.py @@ -10,6 +10,7 @@ import hashlib import csv from itertools import product +import logging from wheel.util import urlsafe_b64encode, open_for_csv, native # type: ignore from wheel.pkginfo import read_pkg_info, write_pkg_info # type: ignore @@ -19,6 +20,9 @@ from .tools import unique_by_index, zip2dir, dir2zip +logger = logging.getLogger(__name__) + + class WheelToolsError(Exception): pass @@ -189,7 +193,6 @@ def add_platforms(wheel_ctx, platforms, remove_platforms=()): info_fname = pjoin(_dist_info_dir(wheel_ctx.path), 'WHEEL') info = read_pkg_info(info_fname) - # Check what tags we have if wheel_ctx.out_wheel is not None: out_dir = dirname(wheel_ctx.out_wheel) @@ -201,7 +204,7 @@ def add_platforms(wheel_ctx, platforms, remove_platforms=()): parsed_fname = WHEEL_INFO_RE(wheel_fname) fparts = parsed_fname.groupdict() original_fname_tags = fparts['plat'].split('.') - print('Previous filename tags:', ', '.join(original_fname_tags)) + logger.info('Previous filename tags:', ', '.join(original_fname_tags)) fname_tags = {tag for tag in original_fname_tags if tag not in remove_platforms} fname_tags |= set(platforms) @@ -213,9 +216,9 @@ def add_platforms(wheel_ctx, platforms, remove_platforms=()): definitely_not_purelib = True if fname_tags != original_fname_tags: - print('New filename tags:', ', '.join(fname_tags)) + logger.info('New filename tags:', ', '.join(fname_tags)) else: - print('No filename tags change needed.') + logger.info('No filename tags change needed.') wheel_base, ext = splitext(wheel_fname) fparts['plat'] = '.'.join(fname_tags) @@ -224,7 +227,7 @@ def add_platforms(wheel_ctx, platforms, remove_platforms=()): out_wheel = pjoin(out_dir, out_wheel_fname) in_info_tags = [tag for name, tag in info.items() if name == 'Tag'] - print('Previous WHEEL info tags:', ', '.join(in_info_tags)) + logger.info('Previous WHEEL info tags:', ', '.join(in_info_tags)) # Python version, C-API version combinations pyc_apis = ['-'.join(tag.split('-')[:2]) for tag in in_info_tags] # unique Python version, C-API version combinations @@ -243,10 +246,10 @@ def add_platforms(wheel_ctx, platforms, remove_platforms=()): if definitely_not_purelib: info['Root-Is-Purelib'] = 'False' - print('Changed wheel type to Platlib') + logger.info('Changed wheel type to Platlib') - print('New WHEEL info tags:', ', '.join(info.get_all('Tag'))) + logger.info('New WHEEL info tags:', ', '.join(info.get_all('Tag'))) write_pkg_info(info_fname, info) else: - print('No WHEEL info change needed.') + logger.info('No WHEEL info change needed.') return out_wheel diff --git a/setup.cfg b/setup.cfg index 99170922..007512c9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,3 +27,7 @@ packages = auditwheel [entry_points] console_scripts = auditwheel = auditwheel.main:main + +[tool:pytest] +log_cli = true +log_cli_level = 20 diff --git a/test-requirements.txt b/test-requirements.txt index 763e956b..4a7e7c40 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,4 +1,4 @@ -pytest +pytest>=3.4 jsonschema numpy pypatchelf diff --git a/tests/test_manylinux.py b/tests/test_manylinux.py index d4ca1a47..5e6cacd2 100644 --- a/tests/test_manylinux.py +++ b/tests/test_manylinux.py @@ -6,8 +6,12 @@ import shutil import warnings from distutils.spawn import find_executable +import logging + + +logger = logging.getLogger(__name__) + -VERBOSE = True ENCODING = 'utf-8' MANYLINUX_IMAGE_ID = 'quay.io/pypa/manylinux1_x86_64' DOCKER_CONTAINER_NAME = 'auditwheel-test-manylinux' @@ -33,19 +37,16 @@ def docker_start(image, volumes={}, env_variables={}): """ # Make sure to use the latest public version of the docker image cmd = ['docker', 'pull', image] - if VERBOSE: - print("$ " + " ".join(cmd)) + logger.info("$ " + " ".join(cmd)) output = check_output(cmd).decode(ENCODING).strip() - if VERBOSE: - print(output) + logger.info(output) cmd = ['docker', 'run', '-d'] for guest_path, host_path in sorted(volumes.items()): cmd.extend(['-v', '%s:%s' % (host_path, guest_path)]) for name, value in sorted(env_variables.items()): cmd.extend(['-e', '%s=%s' % (name, value)]) cmd.extend([image, 'sleep', '10000']) - if VERBOSE: - print("$ " + " ".join(cmd)) + logger.info("$ " + " ".join(cmd)) return check_output(cmd).decode(ENCODING).strip() @@ -54,11 +55,9 @@ def docker_exec(container_id, cmd): if isinstance(cmd, str): cmd = cmd.split() cmd = ['docker', 'exec', container_id] + cmd - if VERBOSE: - print("$ " + " ".join(cmd)) + logger.info("$ " + " ".join(cmd)) output = check_output(cmd).decode(ENCODING) - if VERBOSE: - print(output) + logger.info(output) return output