-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrun.py
More file actions
161 lines (141 loc) · 3.58 KB
/
Copy pathrun.py
File metadata and controls
161 lines (141 loc) · 3.58 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright (C) 2023-present kastaid
# https://github.com/kastaid/ds
# MIT License
import argparse
import shlex
import subprocess
import sys
from pathlib import Path
from version import __version__
RST = "\x1b[0m"
BOLD = "\x1b[1m"
GREEN = "\x1b[32m"
YELLOW = "\x1b[33m"
BLUE = "\x1b[34m"
CYAN = "\x1b[36m"
python = "python3"
nocache = f"{python} -B"
app = f"{python} -m ds"
app_watch = f"{python} -m scripts.autoreload {app}"
ruff_check = "ruff check ."
ruff_format = "ruff format ."
ruff_fix = "ruff check --fix --unsafe-fixes ."
prettyjson = f"{nocache} -m scripts.prettyjson"
def run_cmd(cmd: str) -> None:
try:
proc = subprocess.run(shlex.split(cmd), shell=False, check=False)
if proc.returncode != 0:
print(f"Exit code {proc.returncode}")
sys.exit(1)
except BaseException:
sys.exit(1)
def clean() -> None:
for i in Path().rglob("*.py[co]"):
i.unlink(missing_ok=True)
for i in Path().rglob("__pycache__"):
i.rmdir()
def lint() -> None:
print(f"{CYAN}>> {prettyjson}{RST}")
run_cmd(prettyjson)
print(f"{CYAN}>> {ruff_check}{RST}")
run_cmd(ruff_check)
print(f"{CYAN}>> {ruff_format}{RST}")
run_cmd(ruff_format)
def fix() -> None:
print(f"{CYAN}>> {ruff_fix}{RST}")
run_cmd(ruff_fix)
class CapitalisedHelpFormatter(argparse.HelpFormatter):
def add_usage(self, usage, actions, groups, prefix=None):
if not prefix:
prefix = "Usage: "
return super().add_usage(usage, actions, groups, prefix)
parser = argparse.ArgumentParser(
formatter_class=CapitalisedHelpFormatter,
prog=f"{GREEN}{python} -m run{RST}",
usage="%(prog)s [options]",
epilog="Source code https://github.com/kastaid/ds",
add_help=False,
)
parser._optionals.title = "Options"
parser.add_argument(
"-p",
"--prod",
help="run in production mode",
action="store_true",
)
parser.add_argument(
"-d",
"--dev",
help="run in development mode",
action="store_true",
)
parser.add_argument(
"-w",
"--watch",
help="run and watch in development mode",
action="store_true",
)
parser.add_argument(
"-l",
"--lint",
help="run linting and format code",
action="store_true",
)
parser.add_argument(
"-f",
"--fix",
help="run fixed code",
action="store_true",
)
parser.add_argument(
"-c",
"--clean",
help="remove python caches",
action="store_true",
)
parser.add_argument(
"-v",
"--version",
help="show this program version",
action="version",
version=__version__,
)
parser.add_argument(
"-h",
"--help",
help="show this help information",
default=argparse.SUPPRESS,
action="help",
)
def main() -> None:
args = parser.parse_args()
if args.prod:
print(f"{BOLD}{GREEN}PRODUCTION MODE...{RST}")
clean()
print(f"{BOLD}{BLUE}>> {app}{RST}")
run_cmd(app)
elif args.dev:
print(f"{BOLD}{GREEN}DEVELOPMENT MODE...{RST}")
clean()
lint()
print(f"{BOLD}{BLUE}>> {app}{RST}")
run_cmd(app)
elif args.watch:
print(f"{BOLD}{GREEN}WATCHED DEVELOPMENT MODE...{RST}")
clean()
print(f"{BOLD}{BLUE}>> {app_watch}{RST}")
run_cmd(app_watch)
elif args.lint:
print(f"{BOLD}{YELLOW}Run linting and format code...{RST}")
clean()
lint()
elif args.fix:
print(f"{BOLD}{YELLOW}Run fixed code...{RST}")
clean()
fix()
elif args.clean:
clean()
else:
print(f"{python} -m run --help")
if __name__ == "__main__":
raise SystemExit(main())