-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·73 lines (56 loc) · 1.71 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
#!/usr/bin/env python3
"""
Script to start Plus+ and Touch Pets servers
"""
import os
import subprocess
import tomllib
import time
from pathlib import Path
katten_dir = str(Path(__file__).parent)
try:
config = tomllib.loads(Path(katten_dir + "/runconfig.toml").read_text())
except:
print("""You need to create a runconfig.toml next to this script with:
- 'mongo_exec' set to the mongod executable name/path
- 'mongo_db' set to the mongodb database path (folder)
- 'mitmproxy_exec' set to the mitmproxy executable name/path""")
os.exit(127)
def run(args, runpath=None):
if runpath:
print(f" -- chdir to {runpath}")
os.chdir(runpath)
print(f" -- execute {' '.join(args)}")
return subprocess.Popen(args)
class RunMgr:
def __init__(self):
self.processes = []
self.orig_cwd = os.getcwd()
def run(self, cmd, runpath=None):
self.processes.append(run(cmd, katten_dir + "/" + runpath if runpath else None))
def poll(self):
for proc in self.processes:
proc.poll()
def stop(self):
for proc in self.processes:
print(f' -- terminate pid {proc.pid}')
proc.terminate()
while proc.poll() == None:
time.sleep(0.1)
os.chdir(self.orig_cwd)
def main():
mgr = RunMgr()
mgr.run([config['mongo_exec'], '--quiet', '--dbpath', config['mongo_db']])
mgr.run(['flask', 'run', '--debug', '--port', '5100'], 'plus')
mgr.run(['flask', 'run', '--debug', '--port', '5200'], 'touchpet')
mgr.run(['flask', 'run', '--debug', '--address', '0.0.0.0', '--port', '5000'], 'router')
mgr.run([config['mitmproxy_exec'], '--mode=reverse:https://localhost:5000'])
try:
while True:
mgr.poll()
time.sleep(0.1)
except KeyboardInterrupt:
print("Exiting...")
mgr.stop()
if __name__ == "__main__":
main()