#!/usr/bin/env python3
from glob import glob
import subprocess

import click

from ci import git_ls_py_files


@click.command()
@click.option("--check", is_flag=True)
def main(check):
    if check:
        black_args = ["--check"]
        clang_format_args = ["--dry-run", "-Werror"]
    else:
        black_args = []
        clang_format_args = ["-i"]
    subprocess.check_call(["black", *git_ls_py_files(), *black_args])
    memtrace_ext_sources = [
        *glob("memtrace_ext/*.cc"),
        *glob("memtrace_ext/*.h"),
    ]
    subprocess.check_call(
        [
            "clang-format",
            *memtrace_ext_sources,
            *clang_format_args,
        ]
    )
    subprocess.check_call(
        [
            "cpplint",
            "--root=memtrace_ext",
            *memtrace_ext_sources,
        ]
    )


if __name__ == "__main__":
    main()
