# Gzips web/index.html into the SPIFFS data folder (ESP32_UniClock2/data/index.html.gz).
# Runs before every PlatformIO build/uploadfs (extra_scripts in platformio.ini); also runs on its own:
# python web/gzip_web.py
# The web server sends the .gz file with Content-Encoding: gzip (serveStatic in ESP32_UniClock2.ino).
import gzip
import os
try:
Import("env") # noqa: F821 (PlatformIO/SCons)
root = env["PROJECT_DIR"] # noqa: F821
except NameError:
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
src = os.path.join(root, "web", "index.html")
dst = os.path.join(root, "ESP32_UniClock2", "data", "index.html.gz")
with open(src, "rb") as f:
packed = gzip.compress(f.read(), 9, mtime=0) # mtime=0: same input, same bytes (no git noise)
old = open(dst, "rb").read() if os.path.exists(dst) else b""
if packed != old:
with open(dst, "wb") as f:
f.write(packed)
print("gzip_web: %s (%d bytes)" % (dst, len(packed)))