summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/list.py
blob: 0bfe8041f5ec8e9138d8641f94ecb431007ad994 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# 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 argparse
import fnmatch
import json
import math
import os
import re
import sys
from html import escape

import llnl.util.tty as tty
from llnl.util.tty.colify import colify

import spack.deptypes as dt
import spack.repo
from spack.cmd.common import arguments
from spack.version import VersionList

description = "list and search available packages"
section = "basic"
level = "short"


formatters = {}


def formatter(func):
    """Decorator used to register formatters"""
    formatters[func.__name__] = func
    return func


def setup_parser(subparser):
    subparser.add_argument(
        "filter",
        nargs=argparse.REMAINDER,
        help="optional case-insensitive glob patterns to filter results",
    )
    subparser.add_argument(
        "-d",
        "--search-description",
        action="store_true",
        default=False,
        help="filtering will also search the description for a match",
    )
    subparser.add_argument(
        "--format",
        default="name_only",
        choices=formatters,
        help="format to be used to print the output [default: name_only]",
    )
    subparser.add_argument(
        "-v",
        "--virtuals",
        action="store_true",
        default=False,
        help="include virtual packages in list",
    )
    arguments.add_common_arguments(subparser, ["tags"])

    # Doesn't really make sense to update in count mode.
    count_or_update = subparser.add_mutually_exclusive_group()
    count_or_update.add_argument(
        "--count",
        action="store_true",
        default=False,
        help="display the number of packages that would be listed",
    )
    count_or_update.add_argument(
        "--update",
        metavar="FILE",
        default=None,
        action="store",
        help="write output to the specified file, if any package is newer",
    )


def filter_by_name(pkgs, args):
    """
    Filters the sequence of packages according to user prescriptions

    Args:
        pkgs: sequence of packages
        args: parsed command line arguments

    Returns:
        filtered and sorted list of packages
    """
    if args.filter:
        res = []
        for f in args.filter:
            if "*" not in f and "?" not in f:
                r = fnmatch.translate("*" + f + "*")
            else:
                r = fnmatch.translate(f)

            rc = re.compile(r, flags=re.IGNORECASE)
            res.append(rc)

        if args.search_description:

            def match(p, f):
                if f.match(p):
                    return True

                pkg_cls = spack.repo.PATH.get_pkg_class(p)
                if pkg_cls.__doc__:
                    return f.match(pkg_cls.__doc__)
                return False

        else:

            def match(p, f):
                return f.match(p)

        pkgs = [p for p in pkgs if any(match(p, f) for f in res)]

    return sorted(pkgs, key=lambda s: s.lower())


@formatter
def name_only(pkgs, out):
    indent = 0
    colify(pkgs, indent=indent, output=out)
    if out.isatty():
        tty.msg("%d packages" % len(pkgs))


def github_url(pkg):
    """Link to a package file on github."""
    url = "https://github.com/spack/spack/blob/develop/var/spack/repos/builtin/packages/{0}/package.py"
    return url.format(pkg.name)


def rows_for_ncols(elts, ncols):
    """Print out rows in a table with ncols of elts laid out vertically."""
    clen = int(math.ceil(len(elts) / ncols))
    for r in range(clen):
        row = []
        for c in range(ncols):
            i = c * clen + r
            row.append(elts[i] if i < len(elts) else None)
        yield row


def get_dependencies(pkg):
    all_deps = {}
    for deptype in dt.ALL_TYPES:
        deps = pkg.dependencies_of_type(dt.flag_from_string(deptype))
        all_deps[deptype] = [d for d in deps]

    return all_deps


@formatter
def version_json(pkg_names, out):
    """Print all packages with their latest versions."""
    pkg_classes = [spack.repo.PATH.get_pkg_class(name) for name in pkg_names]

    out.write("[\n")

    # output name and latest version for each package
    pkg_latest = ",\n".join(
        [
            '  {{"name": "{0}",\n'
            '   "latest_version": "{1}",\n'
            '   "versions": {2},\n'
            '   "homepage": "{3}",\n'
            '   "file": "{4}",\n'
            '   "maintainers": {5},\n'
            '   "dependencies": {6}'
            "}}".format(
                pkg_cls.name,
                VersionList(pkg_cls.versions).preferred(),
                json.dumps([str(v) for v in reversed(sorted(pkg_cls.versions))]),
                pkg_cls.homepage,
                github_url(pkg_cls),
                json.dumps(pkg_cls.maintainers),
                json.dumps(get_dependencies(pkg_cls)),
            )
            for pkg_cls in pkg_classes
        ]
    )
    out.write(pkg_latest)
    # important: no trailing comma in JSON arrays
    out.write("\n]\n")


@formatter
def html(pkg_names, out):
    """Print out information on all packages in Sphinx HTML.

    This is intended to be inlined directly into Sphinx documentation.
    We write HTML instead of RST for speed; generating RST from *all*
    packages causes the Sphinx build to take forever. Including this as
    raw HTML is much faster.
    """

    # Read in all packages
    pkg_classes = [spack.repo.PATH.get_pkg_class(name) for name in pkg_names]

    # Start at 2 because the title of the page from Sphinx is id1.
    span_id = 2

    # HTML header with an increasing id span
    def head(n, span_id, title, anchor=None):
        if anchor is None:
            anchor = title
        out.write(
            (
                '<span id="id%d"></span>'
                '<h1>%s<a class="headerlink" href="#%s" '
                'title="Permalink to this headline">&para;</a>'
                "</h1>\n"
            )
            % (span_id, title, anchor)
        )

    # Start with the number of packages, skipping the title and intro
    # blurb, which we maintain in the RST file.
    out.write("<p>\n")
    out.write("Spack currently has %d mainline packages:\n" % len(pkg_classes))
    out.write("</p>\n")

    # Table of links to all packages
    out.write('<table border="1" class="docutils">\n')
    out.write('<tbody valign="top">\n')
    for i, row in enumerate(rows_for_ncols(pkg_names, 3)):
        out.write('<tr class="row-odd">\n' if i % 2 == 0 else '<tr class="row-even">\n')
        for name in row:
            out.write("<td>\n")
            out.write('<a class="reference internal" href="#%s">%s</a></td>\n' % (name, name))
            out.write("</td>\n")
        out.write("</tr>\n")
    out.write("</tbody>\n")
    out.write("</table>\n")
    out.write('<hr class="docutils"/>\n')

    # Output some text for each package.
    for pkg_cls in pkg_classes:
        out.write('<div class="section" id="%s">\n' % pkg_cls.name)
        head(2, span_id, pkg_cls.name)
        span_id += 1

        out.write('<dl class="docutils">\n')

        out.write("<dt>Homepage:</dt>\n")
        out.write('<dd><ul class="first last simple">\n')

        if pkg_cls.homepage:
            out.write(
                ("<li>" '<a class="reference external" href="%s">%s</a>' "</li>\n")
                % (pkg_cls.homepage, escape(pkg_cls.homepage, True))
            )
        else:
            out.write("No homepage\n")
        out.write("</ul></dd>\n")

        out.write("<dt>Spack package:</dt>\n")
        out.write('<dd><ul class="first last simple">\n')
        out.write(
            ("<li>" '<a class="reference external" href="%s">%s/package.py</a>' "</li>\n")
            % (github_url(pkg_cls), pkg_cls.name)
        )
        out.write("</ul></dd>\n")

        if pkg_cls.versions:
            out.write("<dt>Versions:</dt>\n")
            out.write("<dd>\n")
            out.write(", ".join(str(v) for v in reversed(sorted(pkg_cls.versions))))
            out.write("\n")
            out.write("</dd>\n")

        for deptype in dt.ALL_TYPES:
            deps = pkg_cls.dependencies_of_type(dt.flag_from_string(deptype))
            if deps:
                out.write("<dt>%s Dependencies:</dt>\n" % deptype.capitalize())
                out.write("<dd>\n")
                out.write(
                    ", ".join(
                        d
                        if d not in pkg_names
                        else '<a class="reference internal" href="#%s">%s</a>' % (d, d)
                        for d in deps
                    )
                )
                out.write("\n")
                out.write("</dd>\n")

        out.write("<dt>Description:</dt>\n")
        out.write("<dd>\n")
        out.write(escape(pkg_cls.format_doc(indent=2), True))
        out.write("\n")
        out.write("</dd>\n")
        out.write("</dl>\n")

        out.write('<hr class="docutils"/>\n')
        out.write("</div>\n")


def list(parser, args):
    # retrieve the formatter to use from args
    formatter = formatters[args.format]

    # Retrieve the names of all the packages
    pkgs = set(spack.repo.all_package_names(args.virtuals))
    # Filter the set appropriately
    sorted_packages = filter_by_name(pkgs, args)

    # If tags have been specified on the command line, filter by tags
    if args.tags:
        packages_with_tags = spack.repo.PATH.packages_with_tags(*args.tags)
        sorted_packages = [p for p in sorted_packages if p in packages_with_tags]

    if args.update:
        # change output stream if user asked for update
        if os.path.exists(args.update):
            if os.path.getmtime(args.update) > spack.repo.PATH.last_mtime():
                tty.msg("File is up to date: %s" % args.update)
                return

        tty.msg("Updating file: %s" % args.update)
        with open(args.update, "w") as f:
            formatter(sorted_packages, f)

    elif args.count:
        # just print the number of packages in the result
        print(len(sorted_packages))
    else:
        # print formatted package list
        formatter(sorted_packages, sys.stdout)