Tools: Bring flake8 speed improvements to pre-commit

This commit is contained in:
TunaLobster 2022-08-24 14:56:49 -05:00 committed by Peter Barker
parent 71d61a920c
commit 746aebbd0c

View File

@ -20,22 +20,28 @@ class PreCommitFlake8(object):
def __init__(self): def __init__(self):
pass pass
def progress(self, message): @staticmethod
def progress(message):
print("***** %s" % (message, )) print("***** %s" % (message, ))
def check_file(self, filepath): @staticmethod
def has_flake8_tag(filepath):
content = open(filepath).read() content = open(filepath).read()
if "AP_FLAKE8_CLEAN" not in content: return "AP_FLAKE8_CLEAN" in content
return True
self.progress("Checking (%s)" % filepath) def files_are_flake8_clean(self, files_to_check):
retcode = subprocess.call(["flake8", filepath]) if files_to_check:
if retcode != 0: for path in files_to_check:
self.progress("File (%s) failed with retcode (%s)" % self.progress("Checking (%s)" % path)
(filepath, retcode)) try:
return False subprocess.check_output(["flake8"] + files_to_check, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
self.progress("Flake8 check failed: (%s)" % (e.output))
return False
return True return True
def split_git_diff_output(self, output): @staticmethod
def split_git_diff_output(output):
'''split output from git-diff into a list of (status, filepath) tuples''' '''split output from git-diff into a list of (status, filepath) tuples'''
ret = [] ret = []
if type(output) == bytes: if type(output) == bytes:
@ -59,7 +65,7 @@ class PreCommitFlake8(object):
output = subprocess.check_output([ output = subprocess.check_output([
"git", "diff", "--cached", "--name-status"]) "git", "diff", "--cached", "--name-status"])
output_tuples = self.split_git_diff_output(output) output_tuples = self.split_git_diff_output(output)
self.retcode = 0 files_to_check_flake8 = []
for output_tuple in output_tuples: for output_tuple in output_tuples:
if len(output_tuple) > 2: if len(output_tuple) > 2:
if output_tuple[0].startswith('R'): if output_tuple[0].startswith('R'):
@ -75,11 +81,11 @@ class PreCommitFlake8(object):
# don't check deleted files # don't check deleted files
continue continue
(base, extension) = os.path.splitext(filepath) (base, extension) = os.path.splitext(filepath)
if extension != ".py": if extension == ".py" and self.has_flake8_tag(filepath):
continue files_to_check_flake8.append(filepath)
if not self.check_file(filepath): if not self.files_are_flake8_clean(files_to_check_flake8):
self.retcode = 1 return 1
return self.retcode return 0
if __name__ == '__main__': if __name__ == '__main__':