-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstart.py
237 lines (218 loc) · 4.98 KB
/
start.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# 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 os
import shlex
import sys
from contextlib import (
suppress,)
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"
filechecker = "bash filecheck.sh --log-level info"
python = "python3"
nocache = f"{python} -B"
app = f"{python} -m pytel"
app_watch = f"{python} -m scripts.autoreload {app}"
# Pylint
autoflake = "autoflake --in-place --ignore-init-module-imports --remove-all-unused-imports -i -r ."
black = "black --line-length 25 --exclude version.py ./pytel"
isort = (
"isort --settings-file=setup.cfg ."
)
flake8 = "flake8 --config=setup.cfg ."
prettyjson = (
f"{nocache} -m scripts.prettyjson"
)
def run_cmd(
cmd,
) -> None:
try:
proc = run(
shlex.split(cmd),
shell=False,
check=True,
)
if proc.returncode != 0:
print(
f"Exit code {proc.returncode}"
)
sys.exit(1)
else:
return proc.stdout
except BaseException:
sys.exit(1)
def clean() -> None:
with suppress(BaseException):
for _ in Path(".").rglob(
"*.py[co]"
):
_.unlink(missing_ok=True)
for _ in Path(".").rglob(
"__pycache__"
):
_.rmdir()
for _ in Path(".").rglob(
"cache/pip"
):
_.rmdir()
def lint() -> None:
print(f"{CYAN}>> {prettyjson}{RST}")
run_cmd(prettyjson)
print(f"{CYAN}>> {autoflake}{RST}")
run_cmd(autoflake)
print(f"{CYAN}>> {black}{RST}")
run_cmd(black)
print(f"{CYAN}>> {isort}{RST}")
run_cmd(isort)
print(f"{CYAN}>> {flake8}{RST}")
run_cmd(flake8)
class CapitalisedHelpFormatter(
argparse.HelpFormatter
):
def add_usage(
self,
usage,
actions,
groups,
prefix=None,
):
if not prefix:
prefix = "Usage: "
return super(
CapitalisedHelpFormatter,
self,
).add_usage(
usage,
actions,
groups,
prefix,
)
parser = argparse.ArgumentParser(
formatter_class=CapitalisedHelpFormatter,
prog=f"{GREEN}{python} -m start{RST}",
usage="%(prog)s [options]",
epilog="Source code https://github.com/kastaid",
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(
"-ch",
"--checker",
help="checking attributes file",
action="store_true",
)
parser.add_argument(
"-v",
"--version",
help="show this program version",
action="version",
version=f"PYTEL-Premium v{__version__}",
)
parser.add_argument(
"-h",
"--help",
help="show this help information",
default=argparse.SUPPRESS,
action="help",
)
def main():
args = parser.parse_args()
if args.prod:
os.system("clear")
print(
f"{BOLD}{GREEN}PRODUCTION MODE...{RST}"
)
clean()
print(
f"{BOLD}{BLUE}>> {app}{RST}"
)
run_cmd(app)
elif args.dev:
os.system("clear")
print(
f"{BOLD}{GREEN}DEVELOPMENT MODE...{RST}"
)
clean()
lint()
print(
f"{BOLD}{BLUE}>> {app}{RST}"
)
run_cmd(app)
elif args.watch:
os.system("clear")
print(
f"{BOLD}{GREEN}WATCHED DEVELOPMENT MODE...{RST}"
)
clean()
print(
f"{BOLD}{BLUE}>> {app_watch}{RST}"
)
run_cmd(app_watch)
elif args.lint:
os.system("clear")
print(
f"{BOLD}{YELLOW}Run linting and format code...{RST}"
)
clean()
lint()
elif args.clean:
os.system("clear")
clean()
print(
f"{BOLD}{BLUE}✓ Cached has been clear.{RST}"
)
elif args.checker:
os.system("clear")
print(
f"{BOLD}{GREEN}Bash >>> Checking Attributes Files{RST}"
)
run_cmd(filechecker)
else:
print(
f"{python} -m start --help"
)
if __name__ == "__main__":
main()