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 sys
 | 
				
			||||||
import shutil
 | 
					import shutil
 | 
				
			||||||
import tempfile
 | 
					import tempfile
 | 
				
			||||||
 | 
					import argparse
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from llnl.util.filesystem import *
 | 
					from llnl.util.filesystem import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -75,17 +76,18 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def filter_file(source, dest):
 | 
					def filter_file(source, dest):
 | 
				
			||||||
    """Filter a single file through all the patterns in exemptions."""
 | 
					    """Filter a single file through all the patterns in exemptions."""
 | 
				
			||||||
    for file_pattern, errors in exemptions.items():
 | 
					    with open(source) as infile:
 | 
				
			||||||
        if not file_pattern.search(source):
 | 
					        parent = os.path.dirname(dest)
 | 
				
			||||||
            continue
 | 
					        mkdirp(parent)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        with open(source) as infile:
 | 
					        with open(dest, 'w') as outfile:
 | 
				
			||||||
            parent = os.path.dirname(dest)
 | 
					            for file_pattern, errors in exemptions.items():
 | 
				
			||||||
            mkdirp(parent)
 | 
					                if not file_pattern.search(source):
 | 
				
			||||||
 | 
					                    continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            with open(dest, 'w') as outfile:
 | 
					 | 
				
			||||||
                for line in infile:
 | 
					                for line in infile:
 | 
				
			||||||
                    line = line.rstrip()
 | 
					                    line = line.rstrip()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    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):
 | 
				
			||||||
@@ -99,6 +101,11 @@ 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(
 | 
				
			||||||
 | 
					        '-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):
 | 
					def flake8(parser, args):
 | 
				
			||||||
@@ -108,28 +115,51 @@ def flake8(parser, args):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    temp = tempfile.mkdtemp()
 | 
					    temp = tempfile.mkdtemp()
 | 
				
			||||||
    try:
 | 
					    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):
 | 
					        with working_dir(spack.prefix):
 | 
				
			||||||
            changed = changed_files('*.py', output=str)
 | 
					            if not file_list:
 | 
				
			||||||
            changed = [x for x in changed.split('\n') if x]
 | 
					                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'))
 | 
					            shutil.copy('.flake8', os.path.join(temp, '.flake8'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        print '======================================================='
 | 
					        print '======================================================='
 | 
				
			||||||
        print 'flake8: running flake8 code checks on spack.'
 | 
					        print 'flake8: running flake8 code checks on spack.'
 | 
				
			||||||
        print
 | 
					        print
 | 
				
			||||||
        print 'Modified files:'
 | 
					        print 'Modified files:'
 | 
				
			||||||
        for filename in changed:
 | 
					        for filename in file_list:
 | 
				
			||||||
            print "  %s" % filename.strip()
 | 
					            print "  %s" % filename.strip()
 | 
				
			||||||
        print('=======================================================')
 | 
					        print('=======================================================')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # filter files into a temporary directory with exemptions added.
 | 
					        # 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)
 | 
					            src_path = os.path.join(spack.prefix, filename)
 | 
				
			||||||
            dest_path = os.path.join(temp, filename)
 | 
					            dest_path = os.path.join(temp, filename)
 | 
				
			||||||
            filter_file(src_path, dest_path)
 | 
					            filter_file(src_path, dest_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # run flake8 on the temporary tree.
 | 
					        # run flake8 on the temporary tree.
 | 
				
			||||||
        with working_dir(temp):
 | 
					        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:
 | 
					        if flake8.returncode != 0:
 | 
				
			||||||
            print "Flake8 found errors."
 | 
					            print "Flake8 found errors."
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user