summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/tags.py
blob: a4f9786b93de34f96e85c2899bf4c0a7521bff38 (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
# 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 io
import sys

import llnl.string
import llnl.util.tty as tty
import llnl.util.tty.colify as colify

import spack.repo
import spack.store
import spack.tag

description = "show package tags and associated packages"
section = "basic"
level = "long"


def report_tags(category, tags):
    buffer = io.StringIO()
    isatty = sys.stdout.isatty()

    if isatty:
        num = len(tags)
        fmt = "{0} package tag".format(category)
        buffer.write("{0}:\n".format(llnl.string.plural(num, fmt)))

    if tags:
        colify.colify(tags, output=buffer, tty=isatty, indent=4)
    else:
        buffer.write("    None\n")
    print(buffer.getvalue())


def setup_parser(subparser):
    subparser.epilog = (
        "Tags from known packages will be used if no tags are provided on "
        "the command\nline. If tags are provided, packages with at least one "
        "will be reported.\n\nYou are not allowed to provide tags and use "
        "'--all' at the same time."
    )
    subparser.add_argument(
        "-i",
        "--installed",
        action="store_true",
        default=False,
        help="show information for installed packages only",
    )
    subparser.add_argument(
        "-a",
        "--all",
        action="store_true",
        default=False,
        help="show packages for all available tags",
    )
    subparser.add_argument("tag", nargs="*", help="show packages with the specified tag")


def tags(parser, args):
    # Disallow combining all option with (positional) tags to avoid confusion
    if args.all and args.tag:
        tty.die("Use the '--all' option OR provide tag(s) on the command line")

    # Provide a nice, simple message if database is empty
    if args.installed and not spack.environment.installed_specs():
        tty.msg("No installed packages")
        return

    # unique list of available tags
    available_tags = sorted(spack.repo.PATH.tag_index.keys())
    if not available_tags:
        tty.msg("No tagged packages")
        return

    show_packages = args.tag or args.all

    # Only report relevant, available tags if no packages are to be shown
    if not show_packages:
        if not args.installed:
            report_tags("available", available_tags)
        else:
            tag_pkgs = spack.tag.packages_with_tags(available_tags, True, True)
            tags = tag_pkgs.keys() if tag_pkgs else []
            report_tags("installed", tags)
        return

    # Report packages associated with tags
    buffer = io.StringIO()
    isatty = sys.stdout.isatty()

    tags = args.tag if args.tag else available_tags
    tag_pkgs = spack.tag.packages_with_tags(tags, args.installed, False)
    missing = "No installed packages" if args.installed else "None"
    for tag in sorted(tag_pkgs):
        # TODO: Remove the sorting once we're sure noone has an old
        # TODO: tag cache since it can accumulate duplicates.
        packages = sorted(list(set(tag_pkgs[tag])))
        if isatty:
            buffer.write("{0}:\n".format(tag))

        if packages:
            colify.colify(packages, output=buffer, tty=isatty, indent=4)
        else:
            buffer.write("    {0}\n".format(missing))
        buffer.write("\n")
    print(buffer.getvalue())