forked from commaai/openpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
369 lines (288 loc) · 10.5 KB
/
generate.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python3
import ast
import datetime
import logging
import os
import pprint
import shutil
from types import CodeType
import markdown
logging.basicConfig(level=logging.INFO, format="%(message)s")
API_HOST = "https://api.bluepilot.app"
ATHENA_HOST = "wss://ws.bluepilot.app"
MAPS_HOST = "https://maps.bluepilot.app"
FILE_ATHENAD = ["selfdrive/athena/athenad.py", "system/athena/athenad.py"]
FILE_POWER_MONITORING = ["system/hardware/power_monitoring.py", "system/thermald/power_monitoring.py", "selfdrive/thermald/power_monitoring.py"]
FILE_NAVD = "selfdrive/navd/navd.py"
def get_path(path: str | list[str]) -> str:
if isinstance(path, str):
return path
for p in path:
if os.path.exists(p):
return p
raise ValueError(f"File not found: {path}")
def append(path: str | list[str], content: str, end_of_line="\n"):
with open(get_path(path), "a") as f:
f.write(content + end_of_line)
def replace(path: str, old: str, new: str):
with open(path) as f:
content = f.read()
if old not in content:
raise ValueError(f"Old value '{old}' not found in {path}")
content = content.replace(old, new)
with open(path, "w") as f:
f.write(content)
def delete(path: str):
shutil.rmtree(path)
class ASTWriter(object):
def __init__(self, path: str):
self.path = path
def __enter__(self) -> CodeType:
with open(self.path, "r") as f:
content = f.read()
self.tree = ast.parse(content)
self.interpreter = None
if content.startswith("#!"):
self.interpreter = content[0 : content.index("\n") + 1]
return self.tree
def __exit__(self, *args):
content = ast.unparse(self.tree)
with open(self.path, "w") as f:
if self.interpreter:
f.write(self.interpreter)
f.write(content + "\n")
def patch_assignment(path: str, variable_name: str, value: str):
with ASTWriter(path) as tree:
for node in ast.walk(tree):
if not isinstance(node, ast.Assign):
continue
for target in node.targets:
if not isinstance(target, ast.Name) or target.id != variable_name:
continue
node.value = ast.parse(value).body[0].value
def patch_method_noop(path: str | list[str], method_name: str):
"""Replace method with a no-op 'return' statement"""
with ASTWriter(get_path(path)) as tree:
for node in ast.walk(tree):
if not isinstance(node, ast.FunctionDef) or node.name != method_name:
continue
node.body = [ast.parse("return").body[0]]
def patch_method(path: str | list[str], method_name: str, code: str):
"""Replace method with custom code"""
with ASTWriter(get_path(path)) as tree:
for node in ast.walk(tree):
if not isinstance(node, ast.FunctionDef) or node.name != method_name:
continue
node.body = ast.parse(code).body
def strip_github_workflows() -> str:
delete(".github/workflows")
return "remove github workflows"
def patch_api() -> str:
append("launch_env.sh", f'export API_HOST="{API_HOST}"')
append("launch_env.sh", f'export ATHENA_HOST="{ATHENA_HOST}"')
return "api: update host"
def patch_power_monitoring() -> str:
replace(
get_path(FILE_POWER_MONITORING),
"MAX_TIME_OFFROAD_S = 30*3600",
"MAX_TIME_OFFROAD_S = 3*3600",
)
return "hardwared: set MAX_TIME_OFFROAD_S to 3 hours"
def patch_nav():
# NOTE: openpilot doesn't support this as an environment variable, only setting the token directly
replace("selfdrive/navd/navd.py", "https://maps.comma.ai", MAPS_HOST)
return "navd: use custom maps proxy"
# def patch_ford() -> str:
# value = "FwQueryConfig(requests=[])"
# patch_assignment("selfdrive/car/nissan/values.py", "FW_QUERY_CONFIG", value)
# return "ford: remove conflicting nissan fw queries"
def patch_athena() -> str:
patch_method_noop(FILE_ATHENAD, "log_handler")
patch_method_noop(FILE_ATHENAD, "stat_handler")
append(
FILE_ATHENAD,
"@dispatcher.add_method\n"
+ "def ping() -> None:\n"
+ " last_ping = int(time.monotonic() * 1e9)\n"
+ " Params().put('LastAthenaPingTime', str(last_ping))\n",
)
patch_method(
FILE_ATHENAD,
"ws_recv",
"while not end_event.is_set():\n"
+ " try:\n"
+ " opcode, data = ws.recv_data(control_frame=True)\n"
+ " if opcode == ABNF.OPCODE_TEXT:\n"
+ " data = data.decode('utf-8')\n"
+ " recv_queue.put_nowait(data)\n"
+ " except WebSocketTimeoutException:\n"
+ " last_ping = int(Params().get('LastAthenaPingTime') or b'0')\n"
+ " ns_since_last_ping = int(time.monotonic() * 1e9) - last_ping\n"
+ " if ns_since_last_ping > RECONNECT_TIMEOUT_S * 1e9:\n"
+ " cloudlog.exception('athenad.ws_recv.timeout')\n"
+ " end_event.set()\n"
+ " except Exception:\n"
+ " cloudlog.exception('athenad.ws_recv.exception')\n"
+ " end_event.set()\n",
)
return (
"athenad: tweaks and bug fixes\n"
+ "- disable log and stat handlers\n"
+ "- add ping method\n"
+ "- ws_recv: ignore binary messages\n"
+ "- ws_recv: update last ping time check\n"
)
def patch_prime() -> str:
replace(
"selfdrive/ui/qt/widgets/prime.cc",
"https://connect.comma.ai/?pair=",
"https://bluepilot.app/?pair=",
)
return "prime: update QR code URL"
def list_supported_hardware() -> list[str]:
path = "system/hardware" if os.path.isdir("system/hardware") else "selfdrive/hardware"
return list(filter(lambda x: os.path.isdir(f"{path}/{x}") and x != "pc", os.listdir(path)))
def hardware_human_readable(hardware: str) -> str:
if hardware == "eon":
return "comma two, freon"
elif hardware == "tici":
return '<a href="https://comma.ai/shop/comma-3x" target="_blank">comma 3/3X</a>'
else:
return hardware
BRANCHES = [
# local branch, remote branch, patches
(
"master",
"master",
[strip_github_workflows, patch_api, patch_prime, patch_nav, patch_athena],
),
(
"nightly-3h-power-off",
"nightly",
[patch_api, patch_prime, patch_nav, patch_athena, patch_power_monitoring],
),
] + [
(
branch,
branch,
[patch_api, patch_prime, patch_nav, patch_athena]
) for branch in ["master-ci", "nightly", "devel", "release3-staging", "release3"]
] + [
(
branch,
branch,
[patch_api, patch_prime, patch_athena]
) for branch in ["release2"]
]
def prepare_op_repo():
"""
Prepare the openpilot repo
"""
logging.info("Setting up openpilot repo.")
os.system("git remote add commaai https://github.com/commaai/openpilot.git")
logging.info("Done setting up openpilot repo.")
def generate_branch(local, remote, patches) -> str:
"""
Make a new branch from remote with patches applied
"""
logging.info("Generating branch %s", local)
# Make sure branch is clean
os.system(f"git fetch commaai {remote}")
os.system(f"git checkout -B {local} FETCH_HEAD")
# Get date of current commit
commit_date = os.popen("git log -1 --format=%cd --date=iso-strict").read()
author_date = os.popen("git log -1 --format=%ad --date=iso-strict").read()
env = f"GIT_AUTHOR_DATE='{author_date}' GIT_COMMITTER_DATE='{commit_date}'"
# Apply patches to the branch
messages = []
for patch in patches:
message = patch()
messages.append(message)
# Commit the patch
os.system("git add -A")
os.system(f"{env} git commit -m '{message}'")
# skip custom branch
# if local in ("incognitojam", ):
# return ""
supported_hardware = ["eon"] if "release2" in local else list_supported_hardware()
supported_hardware = list(map(hardware_human_readable, supported_hardware))
output = f"<h3>{local}</h3>"
output += "<ul>"
output += f"<li>Supported hardware: {', '.join(supported_hardware)}</li>"
output += f"<li>Custom Software URL: <code>installer.comma.ai/dash-software-ltd/{local}</code></li>"
output += f'<li><a href="https://github.com/dash-software-ltd/openpilot/commits/{local}">View source code on GitHub</a></li>'
output += "<li><details><summary>Change log:</summary>"
output += "<ul>"
for message in messages:
message = message.split("\n")
if len(message) == 1:
output += "<li>" + message[0] + "</li>"
else:
output += "<li>" + message[0] + "<ul>"
for line in filter(bool, message[1:]):
output += "<li>" + line.lstrip("- ").lstrip("* ") + "</li>"
output += "</ul></li>"
output += "</ul></details></li>"
output += "</ul>"
return output
def generate():
# Restore docs branch
os.system("git checkout --force docs")
# Generate a date for the page
now = datetime.datetime.now()
now_str = now.strftime("%Y-%m-%d %H:%M:%S UTC")
# Generate HTML output
header = """
<html>
<head>
<title>bluepilot fork generator</title>
<style>
body {
font-family: sans-serif;
}
code {
background: lightgray;
border-radius: 4px;
padding: 2px;
}
</style>
</head>
<body>
"""
with open("README.md") as f:
header += markdown.markdown(f.read())
header += "\n"
body = "<hr><h2>Branches</h2>\n"
# Generate branches
for local, remote, patches in BRANCHES:
body += generate_branch(local, remote, patches) + "\n"
footer = f"""<hr>
<p>
This page was generated at {now_str}.
</p>
</body>
</html>
"""
# Make pages directory if it doesn't exist
os.system("mkdir -p pages")
with open("pages/index.html", "w") as f:
f.write(header + body + footer)
def main(push=True):
prepare_op_repo()
branch_names = [branch[0] for branch in BRANCHES]
logging.info("branches:")
logging.info(pprint.pformat(branch_names))
generate()
if push:
# Push branches
logging.info("Pushing branches to origin")
for branch in branch_names:
# os.system(f"git fetch origin {branch}")
os.system(f"git push --no-verify --force --set-upstream origin {branch}")
if __name__ == "__main__":
# Check if args has dry run, if so, don't push
import sys
if len(sys.argv) > 1 and sys.argv[1] == "--no-dry-run":
main()
else:
main(push=False)