run_dev_checks.py 2.51 KB
Newer Older
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
"""Simple yaml debugger"""
import subprocess

import yaml
from rich.console import Console
from rich.style import Style

console = Console(width=120)

LOCAL_TESTS = [
    "Run license checks",
    "Run isort",
    "Run Black",
    "Python Pylint",
    "Test with pytest",
]


def run_command(command: str) -> bool:
    """Run a command kill actions if it fails

    Args:
        command: command to run
        continue_on_fail: whether to continue running commands if the current one fails.
    """
    ret_code = subprocess.call(command, shell=True)
    if ret_code != 0:
        console.print(f"[bold red]Error: `{command}` failed.")
    return ret_code == 0


def run_github_actions_file(filename: str):
    """Run a github actions file locally.

    Args:
        filename: Which yml github actions file to run.
    """
    with open(filename, "rb") as f:
        my_dict = yaml.safe_load(f)
    steps = my_dict["jobs"]["build"]["steps"]

    success = True

    for step in steps:
        if "name" in step and step["name"] in LOCAL_TESTS:
            compressed = step["run"].replace("\n", ";").replace("\\", "")
            compressed = compressed.replace("--check", "")
            curr_command = f"{compressed}"

            console.line()
            console.rule(f"[bold green]Running: {curr_command}")
            success = success and run_command(curr_command)
        else:
            skip_name = step["name"] if "name" in step else step["uses"]
            console.print(f"Skipping {skip_name}")

    # Code Testing
    console.line()
    console.rule("[bold green]Running pytest")
    success = success and run_command("pytest")

    # Add checks for building documentation
    console.line()
    console.rule("[bold green]Building Documentation")
    success = success and run_command(
        "cd docs/; make clean; make html SPHINXOPTS='-W;'"
    )

    if success:
        console.line()
        console.rule(characters="=")
        console.print(
            "[bold green]:TADA: :TADA: :TADA: ALL CHECKS PASSED :TADA: :TADA: :TADA:",
            justify="center",
        )
        console.rule(characters="=")
    else:
        console.line()
        console.rule(characters="=", style=Style(color="red"))
        console.print(
            "[bold red]:skull: :skull: :skull: ERRORS FOUND :skull: :skull: :skull:",
            justify="center",
        )
        console.rule(characters="=", style=Style(color="red"))


if __name__ == "__main__":
    run_github_actions_file(filename=".github/workflows/code_checks.yml")