From faeb7afef4af2e5998288de2fa769d5701a16821 Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Wed, 27 Nov 2019 11:27:27 +0100 Subject: [PATCH 1/2] [Issue287] IMAS version check operational in imas2tofu.__init__, made non-critical --- tofu/imas2tofu/__init__.py | 66 +++++++++++++++++++++++++++++++++++++- tofu/version.py | 2 +- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/tofu/imas2tofu/__init__.py b/tofu/imas2tofu/__init__.py index 184fc8773..27d26e50a 100644 --- a/tofu/imas2tofu/__init__.py +++ b/tofu/imas2tofu/__init__.py @@ -6,13 +6,13 @@ """ import warnings import traceback +import itertools as itt try: try: from tofu.imas2tofu._core import * except Exception: from ._core import * - del warnings, traceback except Exception as err: if str(err) == 'imas not available': msg = "" @@ -26,5 +26,69 @@ msg += "\n\n => the optional sub-package tofu.imas2tofu is not usable\n" raise Exception(msg) + +# ----------------------------------------------- +# Check IMAS version vs latest available in linux modules +# ----------------------------------------------- + +_KEYSTR = 'IMAS/' + +# extract all IMAS versions from a str returned by modules +def extractIMAS(ss, keystr=_KEYSTR): + if not keystr in ss: + raise Exception + ls = ss[ss.index(keystr):].split('\n') + ls = itt.chain.from_iterable([s.split(' ') for s in ls]) + ls = [s for s in ls if keystr in s] + ls = [s[len(keystr):s.index('(')] if '(' in s else s[len(keystr):] + for s in ls] + return sorted(ls) + +# Compare current and latest available IMAS versions +def check_IMAS_version(verb=True, keystr=_KEYSTR): + import subprocess + + # Get currently loaded IMAS + cmd = "module list" + proc = subprocess.run(cmd, check=True, shell=True, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + lcur = extractIMAS(proc.stdout.decode(), keystr=keystr) + if len(lcur) != 1: + msg = ("You seem to have no / several IMAS version loaded:\n" + + "\t- module list: {}".format(lcur)) + raise Exception(msg) + + # Get all available IMAS + cmd = "module av IMAS" + proc = subprocess.run(cmd, check=True, shell=True, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + lav = extractIMAS(proc.stdout.decode(), keystr=keystr) + if len(lav) == 0: + msg = "There is not available IMAS version" + raise Exception(msg) + + # Compare and warn + if lcur[0] not in lav: + msg = "The current IMAS version is not available!" + raise Exception(msg) + + msg = None + if lav.index(lcur[0]) != len(lav)-1: + msg = ("\nYou do not seem to be using the latest IMAS version:\n" + + "'module list' vs 'module av IMAS' suggests:\n" + + "\t- Current version: {}\n".format(lcur[0]) + + "\t- Latest version : {}".format(lav[-1])) + warnings.warn(msg) + return lcur[0], lav + +# Try comparing and warning +try: + _, _ = check_IMAS_version(verb=True) +except Exception as err: + # This warning is an optional luxury, should not block anything + pass + + __all__ = ['MultiIDSLoader', 'load_Config', 'load_Plasma2D', 'load_Cam', 'load_Data'] +del warnings, traceback, itt, _KEYSTR diff --git a/tofu/version.py b/tofu/version.py index a9ed57da4..382360f86 100644 --- a/tofu/version.py +++ b/tofu/version.py @@ -1,2 +1,2 @@ # Do not edit, pipeline versioning governed by git tags! -__version__ = '1.4.2a3' +__version__ = '1.4.2a3-1-g88f9524' From 7a705e84cfd7acf116dab3ade698c88ed40ec040 Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Wed, 27 Nov 2019 11:43:12 +0100 Subject: [PATCH 2/2] [Issue287] PEP8 Compliance 1 --- tofu/imas2tofu/__init__.py | 5 ++++- tofu/version.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tofu/imas2tofu/__init__.py b/tofu/imas2tofu/__init__.py index 27d26e50a..842ecaef4 100644 --- a/tofu/imas2tofu/__init__.py +++ b/tofu/imas2tofu/__init__.py @@ -33,9 +33,10 @@ _KEYSTR = 'IMAS/' + # extract all IMAS versions from a str returned by modules def extractIMAS(ss, keystr=_KEYSTR): - if not keystr in ss: + if keystr not in ss: raise Exception ls = ss[ss.index(keystr):].split('\n') ls = itt.chain.from_iterable([s.split(' ') for s in ls]) @@ -44,6 +45,7 @@ def extractIMAS(ss, keystr=_KEYSTR): for s in ls] return sorted(ls) + # Compare current and latest available IMAS versions def check_IMAS_version(verb=True, keystr=_KEYSTR): import subprocess @@ -81,6 +83,7 @@ def check_IMAS_version(verb=True, keystr=_KEYSTR): warnings.warn(msg) return lcur[0], lav + # Try comparing and warning try: _, _ = check_IMAS_version(verb=True) diff --git a/tofu/version.py b/tofu/version.py index 382360f86..53c102bb6 100644 --- a/tofu/version.py +++ b/tofu/version.py @@ -1,2 +1,2 @@ # Do not edit, pipeline versioning governed by git tags! -__version__ = '1.4.2a3-1-g88f9524' +__version__ = '1.4.2a3-2-gfaeb7af'