lmod.module module

class lmod.module.Module(modules=None, purge=True, force=False, debug=False)[source]

Bases: object

This is the class declaration of Module class which emulates the module command provided by Lmod

Public Methods:

avail: implements module avail option version: gets Lmod version by reading LMOD_VERSION is_avail: implements module is-avail option get_command: gets the module load command based on modules passed to the class test_modules: test the module load command based on modules passed to the class save: implements module save option describe: implements module describe option get_collection: gets the module restore command with the specified collection name test_collection: test the module restore command with the specified collection name

avail(name=None)[source]

This method implements the module avail command. The output of module avail will return available modules in the system. The output is returned as a list using the module -t avail which presents the output in a single line per module.

Parameters:

Parameters:name (str, optional) – argument passed to module avail. This is used for showing what modules are available
Returns:Return output of module avail as a list
Return type:list
checkSyntax(name=None)[source]

This method implements the module --checkSyntax load command which is used for checking syntax error for modulefile. The return is an exit code, if its 0 there is no error otherwise there is a syntax error in modulefile.

If name is specified, we can check check for any modulefile, otherwise we use the default modules passed to the Module class.

Parameters:

Parameters:name (str, optional) – argument passed to module --checkSyntax load to check for syntax error
describe(collection='default')[source]

Show content of a user collection and implements the command module describe. By default, if no argument is specified it will resort to showing contents of default collection. One can pass a collection name which must be of type str that is the user collection name. Internally it will run module describe <collection>. If collection name is not of type str, then an exception of TypeError will be raised.

Parameters:

Parameters:collection (str, optional) – name of user collection to show.
get_collection(collection='default')[source]

Return the module command to restore a collection. If no argument is specified, it will resort to the default collection, otherwise one can specify a collection name of type str. The output will be of type str such as module restore default. If argument to class is not of type str then an exception of type TypeError will be raised.

Parameters:

Parameters:collection (str, optional) – collection name to restore
Returns:return the module restore command with the collection name
Return type:str
get_command()[source]

Get the actual module load command that can be used to load the given modules.

Returns:return the actual module load command
Return type:str
is_avail(name)[source]

This method implements the module is-avail command which is used for checking if a module is available before loading it. The return value is a 0 or 1.

Parameters:

Parameters:name (str, required) – argument passed to module is-avail. This is used for checking if module is available
Returns:Return output of module is-avail. This checks if module is available and return code is a 0 or 1
Return type:int
overview(name=None)[source]

This method implements the module overview command. The output of module overview will return list of modules with number of versions available.

Parameters:name (str or list, optional) – Specify a list of modules to run module overview command
Returns:Return output of module overview as a string
save(collection='default')[source]

Save modules specified in Module class into a user collection. This implements the module save command for active modules. In this case, we are saving modules that were passed to the Module class. If no argument is specified, we will save to default collection, but user can specify a collection name, in that case we are running module save <collection>. The collection name must be of type str in order for this to work, otherwise an exception of TypeError will be raised.

Parameters:

Parameters:collection (str, optional) – collection name to save modules. If none specified, default is the collection.
spider(name=None)[source]

This method invokes module spider command. One can specify input arguments as a string or list type which are converted into string.

If no arguments are specified to Module() and spider class we will return the output of module spider command which is a list of all modules.

m = Module()
m.spider()

If modules are specified during instance creation then we will use those modules during module spider output. In this following example, we will run module spider gcc python

m = Module("gcc python")
m.spider()

We can also specify modules via spider method which will be read first even if modules are passed during object creation time. In example below

>>> m = Module(["gcc", "python"])
>>> m.modules
['gcc', 'python']
>>> out = m.spider("gcc")
>>> print(out)

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  gcc:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     Versions:
        gcc/9.3.0-n7p74fd
        gcc/10.2.0-37fmsw7

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  For detailed information about a specific "gcc" package (including how to load the modules) use the module's full name.
  Note that names that have a trailing (E) are extensions provided by other modules.
  For example:

     $ module spider gcc/10.2.0-37fmsw7
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Parameters:name (str, list) – Input modules to run module spider. Input can be a string or list
Returns:Output of module spider command as a string type
Return type:str
test_collection(collection='default')[source]

Test the user collection by running module restore against a collection name. This is useful, to test a user collection is working before using it in your scripts. If no argument is specified, it will test the default collection. One can specify a user collection name which must of be of type str and it must exist. The output will be a return code of the module restore command which would be of type int. If argument to method is not of type str an exception of TypeError will be raised.

Parameters:

Parameters:collection (str, optional) – collection name to test
Returns:return code of module restore against the collection name
Return type:int
test_modules(login=False)[source]

Test all modules passed to Module class by loading them using module load. The default behavior is to run the command in a sub-shell but this can be changed to run in a new login shell if login=True is specified. The return value is a return code (type int) of the module load command.

Parameters:

Parameters:login (bool, optional) – When login=True is set, it will run the test in a login shell, the default is to run in a sub-shell
Returns:return code of module load command
Return type:int
version()[source]

Get Lmod version by reading environment variable LMOD_VERSION and return as a string

Returns:Return the Lmod version as a string
Return type:str
lmod.module.get_user_collections()[source]

Get all user collections that is retrieved by running module -t savelist. The output is of type list and each entry in list is the name of the user collection.

Returns:Return all user collections
Return type:list