Fix style checker bug. (#2214)
* Fix style checker bug. * spack flake8: print cwd-relative paths by default, with root-relative option.
This commit is contained in:
		
							
								
								
									
										54
									
								
								lib/spack/spack/cmd/flake8.py
									
									
									
									
									
										
										
										Executable file → Normal file
									
								
							
							
						
						
									
										54
									
								
								lib/spack/spack/cmd/flake8.py
									
									
									
									
									
										
										
										Executable file → Normal file
									
								
							@@ -27,6 +27,7 @@
 | 
			
		||||
import sys
 | 
			
		||||
import shutil
 | 
			
		||||
import tempfile
 | 
			
		||||
import argparse
 | 
			
		||||
 | 
			
		||||
from llnl.util.filesystem import *
 | 
			
		||||
 | 
			
		||||
@@ -75,17 +76,18 @@
 | 
			
		||||
 | 
			
		||||
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."
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user