"""Build a portable SteamBase distribution with embedded Python. python tools/build_portable.py python tools/build_portable.py --python-zip python-3.13.14-embed-amd64.zip python tools/build_portable.py --out dist/SteamBase-portable --no-zip The result contains app/ (SteamBase), python/ (embeddable Python), start_web.bat, update.bat and a short README. The database, cache and data are created on first run and are not included in the archive. """ import argparse import os import shutil import sys import tempfile import urllib.request import zipfile for stream in (sys.stdout, sys.stderr): try: stream.reconfigure(encoding="utf-8") except Exception: pass BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEFAULT_OUT = os.path.join(BASE_DIR, "dist", "SteamBase-portable") APP_DIRS = ("tools", "web", "docs") ROOT_FILES = ("README.md", "CHANGELOG.md", "LICENSE", "VERSION", "requirements-hltb.txt") SKIP_DIRS = {"__pycache__", ".git", ".gitea", "tests", "dist", "cache", "data", "build", ".venv", "venv", "snapshots", "assets"} SKIP_FILES = {"steam.db", "steam.db-wal", "steam.db-shm", "steam_library.csv", "snapshot.zip", ".env"} START_BAT = """@echo off setlocal cd /d "%~dp0app" rem Stop the previous server (PID file, then port) and start a new one. rem Pass a port to override the default: start_web.bat 9000 "..\\python\\python.exe" tools\\restart_server.py %* if errorlevel 1 pause """ UPDATE_BAT = """@echo off setlocal cd /d "%~dp0app" "..\\python\\python.exe" tools\\import_steam.py if errorlevel 1 goto :error "..\\python\\python.exe" tools\\fetch_details.py --limit 200 if errorlevel 1 goto :error "..\\python\\python.exe" tools\\cache_covers.py --all if errorlevel 1 goto :error echo. echo Done. Start the UI with start_web.bat exit /b 0 :error echo. echo Something failed - see the message above. pause exit /b 1 """ PORTABLE_README = """SteamBase portable ================== 1. Run update.bat once: imports the library, loads descriptions and covers. 2. Run start_web.bat: opens http://127.0.0.1:8765/. Run it again at any time to stop the previous server and restart it. You can pass a port: start_web.bat 9000 If the requested port is still busy, the next free port is used. The database is app\\steam.db; cache is app\\cache; settings are app\\data. HLTB and WebP previews are not included in the portable build by default; the rest of the application works with the standard library only. Server upload: open "Сервисы" -> "Сервер" and set URL, handle and upload token. The token is stored locally in app\\data\\server.json. Do not move files out of app/; the launchers expect this layout. """ def embed_url(version): return (f"https://www.python.org/ftp/python/{version}/" f"python-{version}-embed-amd64.zip") def download_python(version, dest): url = embed_url(version) print(f"Downloading {url}") request = urllib.request.Request(url, headers={"User-Agent": "SteamBase-portable"}) with urllib.request.urlopen(request, timeout=120) as response, \ open(dest, "wb") as handle: shutil.copyfileobj(response, handle) return dest def copy_tree(src, dst, skip_dirs=SKIP_DIRS, skip_files=SKIP_FILES): for root, dirs, files in os.walk(src): dirs[:] = [name for name in dirs if name not in skip_dirs] rel = os.path.relpath(root, src) target_root = os.path.join(dst, rel) if rel != "." else dst os.makedirs(target_root, exist_ok=True) for name in files: if name in skip_files or name.endswith(".pyc"): continue shutil.copy2(os.path.join(root, name), os.path.join(target_root, name)) def write_text(path, text): with open(path, "w", encoding="utf-8", newline="\r\n") as handle: handle.write(text) def build(out_dir=DEFAULT_OUT, python_zip=None, python_version=None, make_zip=True, force=False, download=True): out_dir = os.path.abspath(out_dir) if os.path.exists(out_dir): if not force: raise SystemExit(f"Output exists: {out_dir} (use --force)") shutil.rmtree(out_dir) app_dir = os.path.join(out_dir, "app") py_dir = os.path.join(out_dir, "python") os.makedirs(app_dir, exist_ok=True) os.makedirs(py_dir, exist_ok=True) for name in APP_DIRS: source = os.path.join(BASE_DIR, name) if os.path.isdir(source): copy_tree(source, os.path.join(app_dir, name)) for name in ROOT_FILES: source = os.path.join(BASE_DIR, name) if os.path.isfile(source): shutil.copy2(source, os.path.join(app_dir, name)) os.makedirs(os.path.join(app_dir, "data"), exist_ok=True) os.makedirs(os.path.join(app_dir, "cache", "covers"), exist_ok=True) temp_zip = None try: source_zip = python_zip if not source_zip: if not download: raise SystemExit("--python-zip is required with --no-download") version = python_version or ( f"{sys.version_info.major}.{sys.version_info.minor}." f"{sys.version_info.micro}") handle, temp_zip = tempfile.mkstemp(prefix="python-embed-", suffix=".zip") os.close(handle) source_zip = download_python(version, temp_zip) with zipfile.ZipFile(source_zip) as archive: names = archive.namelist() if "python.exe" not in names: raise SystemExit(f"Not an embeddable Python zip: {source_zip}") archive.extractall(py_dir) finally: if temp_zip and os.path.exists(temp_zip): os.unlink(temp_zip) write_text(os.path.join(out_dir, "start_web.bat"), START_BAT) write_text(os.path.join(out_dir, "update.bat"), UPDATE_BAT) write_text(os.path.join(out_dir, "README-portable.txt"), PORTABLE_README) archive_path = None if make_zip: archive_path = out_dir + ".zip" if os.path.exists(archive_path): os.unlink(archive_path) with zipfile.ZipFile(archive_path, "w", zipfile.ZIP_DEFLATED) as archive: for root, dirs, files in os.walk(out_dir): dirs[:] = [name for name in dirs if name not in SKIP_DIRS] for name in files: full = os.path.join(root, name) rel = os.path.relpath(full, out_dir) archive.write(full, rel) return out_dir, archive_path def main(): ap = argparse.ArgumentParser(description="Build a portable SteamBase") ap.add_argument("--out", default=DEFAULT_OUT) ap.add_argument("--python-zip", help="local python-*-embed-amd64.zip") ap.add_argument("--python-version", help="override embeddable Python version") ap.add_argument("--no-zip", dest="make_zip", action="store_false", help="only create the folder, not the .zip") ap.add_argument("--no-download", dest="download", action="store_false", help="do not download Python automatically") ap.add_argument("--force", action="store_true", help="replace existing output") args = ap.parse_args() out_dir, archive = build( out_dir=args.out, python_zip=args.python_zip, python_version=args.python_version, make_zip=args.make_zip, force=args.force, download=args.download) print(f"Portable folder: {out_dir}") if archive: print(f"Archive: {archive} ({os.path.getsize(archive)} bytes)") return 0 if __name__ == "__main__": raise SystemExit(main())