diff options
Diffstat (limited to 'lib/spack/spack/modules/common.py')
-rw-r--r-- | lib/spack/spack/modules/common.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/spack/spack/modules/common.py b/lib/spack/spack/modules/common.py index 5e34315c1c..748013ade4 100644 --- a/lib/spack/spack/modules/common.py +++ b/lib/spack/spack/modules/common.py @@ -33,6 +33,7 @@ import datetime import inspect import os.path import re +import collections import six import llnl.util.filesystem @@ -45,6 +46,7 @@ import spack.tengine as tengine import spack.util.path import spack.util.environment import spack.error +import spack.util.spack_yaml as syaml #: config section for this file configuration = spack.config.get('modules') @@ -215,6 +217,64 @@ def root_path(name): return spack.util.path.canonicalize_path(path) +def generate_module_index(root, modules): + entries = syaml.syaml_dict() + for m in modules: + entry = { + 'path': m.layout.filename, + 'use_name': m.layout.use_name + } + entries[m.spec.dag_hash()] = entry + index = {'module_index': entries} + index_path = os.path.join(root, 'module-index.yaml') + llnl.util.filesystem.mkdirp(root) + with open(index_path, 'w') as index_file: + syaml.dump(index, index_file, default_flow_style=False) + + +ModuleIndexEntry = collections.namedtuple( + 'ModuleIndexEntry', ['path', 'use_name']) + + +def read_module_index(root): + index_path = os.path.join(root, 'module-index.yaml') + if not os.path.exists(index_path): + return {} + with open(index_path, 'r') as index_file: + yaml_content = syaml.load(index_file) + index = {} + yaml_index = yaml_content['module_index'] + for dag_hash, module_properties in yaml_index.items(): + index[dag_hash] = ModuleIndexEntry( + module_properties['path'], + module_properties['use_name']) + return index + + +def read_module_indices(): + module_type_to_indices = {} + other_spack_instances = spack.config.get( + 'upstreams') or {} + + for install_properties in other_spack_instances.values(): + module_type_to_root = install_properties.get('modules', {}) + for module_type, root in module_type_to_root.items(): + indices = module_type_to_indices.setdefault(module_type, []) + indices.append(read_module_index(root)) + + return module_type_to_indices + + +module_type_to_indices = read_module_indices() + + +def upstream_module(spec, module_type): + indices = module_type_to_indices[module_type] + for index in indices: + if spec.dag_hash() in index: + return index[spec.dag_hash()] + + class BaseConfiguration(object): """Manipulates the information needed to generate a module file to make querying easier. It needs to be sub-classed for specific module types. |