From b34a8099e6e72feb8498cf5a1e3bb4504a9dd0c7 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Wed, 29 May 2019 21:27:37 +0900 Subject: [PATCH] Add mirror url option Signed-off-by: Hiroshi Miura --- CHANGELOG.rst | 2 ++ aqt/archives.py | 36 ++++++++++++++++++------------------ aqt/cli.py | 12 ++++++++++-- aqt/installer.py | 9 ++++++--- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index aff1c89..1c26491 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,8 @@ Current changes Added ----- +* Option to specify mirror site. + Changed ------- diff --git a/aqt/archives.py b/aqt/archives.py index 41c9e0b..a6ac890 100644 --- a/aqt/archives.py +++ b/aqt/archives.py @@ -33,44 +33,43 @@ class QtPackage: url = "" archive = "" desc = "" + mirror = None - def __init__(self, name, archive_url, archive, package_desc): + def __init__(self, name, archive_url, archive, package_desc, has_mirror=False): self.name = name self.url = archive_url self.archive = archive self.desc = package_desc - - def get_name(self): - return self.name - - def get_url(self): - return self.url - - def get_archive(self): - return self.archive - - def get_desc(self): - return self.desc + self.has_mirror = has_mirror class QtArchives: BASE_URL = 'https://download.qt.io/online/qtsdkrepository/' archives = [] + base = None - def __init__(self, os_name, qt_version, target, arch): + def __init__(self, os_name, qt_version, target, arch, mirror=None): self.qt_version = qt_version self.target = target self.arch = arch + self.base = mirror + if mirror is not None: + self.has_mirror = True + else: + self.base = self.BASE_URL qt_ver_num = qt_version.replace(".", "") if os_name == 'windows': - archive_url = self.BASE_URL + os_name + '_x86/' + target + '/' + 'qt5_' + qt_ver_num + '/' + archive_url = self.base + os_name + '_x86/' + target + '/' + 'qt5_' + qt_ver_num + '/' else: - archive_url = self.BASE_URL + os_name + '_x64/' + target + '/' + 'qt5_' + qt_ver_num + '/' + archive_url = self.base + os_name + '_x64/' + target + '/' + 'qt5_' + qt_ver_num + '/' # Get packages index update_xml_url = "{0}Updates.xml".format(archive_url) try: - r = aqt.metalink.get(update_xml_url) + if mirror is not None: + r = requests.get(update_xml_url) + else: + r = aqt.metalink.get(update_xml_url) except requests.exceptions.ConnectionError as e: print("Caught download error: %s" % e.args) exc_buffer = StringIO() @@ -98,7 +97,8 @@ class QtArchives: package_desc = packageupdate.find("Description").text for archive in downloadable_archives: package_url = archive_url + name + "/" + full_version + archive - self.archives.append(QtPackage(name, package_url, archive, package_desc)) + self.archives.append(QtPackage(name, package_url, archive, package_desc, + has_mirror=(mirror is not None))) if len(self.archives) == 0: print("Error while parsing package information!") diff --git a/aqt/cli.py b/aqt/cli.py index 0d9f3d3..fe1458f 100644 --- a/aqt/cli.py +++ b/aqt/cli.py @@ -36,6 +36,7 @@ class Cli(): target = args.target os_name = args.host output_dir = args.outputdir + mirror = args.mirror if arch is None: if os_name == "linux" and target == "desktop": arch = "gcc_64" @@ -48,11 +49,15 @@ class Cli(): args.print_help() exit(1) qt_version = args.qt_version + 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)).install(target_dir=output_dir) + QtInstaller(QtArchives(os_name, qt_version, target, arch, mirror=mirror)).install(target_dir=output_dir) else: - QtInstaller(QtArchives(os_name, qt_version, target, arch)).install() + QtInstaller(QtArchives(os_name, qt_version, target, arch, mirror=mirror)).install() sys.stdout.write("\033[K") print("Finished installation") @@ -83,6 +88,9 @@ class Cli(): "\nandroid: android_x86, android_armv7") install_parser.add_argument('-O', '--outputdir', nargs='?', help='Target output directory(default current directory)') + install_parser.add_argument('-m', '--mirror', nargs='?', + help="Specify mirror base url such as http://mirrors.ocf.berkeley.edu/qt/, " + "where 'online' folder exist.") list_parser = subparsers.add_parser('list') list_parser.set_defaults(func=self.run_list) list_parser.add_argument("qt_version", help="Qt version in the format of \"5.X.Y\"") diff --git a/aqt/installer.py b/aqt/installer.py index 3fcef54..b182f01 100644 --- a/aqt/installer.py +++ b/aqt/installer.py @@ -50,11 +50,14 @@ class QtInstaller: @staticmethod def retrieve_archive(package, path=None): - archive = package.get_archive() - url = package.get_url() + archive = package.archive + url = package.url print("-Downloading {}...".format(url)) try: - r = aqt.metalink.get(url, stream=True) + if package.has_mirror: + r = aqt.metalink.get(url, stream=True) + else: + r = requests.get(url, stream=True) except requests.exceptions.ConnectionError as e: print("Caught download error: %s" % e.args) return False