1#!/usr/bin/env python3
2"""Run static analysis on the project."""
3from shutil import rmtree
4from subprocess import CalledProcessError, check_call
5from tempfile import mkdtemp
6import sys
7
8
9def do_process(args, shell=False):
10    """Run program provided by args.
11
12    Return True on success.
13
14    Output failed message on non-zero exit and return False.
15
16    Exit if command is not found.
17    """
18    print("Running: {}".format(" ".join(args)))
19    try:
20        check_call(args, shell=shell)
21    except CalledProcessError:
22        print("\nFailed: {}".format(" ".join(args)))
23        return False
24    except Exception as exc:
25        sys.stderr.write(str(exc) + "\n")
26        sys.exit(1)
27    return True
28
29
30def main():
31    """Entry point to pre_push.py."""
32    success = True
33    success &= do_process(["black *.py docs praw tests"], shell=True)
34    success &= do_process(["flake8", "--exclude=.eggs,build,dist,docs,.tox"])
35    success &= do_process(["pydocstyle", "praw"])
36    # success &= do_process(["pylint", "--rcfile=.pylintrc", "praw"])
37
38    tmp_dir = mkdtemp()
39    try:
40        success &= do_process(["sphinx-build", "-W", "docs", tmp_dir])
41    finally:
42        rmtree(tmp_dir)
43
44    return 0 if success else 1
45
46
47if __name__ == "__main__":
48    exit_code = main()
49    print()
50    print("pre_push.py: Success!" if exit_code == 0 else "pre_push.py: Fail")
51    sys.exit(exit_code)
52