From b304387308898da4e820e0fd683db536ba7d2d55 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Wed, 2 Nov 2016 12:17:35 -0500 Subject: Fix style checker bug. (#2214) * Fix style checker bug. * spack flake8: print cwd-relative paths by default, with root-relative option. --- lib/spack/spack/cmd/flake8.py | 54 +++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 12 deletions(-) mode change 100755 => 100644 lib/spack/spack/cmd/flake8.py (limited to 'lib') diff --git a/lib/spack/spack/cmd/flake8.py b/lib/spack/spack/cmd/flake8.py old mode 100755 new mode 100644 index 8648bc88d6..3893815855 --- a/lib/spack/spack/cmd/flake8.py +++ b/lib/spack/spack/cmd/flake8.py @@ -27,6 +27,7 @@ import os import sys import shutil import tempfile +import argparse from llnl.util.filesystem import * @@ -75,17 +76,18 @@ exemptions = dict((re.compile(file_pattern), def filter_file(source, dest): """Filter a single file through all the patterns in exemptions.""" - for file_pattern, errors in exemptions.items(): - if not file_pattern.search(source): - continue + with open(source) as infile: + parent = os.path.dirname(dest) + mkdirp(parent) - with open(source) as infile: - parent = os.path.dirname(dest) - mkdirp(parent) + with open(dest, 'w') as outfile: + for file_pattern, errors in exemptions.items(): + if not file_pattern.search(source): + continue - with open(dest, 'w') as outfile: for line in infile: line = line.rstrip() + for code, patterns in errors.items(): for pattern in patterns: if pattern.search(line): @@ -99,6 +101,11 @@ def setup_parser(subparser): '-k', '--keep-temp', action='store_true', help="Do not delete temporary directory where flake8 runs. " "Use for debugging, to see filtered files.") + subparser.add_argument( + '-r', '--root-relative', action='store_true', default=False, + help="print root-relative paths (default is cwd-relative)") + subparser.add_argument( + 'files', nargs=argparse.REMAINDER, help="specific files to check") def flake8(parser, args): @@ -108,28 +115,51 @@ def flake8(parser, args): temp = tempfile.mkdtemp() try: + file_list = args.files + if file_list: + def prefix_relative(path): + return os.path.relpath( + os.path.abspath(os.path.realpath(path)), spack.prefix) + + file_list = [prefix_relative(p) for p in file_list] + with working_dir(spack.prefix): - changed = changed_files('*.py', output=str) - changed = [x for x in changed.split('\n') if x] + if not file_list: + file_list = changed_files('*.py', output=str) + file_list = [x for x in file_list.split('\n') if x] + shutil.copy('.flake8', os.path.join(temp, '.flake8')) print '=======================================================' print 'flake8: running flake8 code checks on spack.' print print 'Modified files:' - for filename in changed: + for filename in file_list: print " %s" % filename.strip() print('=======================================================') # filter files into a temporary directory with exemptions added. - for filename in changed: + for filename in file_list: src_path = os.path.join(spack.prefix, filename) dest_path = os.path.join(temp, filename) filter_file(src_path, dest_path) # run flake8 on the temporary tree. with working_dir(temp): - flake8('--format', 'pylint', *changed, fail_on_error=False) + output = flake8('--format', 'pylint', *file_list, + fail_on_error=False, output=str) + + if args.root_relative: + # print results relative to repo root. + print output + else: + # print results relative to current working directory + def cwd_relative(path): + return '%s: [' % os.path.relpath( + os.path.join(spack.prefix, path.group(1)), os.getcwd()) + + for line in output.split('\n'): + print re.sub(r'^(.*): \[', cwd_relative, line) if flake8.returncode != 0: print "Flake8 found errors." -- cgit v1.2.3-60-g2f50