-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.py
161 lines (129 loc) · 3.44 KB
/
cluster.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
#!/bin/python3
import signal
import sys
import subprocess
import importlib.util
import colorama
import pty
import os
next_cluster_executable_path = "bin/release/next_cluster"
make_states_executable_path = "bin/release/make_states"
simulate_many_executable_path = "bin/release/simulate_many"
width=500
height=500
col=250
row=250
gen_limit="1'000'000'000"
out_dir = None
sim_count = None
def ctrl_c_handler(sig, frame):
print('')
print('Terminating...')
sys.exit(1)
if (__name__ == "__main__"):
signal.signal(signal.SIGINT, ctrl_c_handler)
spec = importlib.util.find_spec("colorama")
if spec is None:
raise ImportError("Package 'colorama' is not installed. Please install it using 'pip install colorama'")
colorama.init(autoreset=True)
if (len(sys.argv) == 1):
print(f"Usage: {sys.argv[0]} <out_dir> <sim_count>")
sys.exit(1)
out_dir = sys.argv[1]
sim_count = sys.argv[2]
arguments = [
next_cluster_executable_path,
out_dir
]
proc = subprocess.Popen(
arguments,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Wait for the subprocess to finish and capture the exit code
next_cluster_exit_code = proc.wait()
cluster = next_cluster_exit_code
print(f"Cluster {cluster}, {out_dir}")
print("[[ make_states ]]")
master_fd, slave_fd = pty.openpty()
arguments = [
make_states_executable_path,
"-N", f"{sim_count}",
"-o", f"{out_dir}/cluster{cluster}",
"-n", "randwords,2",
"-m", "50", "-M", "50",
"-t", "LR",
"-s", "rand",
"-w", f"{width}", "-h", f"{height}",
"-x", f"{col}", "-y", f"{row}",
"-O", "NESW",
"-g", "fill=0",
"-W", "resources/words.txt",
"-c",
]
# print("[" + ", ".join(f'"{arg}"' for arg in arguments[1:]) + "]")
proc = subprocess.Popen(
arguments,
stdout=slave_fd,
stderr=slave_fd,
text=True,
bufsize=1,
close_fds=True,
universal_newlines=True
)
os.close(slave_fd)
# Process the output in real-time
while True:
try:
output = os.read(master_fd, 1024).decode()
except OSError:
break
if not output:
break
print(output.strip())
make_states_exit_code = proc.wait()
os.close(master_fd)
if (make_states_exit_code != 0):
print(f"make_states exited with code {make_states_exit_code}, terminating!")
colorama.deinit()
exit(1)
print("[[ simulate_many ]]")
master_fd, slave_fd = pty.openpty()
arguments = [
simulate_many_executable_path,
# "-T", "2",
# "-Q", "50",
"-L", f"{out_dir}/cluster{cluster}/log.txt",
"-S", f"{out_dir}/cluster{cluster}",
"-g", f"{gen_limit}",
"-f", "raw",
"-o", f"{out_dir}/cluster{cluster}",
"-s", "-y", "-l", "-C"
]
# print("[" + ", ".join(f'"{arg}"' for arg in arguments[1:]) + "]")
proc = subprocess.Popen(
arguments,
stdout=slave_fd,
stderr=slave_fd,
text=True,
bufsize=1,
close_fds=True,
universal_newlines=True
)
os.close(slave_fd)
# Process the output in real-time
while True:
try:
output = os.read(master_fd, 1024).decode()
except OSError:
break
if not output:
break
print(output.strip())
simulate_many_exit_code = proc.wait()
os.close(master_fd)
if (simulate_many_exit_code != 0):
print(f"simulate_many exited with code {simulate_many_exit_code}")
if (simulate_many_exit_code == -9):
print(f"SIGKILL, you may not have enough memory")
colorama.deinit()