diff options
author | Ben Sima <ben@bsima.me> | 2020-04-21 03:21:49 -0700 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2020-04-21 03:28:45 -0700 |
commit | b0bc9e6a95b4384d13f5c460a249c199b69567d9 (patch) | |
tree | cc42cb47ecd10351ecc63189659a34ec7092447a /lint | |
parent | 732ce502822fc81e764ea43e7e1caed0ad6dfdfd (diff) |
lint 'lint' and make it only run on changed files
It was getting annoying how long it would take to run.
Diffstat (limited to 'lint')
-rwxr-xr-x | lint | 40 |
1 files changed, 25 insertions, 15 deletions
@@ -1,45 +1,55 @@ #!/usr/bin/env python +""" +all your lint are belong to us +""" import os -import glob import subprocess import sys -def run(cmd, f): - global ERRORS +def run(cmd, file): + "Exec a linter for a file." + global ERRORS # pylint: disable=global-statement args = { "ormolu": ["--mode", "check"], "hlint": [], "black": ["--quiet", "--check"], "pylint": [], } - ret = subprocess.run([cmd, *args[cmd], f], stdout=subprocess.PIPE) + # pylint: disable=subprocess-run-check + ret = subprocess.run([cmd, *args[cmd], file], stdout=subprocess.PIPE) if ret.returncode != 0: - ERRORS += 1 + ERRORS += 1 # pylint: disable=undefined-variable msg = ret.stdout.decode("utf-8").strip() - print(f"lint error: {cmd} on {f}") + print(f"lint error: {cmd} on {file}") if msg: print(msg) def find_files(bizroot, extensions): + "Return a dict of {ext: [files]} that we should lint." ret = {k: [] for k in extensions} - for root, dirs, files in os.walk(bizroot, topdown=True): - (dirs.remove(d) for d in dirs if d.startswith(("_", "."))) - for ext in extensions: - for f in files: - if f.endswith(ext): - ret[ext].append(os.path.join(root, f)) + changed = ( + subprocess.check_output(["git", "diff", "--cached", "--name-only"]) + .decode("utf-8") + .strip() + ) + for ext in extensions: + for file in changed: + if file.endswith(ext): + ret[ext].append(os.path.join(bizroot, file)) return ret if __name__ == "__main__": ERRORS = 0 - files = find_files(os.getenv("BIZ_ROOT"), [".hs", ".py"]) - for hs in files[".hs"]: + FILES = find_files(os.getenv("BIZ_ROOT"), [".hs", ".py"]) + for hs in FILES[".hs"]: run("ormolu", hs) run("hlint", hs) - for py in files[".py"]: + for py in FILES[".py"]: run("black", py) run("pylint", py) + run("black", __file__) + run("pylint", __file__) sys.exit(ERRORS) |