summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/find.py
blob: 2238484a2167352d271272daa56880edbb263850 (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
##############################################################################
# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://scalability-llnl.github.io/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (as published by
# the Free Software Foundation) version 2.1 dated February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import sys
import collections
from external import argparse
from StringIO import StringIO

import llnl.util.tty as tty
from llnl.util.tty.colify import colify
from llnl.util.tty.color import *
from llnl.util.lang import partition_list, index_by

import spack
import spack.spec

description ="Find installed spack packages"

def setup_parser(subparser):
    format_group = subparser.add_mutually_exclusive_group()
    format_group.add_argument(
        '-p', '--paths', action='store_true', dest='paths',
        help='Show paths to package install directories')
    format_group.add_argument(
        '-l', '--long', action='store_true', dest='full_specs',
        help='Show full-length specs of installed packages')

    subparser.add_argument(
        'query_specs', nargs=argparse.REMAINDER,
        help='optional specs to filter results')


def find(parser, args):
    # Filter out specs that don't exist.
    query_specs = spack.cmd.parse_specs(args.query_specs)
    query_specs, nonexisting = partition_list(
        query_specs, lambda s: spack.db.exists(s.name))

    if nonexisting:
        msg = "No such package%s: " % ('s' if len(nonexisting) > 1 else '')
        msg += ", ".join(s.name for s in nonexisting)
        tty.msg(msg)

        if not query_specs:
            return

    specs = [s for s in spack.db.installed_package_specs()
             if not query_specs or any(s.satisfies(q) for q in query_specs)]

    # Make a dict with specs keyed by architecture and compiler.
    index = index_by(specs, 'architecture', 'compiler')

    # Traverse the index and print out each package
    for architecture in index:
        tty.hline(architecture, char='=', color=spack.spec.architecture_color)
        for compiler in index[architecture]:
            tty.hline(compiler, char='-', color=spack.spec.compiler_color)

            specs = index[architecture][compiler]
            specs.sort()

            abbreviated = [s.format('$_$@$+$#', color=True) for s in specs]

            if args.paths:
                # Print one spec per line along with prefix path
                width = max(len(s) for s in abbreviated)
                width += 2
                format = "    %-{}s%s".format(width)

                for abbrv, spec in zip(abbreviated, specs):
                    print format % (abbrv, spec.prefix)

            elif args.full_specs:
                for spec in specs:
                    print spec.tree(indent=4, format='$_$@$+', color=True),
            else:
                max_len = max([len(s.name) for s in specs])
                max_len += 4

                for spec in specs:
                    format = '$-' + str(max_len) + '_$@$+$#'
                    print "   " + spec.format(format, color=True)