Repository navigation
Expand file tree
/
Copy pathpatch_p4a_boot_entrypoint.py
More file actions
executable file
·69 lines (58 loc) · 1.96 KB
/
Copy pathpatch_p4a_boot_entrypoint.py
File metadata and controls
executable file
·69 lines (58 loc) · 1.96 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
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Patch p4a PythonActivity.getEntryPoint to prefer boot.py over main.py.
Upstream sdl2 hardcodes main.py/main.pyc. PyDevices uses MicroPython-shaped
boot.py (setup) + optional main.py (user code).
"""
from __future__ import annotations
import pathlib
import re
import sys
NEW_METHOD = """
public String getEntryPoint(String search_dir) {
/* PyDevices: MicroPython-shaped boot.py, then main.py (user code). */
List<String> entryPoints = new ArrayList<String>();
entryPoints.add("boot.pyc");
entryPoints.add("boot.py");
entryPoints.add("main.pyc"); // python 3 compiled files
entryPoints.add("main.py");
for (String value : entryPoints) {
File mainFile = new File(search_dir + "/" + value);
if (mainFile.exists()) {
return value;
}
}
return "boot.py";
}
""".strip(
"\n"
)
METHOD_RE = re.compile(
r" public String getEntryPoint\(String search_dir\) \{.*?\n \}",
re.DOTALL,
)
def patch_file(path: pathlib.Path) -> str:
text = path.read_text(encoding="utf-8")
if "entryPoints.add(\"boot.pyc\")" in text or "entryPoints.add(\"boot.py\")" in text:
return "already"
if not METHOD_RE.search(text):
return "no-match"
path.write_text(METHOD_RE.sub(NEW_METHOD, text, count=1), encoding="utf-8")
return "patched"
def main(argv: list[str]) -> int:
if len(argv) < 2:
print("Usage: patch_p4a_boot_entrypoint.py <PythonActivity.java> [...]", file=sys.stderr)
return 2
rc = 0
for raw in argv[1:]:
path = pathlib.Path(raw)
if not path.is_file():
print(f"skip missing: {path}", file=sys.stderr)
continue
status = patch_file(path)
print(f"{status}: {path}")
if status == "no-match":
rc = 1
return rc
if __name__ == "__main__":
raise SystemExit(main(sys.argv))