From 5a3eef41018ec91e186c4b1f17e298a48e8274cb Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Sun, 26 Apr 2020 10:13:38 -0700 Subject: Lint files explicitly --- lint | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'lint') diff --git a/lint b/lint index 9bdcc57..d12431c 100755 --- a/lint +++ b/lint @@ -26,28 +26,45 @@ def run(cmd, file): print(msg) -def find_files(bizroot, extensions): - "Return a dict of {ext: [files]} that we should lint." - ret = {k: [] for k in extensions} - changed = ( +def changed_files(): + "Return a list of changed files according to git." + return ( subprocess.check_output(["git", "diff", "--cached", "--name-only"]) .decode("utf-8") .strip() ) + + +def group_files(files, extensions): + """Given a list of files and list of extensions, return a dict of: + {ext: [files]} + + """ + root = os.getenv("BIZ_ROOT") + ret = {k: [] for k in extensions} for ext in extensions: - for file in changed: + for file in files: if file.endswith(ext): - ret[ext].append(os.path.join(bizroot, file)) + ret[ext].append(os.path.join(root, file)) return ret if __name__ == "__main__": ERRORS = 0 - FILES = find_files(os.getenv("BIZ_ROOT"), [".hs", ".py"]) + if "-h" in sys.argv: + print(f"usage: {os.path.basename(__file__)} ") + print("if no files given, lint files staged in git") + sys.exit(0) + elif len(sys.argv) == 1: + FILES = group_files(changed_files(), [".hs", ".py"]) + else: + FILES = group_files(sys.argv[1:], [".hs", ".py"]) for hs in FILES[".hs"]: + print(f"lint: {hs}") run("ormolu", hs) run("hlint", hs) for py in FILES[".py"]: + print(f"lint: {py}") run("black", py) run("pylint", py) run("black", __file__) -- cgit v1.2.3