From 6e86e34f75891c06f048968bd29734db2e00be32 Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Tue, 4 Aug 2020 14:19:54 +0100 Subject: [PATCH 1/8] Add requests dependency --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index c43d3d681..8ec5947c8 100644 --- a/setup.py +++ b/setup.py @@ -216,6 +216,7 @@ def read(fname): 'PyYAML', 'zeroc-ice>=3.6.4,<3.7', 'pywin32; platform_system=="Windows"', + 'requests' ], tests_require=[ 'pytest', From 192b43a50e69fb4dfb88b13413f47537764c4f12 Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Tue, 4 Aug 2020 14:20:24 +0100 Subject: [PATCH 2/8] Use requests for upgrade check --- src/omero/util/upgrade_check.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/omero/util/upgrade_check.py b/src/omero/util/upgrade_check.py index 3d422de64..e032f9bd2 100644 --- a/src/omero/util/upgrade_check.py +++ b/src/omero/util/upgrade_check.py @@ -13,9 +13,7 @@ import platform import logging -import urllib.request, urllib.error, urllib.parse -import urllib.request, urllib.parse, urllib.error -import socket +import requests class UpgradeCheck(object): @@ -129,20 +127,13 @@ def run(self): params["python.version"] = platform.python_version() params["python.compiler"] = platform.python_compiler() params["python.build"] = platform.python_build() - params = urllib.parse.urlencode(params) - - old_timeout = socket.getdefaulttimeout() - try: - socket.setdefaulttimeout(self.timeout) - full_url = "%s?%s" % (self.url, params) - request = urllib.request.Request(full_url) - request.add_header('User-Agent', self.agent) - self.log.debug("Attempting to connect to %s" % full_url) - response = urllib.request.urlopen(request) - result = response.read() - finally: - socket.setdefaulttimeout(old_timeout) + headers = {'User-Agent': self.agent} + request = requests.get( + self.url, headers=headers, params=params, + timeout=self.DEFAULT_TIMEOUT + ) + result = request.text except Exception as e: self.log.error(str(e), exc_info=0) self._set(None, e) From 5ae046aef82a7006c7380ac6dbabc410c1f60cf3 Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Tue, 4 Aug 2020 14:40:42 +0100 Subject: [PATCH 3/8] Fix flake8 errors --- src/omero/util/upgrade_check.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/omero/util/upgrade_check.py b/src/omero/util/upgrade_check.py index e032f9bd2..97adfd7d3 100644 --- a/src/omero/util/upgrade_check.py +++ b/src/omero/util/upgrade_check.py @@ -6,7 +6,6 @@ """ from future import standard_library -standard_library.install_aliases() from builtins import str from builtins import object from omero_version import omero_version @@ -14,6 +13,7 @@ import platform import logging import requests +standard_library.install_aliases() class UpgradeCheck(object): @@ -100,7 +100,7 @@ def getOSVersion(self): platform.mac_ver()[0]) else: version = platform.platform() - except: + except Exception: version = platform.platform() return version From c889c4902562cf856e30bbd73bce692c1f797532 Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Tue, 4 Aug 2020 14:43:05 +0100 Subject: [PATCH 4/8] Use requests for OMERO Java download --- src/omero/plugins/import.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/omero/plugins/import.py b/src/omero/plugins/import.py index 5762e7b1e..0a219e9de 100644 --- a/src/omero/plugins/import.py +++ b/src/omero/plugins/import.py @@ -35,7 +35,7 @@ import csv import sys import shlex -from urllib.request import urlopen +import requests from zipfile import ZipFile @@ -575,8 +575,8 @@ def download_omero_java(self, version): jars_dir, omero_java_txt = self._userdir_jars(parentonly=True) if not jars_dir.exists(): jars_dir.mkdir() - with urlopen(omero_java_zip) as resp: - with ZipFile(BytesIO(resp.read())) as zipfile: + with requests.get(omero_java_zip) as resp: + with ZipFile(BytesIO(resp.content)) as zipfile: topdirs = set(f.filename.split( os.path.sep)[0] for f in zipfile.filelist if f.is_dir()) if len(topdirs) != 1: From 573bda86d9f355029b6e4ef944d5e7aad634efc2 Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Tue, 4 Aug 2020 14:43:14 +0100 Subject: [PATCH 5/8] Fix flake8 errors --- src/omero/plugins/import.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/omero/plugins/import.py b/src/omero/plugins/import.py index 0a219e9de..029589c35 100644 --- a/src/omero/plugins/import.py +++ b/src/omero/plugins/import.py @@ -312,8 +312,14 @@ def add_python_argument(*args, **kwargs): for name, help in ( ("bulk", "Bulk YAML file for driving multiple imports"), ("logprefix", "Directory or file prefix for --file and --errs"), - ("file", "File for storing the standard output from the Java process"), - ("errs", "File for storing the standard error from the Java process") + ( + "file", + "File for storing the standard output from the Java process" + ), + ( + "errs", + "File for storing the standard error from the Java process" + ) ): add_python_argument("--%s" % name, nargs="?", help=help) add_python_argument("---%s" % name, nargs="?", help=SUPPRESS) @@ -767,6 +773,7 @@ def parse_csv(self, path, delimiter=","): class TestEngine(ImportControl): COMMAND = [TEST_CLASS] + try: register("import", ImportControl, HELP, epilog=EXAMPLES) register("testengine", TestEngine, TESTHELP) From 16dd113f7f3757c23c8e095cb2472aa586725071 Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Wed, 5 Aug 2020 11:01:12 +0100 Subject: [PATCH 6/8] Accurate timeout doc reference --- src/omero/util/upgrade_check.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/omero/util/upgrade_check.py b/src/omero/util/upgrade_check.py index 97adfd7d3..96c29aea4 100644 --- a/src/omero/util/upgrade_check.py +++ b/src/omero/util/upgrade_check.py @@ -45,8 +45,8 @@ class UpgradeCheck(object): """ # - # Default timeout is 3 seconds. - # * http://docs.python.org/2/library/socket.html#socket.setdefaulttimeout + # requests timeout, default is no timeout + # * https://requests.readthedocs.io/en/master/user/quickstart/#timeouts # DEFAULT_TIMEOUT = 6.0 From 4f8d9fbc1984a59d5086893b1c5e0ebfac8fe85a Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Wed, 5 Aug 2020 11:01:44 +0100 Subject: [PATCH 7/8] Result has already been decoded --- src/omero/util/upgrade_check.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/omero/util/upgrade_check.py b/src/omero/util/upgrade_check.py index 96c29aea4..018c10785 100644 --- a/src/omero/util/upgrade_check.py +++ b/src/omero/util/upgrade_check.py @@ -143,5 +143,5 @@ def run(self): self.log.info("no update needed") self._set(None, None) else: - self.log.warn("UPGRADE AVAILABLE:" + result.decode('utf-8')) - self._set(result.decode('utf-8'), None) + self.log.warn("UPGRADE AVAILABLE:" + result) + self._set(result, None) From 0c01a0b666072bcef686ee86899463e2e4a957d7 Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Wed, 5 Aug 2020 13:32:47 +0100 Subject: [PATCH 8/8] Reword comment --- src/omero/util/upgrade_check.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/omero/util/upgrade_check.py b/src/omero/util/upgrade_check.py index 018c10785..2ec6d52ae 100644 --- a/src/omero/util/upgrade_check.py +++ b/src/omero/util/upgrade_check.py @@ -45,7 +45,7 @@ class UpgradeCheck(object): """ # - # requests timeout, default is no timeout + # our default timeout to use for requests; package default is no timeout # * https://requests.readthedocs.io/en/master/user/quickstart/#timeouts # DEFAULT_TIMEOUT = 6.0