Skip to content

Commit

Permalink
Merge pull request #198 from miurahr/patch-requests-timeout
Browse files Browse the repository at this point in the history
Add --timeout option for connection
  • Loading branch information
miurahr authored Jan 21, 2021
2 parents 1cc7c59 + 01ed433 commit 49dd701
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ General usage looks like this:
aqt [-h][--help][-O | --outputdir <directory>][-b | --base <mirror url>][-E | --external <7zip command>] \
install <qt-version> <host> <target> [<arch>] [-m all | -m [extra module] [extra module]...] [--internal]
[--archives archive]
[--archives <archive>[ <archive>...]] [--timeout <timeout(sec)>]
You can also call with ``python -m aqt`` syntax as well as command script ``aqt``.
Expand Down Expand Up @@ -110,7 +110,7 @@ You can install tools and utilities using following syntax;
.. code-block:: bash
python -m aqt [-h][--help][-O | --outputdir <directory>][-b | --base <mirror url>][-E | --external <7zip command>] \
tool <host> <tool_name> <tool-version> <arch>
tool <host> <tool_name> <tool-version> <arch> [--timeout <timeout>]
* tool_name is one of `tools_ifw`, `tools_vcredist`, and `tools_openssl`.
* arch is full qualified tool name such as `qt.tools.ifw.31` which values can be seen on Qt `archive_site`_
Expand Down
18 changes: 10 additions & 8 deletions aqt/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ class PackagesList:
Hold packages list information.
"""

def __init__(self, version, os_name, target, base):
def __init__(self, version, os_name, target, base, timeout=(5, 5)):
self.version = version
self.os_name = os_name
self.target = target
self.archives = []
self.base = base
self.timeout = timeout
self.logger = getLogger('aqt')
self._get_archives()

Expand All @@ -91,7 +92,7 @@ def _get_archives(self):
archive_path = "{0}{1}{2}/qt5_{3}/".format(self.os_name, '_x86/' if self.os_name == 'windows' else '_x64/',
self.target, qt_ver_num)
update_xml_url = "{0}{1}Updates.xml".format(self.base, archive_path)
r = requests.get(update_xml_url)
r = requests.get(update_xml_url, timeout=self.timeout)
self.update_xml = ElementTree.fromstring(r.text)
for packageupdate in self.update_xml.iter("PackageUpdate"):
name = packageupdate.find("Name").text
Expand All @@ -116,7 +117,7 @@ class QtArchives:
"""Hold Qt archive packages list."""

def __init__(self, os_name, target, version, arch, base, subarchives=None,
modules=None, logging=None, all_extra=False):
modules=None, logging=None, all_extra=False, timeout=(5, 5)):
self.version = version
self.target = target
self.arch = arch
Expand All @@ -139,6 +140,7 @@ def __init__(self, os_name, target, version, arch, base, subarchives=None,
for m in modules if modules is not None else []:
self.mod_list.append("qt.qt{}.{}.{}.{}".format(self.qt_ver_base, qt_ver_num, m, arch))
self.mod_list.append("qt.{}.{}.{}".format(qt_ver_num, m, arch))
self.timeout = timeout
self._get_archives(qt_ver_num)
if not all_archives:
self.archives = list(filter(lambda a: a.name in subarchives, self.archives))
Expand All @@ -160,7 +162,7 @@ def _get_archives(self, qt_ver_num):

def _download_update_xml(self, update_xml_url):
try:
r = requests.get(update_xml_url)
r = requests.get(update_xml_url, timeout=self.timeout)
except (ConnectionResetError, requests.exceptions.ConnectionError):
raise ArchiveConnectionError()
else:
Expand Down Expand Up @@ -226,14 +228,14 @@ class SrcDocExamplesArchives(QtArchives):
"""Hold doc/src/example archive package list."""

def __init__(self, flavor, os_name, target, version, base, subarchives=None,
modules=None, logging=None, all_extra=False):
modules=None, logging=None, all_extra=False, timeout=(5, 5)):
self.flavor = flavor
self.target = target
self.os_name = os_name
self.base = base
super(SrcDocExamplesArchives, self).__init__(os_name, target, version, self.flavor, base,
subarchives=subarchives, modules=modules, logging=logging,
all_extra=all_extra)
all_extra=all_extra, timeout=timeout)

def _get_archives(self, qt_ver_num):
archive_path = "{0}{1}{2}/qt{3}_{4}{5}".format(self.os_name,
Expand Down Expand Up @@ -264,10 +266,10 @@ class ToolArchives(QtArchives):
ToolArchive(linux, desktop, 3.1.1, ifw)
"""

def __init__(self, os_name, tool_name, version, arch, base, logging=None):
def __init__(self, os_name, tool_name, version, arch, base, logging=None, timeout=(5, 5)):
self.tool_name = tool_name
self.os_name = os_name
super(ToolArchives, self).__init__(os_name, 'desktop', version, arch, base, logging=logging)
super(ToolArchives, self).__init__(os_name, 'desktop', version, arch, base, logging=logging, timeout=timeout)

def _get_archives(self, qt_ver_num):
if self.os_name == 'windows':
Expand Down
26 changes: 20 additions & 6 deletions aqt/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ def run_install(self, args):
base_dir = os.getcwd()
else:
base_dir = output_dir
if args.timeout is not None:
timeout = (args.timeout, args.timeout)
else:
timeout = (5, 5)
arch = self._set_arch(args, arch, os_name, target, qt_version)
modules = args.modules
sevenzip = self._set_sevenzip(args)
Expand All @@ -190,13 +194,13 @@ def run_install(self, args):
self.logger.warning("Some of specified modules are unknown.")
try:
qt_archives = QtArchives(os_name, target, qt_version, arch, base, subarchives=archives, modules=modules,
logging=self.logger, all_extra=all_extra)
logging=self.logger, all_extra=all_extra, timeout=timeout)
except ArchiveConnectionError:
try:
self.logger.warning("Connection to the download site failed and fallback to mirror site.")
qt_archives = QtArchives(os_name, target, qt_version, arch, random.choice(FALLBACK_URLS),
subarchives=archives, modules=modules, logging=self.logger,
all_extra=all_extra)
all_extra=all_extra, timeout=timeout)
except Exception:
self.logger.error("Connection to the download site failed. Aborted...")
exit(1)
Expand All @@ -218,6 +222,10 @@ def _run_src_doc_examples(self, flavor, args):
base = args.base + '/online/qtsdkrepository/'
else:
base = BASE_URL
if args.timeout is not None:
timeout = (args.timeout, args.timeout)
else:
timeout = (5, 5)
sevenzip = self._set_sevenzip(args)
modules = args.modules
archives = args.archives
Expand All @@ -228,14 +236,14 @@ def _run_src_doc_examples(self, flavor, args):
try:
srcdocexamples_archives = SrcDocExamplesArchives(flavor, os_name, target, qt_version, base,
subarchives=archives, modules=modules, logging=self.logger,
all_extra=all_extra)
all_extra=all_extra, timeout=timeout)
except ArchiveConnectionError:
try:
self.logger.warning("Connection to the download site failed and fallback to mirror site.")
srcdocexamples_archives = SrcDocExamplesArchives(flavor, os_name, target, qt_version,
random.choice(FALLBACK_URLS),
subarchives=archives, modules=modules,
logging=self.logger, all_extra=all_extra)
logging=self.logger, all_extra=all_extra, timeout=timeout)
except Exception:
self.logger.error("Connection to the download site failed. Aborted...")
exit(1)
Expand Down Expand Up @@ -266,16 +274,20 @@ def run_tool(self, args):
base = args.base + '/online/qtsdkrepository/'
else:
base = BASE_URL
if args.timeout is not None:
timeout = (args.timeout, args.timeout)
else:
timeout = (5, 5)
self._run_common_part(output_dir, base)
if not self._check_tools_arg_combination(os_name, tool_name, arch):
self.logger.warning("Specified target combination is not valid: {} {} {}".format(os_name, tool_name, arch))
try:
tool_archives = ToolArchives(os_name, tool_name, version, arch, base, logging=self.logger)
tool_archives = ToolArchives(os_name, tool_name, version, arch, base, logging=self.logger, timeout=timeout)
except ArchiveConnectionError:
try:
self.logger.warning("Connection to the download site failed and fallback to mirror site.")
tool_archives = ToolArchives(os_name, tool_name, version, arch, random.choice(FALLBACK_URLS),
logging=self.logger)
logging=self.logger, timeout=timeout)
except Exception:
self.logger.error("Connection to the download site failed. Aborted...")
exit(1)
Expand Down Expand Up @@ -323,6 +335,8 @@ def _set_common_options(self, subparser):
"where 'online' folder exist.")
subparser.add_argument('-E', '--external', nargs='?', help='Specify external 7zip command path.')
subparser.add_argument('--internal', action='store_true', help='Use internal extractor.')
subparser.add_argument('--timeout', nargs='?', type=float,
help="Specify connection timeout for download site.(default: 5 sec)")

def _set_module_options(self, subparser):
subparser.add_argument('-m', '--modules', nargs='*', help="Specify extra modules to install")
Expand Down

0 comments on commit 49dd701

Please sign in to comment.