-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
run.py
150 lines (133 loc) · 3.52 KB
/
run.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
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
# getter < https://t.me/kastaid >
# Copyright (C) 2022-present kastaid
#
# This file is a part of < https://github.com/kastaid/getter/ >
# Please read the GNU Affero General Public License in
# < https://github.com/kastaid/getter/blob/main/LICENSE/ >.
import argparse
import shlex
import sys
from pathlib import Path
from subprocess import run
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 getter"
app_watch = f"{python} -m scripts.autoreload {app}"
ruff_check = "ruff check ."
ruff_format = "ruff format ."
isort = "isort --settings-file=pyproject.toml ."
prettyjson = f"{nocache} -m scripts.prettyjson"
def run_cmd(cmd: str) -> None:
try:
proc = run(shlex.split(cmd), shell=False)
if proc.returncode != 0:
print(f"Exit code {proc.returncode}")
sys.exit(1)
except BaseException:
sys.exit(1)
def clean() -> None:
for _ in Path(".").rglob("*.py[co]"):
_.unlink(missing_ok=True)
for _ in Path(".").rglob("__pycache__"):
_.rmdir()
def lint() -> None:
print(f"{CYAN}>> {prettyjson}{RST}")
run_cmd(prettyjson)
print(f"{CYAN}>> {isort}{RST}")
run_cmd(isort)
print(f"{CYAN}>> {ruff_check}{RST}")
run_cmd(ruff_check)
print(f"{CYAN}>> {ruff_format}{RST}")
run_cmd(ruff_format)
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/getter",
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(
"-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.clean:
clean()
else:
print(f"{python} -m run --help")
if __name__ == "__main__":
raise SystemExit(main())