summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Viren <brett.viren@gmail.com>2016-04-30 08:46:47 -0400
committerBrett Viren <brett.viren@gmail.com>2016-05-01 17:13:43 -0400
commit8c140f4725608510d0d7ecc6195736ca513b02ab (patch)
tree3268c4208e031783bc3496c721235c9e92f8a870
parenta2b9a000dcb18868ea9f1c110632a251fbea92f3 (diff)
downloadspack-8c140f4725608510d0d7ecc6195736ca513b02ab.tar.gz
spack-8c140f4725608510d0d7ecc6195736ca513b02ab.tar.bz2
spack-8c140f4725608510d0d7ecc6195736ca513b02ab.tar.xz
spack-8c140f4725608510d0d7ecc6195736ca513b02ab.zip
Change the action from parameter to subcommand to fit Spack convention.
-rw-r--r--lib/spack/spack/cmd/view.py58
1 files changed, 41 insertions, 17 deletions
diff --git a/lib/spack/spack/cmd/view.py b/lib/spack/spack/cmd/view.py
index dd698350b2..a82c034398 100644
--- a/lib/spack/spack/cmd/view.py
+++ b/lib/spack/spack/cmd/view.py
@@ -41,17 +41,30 @@ description = "Produce a single-rooted directory view of a spec."
def setup_parser(subparser):
setup_parser.parser = subparser
- subparser.add_argument('-e','--exclude', action='append', default=[],
- help="exclude any packages which the given re pattern")
- subparser.add_argument('-a','--action', default='link',
- choices=['link','remove','status'], # names match action_*() functions below
- help="what action to perform on the view")
- subparser.add_argument('--no-dependencies', action='store_true', default=False,
- help="just operate on named packages and do not follow dependencies")
- subparser.add_argument('-p','--prefix', required=True,
- help="Path to a top-level directory to receive the view.")
- subparser.add_argument('specs', nargs=argparse.REMAINDER,
- help="specs of packages to expose in the view.")
+ sp = subparser.add_subparsers(metavar='ACTION', dest='action')
+
+ # The action parameterizes the command but in keeping with Spack
+ # patterns we make it a subcommand.
+ sps = [
+ sp.add_parser('link', aliases=['add'],
+ help='Add packages to the view, create view if needed.'),
+ sp.add_parser('remove', aliases=['rm'],
+ help='Remove packages from the view, and view if empty.'),
+ sp.add_parser('status', aliases=['check'],
+ help='Check status of packages in the view.')
+ ]
+
+ # All these options and arguments are common to every action.
+ for p in sps:
+ p.add_argument('-e','--exclude', action='append', default=[],
+ help="exclude any packages which the given re pattern")
+ p.add_argument('--no-dependencies', action='store_true', default=False,
+ help="just operate on named packages and do not follow dependencies")
+ p.add_argument('prefix', nargs=1,
+ help="Path to a top-level directory to receive the view.")
+ p.add_argument('specs', nargs=argparse.REMAINDER,
+ help="specs of packages to expose in the view.")
+
def assuredir(path):
'Assure path exists as a directory'
@@ -151,7 +164,9 @@ def purge_empty_directories(path):
tty.warn("Not removing directory with contents: %s" % sdp)
-def view(parser, args):
+
+
+def view_action(action, parser, args):
'The view command.'
to_exclude = [re.compile(e) for e in args.exclude]
def exclude(spec):
@@ -162,7 +177,7 @@ def view(parser, args):
specs = spack.cmd.parse_specs(args.specs, normalize=True, concretize=True)
if not specs:
- setup_parser.parser.print_help()
+ parser.print_help()
return 1
prefix = args.prefix
@@ -175,9 +190,6 @@ def view(parser, args):
continue
flat.update(spec.normalized().traverse())
- action = args.action.lower()
- method = eval('action_' + action)
-
for spec in flat:
if exclude(spec):
tty.info('Skipping excluded package: "%s"' % spec.name)
@@ -186,7 +198,19 @@ def view(parser, args):
tty.warn('Skipping unknown package: %s in %s' % (spec.name, spec.prefix))
continue
tty.info("%s %s" % (action, spec.name))
- method(spec, prefix)
+ action(spec, prefix)
if action in ['remove']:
purge_empty_directories(prefix)
+
+
+def view(parser, args):
+ action = {
+ 'add': action_link,
+ 'link': action_link,
+ 'remove': action_remove,
+ 'rm': action_remove,
+ 'status': action_status,
+ 'check': action_status
+ }[args.action]
+ view_action(action, parser, args)