AI_Forum / tools /make_build_copy.py
Haster1137's picture
Upload 7519 files
57d7008 verified
Raw History Blame Contribute Delete
3.26 kB
"""Create Build/LocalAgent/local_agent.py from the read-only reference copy.
This is the ONE-TIME copy step. It takes ..\\..\\LocalAgent\\local_agent.py
(the reference, never modified) and writes the build copy with every CSS font
size in the embedded web UI scaled up by FONT_SCALE, which is the low-vision
bump. Every other change to the build copy is made by hand afterwards, so the
script refuses to overwrite an existing copy unless you pass --force.
Run: python tools\\make_build_copy.py [--force] [--scale 1.10]
"""
from __future__ import annotations
import os
import re
import shutil
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
BUILD = os.path.dirname(HERE)
ROOT = os.path.dirname(BUILD)
REFERENCE = os.path.join(ROOT, "LocalAgent", "local_agent.py")
TARGET_DIR = os.path.join(BUILD, "LocalAgent")
TARGET = os.path.join(TARGET_DIR, "local_agent.py")
FONT_SCALE = 1.10
# font-size: 20.9px / font: 600 23.2px/1 var(--sans)
FONT_SIZE_RE = re.compile(r"(font-size:\s*)([0-9]*\.?[0-9]+)px")
FONT_SHORTHAND_RE = re.compile(r"(font:\s*)([^;{}\n]+)")
PX_RE = re.compile(r"([0-9]*\.?[0-9]+)px")
def _scale_px(match: "re.Match[str]", scale: float) -> str:
value = float(match.group(1)) * scale
text = ("%.1f" % value).rstrip("0").rstrip(".")
return text + "px"
def scale_fonts(source: str, scale: float) -> tuple[str, int]:
changed = 0
def size_sub(m: "re.Match[str]") -> str:
nonlocal changed
changed += 1
value = float(m.group(2)) * scale
text = ("%.1f" % value).rstrip("0").rstrip(".")
return m.group(1) + text + "px"
def shorthand_sub(m: "re.Match[str]") -> str:
nonlocal changed
value = m.group(2)
if "px" not in value:
return m.group(0)
changed += 1
return m.group(1) + PX_RE.sub(lambda mm: _scale_px(mm, scale), value)
out = FONT_SIZE_RE.sub(size_sub, source)
out = FONT_SHORTHAND_RE.sub(shorthand_sub, out)
return out, changed
def main() -> int:
argv = sys.argv[1:]
force = "--force" in argv
scale = FONT_SCALE
if "--scale" in argv:
scale = float(argv[argv.index("--scale") + 1])
if not os.path.isfile(REFERENCE):
print("reference not found:", REFERENCE)
return 1
if os.path.isfile(TARGET) and not force:
print("build copy already exists, refusing to clobber hand edits:")
print(" ", TARGET)
print("pass --force if you really want to start over")
return 1
os.makedirs(TARGET_DIR, exist_ok=True)
with open(REFERENCE, "r", encoding="utf-8") as fh:
source = fh.read()
out, changed = scale_fonts(source, scale)
with open(TARGET, "w", encoding="utf-8", newline="\n") as fh:
fh.write(out)
print("wrote %s (%d bytes)" % (TARGET, len(out)))
print("scaled %d font declarations by %.2fx" % (changed, scale))
model_pointer = os.path.join(ROOT, "LocalAgent", ".local_agent_model")
if os.path.isfile(model_pointer):
shutil.copy2(model_pointer, os.path.join(TARGET_DIR, ".local_agent_model"))
print("copied model pointer:", open(model_pointer, encoding="utf-8").read().strip())
return 0
if __name__ == "__main__":
raise SystemExit(main())