summaryrefslogtreecommitdiff
path: root/lib/spack/spack/schema/environment.py
blob: a65a4eaf1485f16929264a62fd835caae4a2a795 (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-2023 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)
"""Schema for environment modifications. Meant for inclusion in other
schemas.
"""
import collections.abc

array_of_strings_or_num = {
    "type": "array",
    "default": [],
    "items": {"anyOf": [{"type": "string"}, {"type": "number"}]},
}

dictionary_of_strings_or_num = {
    "type": "object",
    "patternProperties": {r"\w[\w-]*": {"anyOf": [{"type": "string"}, {"type": "number"}]}},
}

definition = {
    "type": "object",
    "default": {},
    "additionalProperties": False,
    "properties": {
        "set": dictionary_of_strings_or_num,
        "unset": array_of_strings_or_num,
        "prepend_path": dictionary_of_strings_or_num,
        "append_path": dictionary_of_strings_or_num,
        "remove_path": dictionary_of_strings_or_num,
    },
}


def parse(config_obj):
    """Returns an EnvironmentModifications object containing the modifications
    parsed from input.

    Args:
        config_obj: a configuration dictionary conforming to the
            schema definition for environment modifications
    """
    import spack.util.environment as ev

    env = ev.EnvironmentModifications()
    for command, variable in config_obj.items():
        # Distinguish between commands that take only a name as argument
        # (e.g. unset) and commands that take a name and a value.
        if isinstance(variable, collections.abc.Sequence):
            for name in variable:
                getattr(env, command)(name)
        else:
            for name, value in variable.items():
                getattr(env, command)(name, value)

    return env