summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/mark.py
blob: cf816a21f544cf7dc93ece68521c4d82c6536916 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright 2013-2024 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 sys

from llnl.util import tty

import spack.cmd
import spack.error
import spack.package_base
import spack.repo
import spack.store
from spack.cmd.common import arguments
from spack.database import InstallStatuses

description = "mark packages as explicitly or implicitly installed"
section = "admin"
level = "long"

error_message = """You can either:
    a) use a more specific spec, or
    b) use `spack mark --all` to mark ALL matching specs.
"""

# Arguments for display_specs when we find ambiguity
display_args = {"long": True, "show_flags": False, "variants": False, "indent": 4}


def setup_parser(subparser):
    arguments.add_common_arguments(subparser, ["installed_specs"])
    subparser.add_argument(
        "-a",
        "--all",
        action="store_true",
        dest="all",
        help="mark ALL installed packages that match each supplied spec",
    )
    exim = subparser.add_mutually_exclusive_group(required=True)
    exim.add_argument(
        "-e",
        "--explicit",
        action="store_true",
        dest="explicit",
        help="mark packages as explicitly installed",
    )
    exim.add_argument(
        "-i",
        "--implicit",
        action="store_true",
        dest="implicit",
        help="mark packages as implicitly installed",
    )


def find_matching_specs(specs, allow_multiple_matches=False):
    """Returns a list of specs matching the not necessarily
       concretized specs given from cli

    Args:
        specs (list): list of specs to be matched against installed packages
        allow_multiple_matches (bool): if True multiple matches are admitted

    Return:
        list of specs
    """
    # List of specs that match expressions given via command line
    specs_from_cli = []
    has_errors = False

    for spec in specs:
        install_query = [InstallStatuses.INSTALLED]
        matching = spack.store.STORE.db.query_local(spec, installed=install_query)
        # For each spec provided, make sure it refers to only one package.
        # Fail and ask user to be unambiguous if it doesn't
        if not allow_multiple_matches and len(matching) > 1:
            tty.error("{0} matches multiple packages:".format(spec))
            sys.stderr.write("\n")
            spack.cmd.display_specs(matching, output=sys.stderr, **display_args)
            sys.stderr.write("\n")
            sys.stderr.flush()
            has_errors = True

        # No installed package matches the query
        if len(matching) == 0 and spec is not any:
            tty.die("{0} does not match any installed packages.".format(spec))

        specs_from_cli.extend(matching)

    if has_errors:
        tty.die(error_message)

    return specs_from_cli


def do_mark(specs, explicit):
    """Marks all the specs in a list.

    Args:
        specs (list): list of specs to be marked
        explicit (bool): whether to mark specs as explicitly installed
    """
    for spec in specs:
        spack.store.STORE.db.update_explicit(spec, explicit)


def mark_specs(args, specs):
    mark_list = find_matching_specs(specs, args.all)

    # Mark everything on the list
    do_mark(mark_list, args.explicit)


def mark(parser, args):
    if not args.specs and not args.all:
        tty.die(
            "mark requires at least one package argument.",
            "  Use `spack mark --all` to mark ALL packages.",
        )

    # [any] here handles the --all case by forcing all specs to be returned
    specs = spack.cmd.parse_specs(args.specs) if args.specs else [any]
    mark_specs(args, specs)