-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·74 lines (57 loc) · 2.32 KB
/
run.py
File metadata and controls
executable file
·74 lines (57 loc) · 2.32 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
#!/bin/python
import subprocess
import time
from argparse import ArgumentParser, Namespace
from pathlib import Path
def sp_run(path: Path, compile: bool):
start_time = time.perf_counter()
try:
subprocess.run(
path.joinpath("compile.sh" if compile else "run.sh"),
capture_output=True,
text=True,
check=True,
)
except subprocess.CalledProcessError as e:
print(e.returncode, e.stderr)
end_time = time.perf_counter()
run_time = end_time - start_time
print(f"{round(run_time * 1000)} ms")
def fstring(lang: str, proj: str, add: str):
return f"{lang.ljust(12, '.')}{proj.ljust(15, '.')}" + add.ljust(10, ".")
def run(path: Path, args: Namespace, lang: str, proj: str):
if args.compile:
print(fstring(lang, proj, "Compiling..."), end="", flush=True)
sp_run(path, True)
if args.run:
print(fstring(lang, proj, "Running....."), end="", flush=True)
sp_run(path, False)
def is_doable(d: Path):
return d.is_dir() and "." not in d.name and d.name[0].isupper()
if __name__ == "__main__":
parser = ArgumentParser(description="Compile and/or run projects for languages")
parser.add_argument("-c", "--compile", action="store_true")
parser.add_argument("-r", "--run", action="store_true")
parser.add_argument("-a", "--all", action="store_true")
parser.add_argument("language", nargs="?", type=str.capitalize)
parser.add_argument("project", nargs="?", type=str.capitalize)
args = parser.parse_args()
if not args.all and not args.language and not args.project:
exit(0)
if not args.compile and not args.run:
args.compile = True
args.run = True
root = Path(__file__).parent
if args.all:
langs = sorted(d for d in root.iterdir() if is_doable(d))
for dir in langs:
for project in dir.iterdir():
if is_doable(project):
run(project, args, dir.name, project.name)
elif args.language and not args.project:
for project in root.joinpath(args.language).iterdir():
if is_doable(project):
run(project, args, args.language, project.name)
else:
path = Path(__file__).parent.joinpath(args.language, args.project)
run(path, args, args.language, args.project)