summaryrefslogtreecommitdiff
path: root/Biz
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2020-07-14 20:29:53 -0700
committerBen Sima <ben@bsima.me>2020-07-14 20:29:53 -0700
commit584239a1a0c97f9d57f2de76c04708127178bceb (patch)
tree9114e816425ad03c521d1b4d37eea986cd5ecbd0 /Biz
parentca28f4a86b839d702bdaa70925884aa2bd5cf9ed (diff)
dev: move some commands to shell
Diffstat (limited to 'Biz')
-rwxr-xr-xBiz/lint.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/Biz/lint.py b/Biz/lint.py
new file mode 100755
index 0000000..d12431c
--- /dev/null
+++ b/Biz/lint.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+"""
+all your lint are belong to us
+"""
+import os
+import subprocess
+import sys
+
+
+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": [],
+ }
+ # pylint: disable=subprocess-run-check
+ ret = subprocess.run([cmd, *args[cmd], file], stdout=subprocess.PIPE)
+ if ret.returncode != 0:
+ ERRORS += 1 # pylint: disable=undefined-variable
+ msg = ret.stdout.decode("utf-8").strip()
+ print(f"lint error: {cmd} on {file}")
+ if msg:
+ print(msg)
+
+
+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 files:
+ if file.endswith(ext):
+ ret[ext].append(os.path.join(root, file))
+ return ret
+
+
+if __name__ == "__main__":
+ ERRORS = 0
+ if "-h" in sys.argv:
+ print(f"usage: {os.path.basename(__file__)} <files...>")
+ 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__)
+ run("pylint", __file__)
+ sys.exit(ERRORS)