Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
#!/usr/bin/env python3
"""Generate and content-validate the compressed firmware dashboard include."""
from __future__ import annotations
import argparse
import gzip
import hashlib
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SOURCE = ROOT / "src" / "dashboard.html"
OUTPUT = ROOT / "src" / "generated" / "dashboard_html_gz.h"
def render(source: bytes) -> str:
payload = gzip.compress(source, compresslevel=9, mtime=0)
rows = []
for offset in range(0, len(payload), 16):
chunk = payload[offset : offset + 16]
rows.append(" " + ", ".join(f"0x{byte:02x}" for byte in chunk) + ",")
return "\n".join([
"// Generated by tools/development/generate_dashboard.py; do not edit.",
"// Source: src/dashboard.html",
f"// Source SHA-256: {hashlib.sha256(source).hexdigest()}",
"#pragma once",
"",
"static const uint8_t DASHBOARD_HTML_GZ[] PROGMEM = {",
*rows,
"};",
f"static constexpr size_t DASHBOARD_HTML_GZ_LEN = {len(payload)};",
"",
])
def validate(output: str, source: bytes) -> bool:
"""Validate the checked-in asset without assuming a zlib version."""
match = re.search(
r"^.*?// Source SHA-256: ([0-9a-f]{64})\n"
r".*?DASHBOARD_HTML_GZ\[\] PROGMEM = \{\n(.*?)\n\};\n"
r"static constexpr size_t DASHBOARD_HTML_GZ_LEN = (\d+);$",
output,
flags=re.DOTALL,
)
if match is None or match.group(1) != hashlib.sha256(source).hexdigest():
return False
payload = bytes(int(value, 16) for value in re.findall(r"0x([0-9a-f]{2})", match.group(2)))
if len(payload) != int(match.group(3)):
return False
try:
return gzip.decompress(payload) == source
except gzip.BadGzipFile:
return False
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--check", action="store_true", help="fail when the generated include is stale")
args = parser.parse_args()
source = SOURCE.read_bytes()
if args.check:
if not OUTPUT.is_file() or not validate(OUTPUT.read_text(encoding="utf-8"), source):
raise SystemExit("[FAIL] dashboard include does not match src/dashboard.html")
print(f"[PASS] dashboard include matches {SOURCE}")
return 0
generated = render(source)
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
OUTPUT.write_text(generated, encoding="utf-8", newline="\n")
print(f"[OK] generated {OUTPUT}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
51e48cccaThis file is pinned to the selected release. Download the original to open it in your design tool.