-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcode_format.py
executable file
·81 lines (61 loc) · 1.6 KB
/
code_format.py
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
#!/usr/bin/env python3
import argparse
import black
import subprocess
from pathlib import Path
PROJECT_DIR = Path(__file__).parent.resolve()
SCUBAINIT_DIR = PROJECT_DIR / "scubainit"
def _run_black(fix: bool) -> bool:
args = [
str(PROJECT_DIR),
]
if not fix:
# check only
args = args + [
"--check",
"--color",
"--diff",
]
status = black.main(args, standalone_mode=False)
if status == 0:
return True
if status == 1:
return False
raise Exception(f"Unexpected exit status: {status}")
def _rust_fmt(fix: bool) -> bool:
args = [
"cargo",
"fmt",
]
if not fix:
# check only
args += [
"--check",
]
status = subprocess.call(args, cwd=SCUBAINIT_DIR)
if status == 0:
print("Ok")
return True
if status == 1:
return False
raise Exception(f"Unexpected exit status: {status}")
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--fix",
action="store_true",
help="Modify files to fix formatting",
)
return parser.parse_args()
def main() -> None:
args = _parse_args()
ok = True
print(f"\n{'Fixing' if args.fix else 'Checking'} Python code formatting...")
ok &= _run_black(args.fix)
print(f"\n{'Fixing' if args.fix else 'Checking'} Rust code formatting...")
ok &= _rust_fmt(args.fix)
if not ok:
print("\nTo fix, rerun with --fix")
raise SystemExit(1)
if __name__ == "__main__":
main()