|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Run static analysis on the project.""" |
| 3 | + |
| 4 | +import sys |
| 5 | +from os import path |
| 6 | +from shutil import rmtree |
| 7 | +from subprocess import CalledProcessError, check_call |
| 8 | +from tempfile import mkdtemp |
| 9 | + |
| 10 | +current_directory = path.abspath(path.join(__file__, "..")) |
| 11 | + |
| 12 | + |
| 13 | +def do_process(args, shell=False): |
| 14 | + """Run program provided by args. |
| 15 | +
|
| 16 | + Return True on success. |
| 17 | +
|
| 18 | + Output failed message on non-zero exit and return False. |
| 19 | +
|
| 20 | + Exit if command is not found. |
| 21 | +
|
| 22 | + """ |
| 23 | + print(f"Running: {' '.join(args)}") |
| 24 | + try: |
| 25 | + check_call(args, shell=shell) |
| 26 | + except CalledProcessError: |
| 27 | + print(f"\nFailed: {' '.join(args)}") |
| 28 | + return False |
| 29 | + except Exception as exc: |
| 30 | + sys.stderr.write(f"{str(exc)}\n") |
| 31 | + sys.exit(1) |
| 32 | + return True |
| 33 | + |
| 34 | + |
| 35 | +def run_static(): |
| 36 | + """Runs static tests. |
| 37 | +
|
| 38 | + Returns a statuscode of 0 if everything ran correctly. Otherwise, it will return |
| 39 | + statuscode 1 |
| 40 | +
|
| 41 | + """ |
| 42 | + success = True |
| 43 | + # Formatters |
| 44 | + success &= do_process(["black", "."]) |
| 45 | + success &= do_process(["isort", "."]) |
| 46 | + # Linters |
| 47 | + success &= do_process(["flake8", "--exclude=.eggs,build,docs,.venv*"]) |
| 48 | + |
| 49 | + tmp_dir = mkdtemp() |
| 50 | + try: |
| 51 | + success &= do_process(["sphinx-build", "-W", "--keep-going", "docs", tmp_dir]) |
| 52 | + finally: |
| 53 | + rmtree(tmp_dir) |
| 54 | + |
| 55 | + return success |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + success = True |
| 60 | + try: |
| 61 | + success &= run_static() |
| 62 | + except KeyboardInterrupt: |
| 63 | + return 1 |
| 64 | + return int(not success) |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + exit_code = main() |
| 69 | + print("\npre_push.py: Success!" if not exit_code else "\npre_push.py: Fail") |
| 70 | + sys.exit(exit_code) |
0 commit comments