From 886376d81bb1bf6743d776b5b690f2ec7daf431c Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Sat, 28 Sep 2019 11:41:53 +0900 Subject: [PATCH] Add logging argument options Signed-off-by: Hiroshi Miura --- aqt/archives.py | 33 ++++++++++++++++++++------------- aqt/cli.py | 24 ++++++++++++++++++++---- aqt/installer.py | 11 ++++++++--- aqt/logging.yml | 22 ++++++++++++++++++++++ requirements.txt | 1 + 5 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 aqt/logging.yml diff --git a/aqt/archives.py b/aqt/archives.py index d89b133..55c48ab 100644 --- a/aqt/archives.py +++ b/aqt/archives.py @@ -20,10 +20,10 @@ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -import logging import requests import traceback import xml.etree.ElementTree as ElementTree +from logging import getLogger from six import StringIO @@ -47,7 +47,7 @@ class QtArchives: archives = [] base = None - def __init__(self, os_name, qt_version, target, arch, mirror=None): + def __init__(self, os_name, qt_version, target, arch, mirror=None, logging=None): self.qt_version = qt_version self.target = target self.arch = arch @@ -56,10 +56,17 @@ class QtArchives: self.base = mirror + '/online/qtsdkrepository/' else: self.base = self.BASE_URL - qt_ver_num = qt_version.replace(".", "") + if logging: + self.logger = logging + else: + self.logger = getLogger('aqt') + self._get_archives(os_name) + + def _get_archives(self, os_name): + qt_ver_num = self.qt_version.replace(".", "") # install mingw runtime package - if arch in ['win64_mingw73', 'win32_mingw73', 'win64_mingw53', 'win32_mingw53']: + if self.arch in ['win64_mingw73', 'win32_mingw73', 'win64_mingw53', 'win32_mingw53']: archive_url = self.base + 'windows_x86/desktop/tools_mingw/' update_xml_url = "{0}Updates.xml".format(archive_url) try: @@ -68,13 +75,13 @@ class QtArchives: print("Caught download error: %s" % e.args) exc_buffer = StringIO() traceback.print_exc(file=exc_buffer) - logging.error('Download error:\n%s', exc_buffer.getvalue()) + self.logger.error('Download error:\n%s', exc_buffer.getvalue()) raise e else: self.update_xml = ElementTree.fromstring(r.text) for packageupdate in self.update_xml.iter("PackageUpdate"): name = packageupdate.find("Name").text - if name.split(".")[-1] != arch: + if name.split(".")[-1] != self.arch: continue downloadable_archives = packageupdate.find("DownloadableArchives").text.split(", ") full_version = packageupdate.find("Version").text @@ -85,12 +92,12 @@ class QtArchives: # ex. 7.3.0-1x86_64-7.3.0-release-posix-seh-rt_v5-rev0.7z package_url = archive_url + name + "/" + named_version + archive self.archives.append(QtPackage(name, package_url, archive, package_desc, - has_mirror=(mirror is not None))) + has_mirror=(self.mirror is not None))) # Ordinary packages if os_name == 'windows': - archive_url = self.base + os_name + '_x86/' + target + '/' + 'qt5_' + qt_ver_num + '/' + archive_url = self.base + os_name + '_x86/' + self.target + '/' + 'qt5_' + qt_ver_num + '/' else: - archive_url = self.base + os_name + '_x64/' + target + '/' + 'qt5_' + qt_ver_num + '/' + archive_url = self.base + os_name + '_x64/' + self.target + '/' + 'qt5_' + qt_ver_num + '/' # Get packages index update_xml_url = "{0}Updates.xml".format(archive_url) @@ -100,19 +107,19 @@ class QtArchives: print("Caught download error: %s" % e.args) exc_buffer = StringIO() traceback.print_exc(file=exc_buffer) - logging.error('Download error:\n%s', exc_buffer.getvalue()) + self.logger.error('Download error:\n%s', exc_buffer.getvalue()) raise e else: self.update_xml = ElementTree.fromstring(r.text) for packageupdate in self.update_xml.iter("PackageUpdate"): name = packageupdate.find("Name").text - if name.split(".")[-1] != arch: + if name.split(".")[-1] != self.arch: continue if name.split(".")[-2] == "debug_info": continue if packageupdate.find("DownloadableArchives").text is None: continue - if name == "qt.qt5.{}.{}".format(qt_ver_num, arch) or name == "qt.{}.{}".format(qt_ver_num, arch): + if name == "qt.qt5.{}.{}".format(qt_ver_num, self.arch) or name == "qt.{}.{}".format(qt_ver_num, self.arch): # basic packages pass else: @@ -124,7 +131,7 @@ class QtArchives: for archive in downloadable_archives: package_url = archive_url + name + "/" + full_version + archive self.archives.append(QtPackage(name, package_url, archive, package_desc, - has_mirror=(mirror is not None))) + has_mirror=self.has_mirror)) if len(self.archives) == 0: print("Error while parsing package information!") diff --git a/aqt/cli.py b/aqt/cli.py index 90d63ec..70b7648 100644 --- a/aqt/cli.py +++ b/aqt/cli.py @@ -21,9 +21,12 @@ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import argparse +import logging.config import os import platform import sys +import yaml +from logging import getLogger from aqt.archives import QtArchives from aqt.installer import QtInstaller @@ -93,17 +96,18 @@ class Cli(): exit(1) qt_version = args.qt_version if not self.check_arg_combination(qt_version, os_name, target, arch): - print("Specified target combination is not valid: {} {} {}".format(os_name, target, arch)) + self.logger.error("Specified target combination is not valid: {} {} {}".format(os_name, target, arch)) exit(1) if mirror is not None: if not mirror.startswith('http://') or mirror.startswith('https://') or mirror.startswith('ftp://'): args.print_help() exit(1) if output_dir is not None: - QtInstaller(QtArchives(os_name, qt_version, target, arch, mirror=mirror)).install(command=sevenzip, - target_dir=output_dir) + QtInstaller(QtArchives(os_name, qt_version, target, arch, mirror=mirror, logging=self.logger), + logging=self.logger).install(command=sevenzip, target_dir=output_dir) else: - QtInstaller(QtArchives(os_name, qt_version, target, arch, mirror=mirror)).install(command=sevenzip) + QtInstaller(QtArchives(os_name, qt_version, target, arch, mirror=mirror, logging=self.logger), + logging = self.logger).install(command=sevenzip) sys.stdout.write("\033[K") print("Finished installation") @@ -118,6 +122,9 @@ class Cli(): def __init__(self): parser = argparse.ArgumentParser(prog='aqt', description='Installer for Qt SDK.', formatter_class=argparse.RawTextHelpFormatter, add_help=True) + parser.add_argument('--logging-conf', type=argparse.FileType('r'), + nargs=1, help="Specify logging configuration YAML file.") + parser.add_argument('--logger', nargs=1, help="Specify logger name") subparsers = parser.add_subparsers(title='subcommands', description='Valid subcommands', help='subcommand for aqt Qt installer') install_parser = subparsers.add_parser('install') @@ -150,4 +157,13 @@ class Cli(): def run(self): args = self.parser.parse_args() + if args.logging_conf: + log_config = yaml.load(args.logging_conf) + else: + log_config = yaml.load(os.path.join(os.path.dirname(__file__), 'logging.yml')) + logging.config.dictConfig(log_config) + if args.logger is not None: + self.logger = getLogger(args.logger) + else: + self.logger = getLogger('aqt') args.func(args) diff --git a/aqt/installer.py b/aqt/installer.py index 9588579..34df226 100644 --- a/aqt/installer.py +++ b/aqt/installer.py @@ -21,12 +21,12 @@ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import functools -import logging import os import py7zr import requests import traceback import xml.etree.ElementTree as ElementTree +from logging import getLogger from six import StringIO from multiprocessing.dummy import Pool from operator import and_ @@ -47,8 +47,13 @@ class QtInstaller: Installer class to download packages and extract it. """ - def __init__(self, qt_archives): + def __init__(self, qt_archives, logging=None): self.qt_archives = qt_archives + if logging: + self.logger = logging + else: + self.logger = getLogger('aqt') + @staticmethod def retrieve_archive(package, path=None, command=None): @@ -126,7 +131,7 @@ class QtInstaller: print("Configuration file generation error: %s" % e.args) exc_buffer = StringIO() traceback.print_exc(file=exc_buffer) - logging.error('Error happened when writing configuration files:\n%s', exc_buffer.getvalue()) + self.logger.error('Error happened when writing configuration files:\n%s', exc_buffer.getvalue()) raise e else: exit(1) diff --git a/aqt/logging.yml b/aqt/logging.yml new file mode 100644 index 0000000..a04a77b --- /dev/null +++ b/aqt/logging.yml @@ -0,0 +1,22 @@ +version: 1 +formatters: + brief: + format: '%(message)s' + default: + format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s' + datefmt: '%Y-%m-%d %H:%M:%S' +handlers: + console: + class: logging.StreamHandler + level: WARN + formatter: brief + stream: ext://sys.stdout + file: + class : logging.FileHandler + formatter: default + filename: aqtinstall.log +root: + level: INFO + handlers: + - console + - file \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 78f38c5..ef6804c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ flake8 wheel twine py7zr +pyyaml \ No newline at end of file