summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/undevelop.py
blob: 71597087965065dcb28d918ca6f79e8185089a36 (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
57
58
59
60
61
62
63
64
65
66
67
# 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)

import llnl.util.tty as tty

import spack.cmd
from spack.cmd.common import arguments

description = "remove specs from an environment"
section = "environments"
level = "long"


def setup_parser(subparser):
    subparser.add_argument(
        "-a", "--all", action="store_true", help="remove all specs from (clear) the environment"
    )

    arguments.add_common_arguments(subparser, ["specs"])


def _update_config(specs_to_remove, remove_all=False):
    def change_fn(dev_config):
        modified = False
        for spec in specs_to_remove:
            if spec.name in dev_config:
                tty.msg("Undevelop: removing {0}".format(spec.name))
                del dev_config[spec.name]
                modified = True
        if remove_all and dev_config:
            dev_config.clear()
            modified = True
        return modified

    spack.config.update_all("develop", change_fn)


def undevelop(parser, args):
    remove_specs = None
    remove_all = False
    if args.all:
        remove_all = True
    else:
        remove_specs = spack.cmd.parse_specs(args.specs)

    # TODO: when https://github.com/spack/spack/pull/35307 is merged,
    # an active env is not required if a scope is specified
    env = spack.cmd.require_active_env(cmd_name="undevelop")
    with env.write_transaction():
        _update_config(remove_specs, remove_all)

    updated_all_dev_specs = set(spack.config.get("develop"))
    remove_spec_names = set(x.name for x in remove_specs)

    if remove_all:
        not_fully_removed = updated_all_dev_specs
    else:
        not_fully_removed = updated_all_dev_specs & remove_spec_names

    if not_fully_removed:
        tty.msg(
            "The following specs could not be removed as develop specs"
            " - see `spack config blame develop` to locate files requiring"
            f" manual edits: {', '.join(not_fully_removed)}"
        )