MAOS.git
Publicgit clone https://git.maproj.org/MAOS.gitMAOS.git / installer / script_to_manifest.py
script_to_manifest.py175 lines · 6.9 KiBRaw
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | script_to_manifest.py — translate a GrapheneOS install-zip `script.txt` into the ordered |
| 4 | flash plan the MAOS web installer consumes (manifest.json `steps`). |
| 5 | |
| 6 | Why: the install zip (`<device>-install-<build>.zip`) already contains the whole `super` |
| 7 | pre-split into `super_1..N.img` (sized to the device max-download-size) AND `script.txt`, |
| 8 | GrapheneOS's *verified* flash sequence — it flashes `super` directly in bootloader mode |
| 9 | (no fastbootd), which is the lowest-brick-risk path. Rather than invent a plan, we translate |
| 10 | that script verbatim into installer steps so the browser flasher does exactly what |
| 11 | GrapheneOS's own tooling does, in the same order. |
| 12 | |
| 13 | script.txt grammar (one command per line; blank lines and `#` comments ignored): |
| 14 | |
| 15 | check-var NAME VALUE # or `check-var NAME:VALUE` |
| 16 | flash PART FILE [other-slot] # a 3rd token flashes to the inactive slot |
| 17 | toggle-active-slot |
| 18 | reboot-bootloader |
| 19 | run-cmd CMD... # raw fastboot command (e.g. `set_active:a`, `oem uart disable`) |
| 20 | erase PART |
| 21 | maybe-cancel-snapshot-update |
| 22 | check-requirements FILE # android-info.txt with `require KEY=VAL[|VAL...]` lines |
| 23 | reboot |
| 24 | |
| 25 | Emitted manifest steps (see location_share_server/os_installer/installer.js for handlers): |
| 26 | |
| 27 | {"action":"check-var","name":NAME,"value":VALUE} |
| 28 | {"action":"flash","partition":PART,"image":FILE[,"slot":"other"]} |
| 29 | {"action":"toggle-active-slot"} |
| 30 | {"action":"reboot-bootloader"} |
| 31 | {"action":"run-cmd","command":CMD} |
| 32 | {"action":"erase","partition":PART} |
| 33 | {"action":"snapshot-update-cancel"} |
| 34 | {"action":"check-requirements","requirements":[{"name":NAME,"values":[...]}, ...]} |
| 35 | {"action":"reboot"} |
| 36 | |
| 37 | Unknown commands are a hard error (fail loud rather than silently drop a step that could |
| 38 | matter for a safe flash). |
| 39 | |
| 40 | USAGE: |
| 41 | script_to_manifest.py --script script.txt --device DEVICE --build BUILD [--srcdir DIR] |
| 42 | |
| 43 | Prints the full manifest JSON ({"device","build","steps":[...]}) to stdout. `check-requirements` |
| 44 | resolves its android-info file relative to --srcdir (defaults to the script's own directory). |
| 45 | """ |
| 46 | import argparse |
| 47 | import json |
| 48 | import os |
| 49 | import sys |
| 50 | |
| 51 | |
| 52 | def parse_android_info(path): |
| 53 | """Parse `require KEY=VAL[|VAL...]` lines from an android-info.txt into a list of |
| 54 | {"name": KEY, "values": [VAL, ...]} requirement objects.""" |
| 55 | reqs = [] |
| 56 | with open(path, "r", encoding="utf-8", errors="replace") as f: |
| 57 | for raw in f: |
| 58 | line = raw.strip() |
| 59 | if not line or line.startswith("#"): |
| 60 | continue |
| 61 | if not line.startswith("require "): |
| 62 | continue |
| 63 | body = line[len("require "):].strip() |
| 64 | if "=" not in body: |
| 65 | continue |
| 66 | name, _, value = body.partition("=") |
| 67 | name = name.strip() |
| 68 | values = [v.strip() for v in value.split("|") if v.strip()] |
| 69 | if name: |
| 70 | reqs.append({"name": name, "values": values}) |
| 71 | return reqs |
| 72 | |
| 73 | |
| 74 | def translate(script_path, srcdir): |
| 75 | steps = [] |
| 76 | with open(script_path, "r", encoding="utf-8", errors="replace") as f: |
| 77 | for lineno, raw in enumerate(f, 1): |
| 78 | line = raw.strip() |
| 79 | if not line or line.startswith("#"): |
| 80 | continue |
| 81 | toks = line.split() |
| 82 | cmd = toks[0] |
| 83 | |
| 84 | if cmd == "check-var": |
| 85 | # `check-var NAME VALUE` or `check-var NAME:VALUE` |
| 86 | if len(toks) >= 3: |
| 87 | name, value = toks[1], toks[2] |
| 88 | elif len(toks) == 2 and ":" in toks[1]: |
| 89 | name, value = toks[1].split(":", 1) |
| 90 | else: |
| 91 | raise ValueError(f"line {lineno}: malformed check-var: {line!r}") |
| 92 | steps.append({"action": "check-var", "name": name, "value": value}) |
| 93 | |
| 94 | elif cmd == "flash": |
| 95 | if len(toks) < 3: |
| 96 | raise ValueError(f"line {lineno}: malformed flash: {line!r}") |
| 97 | step = {"action": "flash", "partition": toks[1], "image": toks[2]} |
| 98 | # Any trailing token (e.g. `other-slot`) => flash to the inactive slot. |
| 99 | if len(toks) >= 4: |
| 100 | step["slot"] = "other" |
| 101 | steps.append(step) |
| 102 | |
| 103 | elif cmd == "toggle-active-slot": |
| 104 | steps.append({"action": "toggle-active-slot"}) |
| 105 | |
| 106 | elif cmd == "reboot-bootloader": |
| 107 | steps.append({"action": "reboot-bootloader"}) |
| 108 | |
| 109 | elif cmd == "run-cmd": |
| 110 | if len(toks) < 2: |
| 111 | raise ValueError(f"line {lineno}: run-cmd needs a command: {line!r}") |
| 112 | steps.append({"action": "run-cmd", "command": " ".join(toks[1:])}) |
| 113 | |
| 114 | elif cmd == "erase": |
| 115 | if len(toks) < 2: |
| 116 | raise ValueError(f"line {lineno}: erase needs a partition: {line!r}") |
| 117 | steps.append({"action": "erase", "partition": toks[1]}) |
| 118 | |
| 119 | elif cmd == "maybe-cancel-snapshot-update": |
| 120 | steps.append({"action": "snapshot-update-cancel"}) |
| 121 | |
| 122 | elif cmd == "check-requirements": |
| 123 | if len(toks) < 2: |
| 124 | raise ValueError(f"line {lineno}: check-requirements needs a file: {line!r}") |
| 125 | info = toks[1] |
| 126 | if not os.path.isabs(info): |
| 127 | info = os.path.join(srcdir, info) |
| 128 | if not os.path.isfile(info): |
| 129 | raise FileNotFoundError( |
| 130 | f"line {lineno}: check-requirements file not found: {info}" |
| 131 | ) |
| 132 | steps.append( |
| 133 | {"action": "check-requirements", "requirements": parse_android_info(info)} |
| 134 | ) |
| 135 | |
| 136 | elif cmd == "reboot": |
| 137 | steps.append({"action": "reboot"}) |
| 138 | |
| 139 | else: |
| 140 | raise ValueError( |
| 141 | f"line {lineno}: unknown script.txt command {cmd!r}. Extend " |
| 142 | f"script_to_manifest.py (and installer.js) before publishing: {line!r}" |
| 143 | ) |
| 144 | |
| 145 | if not steps: |
| 146 | raise ValueError("script.txt produced no steps") |
| 147 | return steps |
| 148 | |
| 149 | |
| 150 | def main(): |
| 151 | ap = argparse.ArgumentParser() |
| 152 | ap.add_argument("--script", required=True, help="path to the install zip's script.txt") |
| 153 | ap.add_argument("--device", required=True) |
| 154 | ap.add_argument("--build", required=True) |
| 155 | ap.add_argument( |
| 156 | "--srcdir", |
| 157 | default=None, |
| 158 | help="dir to resolve check-requirements files against (default: script's dir)", |
| 159 | ) |
| 160 | args = ap.parse_args() |
| 161 | |
| 162 | srcdir = args.srcdir or os.path.dirname(os.path.abspath(args.script)) |
| 163 | try: |
| 164 | steps = translate(args.script, srcdir) |
| 165 | except Exception as e: # noqa: BLE001 - surface a clear message and fail |
| 166 | print(f"script_to_manifest: {e}", file=sys.stderr) |
| 167 | sys.exit(1) |
| 168 | |
| 169 | manifest = {"device": args.device, "build": args.build, "steps": steps} |
| 170 | json.dump(manifest, sys.stdout, indent=2) |
| 171 | sys.stdout.write("\n") |
| 172 | |
| 173 | |
| 174 | if __name__ == "__main__": |
| 175 | main() |