From 1c60b3967d426a6ebf42304cda113377499ea8fb Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Mon, 6 Oct 2014 14:11:19 -0700 Subject: [PATCH] Add simple fnmatch filtering to spack list. --- lib/spack/spack/cmd/list.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/spack/spack/cmd/list.py b/lib/spack/spack/cmd/list.py index 2b996371ea..99bc64e991 100644 --- a/lib/spack/spack/cmd/list.py +++ b/lib/spack/spack/cmd/list.py @@ -22,15 +22,44 @@ # 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 argparse +import llnl.util.tty as tty from llnl.util.tty.colify import colify + import spack +import fnmatch description ="List available spack packages" def setup_parser(subparser): - pass + subparser.add_argument( + 'filter', nargs=argparse.REMAINDER, + help='Optional glob patterns to filter results.') + subparser.add_argument( + '-i', '--insensitive', action='store_true', default=False, + help='Filtering will be case insensitive.') def list(parser, args): + # Start with all package names. + pkgs = spack.db.all_package_names() + + # filter if a filter arg was provided + if args.filter: + 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)] + + # sort before displaying. + sorted_packages = sorted(pkgs, key=lambda s:s.lower()) + # Print all the package names in columns - colify(spack.db.all_package_names()) + indent=0 + if sys.stdout.isatty(): + tty.msg("%d packages." % len(sorted_packages)) + indent=2 + colify(sorted_packages, indent=indent)