Skip to content
Snippets Groups Projects
Commit 78e22940 authored by Todd Gamblin's avatar Todd Gamblin
Browse files

add --all option to `spack flake8`

- `-a`/`--all` causes flake8 to run on all files rather than just
  different ones.
parent e660611a
No related branches found
No related tags found
No related merge requests found
...@@ -111,6 +111,10 @@ def changed_files(args): ...@@ -111,6 +111,10 @@ def changed_files(args):
if args.untracked: if args.untracked:
git_args.append(['ls-files', '--exclude-standard', '--other']) git_args.append(['ls-files', '--exclude-standard', '--other'])
# add everything if the user asked for it
if args.all:
git_args.append(['ls-files', '--exclude-standard'])
excludes = [os.path.realpath(f) for f in exclude_directories] excludes = [os.path.realpath(f) for f in exclude_directories]
changed = set() changed = set()
...@@ -131,6 +135,33 @@ def changed_files(args): ...@@ -131,6 +135,33 @@ def changed_files(args):
return sorted(changed) return sorted(changed)
def add_exemptions(line, codes):
"""Add a flake8 exemption to a line."""
if line.startswith('#'):
return line
line = line.rstrip('\n')
# Line is already ignored
if line.endswith('# noqa'):
return line + '\n'
orig_len = len(line)
exemptions = ','.join(sorted(set(codes)))
# append exemption to line
if '# noqa: ' in line:
line += ',{0}'.format(exemptions)
elif line: # ignore noqa on empty lines
line += ' # noqa: {0}'.format(exemptions)
# if THIS made the line too long, add an exemption for that
if len(line) > 79 and orig_len <= 79:
line += ',E501'
return line + '\n'
def filter_file(source, dest, output=False): def filter_file(source, dest, output=False):
"""Filter a single file through all the patterns in exemptions.""" """Filter a single file through all the patterns in exemptions."""
with open(source) as infile: with open(source) as infile:
...@@ -139,10 +170,7 @@ def filter_file(source, dest, output=False): ...@@ -139,10 +170,7 @@ def filter_file(source, dest, output=False):
with open(dest, 'w') as outfile: with open(dest, 'w') as outfile:
for line in infile: for line in infile:
# Only strip newline characters line_errors = []
# We still want to catch trailing whitespace warnings
line = line.rstrip('\n')
for file_pattern, errors in exemptions.items(): for file_pattern, errors in exemptions.items():
if not file_pattern.search(source): if not file_pattern.search(source):
continue continue
...@@ -150,19 +178,15 @@ def filter_file(source, dest, output=False): ...@@ -150,19 +178,15 @@ def filter_file(source, dest, output=False):
for code, patterns in errors.items(): for code, patterns in errors.items():
for pattern in patterns: for pattern in patterns:
if pattern.search(line): if pattern.search(line):
if line.endswith('# noqa'): line_errors.append(code)
# Line is already ignored
pass
elif '# noqa: ' in line:
line += ',{0}'.format(code)
else:
line += ' # noqa: {0}'.format(code)
break break
oline = line + '\n' if line_errors:
outfile.write(oline) line = add_exemptions(line, line_errors)
outfile.write(line)
if output: if output:
sys.stdout.write(oline) sys.stdout.write(line)
def setup_parser(subparser): def setup_parser(subparser):
...@@ -170,6 +194,9 @@ def setup_parser(subparser): ...@@ -170,6 +194,9 @@ def setup_parser(subparser):
'-k', '--keep-temp', action='store_true', '-k', '--keep-temp', action='store_true',
help="do not delete temporary directory where flake8 runs. " help="do not delete temporary directory where flake8 runs. "
"use for debugging, to see filtered files") "use for debugging, to see filtered files")
subparser.add_argument(
'-a', '--all', action='store_true',
help="check all files, not just changed files")
subparser.add_argument( subparser.add_argument(
'-o', '--output', action='store_true', '-o', '--output', action='store_true',
help="send filtered files to stdout as well as temp files") help="send filtered files to stdout as well as temp files")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment