forked from Go-To-Byte/DouSheng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
133 lines (107 loc) · 3.29 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
# ! /usr/bin/env python3
# -*- coding: utf-8 -*-
# @author : BeYoung
# @time : 2023/2/24
import argparse
import multiprocessing
import os
import pathlib
import subprocess
import time
PATH = []
TASKS = []
def parser_arg():
parser = argparse.ArgumentParser()
parser.add_argument("--kill",
nargs="?",
type=int,
default=0,
help="kill all service")
parser.add_argument("--run",
nargs="?",
type=int,
default=0,
help="run all service, but not execute command: make dep, make init")
parser.add_argument("--main",
nargs="?",
type=int,
default=0,
help="run all service, and execute command: make dep, make init")
args = parser.parse_args()
if args.kill != 0:
kill_all_main()
os._exit(0)
elif args.run != 0:
get_all_makefile()
make_all_run()
time.sleep(3)
wait_all_main()
elif args.main != 0:
get_all_makefile()
make_dep()
make_init()
make_all_run()
time.sleep(3)
wait_all_main()
def on_exit(signum, frame):
for task in TASKS:
task: multiprocessing.Process
task.close()
def kill_all_main():
# 因为要获取 shell 的输出,所以使用 subprocess.getoutput 执行 shell 命令
pids = subprocess.getoutput("pidof main").split(" ")
for pid in pids:
os.system(f"kill {pid}")
def run_main(path):
path: pathlib.Path
os.chdir(path.parent)
print(f"Now path: {os.getcwd()}")
print(f"make run: {path.cwd()}")
os.system(f"make run >> ~/log/{path.name}.log")
def make_all_run():
for path in PATH:
task = multiprocessing.Process(target=run_main, args=(path,))
TASKS.append(task)
task.daemon = True
task.start()
def wait_all_main():
for task in TASKS:
task: multiprocessing.Process
task.join()
def get_all_makefile():
global PATH
cwd = pathlib.Path.cwd()
for path in cwd.iterdir():
if path.is_dir() and not path.is_file():
PATH += [makefile for makefile in path.iterdir() if makefile.name == "Makefile" and makefile.is_file()]
def make_dep():
for makefile in PATH:
makefile: pathlib.Path
os.chdir(makefile.parent)
print(f"Now path: {os.getcwd()}")
print(f"make dep: {makefile.cwd()}")
os.system(f"make dep >> ~/log/{makefile.parent.name}.log")
def make_init():
for makefile in PATH:
makefile: pathlib.Path
os.chdir(makefile.parent)
print(f"Now path: {os.getcwd()}")
print(f"make init: {makefile.cwd()}")
os.system(f"make init >> ~/log/{makefile.parent.name}.log")
def make_install():
for makefile in PATH:
makefile: pathlib.Path
os.chdir(makefile)
print(f"Now path: {os.getcwd()}")
print(f"make install: {makefile.cwd()}")
os.system(f"make install >> ~/log/{makefile.parent.name}.log")
def main():
parser_arg()
get_all_makefile()
make_dep()
make_init()
make_all_run()
time.sleep(3)
wait_all_main()
if __name__ == '__main__':
main()