diff --git a/tofu/imas2tofu/__init__.py b/tofu/imas2tofu/__init__.py index 184fc8773..842ecaef4 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,72 @@ 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 keystr not 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 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'