Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion tofu/imas2tofu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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
2 changes: 1 addition & 1 deletion tofu/version.py
Original file line number Diff line number Diff line change
@@ -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'