update-supported-devices.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python3
  2. """Update docs/supported-devices.md from a device-support issue."""
  3. import re, sys, datetime, pathlib
  4. ISSUE_BODY = pathlib.Path(sys.argv[1]).read_text() if len(sys.argv) > 1 else sys.stdin.read()
  5. MD = pathlib.Path("docs/supported-devices.md")
  6. def field(id):
  7. # issue form bodies render as "### <Label>\n\nvalue"
  8. # we match by id label text variations
  9. labels = {
  10. "manufacturer": "Manufacturer",
  11. "device": "Device",
  12. "codename": "Codename",
  13. "gki_kernel": "GKI Kernel",
  14. "firmware": "Firmware",
  15. "status": "Status",
  16. }
  17. label = labels[id]
  18. m = re.search(rf"### {re.escape(label)}\s*\n+([^\n#]+)", ISSUE_BODY)
  19. if m:
  20. return m.group(1).strip()
  21. # fallback: try id
  22. m = re.search(rf"### {re.escape(id)}\s*\n+([^\n#]+)", ISSUE_BODY)
  23. return m.group(1).strip() if m else ""
  24. manufacturer = field("manufacturer") or "Other"
  25. device = field("device").strip()
  26. codename = field("codename").strip()
  27. gki = field("gki_kernel").strip()
  28. firmware = field("firmware").strip() or "stock"
  29. status = field("status").strip() or "Supported"
  30. if not device or not codename or not gki:
  31. print("missing required fields", file=sys.stderr)
  32. sys.exit(0)
  33. heading_map = {
  34. "Google Pixel": "## Google Pixel",
  35. "Samsung": "## Samsung",
  36. "OnePlus": "## OnePlus",
  37. "OPPO": "## OPPO",
  38. "Realme": "## Realme",
  39. "Xiaomi": "## Xiaomi",
  40. "POCO": "## POCO",
  41. "Redmi": "## Redmi",
  42. "Nothing": "## Nothing",
  43. "Other": "## Other",
  44. }
  45. heading = heading_map.get(manufacturer, "## Other")
  46. # Xiaomi/POCO/Redmi share same file section expansion if not present -> create heading
  47. text = MD.read_text()
  48. if heading not in text and manufacturer in ("Xiaomi", "POCO", "Redmi"):
  49. # ensure sections exist (already added as separate? current file has Xiaomi / POCO / Redmi combined? we split?)
  50. # if missing, append before end
  51. pass
  52. today = datetime.date.today().isoformat()
  53. status_cell = f"{status} · {today}"
  54. row = f"| {device} | {codename} | {gki} | {firmware} | {status_cell} |"
  55. if heading not in text:
  56. # append new section at end before source
  57. text = text.rstrip() + f"\n\n{heading}\n\n| Device | Codename | GKI Kernel | Firmware | Status |\n|--------|----------|------------|----------|--------|\n{row}\n"
  58. MD.write_text(text)
  59. print(f"added new heading {heading}")
  60. sys.exit(0)
  61. # find table under heading
  62. # split by headings
  63. parts = re.split(r"(^## .+$)", text, flags=re.MULTILINE)
  64. out = []
  65. for i, part in enumerate(parts):
  66. if part.strip() == heading:
  67. # next part is body until next heading
  68. body = parts[i+1] if i+1 < len(parts) else ""
  69. # check duplicate codename
  70. if re.search(rf"\|\s*{re.escape(device)}\s*\|", body) or re.search(rf"\|\s*[^|]*\|\s*{re.escape(codename)}\s*\|", body):
  71. print("device already listed, updating not duplicating")
  72. # replace existing row's status/gki if needed? skip for now
  73. out.append(part)
  74. out.append(body)
  75. continue
  76. # replace placeholder row if present
  77. placeholder = "| — | — | — | — | Placeholder — add entries |"
  78. if placeholder in body:
  79. new_body = body.replace(placeholder, row, 1)
  80. else:
  81. # append row before next heading or at end of table (before blank line + ##)
  82. # find last table row line
  83. lines = body.splitlines()
  84. insert_idx = None
  85. for idx, line in enumerate(lines):
  86. if line.startswith("|") and "Device | Codename" in line:
  87. # header, continue
  88. continue
  89. if line.startswith("|") and "--------" in line:
  90. continue
  91. # find last row that starts with |
  92. last = -1
  93. for idx, line in enumerate(lines):
  94. if line.startswith("| "):
  95. last = idx
  96. if last >= 0:
  97. lines.insert(last+1, row)
  98. else:
  99. lines.append(row)
  100. new_body = "\n".join(lines)
  101. out.append(part)
  102. out.append(new_body)
  103. else:
  104. # already handled body as part of heading case? avoid double
  105. if i>0 and parts[i-1].strip() == heading:
  106. continue
  107. out.append(part)
  108. MD.write_text("".join(out))
  109. print(f"inserted {device} into {heading}")