-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
176 lines (145 loc) · 5.28 KB
/
parser.py
File metadata and controls
176 lines (145 loc) · 5.28 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
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
import re
import json
import asyncio
import os
import argparse
import tornado.web
import tornado.websocket
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--workspace", default="/root/lunabot", help="path to your ROS2 workspace")
args = arg_parser.parse_args()
WORKSPACE = args.workspace
if not os.path.isdir(WORKSPACE):
print(f"error: workspace '{WORKSPACE}' does not exist")
exit(1)
clients = []
package_states = {}
current_stderr_pkg = None
stderr_buffer = []
build_running = False
def parse_line(line):
global current_stderr_pkg, stderr_buffer
if line.startswith("--- stderr:"):
current_stderr_pkg = line.split("--- stderr:")[1].strip()
stderr_buffer = []
return None
if line == "---" and current_stderr_pkg:
pkg = current_stderr_pkg
error_text = "\n".join(stderr_buffer)
current_stderr_pkg = None
stderr_buffer = []
return {"package": pkg, "status": "error_detail", "error": error_text}
if current_stderr_pkg:
stderr_buffer.append(line)
return None
if line.startswith("Starting >>>"):
package = line.split(">>>")[1].strip()
return {"package": package, "status": "building"}
if line.startswith("Finished <<<"):
match = re.search(r"Finished <<< (\S+) \[(.+)\]", line)
if match:
return {"package": match.group(1), "status": "done", "time": match.group(2)}
if line.strip().startswith("Failed"):
match = re.search(r"Failed\s+<<< (\S+)", line)
if match:
return {"package": match.group(1), "status": "failed"}
if line.strip().startswith("Aborted"):
match = re.search(r"Aborted\s+<<< (\S+)", line)
if match:
return {"package": match.group(1), "status": "aborted"}
return None
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
clients.append(self)
print("browser connected")
self.write_message(json.dumps({
"status": "workspace",
"path": WORKSPACE
}))
for state in package_states.values():
self.write_message(json.dumps(state))
if "error" in state:
self.write_message(json.dumps({
"package": state["package"],
"status": "error_detail",
"error": state["error"]
}))
def on_message(self, message):
data = json.loads(message)
if data.get("action") == "build" and not build_running:
package_states.clear()
packages = data.get("packages", None)
asyncio.create_task(run_build(packages))
def on_close(self):
clients.remove(self)
print("browser disconnected")
class MainHandler(tornado.web.RequestHandler):
def get(self):
index_path = os.path.join(os.path.dirname(__file__), "index.html")
with open(index_path, "r") as f:
self.set_header("Content-Type", "text/html")
self.write(f.read())
async def run_build(packages=None):
global build_running
build_running = True
for client in clients:
client.write_message(json.dumps({"status": "build_started"}))
cmd = ["colcon", "build"]
if packages:
cmd += ["--packages-select"] + packages
try:
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
cwd=WORKSPACE
)
except Exception as e:
for client in clients:
client.write_message(json.dumps({
"status": "error_detail",
"package": "server",
"error": str(e)
}))
build_running = False
return
async for line in process.stdout:
line = line.decode().strip()
print("RAW:", line)
result = parse_line(line)
if result:
print("PARSED:", result)
if result["status"] == "error_detail":
if result["package"] in package_states:
package_states[result["package"]]["error"] = result["error"]
else:
package_states[result["package"]] = result
for client in clients:
client.write_message(json.dumps(result))
elif result["status"] == "failed":
if result["package"] in package_states:
package_states[result["package"]]["status"] = "failed"
else:
package_states[result["package"]] = result
for client in clients:
client.write_message(json.dumps(result))
else:
package_states[result["package"]] = result
for client in clients:
client.write_message(json.dumps(result))
build_running = False
for client in clients:
client.write_message(json.dumps({"status": "build_done"}))
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/ws", WSHandler),
])
async def main():
app = make_app()
app.listen(8888)
print(f"server running at http://localhost:8888")
print(f"workspace: {WORKSPACE}")
await asyncio.Event().wait()
if __name__ == "__main__":
asyncio.run(main())