ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.
Python ai-agentsamdcomfyuidockerllama-cppllmlocal-ain8nnvidiaopen-webuiragself-hostedspeech-to-textstrix-halotext-to-speechworkflow-automation
#!/usr/bin/env python3
"""Remove transient build paths from an installed release venv.
The release is assembled in a per-process staging directory and atomically renamed to
its final versioned path. Python venvs otherwise retain the staging path in generated
scripts, activation helpers, pyvenv.cfg, and bytecode. That both breaks generated entry
points after the rename and makes an exact retained-release rebuild non-reproducible.
This helper is intentionally narrow: it deletes generated bytecode, rewrites only the
known venv-generated text surfaces to the final path, repairs the affected wheel RECORD
entries, and then rejects any remaining staging-path byte anywhere in the venv.
"""
from __future__ import annotations
import base64
import csv
import hashlib
import io
import os
from pathlib import Path
import stat
import sys
import tempfile
from typing import NoReturn
def die(message: str) -> "NoReturn":
raise SystemExit(f"normalize-release-venv: {message}")
def safe_regular(path: Path, root: Path, label: str) -> os.stat_result:
try:
info = path.lstat()
except OSError as exc:
die(f"{label} could not be inspected: {exc}")
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
die(f"{label} is not a single-link regular file")
# Match the release-tree verifier: owner custody and no world-write bit. Some
# qualified hosts intentionally build with a collaborative owner-group umask.
if os.name != "nt" and (info.st_uid != os.geteuid() or stat.S_IMODE(info.st_mode) & 0o002):
die(f"{label} has unsafe ownership or mode")
try:
path.relative_to(root)
except ValueError:
die(f"{label} is outside the venv")
return info
def atomic_rewrite(path: Path, payload: bytes, mode: int) -> None:
descriptor, temporary_name = tempfile.mkstemp(prefix=f".{path.name}.normalize-", dir=path.parent)
temporary = Path(temporary_name)
try:
with os.fdopen(descriptor, "wb") as handle:
handle.write(payload)
handle.flush()
os.fsync(handle.fileno())
os.chmod(temporary, mode)
os.replace(temporary, path)
directory = os.open(path.parent, os.O_RDONLY | getattr(os, "O_DIRECTORY", 0))
try:
os.fsync(directory)
finally:
os.close(directory)
finally:
temporary.unlink(missing_ok=True)
def record_digest(payload: bytes) -> str:
encoded = base64.urlsafe_b64encode(hashlib.sha256(payload).digest()).rstrip(b"=").decode("ascii")
return f"sha256={encoded}"
def lexical_record_target(record: Path, value: str, root: Path) -> Path:
if not value or "\x00" in value or "\\" in value or value.startswith("/"):
die("wheel RECORD contains an unsafe path")
candidate = Path(os.path.normpath(record.parent / value))
try:
candidate.relative_to(root)
except ValueError:
die("wheel RECORD path escapes the venv")
return candidate
def repair_records(root: Path, modified: set[Path]) -> None:
for record in sorted(root.glob("lib/python*/site-packages/*.dist-info/RECORD")):
info = safe_regular(record, root, "wheel RECORD")
with record.open("r", encoding="utf-8", newline="") as handle:
source = handle.read()
rows = list(csv.reader(io.StringIO(source, newline="")))
changed = False
for row in rows:
if len(row) != 3:
die("wheel RECORD row is malformed")
# Wheel RECORD paths are relative to the site-packages directory, not
# to the containing .dist-info directory.
target = lexical_record_target(record.parent, row[0], root)
if target not in modified:
continue
target_info = safe_regular(target, root, "rewritten venv file")
payload = target.read_bytes()
row[1] = record_digest(payload)
row[2] = str(len(payload))
if target_info.st_size == len(payload):
changed = True
if changed:
output = io.StringIO(newline="")
csv.writer(output, lineterminator="\n").writerows(rows)
atomic_rewrite(record, output.getvalue().encode("utf-8"), stat.S_IMODE(info.st_mode))
def normalize(venv: Path, final_venv: Path) -> dict[str, int]:
if not venv.is_absolute() or not final_venv.is_absolute():
die("venv and final venv paths must be absolute")
if venv.is_symlink() or not venv.is_dir():
die("venv must be a real directory")
root = Path(os.path.abspath(venv))
transient = os.fsencode(root)
final = os.fsencode(os.path.abspath(final_venv))
if transient == final:
die("staging and final venv paths must differ")
if b"\n" in final or b"\x00" in final:
die("final venv path contains an unsafe byte")
removed_bytecode = 0
for path in sorted(root.rglob("*.pyc")):
safe_regular(path, root, "generated bytecode")
path.unlink()
removed_bytecode += 1
for directory in sorted(root.rglob("__pycache__"), key=lambda item: len(item.parts), reverse=True):
if directory.is_symlink() or not directory.is_dir():
die("bytecode cache is not a real directory")
try:
directory.rmdir()
except OSError as exc:
die(f"bytecode cache contains an unexpected retained entry: {exc}")
candidates = [root / "pyvenv.cfg"]
candidates.extend(root.glob("bin/activate*"))
candidates.extend(root.glob("bin/*"))
modified: set[Path] = set()
for path in sorted(set(candidates)):
if path.is_symlink() or not path.exists() or path.is_dir():
continue
info = safe_regular(path, root, "venv-generated file")
payload = path.read_bytes()
if transient not in payload:
continue
if path.parent == root / "bin" and not path.name.startswith("activate") and not payload.startswith(b"#!"):
die("transient path appeared in an unexpected venv binary")
rewritten = payload.replace(transient, final)
atomic_rewrite(path, rewritten, stat.S_IMODE(info.st_mode))
modified.add(path)
repair_records(root, modified)
for directory, names, filenames in os.walk(root, followlinks=False):
names.sort()
filenames.sort()
for name in [*names, *filenames]:
path = Path(directory) / name
if path.is_symlink() and transient in os.fsencode(os.readlink(path)):
die(f"transient staging path remains in symlink {path.relative_to(root)}")
for filename in filenames:
path = Path(directory) / filename
if path.is_symlink():
continue
safe_regular(path, root, "normalized venv file")
if transient in path.read_bytes():
die(f"transient staging path remains in {path.relative_to(root)}")
if path.suffix == ".pyc":
die("generated bytecode remained after normalization")
return {"rewrittenFiles": len(modified), "removedBytecodeFiles": removed_bytecode}
def main() -> None:
if len(sys.argv) != 3:
die("usage: normalize-release-venv.py VENV FINAL_VENV")
result = normalize(Path(sys.argv[1]), Path(sys.argv[2]))
print(f"normalized venv: rewritten={result['rewrittenFiles']} bytecode_removed={result['removedBytecodeFiles']}")
if __name__ == "__main__":
main()