summaryrefslogtreecommitdiff
path: root/lib/spack/spack/hooks/module_file_generation.py
blob: b95040aed275c3324ddff02da195bcb9c793cea6 (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import llnl.util.tty as tty

import spack.config
import spack.modules
import spack.modules.common


def _for_each_enabled(spec, method_name):
    """Calls a method for each enabled module"""
    set_names = set(spack.config.get('modules', {}).keys())
    # If we have old-style modules enabled, we put those in the default set
    old_default_enabled = spack.config.get('modules:enable')
    if old_default_enabled:
        set_names.add('default')
    for name in set_names:
        enabled = spack.config.get('modules:%s:enable' % name)
        if name == 'default':
            # combine enabled modules from default and old format
            enabled = spack.config.merge_yaml(old_default_enabled,  enabled)
        if not enabled:
            tty.debug('NO MODULE WRITTEN: list of enabled module files is empty')
            continue

        for type in enabled:
            generator = spack.modules.module_types[type](spec, name)
            try:
                getattr(generator, method_name)()
            except RuntimeError as e:
                msg = 'cannot perform the requested {0} operation on module files'
                msg += ' [{1}]'
                tty.warn(msg.format(method_name, str(e)))


def post_install(spec):
    import spack.environment  # break import cycle
    if spack.environment.get_env({}, ''):
        # If the installed through an environment, we skip post_install
        # module generation and generate the modules on env_write so Spack
        # can manage interactions between env views and modules
        return

    _for_each_enabled(spec, 'write')


def post_uninstall(spec):
    _for_each_enabled(spec, 'remove')


def post_env_write(env):
    for spec in env.new_installs:
        _for_each_enabled(spec, 'write')