flake8-clean.

This commit is contained in:
Brett Viren 2016-05-16 10:08:59 -04:00
parent b3ede099e2
commit 77a34ebdf9

View File

@ -29,8 +29,11 @@
This set consists of:
- specs resolved from the package names given by the user (the seeds)
- all depenencies of the seeds unless user specifies `--no-depenencies`
- less any specs with names matching the regular expressions given by `--exclude`
- less any specs with names matching the regular expressions given by
`--exclude`
The `view` command provides a number of functions (the "actions"):
@ -66,26 +69,27 @@
import os
import re
import sys
import argparse
import spack
import spack.cmd
import llnl.util.tty as tty
description = "Produce a single-rooted directory view of a spec."
def setup_parser(sp):
setup_parser.parser = sp
sp.add_argument('-v','--verbose', action='store_true', default=False,
sp.add_argument(
'-v', '--verbose', action='store_true', default=False,
help="Display verbose output.")
sp.add_argument('-e','--exclude', action='append', default=[],
sp.add_argument(
'-e', '--exclude', action='append', default=[],
help="Exclude packages with names matching the given regex pattern.")
sp.add_argument('-d', '--dependencies', choices=['true','false','yes','no'],
sp.add_argument(
'-d', '--dependencies', choices=['true', 'false', 'yes', 'no'],
default='true',
help="Follow dependencies.")
ssp = sp.add_subparsers(metavar='ACTION', dest='action')
specs_opts = dict(metavar='spec', nargs='+',
@ -94,13 +98,17 @@ def setup_parser(sp):
# The action parameterizes the command but in keeping with Spack
# patterns we make it a subcommand.
file_system_view_actions = [
ssp.add_parser('symlink', aliases=['add','soft'],
ssp.add_parser(
'symlink', aliases=['add', 'soft'],
help='Add package files to a filesystem view via symbolic links.'),
ssp.add_parser('hardlink', aliases=['hard'],
ssp.add_parser(
'hardlink', aliases=['hard'],
help='Add packages files to a filesystem via via hard links.'),
ssp.add_parser('remove', aliases=['rm'],
ssp.add_parser(
'remove', aliases=['rm'],
help='Remove packages from a filesystem view.'),
ssp.add_parser('statlink', aliases=['status','check'],
ssp.add_parser(
'statlink', aliases=['status', 'check'],
help='Check status of packages in a filesystem view.')
]
# All these options and arguments are common to every action.
@ -116,21 +124,15 @@ def setup_parser(sp):
help="Format describing per-package printout.")
act.add_argument('specs', **specs_opts)
## Other VIEW ACTIONS might be added here.
## Some ideas are the following (and some are redundant with existing cmds)
## A JSON view that dumps a DAG to a JSON file
## A DOT view that dumps to a GraphViz file
## A SHELL INIT view that dumps bash/csh script setting up to use packages in the view
return
### Util functions
def assuredir(path):
'Assure path exists as a directory'
if not os.path.exists(path):
os.makedirs(path)
def relative_to(prefix, path):
'Return end of `path` relative to `prefix`'
assert 0 == path.find(prefix)
@ -139,6 +141,7 @@ def relative_to(prefix, path):
reldir = reldir[1:]
return reldir
def transform_path(spec, path, prefix=None):
'Return the a relative path corresponding to given path spec.prefix'
if os.path.isabs(path):
@ -151,8 +154,10 @@ def transform_path(spec, path, prefix=None):
path = os.path.join(prefix, path)
return path
def purge_empty_directories(path):
'Ascend up from the leaves accessible from `path` and remove empty directories.'
'''Ascend up from the leaves accessible from `path`
and remove empty directories.'''
for dirpath, subdirs, files in os.walk(path, topdown=False):
for sd in subdirs:
sdp = os.path.join(dirpath, sd)
@ -161,9 +166,11 @@ def purge_empty_directories(path):
except OSError:
pass
def filter_exclude(specs, exclude):
'Filter specs given sequence of exclude regex'
to_exclude = [re.compile(e) for e in exclude]
def exclude(spec):
for e in to_exclude:
if e.match(spec.name):
@ -171,6 +178,7 @@ def exclude(spec):
return False
return [s for s in specs if not exclude(s)]
def flatten(seeds, descend=True):
'Normalize and flattend seed specs and descend hiearchy'
flat = set()
@ -181,6 +189,7 @@ def flatten(seeds, descend=True):
flat.update(spec.normalized().traverse())
return flat
def spec2dict(spec):
'Convert info in a spec into a simple dictionary.'
@ -188,6 +197,7 @@ def spec2dict(spec):
# some things need processing or are properties.
#
pkg = spec.package
inst_deps = ','.join([s.name for s in pkg.installed_dependents]),
ret = dict(name=spec.name,
spec=spec.short_spec,
colorspec=spec.cshort_spec, # color
@ -207,7 +217,7 @@ def spec2dict(spec):
url=pkg.url,
stage=pkg.stage.path,
installed=pkg.installed,
installed_dependents = ','.join([s.name for s in pkg.installed_dependents]),
installed_dependents=inst_deps,
build_log=pkg.build_log_path,
rpath=':'.join(pkg.rpath),
@ -215,7 +225,6 @@ def spec2dict(spec):
)
return ret
### Action-specific helpers
def check_one(spec, path, verbose=False):
'Check status of view in path against spec'
@ -226,6 +235,7 @@ def check_one(spec, path, verbose=False):
tty.info('Package not in view: "%s"' % spec.name)
return
def remove_one(spec, path, verbose=False):
'Remove any files found in `spec` from `path` and purge empty directories.'
@ -250,6 +260,7 @@ def remove_one(spec, path, verbose=False):
continue
os.unlink(dst)
def link_one(spec, path, link=os.symlink, verbose=False):
'Link all files in `spec` into directory `path`.'
@ -278,9 +289,6 @@ def link_one(spec, path, link = os.symlink, verbose=False):
link(src, dst)
### The canonically named visitor_* functions and their alias maps.
### One for each action.
def visitor_symlink(specs, args):
'Symlink all files found in specs'
path = args.path[0]
@ -290,6 +298,7 @@ def visitor_symlink(specs, args):
visitor_add = visitor_symlink
visitor_soft = visitor_symlink
def visitor_hardlink(specs, args):
'Hardlink all files found in specs'
path = args.path[0]
@ -298,6 +307,7 @@ def visitor_hardlink(specs, args):
link_one(spec, path, os.link, verbose=args.verbose)
visitor_hard = visitor_hardlink
def visitor_remove(specs, args):
'Remove all files and directories found in specs from args.path'
path = args.path[0]
@ -306,6 +316,7 @@ def visitor_remove(specs, args):
purge_empty_directories(path)
visitor_rm = visitor_remove
def visitor_statlink(specs, args):
'Give status of view in args.path relative to specs'
path = args.path[0]
@ -314,6 +325,7 @@ def visitor_statlink(specs, args):
visitor_status = visitor_statlink
visitor_check = visitor_statlink
def visitor_print(specs, args):
'Print a string for each spec using args.format.'
fmt = args.format[0]
@ -325,17 +337,14 @@ def visitor_print(specs, args):
try:
text = t.substitute(kwds)
except KeyError:
tty.error("Format error, use keywords: %s" % (', '.join(kwds.keys()), ))
tty.error("Format error, use keywords: %s" %
(', '.join(kwds.keys()), ))
raise
# argparser escapes these
text = text.replace(r'\n', '\n').replace(r'\t', '\t')
sys.stdout.write(text)
# Finally, the actual "view" command. There should be no need to
# modify anything below when new actions are added.
def view(parser, args):
'Produce a view of a set of packages.'