Skip to content
Open
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
11 changes: 9 additions & 2 deletions build_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

# Regexes to strip package-relative imports and __main__ blocks
REL_IMPORT_RE = re.compile(r'^\s*from\s+\.\w+\s+import\s+.*$', re.M)
COMMON_IMPORT_RE = re.compile(r'^\s*from\s+common\s+import\s+.*$', re.M)
PKG_IMPORT_RE = re.compile(r'^\s*from\s+machinestate(?:\.\w+)?\s+import\s+.*$', re.M)

def strip_relative_imports(text: str) -> str:
# Remove "from .X import Y"
text = REL_IMPORT_RE.sub('', text)
# Remove "from common import Y"
text = COMMON_IMPORT_RE.sub('', text)
# Remove "from machinestate(.X)? import Y"
text = PKG_IMPORT_RE.sub('', text)
# Also remove "import machinestate" (rare)
Expand All @@ -36,7 +39,11 @@ def collect_files(root: pathlib.Path) -> List[str]:
if common_py.exists():
files.append(str(common_py))

files.extend(sorted(glob.glob(str(pkg / "*" / "*.py"))))
allfiles = sorted(glob.glob(str(pkg / "*" / "*.py")))
for f in allfiles:
fname = os.path.basename(str(f))
if not fname.startswith("test_"):
files.append(str(f))

script_py = pkg / "script.py"
if script_py.exists():
Expand Down Expand Up @@ -77,4 +84,4 @@ def main():
print(f"[ok] wrote {OUT} ({out_path.stat().st_size} bytes)")

if __name__ == "__main__":
main()
main()
9 changes: 7 additions & 2 deletions machinestate_pkg/ModulesInfo/ModulesInfo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .common import InfoGroup, which, re, os
from common import InfoGroup, which, re, os

################################################################################
# Infos about loaded modules in the modules system
Expand All @@ -9,8 +9,11 @@ def __init__(self, extended=False, anonymous=False, modulecmd="modulecmd"):
super(ModulesInfo, self).__init__(name="ModulesInfo",
extended=extended,
anonymous=anonymous)
if os.getenv("MODULES_CMD"):
modulecmd = os.getenv("MODULES_CMD")
if os.getenv("LMOD_CMD"):
modulecmd = os.getenv("LMOD_CMD")

self.modulecmd = modulecmd
parse = ModulesInfo.parsemodules
cmd_opts = "sh -t list 2>&1"
Expand All @@ -37,4 +40,6 @@ def parsemodules(value):
slist = [ x.split("'")[1] for x in re.split("\n", value) if "echo" in x and "'" in x ]
if re.match("^Currently Loaded.+$", slist[0]):
slist = slist[1:]
return slist
if re.match("^No Modulefiles Currently Loaded.+$", slist[0]):
slist = slist[1:]
return slist
47 changes: 47 additions & 0 deletions machinestate_pkg/ModulesInfo/test_ModulesInfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3

import sys, os, os.path
sys.path.append("..")

from locale import getpreferredencoding
ENCODING = getpreferredencoding()

import unittest

import ModulesInfo

def readfile(filename):
data = None
if os.path.exists(filename):
with open(filename, 'rb') as filefp:
data = filefp.read().decode(ENCODING)
return data

basedir = "./tests"


def runtest(self, testfolder):
os.environ["MODULES_CMD"] = os.path.join(basedir, "fakemodules.sh")
c = ModulesInfo.ModulesInfo(modulecmd=os.path.join(basedir, "fakemodules.sh"))
c.generate()
os.environ["FAKEMODULES_INPUT"] = os.path.join(testfolder, "input")
c.update()
j = c.get_json()
ref = readfile(os.path.join(testfolder, "output"))
self.assertEqual(ref.strip(), j.strip())
if ref.strip() != j.strip():
print(j.strip())


class TestModulesInfo(unittest.TestCase):
def test_tcl_5_0_1(self):
runtest(self, os.path.join(basedir, "tcl_5.0.1"))
def test_tcl_5_5_0(self):
runtest(self, os.path.join(basedir, "tcl_5.5.0"))
def test_tcl_5_5_0_empty(self):
runtest(self, os.path.join(basedir, "tcl_5.5.0_empty"))
def test_tcl_5_6_0(self):
runtest(self, os.path.join(basedir, "tcl_5.6.0"))

if __name__ == '__main__':
unittest.main()
8 changes: 8 additions & 0 deletions machinestate_pkg/ModulesInfo/tests/fakemodules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

INP=${FAKEMODULES_INPUT}
if [ -z ${INP} ]; then
exit 1
fi

cat ${INP}
5 changes: 5 additions & 0 deletions machinestate_pkg/ModulesInfo/tests/tcl_5.0.1/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Currently Loaded Modulefiles:
intel/2021.4.0
intelmpi/2021.6.0
scorep/7.1-intel-2021.4.0-intelmpi-2021.4.0
scalasca/2.6-intel-2021.4.0-intelmpi-2021.4.0
9 changes: 9 additions & 0 deletions machinestate_pkg/ModulesInfo/tests/tcl_5.0.1/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Loaded": [
"intel/2021.4.0",
"intelmpi/2021.6.0",
"scorep/7.1-intel-2021.4.0-intelmpi-2021.4.0",
"scalasca/2.6-intel-2021.4.0-intelmpi-2021.4.0"
],
"_meta": "ModulesInfo(modulecmd='./tests/fakemodules.sh')"
}
4 changes: 4 additions & 0 deletions machinestate_pkg/ModulesInfo/tests/tcl_5.5.0/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Currently Loaded Modulefiles:
openmpi/5.0.5-gcc13.3.0
fftw/3.3.10-gcc13.3.0-openmpi
python/3.12-conda
8 changes: 8 additions & 0 deletions machinestate_pkg/ModulesInfo/tests/tcl_5.5.0/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Loaded": [
"openmpi/5.0.5-gcc13.3.0",
"fftw/3.3.10-gcc13.3.0-openmpi",
"python/3.12-conda"
],
"_meta": "ModulesInfo(modulecmd='./tests/fakemodules.sh')"
}
1 change: 1 addition & 0 deletions machinestate_pkg/ModulesInfo/tests/tcl_5.5.0_empty/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No Modulefiles Currently Loaded.
4 changes: 4 additions & 0 deletions machinestate_pkg/ModulesInfo/tests/tcl_5.5.0_empty/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Loaded": [],
"_meta": "ModulesInfo(modulecmd='./tests/fakemodules.sh')"
}
5 changes: 5 additions & 0 deletions machinestate_pkg/ModulesInfo/tests/tcl_5.6.0/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Currently Loaded Modulefiles:
intel/2025.0.0
intelmpi/2021.14.0
cuda/12.9.0
likwid/5.5.1-cuda
9 changes: 9 additions & 0 deletions machinestate_pkg/ModulesInfo/tests/tcl_5.6.0/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Loaded": [
"intel/2025.0.0",
"intelmpi/2021.14.0",
"cuda/12.9.0",
"likwid/5.5.1-cuda"
],
"_meta": "ModulesInfo(modulecmd='./tests/fakemodules.sh')"
}
Loading