From e8f1fdda448b6011442ef0157cd51d70afefca37 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Thu, 11 Aug 2016 23:10:10 -0700 Subject: [PATCH] format.py now emits warnings for modified generated files (#195) --- styleguide/format.py | 18 ++++++++++- styleguide/task.py | 71 ++++++++++++++++++++++++++++++-------------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/styleguide/format.py b/styleguide/format.py index 6d090d2964..50f814ec02 100755 --- a/styleguide/format.py +++ b/styleguide/format.py @@ -70,8 +70,24 @@ def main(): print("Error: no files found to format", file = sys.stderr) sys.exit(1) + # Don't check for changes in or run tasks on modifiable files + files = [name for name in files if not Task.isModifiableFile(name)] + + # Create list of all changed files + changedFileList = [] + proc = subprocess.Popen(["git", "diff", "--name-only", "master"], + bufsize = 1, stdout = subprocess.PIPE) + for line in proc.stdout: + changedFileList.append(configPath + os.sep + + line.strip().decode("ascii")) + + # Emit warning if a generated file was editted + for name in files: + if Task.isGeneratedFile(name) and name in changedFileList: + print("Warning: generated file '" + name + "' modified") + # Don't format generated files - files = [name for name in files if Task.notGeneratedFile(name)] + files = [name for name in files if not Task.isGeneratedFile(name)] # Parse command-line arguments parser = argparse.ArgumentParser(description = "Runs all formatting tasks on the code base. This should be invoked from either the styleguide directory or the root directory of the project.") diff --git a/styleguide/task.py b/styleguide/task.py index 2ab4ee2226..aaaa4590cc 100644 --- a/styleguide/task.py +++ b/styleguide/task.py @@ -9,15 +9,20 @@ sep = os.sep if sep == "\\": sep += "\\" -# List of folder regexes which should be excluded from modification -folderExclude = \ +# There are two groups of regexes which prevent tasks from running on matching +# files: +# 1) generated files (shouldn't be modified) +# 2) modifiable files +# +# format.py excludes matches for the "modifiable" regex before checking for +# modifications to generated files because some of the regexes from each group +# overlap. + +# List of regexes for folders which contain generated files +genFolderExclude = \ [name + sep for name in [ - "\.git", - "\.gradle", "FRC_FPGA_ChipObject", "NetworkCommunication", - "__pycache__", - "build", "ctre", "frccansae", "gtest", @@ -26,25 +31,40 @@ folderExclude = \ "ni-libraries", "ni" + sep + "vision", "spilib", - "wpilibj" + sep + "src" + sep + "athena" + sep + "cpp" + sep + "include", - "wpilibj" + sep + "src" + sep + "athena" + sep + "cpp" + sep + "lib", "wpilibj" + sep + "src" + sep + "athena" + sep + "cpp" + sep + "nivision", "visa"]] -# List of file regexes which should be excluded from modification -fileExclude = [name + "$" for name in [ - "CanTalonSRX\.h", - "NIIMAQdx\.h", - "can_proto\.h", - "nivision\.h", - "\.jar", - "\.patch", - "\.png", - "\.py", - "\.so"]] +# List of regexes for generated files +genFileExclude = [name + "$" for name in [ + "CanTalonSRX\.h", + "NIIMAQdx\.h", + "can_proto\.h", + "nivision\.h"]] -# Regex of exclusions -regexExclude = re.compile("|".join(folderExclude + fileExclude)) +# Regex for generated file exclusions +genRegexExclude = re.compile("|".join(genFolderExclude + genFileExclude)) + +# Regex for folders which contain modifiable files +modifiableFolderExclude = \ + [name + sep for name in [ + "\.git", + "\.gradle", + "__pycache__", + "build", + "wpilibj" + sep + "src" + sep + "athena" + sep + "cpp" + sep + "include", + "wpilibj" + sep + "src" + sep + "athena" + sep + "cpp" + sep + "lib"]] + +# List of regexes for modifiable files +modifiableFileExclude = [name + "$" for name in [ + "\.jar", + "\.patch", + "\.png", + "\.py", + "\.so"]] + +# Regex for modifiable file exclusions +modifiableRegexExclude = \ + re.compile("|".join(modifiableFolderExclude + modifiableFileExclude)) class Task(object): __metaclass__ = ABCMeta @@ -62,10 +82,15 @@ class Task(object): def run(self, name): return + # Returns True if file is modifiable but should not have tasks run on it + @staticmethod + def isModifiableFile(name): + return modifiableRegexExclude.search(name) + # Returns True if file isn't generated (generated files are skipped) @staticmethod - def notGeneratedFile(name): - return not regexExclude.search(name) + def isGeneratedFile(name): + return genRegexExclude.search(name) # Returns True if file has an extension this task can process def fileMatchesExtension(self, name):