cmd/list: adding description search and glob by default
This commit is contained in:
parent
f351e011d1
commit
a4ac99877a
1 changed files with 29 additions and 8 deletions
|
@ -28,7 +28,7 @@
|
|||
from llnl.util.tty.colify import colify
|
||||
|
||||
import spack
|
||||
import fnmatch
|
||||
import fnmatch, re
|
||||
|
||||
description ="List available spack packages"
|
||||
|
||||
|
@ -39,20 +39,41 @@ def setup_parser(subparser):
|
|||
subparser.add_argument(
|
||||
'-i', '--insensitive', action='store_true', default=False,
|
||||
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):
|
||||
# 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
|
||||
if args.filter:
|
||||
filters = []
|
||||
for f in args.filter:
|
||||
if '*' not in f and '?' not in f:
|
||||
filters.append('*' + f + '*')
|
||||
else:
|
||||
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 args.insensitive:
|
||||
p = p.lower()
|
||||
f = f.lower()
|
||||
return fnmatch.fnmatchcase(p, f)
|
||||
pkgs = [p for p in pkgs if any(match(p, f) for f in args.filter)]
|
||||
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.
|
||||
sorted_packages = sorted(pkgs, key=lambda s:s.lower())
|
||||
|
|
Loading…
Reference in a new issue