cmd/list: adding description search and glob by default
This commit is contained in:
parent
f351e011d1
commit
a4ac99877a
@ -28,7 +28,7 @@
|
|||||||
from llnl.util.tty.colify import colify
|
from llnl.util.tty.colify import colify
|
||||||
|
|
||||||
import spack
|
import spack
|
||||||
import fnmatch
|
import fnmatch, re
|
||||||
|
|
||||||
description ="List available spack packages"
|
description ="List available spack packages"
|
||||||
|
|
||||||
@ -39,20 +39,41 @@ def setup_parser(subparser):
|
|||||||
subparser.add_argument(
|
subparser.add_argument(
|
||||||
'-i', '--insensitive', action='store_true', default=False,
|
'-i', '--insensitive', action='store_true', default=False,
|
||||||
help='Filtering will be case insensitive.')
|
help='Filtering will be case insensitive.')
|
||||||
|
subparser.add_argument(
|
||||||
|
'-s', '--search_description', action='store_true', default=False,
|
||||||
|
help='Filtering will also search the description for a match.')
|
||||||
|
|
||||||
|
|
||||||
def list(parser, args):
|
def list(parser, args):
|
||||||
# Start with all package names.
|
# Start with all package names.
|
||||||
pkgs = spack.repo.all_package_names()
|
pkgs = set(spack.repo.all_package_names())
|
||||||
|
|
||||||
# filter if a filter arg was provided
|
# filter if a filter arg was provided
|
||||||
if args.filter:
|
if args.filter:
|
||||||
def match(p, f):
|
filters = []
|
||||||
if args.insensitive:
|
for f in args.filter:
|
||||||
p = p.lower()
|
if '*' not in f and '?' not in f:
|
||||||
f = f.lower()
|
filters.append('*' + f + '*')
|
||||||
return fnmatch.fnmatchcase(p, f)
|
else:
|
||||||
pkgs = [p for p in pkgs if any(match(p, f) for f in args.filter)]
|
filters.append(f)
|
||||||
|
|
||||||
|
res = [re.compile(fnmatch.translate(f),
|
||||||
|
flags=re.I if args.insensitive else 0)
|
||||||
|
for f in filters]
|
||||||
|
|
||||||
|
if args.search_description:
|
||||||
|
def match(p, f):
|
||||||
|
if f.match(p):
|
||||||
|
return True
|
||||||
|
|
||||||
|
pkg = spack.repo.get(p)
|
||||||
|
if pkg.__doc__:
|
||||||
|
return f.match(pkg.__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)]
|
||||||
|
|
||||||
# sort before displaying.
|
# sort before displaying.
|
||||||
sorted_packages = sorted(pkgs, key=lambda s:s.lower())
|
sorted_packages = sorted(pkgs, key=lambda s:s.lower())
|
||||||
|
Loading…
Reference in New Issue
Block a user