Lmodule Examples¶
Module Test Example¶
You can use Module class to load arbitrary modules, in this example we run few examples using the Module class to illustrate few of the features.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import os, sys from lmod.module import Module mod_names = ["GCCcore", "Python"] a = Module(mod_names) module_cmds = a.get_command() rc = a.test_modules() if rc == 0: print(f"The following modules: {mod_names} were loaded successfully") print("\n") print(f"Command Executed: {module_cmds}") # passing a module name ``invalid`` this is expected to fail during test bad_names = ["GCCcore", "invalid"] b = Module(bad_names) print(f"Failed to load modules: {bad_names}") print(f"Command Executed: {b.get_command()}") print(f"return code: {b.test_modules()}") # disable purge when loading modules c = Module(mod_names, purge=False) print(c.get_command()) # force purge modules d = Module(mod_names, purge=True, force=True) print(d.get_command()) e = Module(["Anaconda3/5.3.0", "M4/1.4.17"], debug=True) e.test_modules() f = Module("Anaconda3/5.3.0 M4/1.4.17", debug=True) f.test_modules() |
Shown below is the output for the above script.
$ python moduletest.py
The following modules: ['GCCcore', 'Python'] were loaded successfully
Command Executed: module purge && module load GCCcore && module load Python
Failed to load modules: ['GCCcore', 'invalid']
Command Executed: module purge && module load GCCcore && module load invalid
return code: 0
module load GCCcore && module load Python
module --force purge && module load GCCcore && module load Python
[DEBUG] Executing module command: module purge && module load Anaconda3/5.3.0 && module load M4/1.4.17
[DEBUG] Return Code: 0
[DEBUG] Executing module command: module purge && module load Anaconda3/5.3.0 && module load M4/1.4.17
[DEBUG] Return Code: 0
Automating Module Load Test for environment-modules¶
If your system has environment-modules, you can make use of this script to automate module load test using Module class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import os import re import subprocess import sys from lmod.module import Module modules = subprocess.getoutput("module av -t") modules = modules.split() pass_counter = 0 fail_counter = 0 total = 0 for module in modules: # output of module tree is as follows '/path/to/tree:' so we remove trailing colon tree = module[:-1] # skip entry when it's module tree if os.path.exists(tree): print(f"Skipping tree: {tree}") continue if re.search("(\(default\))$", module): module = module.replace("(default)", "") cmd = Module(module, debug=True) ret = cmd.test_modules(login=True) total += 1 # if returncode is 0 mark as PASS if ret == 0: pass_counter += 1 else: fail_counter += 1 pass_rate = pass_counter * 100 / total fail_rate = fail_counter * 100 / total print("-------- SUMMARY ---------") print(f"Total Pass: {pass_counter}/{total}") print(f"Total Failure: {fail_counter}/{total}") print(f"PASS RATE: {pass_rate:.3f}") print(f"FAIL RATE: {fail_rate:.3f}") |
Collection Example¶
This example, get’s all module collections and print their name and tests collection command. Next, we test Python collection in debug mode followed by Default collection. Finally, we wrap up by testing an invalid module collection which should raise error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from lmod.module import get_user_collections, Module collections = get_user_collections() for i in collections: a = Module() restore_cmd, ret_code = a.get_collection(i), a.test_collection(i) print(f"Collection Command: {restore_cmd} Return Code: {ret_code}") # test Python collection with debug enabled a = Module(debug=True) a.test_collection("Python") # test default collection with debug enabled a = Module(debug=True) a.test_collection() # This will raise an exception a.test_collection(1) |
This next example will load zlib module, save into module collection named zlib and show content of collection of newly created collection zlib.
1 2 3 4 5 6 7 8 9 10 11 12 | from lmod.module import Module a = Module("zlib", debug=True) # save module collection with name "zlib" a.save("zlib") # show module collection "zlib" a.describe("zlib") # show "default" module collection a.describe() # Passing a list will throw a type error. since collection name must be a string a.describe(["zlib"]) |
Spider Example¶
This next example makes use of Spider class to get unique names, parent modules, retrieve all version of GCC, and use Spider on directory tree to get all names and test each module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | from lmod.spider import Spider from lmod.module import Module a = Spider() names = a.get_names() print(f"Module Trees: {a.get_trees()}") print("{:_<80}".format("")) print(f"Unique Software: {names}") print("\n") parents = a.get_parents() print(f"Parent Modules: {parents}") print("\n") gcc_versions = a.get_all_versions("GCC") print(f"GCC Versions: {gcc_versions}") print("\n") b = Spider("/mxg-hpc/users/ssi29/easybuild-HMNS/modules/all/Core") mod_names = b.get_modules() print(f"Module Trees: {b.get_trees()}") print(f"Unique Software: {b.get_names()}") print(f"Module Names: {mod_names}") # testing all modules for x in mod_names: cmd = Module(x, debug=True) cmd.test_modules() |