summaryrefslogtreecommitdiff
path: root/lint
blob: a69b4242fc172b0376e1a79d57bb26eea83555ac (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
#!/usr/bin/env python
import os
import glob
import subprocess
import sys


def run(cmd, f):
    global ERRORS
    args = {
        "ormolu": ["--mode", "check"],
        "hlint": [],
        "black": ["--quiet", "--check"],
        "pylint": [],
    }
    ret = subprocess.run([cmd, *args[cmd], f], stdout=subprocess.PIPE)
    if ret.returncode != 0:
        ERRORS += 1
        msg = ret.stdout.decode("utf-8").strip()
        print(f"lint error: {cmd} on {f}")
        if msg:
            print(msg)


def find_files(bizroot, extensions):
    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))
    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)
    sys.exit(ERRORS)