feat: portable Windows data layout and launcher package
Separate persistent user data from application code (data/work_tracker.db, data/uploads, data/storage) with idempotent legacy migration and SQLite integrity checks. Add offline packaging scripts for a Windows x11 x64 portable ZIP (embeddable Python, Start/Stop/Backup.bat). Fix launcher: no multiline python -c in BAT and no os.dup2 — uvicorn runs detached with wvt.log; health wait and busy-port diagnostics included.
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
"""Build Work Volume Tracker portable ZIP for Windows 11 x64 (clean install)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
PACK = ROOT / "packaging"
|
||||
OUT_DIR = ROOT / "_portable_build"
|
||||
STAGE = OUT_DIR / "stage" / "WorkVolumeTracker"
|
||||
ZIP_PATH = OUT_DIR / "WorkVolumeTracker-portable-win11-x64.zip"
|
||||
|
||||
CDN_PATTERNS = [
|
||||
(re.compile(r'https://cdn\.jsdelivr\.net/npm/bootstrap@5\.3\.0/dist/css/bootstrap\.min\.css'),
|
||||
'/static/vendor/bootstrap.min.css'),
|
||||
(re.compile(r'https://cdn\.jsdelivr\.net/npm/bootstrap@5\.3\.0/dist/js/bootstrap\.bundle\.min\.js'),
|
||||
'/static/vendor/bootstrap.bundle.min.js'),
|
||||
]
|
||||
|
||||
# Only document templates actually loaded by application code.
|
||||
ACTIVE_TEMPLATES = {
|
||||
"ks2/ks2_ru_v2.xlsx",
|
||||
"ks2/ks2_ru_markers.docx",
|
||||
"ks2/manifest.json",
|
||||
"ks3/ks3_ru_markers.docx",
|
||||
"ks3/manifest.json",
|
||||
"ks3/ks3_ru_v1.docx",
|
||||
}
|
||||
|
||||
EXCLUDE_DIR_NAMES = {
|
||||
"__pycache__", ".pytest_cache", ".pytest-tmp", ".git", ".mimocode", ".qwen",
|
||||
"data", "backups", "storage", "uploads", "node_modules",
|
||||
}
|
||||
|
||||
|
||||
def rewrite_cdn(text: str) -> tuple[str, int]:
|
||||
n = 0
|
||||
for pat, repl in CDN_PATTERNS:
|
||||
text, k = pat.subn(repl, text)
|
||||
n += k
|
||||
return text, n
|
||||
|
||||
|
||||
def copy_app(src: Path, dst: Path) -> int:
|
||||
rewritten = 0
|
||||
for path in src.rglob("*"):
|
||||
rel = path.relative_to(src)
|
||||
if any(part in EXCLUDE_DIR_NAMES for part in rel.parts):
|
||||
continue
|
||||
if path.is_dir():
|
||||
continue
|
||||
# never ship user/test junk inside app/
|
||||
name = path.name
|
||||
if name.endswith((".db", ".db-wal", ".db-shm")):
|
||||
continue
|
||||
if name.startswith("_tmp_"):
|
||||
continue
|
||||
target = dst / rel
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
if path.suffix.lower() in {".html", ".htm", ".css", ".js"}:
|
||||
text = path.read_text(encoding="utf-8")
|
||||
new_text, k = rewrite_cdn(text)
|
||||
target.write_text(new_text, encoding="utf-8", newline="")
|
||||
rewritten += k
|
||||
else:
|
||||
shutil.copy2(path, target)
|
||||
return rewritten
|
||||
|
||||
|
||||
def copy_templates_docs(src: Path, dst: Path) -> list[str]:
|
||||
copied = []
|
||||
for rel in sorted(ACTIVE_TEMPLATES):
|
||||
s = src / rel
|
||||
if not s.is_file():
|
||||
raise SystemExit(f"missing active template: {s}")
|
||||
t = dst / rel
|
||||
t.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(s, t)
|
||||
copied.append(rel)
|
||||
return copied
|
||||
|
||||
|
||||
def setup_python(stage: Path) -> list[str]:
|
||||
embed_zip = PACK / "python-embed.zip"
|
||||
if not embed_zip.is_file():
|
||||
raise SystemExit("packaging/python-embed.zip missing")
|
||||
py_dir = stage / "python"
|
||||
with zipfile.ZipFile(embed_zip) as zf:
|
||||
zf.extractall(py_dir)
|
||||
# allow site-packages and the package root (app/ lives next to python/)
|
||||
pth_files = list(py_dir.glob("python*._pth"))
|
||||
for pth in pth_files:
|
||||
content = pth.read_text(encoding="utf-8")
|
||||
lines = [ln for ln in content.splitlines() if ln.strip()]
|
||||
wanted = ["python312.zip", ".", "..", "Lib/site-packages"]
|
||||
# keep comment block harmless; write a known-good path list
|
||||
pth.write_text("\n".join(wanted) + "\n", encoding="utf-8")
|
||||
site = py_dir / "Lib" / "site-packages"
|
||||
site.mkdir(parents=True, exist_ok=True)
|
||||
wheels = sorted((PACK / "wheels").glob("*.whl"))
|
||||
if not wheels:
|
||||
raise SystemExit("packaging/wheels is empty")
|
||||
installed = []
|
||||
for whl in wheels:
|
||||
with zipfile.ZipFile(whl) as zf:
|
||||
zf.extractall(site)
|
||||
installed.append(whl.name)
|
||||
return installed
|
||||
|
||||
|
||||
def write_cwd_runner(stage: Path) -> None:
|
||||
# tiny launcher helper used by bats: ensure CWD is app home
|
||||
(stage / "app_home.txt").write_text(
|
||||
"Portable root. Run Start.bat from this folder.\n", encoding="utf-8"
|
||||
)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if STAGE.exists():
|
||||
shutil.rmtree(STAGE)
|
||||
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
||||
STAGE.mkdir(parents=True)
|
||||
|
||||
# 1. application code (templates rewritten offline)
|
||||
rewritten = copy_app(ROOT / "app", STAGE / "app")
|
||||
# 2. offline bootstrap vendor
|
||||
vendor_dst = STAGE / "app" / "static" / "vendor"
|
||||
vendor_dst.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(PACK / "vendor" / "bootstrap.min.css", vendor_dst / "bootstrap.min.css")
|
||||
shutil.copy2(PACK / "vendor" / "bootstrap.bundle.min.js", vendor_dst / "bootstrap.bundle.min.js")
|
||||
# 3. active document templates only
|
||||
templates_copied = copy_templates_docs(ROOT / "app" / "templates_docs",
|
||||
STAGE / "app" / "templates_docs")
|
||||
# 4. runtime
|
||||
wheels = setup_python(STAGE)
|
||||
# 5. scripts + launcher helpers
|
||||
for name in ("Start.bat", "Stop.bat", "Backup.bat", "README.md"):
|
||||
src = PACK / name
|
||||
dst = STAGE / name
|
||||
# cmd.exe requires CRLF; keep UTF-8 (no BOM is fine for ASCII; BOM for safety)
|
||||
raw = src.read_bytes()
|
||||
if raw.startswith(b"\xef\xbb\xbf"):
|
||||
raw = raw[3:]
|
||||
text = raw.decode("utf-8")
|
||||
text = text.replace("\r\n", "\n").replace("\n", "\r\n")
|
||||
# UTF-8 BOM helps cmd.exe render Cyrillic after chcp 65001
|
||||
dst.write_bytes(b"\xef\xbb\xbf" + text.encode("utf-8"))
|
||||
scripts_dst = STAGE / "scripts"
|
||||
scripts_dst.mkdir(parents=True, exist_ok=True)
|
||||
for py in sorted((PACK / "scripts").glob("*.py")):
|
||||
shutil.copy2(py, scripts_dst / py.name)
|
||||
# 6. empty data + backups placeholders (created empty — no user data)
|
||||
(STAGE / "data").mkdir(exist_ok=True)
|
||||
(STAGE / "data" / "uploads").mkdir(exist_ok=True)
|
||||
(STAGE / "data" / "storage").mkdir(exist_ok=True)
|
||||
(STAGE / "backups").mkdir(exist_ok=True)
|
||||
(STAGE / "data" / ".keep").write_text("", encoding="utf-8")
|
||||
(STAGE / "backups" / ".keep").write_text("", encoding="utf-8")
|
||||
write_cwd_runner(STAGE)
|
||||
|
||||
# sanity: no user data inside stage
|
||||
leaked = []
|
||||
for p in STAGE.rglob("*"):
|
||||
rel = p.relative_to(STAGE).as_posix()
|
||||
if p.is_file() and (
|
||||
rel.endswith("work_tracker.db") or "verify_upload" in rel
|
||||
or rel.startswith("app/uploads/") or "/daily_reports/" in rel
|
||||
and rel.startswith("data/")
|
||||
):
|
||||
leaked.append(rel)
|
||||
if leaked:
|
||||
raise SystemExit(f"user data leaked into package: {leaked[:5]}")
|
||||
|
||||
# 7. zip
|
||||
if ZIP_PATH.exists():
|
||||
ZIP_PATH.unlink()
|
||||
with zipfile.ZipFile(ZIP_PATH, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
||||
for path in sorted(STAGE.rglob("*")):
|
||||
if path.is_file():
|
||||
zf.write(path, path.relative_to(STAGE.parent).as_posix())
|
||||
|
||||
size = ZIP_PATH.stat().st_size
|
||||
count = sum(1 for p in STAGE.rglob("*") if p.is_file())
|
||||
print("ZIP:", ZIP_PATH)
|
||||
print("SIZE:", size)
|
||||
print("FILES_IN_STAGE:", count)
|
||||
print("CDN_REWRITES:", rewritten)
|
||||
print("WHEELS:", len(wheels))
|
||||
print("TEMPLATES:", templates_copied)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user