From 08bc4b14b8d34fc7f3ddad8305ac08e20a192a8e Mon Sep 17 00:00:00 2001 From: Alexandre D'Astous Date: Mon, 11 Apr 2022 15:50:44 -0400 Subject: [PATCH 1/8] Remove internet() function and add timeout in check_github_latest() --- dcm2bids/version.py | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/dcm2bids/version.py b/dcm2bids/version.py index 66b4150a..f8b88f8a 100644 --- a/dcm2bids/version.py +++ b/dcm2bids/version.py @@ -17,29 +17,6 @@ logger = logging.getLogger(__name__) -def internet(host="8.8.8.8", port=53, timeout=3): - """ Check if user has internet - - Args: - host (string): 8.8.8.8 (google-public-dns-a.google.com) - port (int): OpenPort 53/tcp - Service: domain (DNS/TCP) - timeout (int): default=3 - - Returns: - boolean - - Source: https://stackoverflow.com/a/33117579 - """ - try: - socket.setdefaulttimeout(timeout) - socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port)) - return True - - except: - return False - - def is_tool(name): """ Check if a program is in PATH @@ -51,18 +28,19 @@ def is_tool(name): return which(name) is not None -def check_github_latest(githubRepo): +def check_github_latest(githubRepo, timeout=3): """ Check the latest version of a github repository Args: githubRepo (string): a github repository ("username/repository") + timeout (int): time in seconds Returns: A string of the version """ url = "https://github.com/{}/releases/latest".format(githubRepo) try: - output = check_output(shlex.split("curl --silent " + url)) + output = check_output(shlex.split("curl --silent " + url), timeout=timeout) except: logger.debug( "Checking latest version of %s was not possible", githubRepo, @@ -109,7 +87,7 @@ def check_latest(name="dcm2bids"): }, } - if internet() and is_tool("curl"): + if is_tool("curl"): host = data.get(name)["host"] if host == "https://github.com": @@ -122,7 +100,7 @@ def check_latest(name="dcm2bids"): else: logger.debug("Checking latest version of %s was not possible", name) - logger.debug("internet: %s, curl: %s", internet(), is_tool("curl")) + logger.debug("curl: %s", is_tool("curl")) return current = data.get(name)["current"] From a82a8361e9480769df8d2ad4b75238cf9e7a1b2f Mon Sep 17 00:00:00 2001 From: Alexandre D'Astous Date: Mon, 11 Apr 2022 15:53:24 -0400 Subject: [PATCH 2/8] Remove testing of the internet() function --- tests/test_version.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/test_version.py b/tests/test_version.py index 5bc79a14..4ac8d361 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -1,11 +1,7 @@ # -*- coding: utf-8 -*- -from dcm2bids.version import internet, is_tool, check_github_latest, __version__ - - -def test_internet(): - assert internet(port=1234) is False +from dcm2bids.version import is_tool, check_github_latest, __version__ def test_is_tool(): From d28579427dd2adfe6f40c83627896f8881f5c811 Mon Sep 17 00:00:00 2001 From: Alexandre D'Astous Date: Mon, 25 Apr 2022 18:32:44 -0400 Subject: [PATCH 3/8] Split logging into info and debug --- dcm2bids/version.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dcm2bids/version.py b/dcm2bids/version.py index f8b88f8a..8429a0cd 100644 --- a/dcm2bids/version.py +++ b/dcm2bids/version.py @@ -42,10 +42,8 @@ def check_github_latest(githubRepo, timeout=3): try: output = check_output(shlex.split("curl --silent " + url), timeout=timeout) except: - logger.debug( - "Checking latest version of %s was not possible", githubRepo, - exc_info=True, - ) + logger.info(f"Checking latest version of {githubRepo} was not possible") + logger.debug(f"Error while 'curl --silent {url}'", exc_info=True) return # The output should have this format From 3fa7f4bed5b163cce9409c18a0601549ed957605 Mon Sep 17 00:00:00 2001 From: Alexandre D'Astous Date: Thu, 28 Apr 2022 17:33:15 -0400 Subject: [PATCH 4/8] Add exception names in the except clause --- dcm2bids/version.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/dcm2bids/version.py b/dcm2bids/version.py index 8429a0cd..dd7a8448 100644 --- a/dcm2bids/version.py +++ b/dcm2bids/version.py @@ -10,7 +10,7 @@ import shlex import socket from distutils.version import LooseVersion -from subprocess import check_output +from subprocess import check_output, CalledProcessError, TimeoutExpired from shutil import which @@ -41,11 +41,14 @@ def check_github_latest(githubRepo, timeout=3): url = "https://github.com/{}/releases/latest".format(githubRepo) try: output = check_output(shlex.split("curl --silent " + url), timeout=timeout) - except: + except CalledProcessError: logger.info(f"Checking latest version of {githubRepo} was not possible") logger.debug(f"Error while 'curl --silent {url}'", exc_info=True) return - + except TimeoutExpired: + logger.info(f"Checking latest version of {githubRepo} was not possible") + logger.debug(f"Command 'curl --silent {url}' timed out after {timeout}s") + return # The output should have this format # You are being redirected. try: From 9060400220844378bd652c1b487afc5c38f6e5c4 Mon Sep 17 00:00:00 2001 From: Alexandre D'Astous Date: Fri, 27 May 2022 13:53:07 -0400 Subject: [PATCH 5/8] Remove unused import --- dcm2bids/version.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dcm2bids/version.py b/dcm2bids/version.py index dd7a8448..886df15a 100644 --- a/dcm2bids/version.py +++ b/dcm2bids/version.py @@ -8,7 +8,6 @@ import logging import shlex -import socket from distutils.version import LooseVersion from subprocess import check_output, CalledProcessError, TimeoutExpired from shutil import which From 6e7cf4a6af999cc29ddb25c3a79e4761298512d5 Mon Sep 17 00:00:00 2001 From: Alexandre D'Astous Date: Fri, 27 May 2022 14:26:20 -0400 Subject: [PATCH 6/8] Check versions after initializing the logger --- dcm2bids/dcm2bids.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dcm2bids/dcm2bids.py b/dcm2bids/dcm2bids.py index ea305c2b..2d828b87 100644 --- a/dcm2bids/dcm2bids.py +++ b/dcm2bids/dcm2bids.py @@ -103,6 +103,10 @@ def run(self): self.participant, self.config.get("dcm2niixOptions", DEFAULT.dcm2niixOptions), ) + + check_latest() + check_latest("dcm2niix") + dcm2niix.run(self.forceDcm2niix) sidecars = [] @@ -236,9 +240,6 @@ def main(): parser = _build_arg_parser() args = parser.parse_args() - check_latest() - check_latest("dcm2niix") - app = Dcm2bids(**vars(args)) return app.run() From 357c401b36bb773ba77b5cf556fa6148689aaa72 Mon Sep 17 00:00:00 2001 From: Alexandre D'Astous Date: Fri, 27 May 2022 14:27:22 -0400 Subject: [PATCH 7/8] Add ability for curl to follow redirect links --- dcm2bids/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dcm2bids/version.py b/dcm2bids/version.py index 886df15a..b027b3a0 100644 --- a/dcm2bids/version.py +++ b/dcm2bids/version.py @@ -39,7 +39,7 @@ def check_github_latest(githubRepo, timeout=3): """ url = "https://github.com/{}/releases/latest".format(githubRepo) try: - output = check_output(shlex.split("curl --silent " + url), timeout=timeout) + output = check_output(shlex.split("curl -L --silent " + url), timeout=timeout) except CalledProcessError: logger.info(f"Checking latest version of {githubRepo} was not possible") logger.debug(f"Error while 'curl --silent {url}'", exc_info=True) From e4ad28b0981963ef08bfb022669210632e302918 Mon Sep 17 00:00:00 2001 From: Alexandre D'Astous Date: Fri, 27 May 2022 15:48:35 -0400 Subject: [PATCH 8/8] Restrict version to 5 charaters --- dcm2bids/version.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dcm2bids/version.py b/dcm2bids/version.py index b027b3a0..5e5d364b 100644 --- a/dcm2bids/version.py +++ b/dcm2bids/version.py @@ -51,11 +51,12 @@ def check_github_latest(githubRepo, timeout=3): # The output should have this format # You are being redirected. try: - return ( - output.decode() - .split("{}/releases/tag/".format(githubRepo))[1] - .split('"')[0] - ) + version = output.decode().split("{}/releases/tag/".format(githubRepo))[1].split('"')[0] + + # Versions are X.X.X + if len(version) > 5: + version = version[:5] + return version except: logger.debug( "Checking latest version of %s was not possible", githubRepo,