summaryrefslogtreecommitdiff
path: root/lint
blob: 9bdcc579bf484e954605014aa2935064857ed409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/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 find_files(bizroot, extensions):
    "Return a dict of {ext: [files]} that we should lint."
    ret = {k: [] for k in extensions}
    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"]:
        run("ormolu", hs)
        run("hlint", hs)
    for py in FILES[".py"]:
        run("black", py)
        run("pylint", py)
    run("black", __file__)
    run("pylint", __file__)
    sys.exit(ERRORS)