summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarmen Stoppels <me@harmenstoppels.nl>2024-09-20 19:30:09 +0200
committerGitHub <noreply@github.com>2024-09-20 19:30:09 +0200
commit7711730f2cfb167a93d93577a0ca180325ccbd0d (patch)
tree98dde231c288a8d1c6e4922a93257689a727f2cf
parent14e8902854127edaeb38ca76560e0e7fa2f1b30c (diff)
downloadspack-7711730f2cfb167a93d93577a0ca180325ccbd0d.tar.gz
spack-7711730f2cfb167a93d93577a0ca180325ccbd0d.tar.bz2
spack-7711730f2cfb167a93d93577a0ca180325ccbd0d.tar.xz
spack-7711730f2cfb167a93d93577a0ca180325ccbd0d.zip
spack.modules: move get_module up (#46428)
-rw-r--r--lib/spack/spack/cmd/modules/__init__.py6
-rw-r--r--lib/spack/spack/modules/__init__.py71
-rw-r--r--lib/spack/spack/modules/common.py62
-rw-r--r--lib/spack/spack/test/modules/common.py4
4 files changed, 76 insertions, 67 deletions
diff --git a/lib/spack/spack/cmd/modules/__init__.py b/lib/spack/spack/cmd/modules/__init__.py
index 4cf940ca17..b11063714f 100644
--- a/lib/spack/spack/cmd/modules/__init__.py
+++ b/lib/spack/spack/cmd/modules/__init__.py
@@ -173,7 +173,7 @@ def loads(module_type, specs, args, out=None):
modules = list(
(
spec,
- spack.modules.common.get_module(
+ spack.modules.get_module(
module_type,
spec,
get_full_path=False,
@@ -222,7 +222,7 @@ def find(module_type, specs, args):
try:
modules = [
- spack.modules.common.get_module(
+ spack.modules.get_module(
module_type,
spec,
args.full_path,
@@ -233,7 +233,7 @@ def find(module_type, specs, args):
]
modules.append(
- spack.modules.common.get_module(
+ spack.modules.get_module(
module_type,
single_spec,
args.full_path,
diff --git a/lib/spack/spack/modules/__init__.py b/lib/spack/spack/modules/__init__.py
index b466b2aafb..39d696b136 100644
--- a/lib/spack/spack/modules/__init__.py
+++ b/lib/spack/spack/modules/__init__.py
@@ -7,8 +7,16 @@
include Tcl non-hierarchical modules, Lua hierarchical modules, and others.
"""
+import os
from typing import Dict, Type
+import llnl.util.tty as tty
+
+import spack.repo
+import spack.spec
+import spack.store
+
+from . import common
from .common import BaseModuleFileWriter, disable_modules
from .lmod import LmodModulefileWriter
from .tcl import TclModulefileWriter
@@ -19,3 +27,66 @@ module_types: Dict[str, Type[BaseModuleFileWriter]] = {
"tcl": TclModulefileWriter,
"lmod": LmodModulefileWriter,
}
+
+
+def get_module(
+ module_type, spec: spack.spec.Spec, get_full_path, module_set_name="default", required=True
+):
+ """Retrieve the module file for a given spec and module type.
+
+ Retrieve the module file for the given spec if it is available. If the
+ module is not available, this will raise an exception unless the module
+ is excluded or if the spec is installed upstream.
+
+ Args:
+ module_type: the type of module we want to retrieve (e.g. lmod)
+ spec: refers to the installed package that we want to retrieve a module
+ for
+ required: if the module is required but excluded, this function will
+ print a debug message. If a module is missing but not excluded,
+ then an exception is raised (regardless of whether it is required)
+ get_full_path: if ``True``, this returns the full path to the module.
+ Otherwise, this returns the module name.
+ module_set_name: the named module configuration set from modules.yaml
+ for which to retrieve the module.
+
+ Returns:
+ The module name or path. May return ``None`` if the module is not
+ available.
+ """
+ try:
+ upstream = spec.installed_upstream
+ except spack.repo.UnknownPackageError:
+ upstream, record = spack.store.STORE.db.query_by_spec_hash(spec.dag_hash())
+ if upstream:
+ module = common.upstream_module_index.upstream_module(spec, module_type)
+ if not module:
+ return None
+
+ if get_full_path:
+ return module.path
+ else:
+ return module.use_name
+ else:
+ writer = module_types[module_type](spec, module_set_name)
+ if not os.path.isfile(writer.layout.filename):
+ fmt_str = "{name}{@version}{/hash:7}"
+ if not writer.conf.excluded:
+ raise common.ModuleNotFoundError(
+ "The module for package {} should be at {}, but it does not exist".format(
+ spec.format(fmt_str), writer.layout.filename
+ )
+ )
+ elif required:
+ tty.debug(
+ "The module configuration has excluded {}: omitting it".format(
+ spec.format(fmt_str)
+ )
+ )
+ else:
+ return None
+
+ if get_full_path:
+ return writer.layout.filename
+ else:
+ return writer.layout.use_name
diff --git a/lib/spack/spack/modules/common.py b/lib/spack/spack/modules/common.py
index cf81bcaba5..b6972aba45 100644
--- a/lib/spack/spack/modules/common.py
+++ b/lib/spack/spack/modules/common.py
@@ -46,7 +46,6 @@ import spack.config
import spack.deptypes as dt
import spack.environment
import spack.error
-import spack.modules
import spack.paths
import spack.projections as proj
import spack.repo
@@ -324,67 +323,6 @@ class UpstreamModuleIndex:
return None
-def get_module(module_type, spec, get_full_path, module_set_name="default", required=True):
- """Retrieve the module file for a given spec and module type.
-
- Retrieve the module file for the given spec if it is available. If the
- module is not available, this will raise an exception unless the module
- is excluded or if the spec is installed upstream.
-
- Args:
- module_type: the type of module we want to retrieve (e.g. lmod)
- spec: refers to the installed package that we want to retrieve a module
- for
- required: if the module is required but excluded, this function will
- print a debug message. If a module is missing but not excluded,
- then an exception is raised (regardless of whether it is required)
- get_full_path: if ``True``, this returns the full path to the module.
- Otherwise, this returns the module name.
- module_set_name: the named module configuration set from modules.yaml
- for which to retrieve the module.
-
- Returns:
- The module name or path. May return ``None`` if the module is not
- available.
- """
- try:
- upstream = spec.installed_upstream
- except spack.repo.UnknownPackageError:
- upstream, record = spack.store.STORE.db.query_by_spec_hash(spec.dag_hash())
- if upstream:
- module = upstream_module_index.upstream_module(spec, module_type)
- if not module:
- return None
-
- if get_full_path:
- return module.path
- else:
- return module.use_name
- else:
- writer = spack.modules.module_types[module_type](spec, module_set_name)
- if not os.path.isfile(writer.layout.filename):
- fmt_str = "{name}{@version}{/hash:7}"
- if not writer.conf.excluded:
- raise ModuleNotFoundError(
- "The module for package {} should be at {}, but it does not exist".format(
- spec.format(fmt_str), writer.layout.filename
- )
- )
- elif required:
- tty.debug(
- "The module configuration has excluded {}: omitting it".format(
- spec.format(fmt_str)
- )
- )
- else:
- return None
-
- if get_full_path:
- return writer.layout.filename
- else:
- return writer.layout.use_name
-
-
class BaseConfiguration:
"""Manipulates the information needed to generate a module file to make
querying easier. It needs to be sub-classed for specific module types.
diff --git a/lib/spack/spack/test/modules/common.py b/lib/spack/spack/test/modules/common.py
index f1430ecf6e..7190689f82 100644
--- a/lib/spack/spack/test/modules/common.py
+++ b/lib/spack/spack/test/modules/common.py
@@ -171,7 +171,7 @@ module_index:
old_index = spack.modules.common.upstream_module_index
spack.modules.common.upstream_module_index = upstream_index
- m1_path = spack.modules.common.get_module("tcl", s1, True)
+ m1_path = spack.modules.get_module("tcl", s1, True)
assert m1_path == "/path/to/a"
finally:
spack.modules.common.upstream_module_index = old_index
@@ -193,7 +193,7 @@ def test_load_installed_package_not_in_repo(install_mockery, mock_fetch, monkeyp
with pytest.raises(spack.repo.UnknownPackageError):
spec.package
- module_path = spack.modules.common.get_module("tcl", spec, True)
+ module_path = spack.modules.get_module("tcl", spec, True)
assert module_path
spack.package_base.PackageBase.uninstall_by_spec(spec)