spack.modules package

This package contains code for creating environment modules, which can include Tcl or Lua modules, and others.

class spack.modules.LmodModulefileWriter(conf: BaseConfiguration)[source]

Bases: BaseModuleFileWriter

Writer class for lmod module files.

configuration_class

alias of LmodConfiguration

default_template: str
hide_cmd_format: str
modulerc_header: List[str]
class spack.modules.TclModulefileWriter(conf: BaseConfiguration)[source]

Bases: BaseModuleFileWriter

Writer class for tcl module files.

configuration_class

alias of TclConfiguration

default_template: str
hide_cmd_format: str
modulerc_header: List[str]
spack.modules.disable_modules() Iterator[None][source]

Disable the generation of modulefiles within the context manager.

Submodules

spack.modules.common module

This module contains the logic for generating environment module files for each installed spec.

The logic is split across four classes:

  • BaseConfiguration: queries the modules.yaml configuration for a given spec.

  • FileLayout: derives the on-disk path and the use name of a module file.

  • ModuleContext: builds the Jinja2 template context dictionary.

  • BaseModuleFileWriter: uses the three classes above to write, update, and remove module files.

To add a new module type, subclass BaseConfiguration and BaseModuleFileWriter.

class spack.modules.common.BaseConfiguration(spec: Spec, module_set_name: str, explicit: bool)[source]

Bases: object

Reads the modules section of the configuration for a given spec and exposes it as a set of properties used by FileLayout, ModuleContext, and BaseModuleFileWriter.

arch_folder: bool
property available: Dict[str, Spec]

Returns a dictionary of the services that are currently available.

compiler: Spec | None
property conflicts: List[str]

Conflicts for this module file

property context: dict
property core_compilers: List[Spec]

Returns the list of “Core” compilers

Raises:

CoreCompilersNotFoundError – if the key was not specified in the configuration file or the sequence is empty

property core_specs: List[str]

Returns the list of “Core” specs

default_projections: Dict[str, str]
property defaults: List[str]

Returns the specs configured as defaults or [].

property env: EnvironmentModifications

List of environment modifications that should be done in the module.

property exclude_env_vars: List[str]

List of variables that should be left unmodified.

property excluded: bool

Returns True if the module has been excluded, False otherwise.

file_extension: ClassVar[str]

File extension for module files (empty string means no extension)

property filter_hierarchy_specs: Dict[str, List[str]]

Returns the dict of specs with modified hierarchies

property hash: str | None

Hash tag for the module or None

property hidden: bool

Returns True if the module has been hidden, False otherwise.

hierarchical: bool
property hierarchy_tokens: List[str]

Returns the list of tokens that are part of the modulefile hierarchy. compiler is always present.

property literals_to_load: List[str]

List of literal modules to be loaded.

classmethod make_configuration(spec: Spec, module_set_name: str, explicit: bool | None = None) BaseConfiguration[source]

Returns the cached configuration object for spec.

classmethod make_layout(spec: Spec, module_set_name: str, explicit: bool | None = None) FileLayout[source]
property missing: List[str]

Returns the list of tokens that are not available.

module_system: str

Name of the module system (must be set by each subclass)

prefix_inspections: dict
property projections: Dict[str, str]

Projection from specs to module names

property provides: Dict[str, Spec]

Returns a dictionary mapping all the services provided by this spec to the spec itself.

Returns an empty dictionary if hierarchical mode is disabled.

property requires: Dict[str, Spec]

Returns a dictionary mapping all the requirements of this spec to the actual provider.

The compiler key is always present among the requirements.

Returns an empty dictionary if hierarchical mode is disabled.

root: str
property specs_to_load: List[Spec]

List of specs that should be loaded in the module file.

property specs_to_prereq: List[Spec]

List of specs that should be prerequisite of the module file.

property suffixes: List[str]

List of suffixes that should be appended to the module file name.

property template: str | None

Returns the name of the template to use for the module file or None if not specified in the configuration.

use_view: bool | str
property verbose: bool | None

Returns the verbosity setting, or None if not configured.

class spack.modules.common.BaseModuleFileWriter(conf: BaseConfiguration)[source]

Bases: object

configuration_class: ClassVar[Type[BaseConfiguration]]
default_template: str
classmethod from_spec(spec: Spec, module_set_name: str, explicit: bool | None = None) BaseModuleFileWriter[source]
hide_cmd_format: str
modulerc_header: List[str]
remove() None[source]

Deletes the module file.

remove_module_defaults() None[source]
property spec: Spec
update_module_defaults() None[source]
update_module_hiddenness(remove: bool = False) None[source]

Update modulerc file corresponding to module to add or remove command that hides module depending on its hidden state.

Parameters:

remove (bool) – if True, hiddenness information for module is removed from modulerc.

write(overwrite: bool = False) None[source]

Writes the module file.

Parameters:

overwrite (bool) – if True it is fine to overwrite an already existing file. If False the operation is skipped an we print a warning to the user.

class spack.modules.common.FileLayout(configuration)[source]

Bases: object

Provides information on the layout of module files.

property arch_dirname: str

Returns the root folder for this architecture.

property available_path_parts: List[str]

List of path parts that are currently available. Needed to construct the file name.

dirname() str[source]

Root folder for module files of this type.

property filename: str

Absolute path to the module file for the current spec.

property modulerc: str

Returns the modulerc file for this module file.

property spec: Spec

Spec under consideration

token_to_path(name: str, value: Spec) str[source]

Transforms a hierarchy token into the corresponding path part.

Parameters:
  • name (str) – name of the service in the hierarchy

  • value – actual provider of the service

Returns:

part of the path associated with the service

Return type:

str

property unlocked_paths: Dict[Tuple[str, ...] | None, List[Tuple[str, ...]]]

Returns a dictionary mapping conditions to a list of unlocked paths.

The paths that are unconditionally unlocked are under the key ‘None’. The other keys represent the list of services you need loaded to unlock the corresponding paths.

property use_name: str

Returns the name used to load the module (e.g. with module load).

class spack.modules.common.ModuleContext(configuration, layout: FileLayout)[source]

Bases: Context

Provides the context dictionary used by the template engine to render a module file.

property autoload: List[str]

List of modules that need to be loaded automatically.

property category: str
property conditionally_unlocked_paths: List[Tuple[str, str]]

Returns the list of paths that are unlocked conditionally. Each item in the list is a tuple with the structure (condition, path).

property configure_options: str | None
property conflicts: List[str]

List of conflicts for the module file.

context_properties: List[str]
property environment_modifications: List[Tuple[str, NameModifier | NameValueModifier]]

List of environment modifications to be processed.

property has_conditional_modifications: bool

True if this module modifies MODULEPATH conditionally to the presence of other services in the environment, False otherwise.

property has_manpath_modifications: bool

True if MANPATH environment variable is modified.

property has_modulepath_modifications: bool

True if this module modifies MODULEPATH, False otherwise.

property long_description: str | None
property missing: List[str]

Returns a list of missing services.

modification_needs_formatting(modification: NameModifier | NameValueModifier) bool[source]

Returns True if environment modification entry needs to be formatted.

property name_part: str

Name of this provider.

property prerequisites: List[str]

List of modules that must be loaded before this one.

property provides: Dict[str, Spec]

Returns the dictionary of provided services.

property short_description: str
property spec: Spec
property tags: List[str]
property timestamp: datetime
property unlocked_paths: List[str]

Returns the list of paths that are unlocked unconditionally.

property verbose: bool | None

Verbosity level.

property version_part: str

Version of this provider.

class spack.modules.common.ModuleIndexEntry(path, use_name)[source]

Bases: NamedTuple

path: str

Alias for field number 0

use_name: str

Alias for field number 1

class spack.modules.common.UpstreamModuleIndex(local_db, module_indices)[source]

Bases: object

This is responsible for taking the individual module indices of all upstream Spack installations and locating the module for a given spec based on which upstream install it is located in.

upstream_module(spec: Spec, module_type: str) ModuleIndexEntry | None[source]
spack.modules.common.dependencies(spec: Spec, request: str = 'all') List[Spec][source]

Returns the list of dependencies for a given spec.

Parameters:
  • spec – spec to be analyzed

  • request – one of "none", "run", "direct", "all"

spack.modules.common.disable_modules() Iterator[None][source]

Disable the generation of modulefiles within the context manager.

spack.modules.common.generate_module_index(root: str, modules: List[BaseModuleFileWriter], overwrite: bool = False) None[source]
spack.modules.common.merge_config_rules(configuration: dict, spec: Spec) dict[source]

Parses the module specific part of a configuration and returns a dictionary containing the actions to be performed on the spec passed as an argument.

Parameters:
  • configuration – module specific configuration (e.g. entries under the top-level ‘tcl’ key)

  • spec – spec for which we need to generate a module file

spack.modules.common.read_module_index(root: str) Dict[str, ModuleIndexEntry][source]
spack.modules.common.read_module_indices() List[Dict[str, Dict[str, ModuleIndexEntry]]][source]
spack.modules.common.root_path(module_type: str, module_set: str) str[source]

Returns the root folder for module file installation.

Parameters:
  • module_type – module type to be used

  • module_set – name of the set of module configs to use

spack.modules.common.update_dictionary_extending_lists(target: dict, update: dict) None[source]

Updates a dictionary, but extends lists instead of overriding them.

spack.modules.error module

Errors and exceptions for the modules package.

exception spack.modules.error.CoreCompilersNotFoundError(message: str, long_message: str | None = None)[source]

Bases: SpackError, KeyError

Raised if core_compilers has not been specified in the configuration file.

exception spack.modules.error.DefaultTemplateNotDefined[source]

Bases: AttributeError, ModulesError

Raised if default_template has not been specified in the derived class.

exception spack.modules.error.HideCmdFormatNotDefined[source]

Bases: AttributeError, ModulesError

Raised if hide_cmd_format has not been specified in the derived class.

exception spack.modules.error.ModuleNotFoundError(message: str, long_message: str | None = None)[source]

Bases: ModulesError

Raised when a module cannot be found for a spec

exception spack.modules.error.ModulercHeaderNotDefined[source]

Bases: AttributeError, ModulesError

Raised if modulerc_header has not been specified in the derived class.

exception spack.modules.error.ModulesError(message: str, long_message: str | None = None)[source]

Bases: SpackError

Base error for modules.

exception spack.modules.error.ModulesTemplateNotFoundError(message: str, long_message: str | None = None)[source]

Bases: ModulesError, RuntimeError

Raised if the template for a module file was not found.

spack.modules.lmod module

class spack.modules.lmod.LmodConfiguration(spec: Spec, module_set_name: str, explicit: bool)[source]

Bases: BaseConfiguration

Configuration class for lmod module files.

file_extension: ClassVar[str]

File extension for module files (empty string means no extension)

format_condition(services_needed: Tuple[str, ...]) str[source]
join_path(parts: Tuple[str, ...]) str[source]
manipulate_path(token: str) str[source]
module_system: str

Name of the module system (must be set by each subclass)

class spack.modules.lmod.LmodModulefileWriter(conf: BaseConfiguration)[source]

Bases: BaseModuleFileWriter

Writer class for lmod module files.

configuration_class

alias of LmodConfiguration

default_template: str
hide_cmd_format: str
modulerc_header: List[str]

spack.modules.tcl module

This module implements the classes necessary to generate Tcl modules.

class spack.modules.tcl.TclConfiguration(spec: Spec, module_set_name: str, explicit: bool)[source]

Bases: BaseConfiguration

Configuration class for tcl module files.

format_condition(services_needed: Tuple[str, ...]) str[source]
join_path(parts: Tuple[str, ...]) str[source]
manipulate_path(token: str) str[source]
module_system: str

Name of the module system (must be set by each subclass)

class spack.modules.tcl.TclModulefileWriter(conf: BaseConfiguration)[source]

Bases: BaseModuleFileWriter

Writer class for tcl module files.

configuration_class

alias of TclConfiguration

default_template: str
hide_cmd_format: str
modulerc_header: List[str]