steam-backlog-enforcer/steam_installer/proton_native.py
Krzysztof kuhy Rudnicki a76e726e36 Merge steam-game-installer into the repo
The installer ships as the steam_installer package beside
steam_backlog_enforcer, with its tests, docs and Proton lock. The
enforcer's fast install now starts the installer from this checkout.

Both appinfo.vdf readers share one key-value decoder
(steam_installer.appinfo_kv), and the duplicated test fixtures and
helpers are folded into conftest and _cli_helpers so the whole-tree
duplicate-code check is clean. check_requirements_sync treats sibling
packages as first-party.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZCxnNWLtpVwFLVk9w
2026-11-22 20:25:39 +01:00

187 lines
7.0 KiB
Python

"""Install the pinned proton-cachyos-native build into Steam's compatibilitytools.d.
python -m steam_installer.proton_native # what install_deps.sh runs
python -m steam_installer.proton_native --check # exit 0 when in place
The package comes straight from the CachyOS mirror: the `proton-cachyos-native`
entry of `cachyos.db`, its signature and sha256 checked (see :mod:`proton_pkg`),
and only `usr/share/steam/compatibilitytools.d/proton-cachyos-native/` taken
out of it. Its `.INSTALL` is never run. It is a WoW64 build that needs no
lib32 packages, and unlike the `-slr` variant its toolmanifest requires no
Steam Linux Runtime -- which is the point.
A `VERSION` stamp equal to the lock means there is nothing to do and no
network is touched. Otherwise the new build is extracted next to the old one
and swapped in by rename -- never while Steam or a game from the tool runs,
since a game would lose its files mid-run. A copy already on disk with no
stamp is adopted as is when every file matches the package's `.MTREE`.
`SGI_PROTON_REPO` points the fetch at another copy of the repo db (a local
test mirror); the pinned key and sha256 still decide what is accepted.
"""
from __future__ import annotations
import dataclasses
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import urllib.request
from steam_installer import proton_pkg, recognize
from steam_installer.library import find_steam_root
from steam_installer.proton_pkg import FINGERPRINT, KEY, Lock, PkgError
TOOL_NAME = "proton-cachyos-native"
# On PATH, as ever (see steamctl.STEAM on why a name and not a literal).
GPGCONF = "gpgconf"
# A generic agent: the mirror answers 403 to urllib's default one.
USER_AGENT = "steam-game-installer"
def in_use(tool: Path) -> list[int]:
"""Steam client pids plus every process running from the tool folder."""
pids = list(recognize.steam_client_pids())
root = tool.resolve()
for entry in recognize.PROC_DIR.iterdir():
if not entry.name.isdigit():
continue
try:
exe = (entry / "exe").resolve(strict=True)
except OSError:
continue
if exe.is_relative_to(root):
pids.append(int(entry.name))
return pids
def _download(url: str, dest: Path) -> None:
# A generic agent: the mirror answers 403 to urllib's default one.
request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with (
urllib.request.urlopen(request, timeout=60) as response,
dest.open("wb") as out,
):
shutil.copyfileobj(response, out, 1 << 20)
def _fetch_verified(lock: Lock, work: Path, key: Path, fingerprint: str) -> Path:
"""Download the pinned package and prove it, or raise PkgError."""
db = work / "repo.db"
_download(lock.repo, db)
entry = proton_pkg.find_in_db(db, lock.name)
if entry.version != lock.version:
msg = (
f"the repo serves {lock.name} {entry.version} but the lock pins "
f"{lock.version}: bump steam_installer/data/proton-native.lock "
"(version and sha256)"
)
raise PkgError(msg)
if entry.sha256 != lock.sha256:
msg = "the repo's sha256 for the pinned version differs from the lock"
raise PkgError(msg)
package = work / entry.filename
url = proton_pkg.package_url(lock, entry.filename)
_download(url, package)
sig = work / f"{entry.filename}.sig"
_download(f"{url}.sig", sig)
home = work / "gnupg"
home.mkdir(mode=0o700)
try:
proton_pkg.verify_signature(package, sig, key, fingerprint, home)
finally:
subprocess.run(
[GPGCONF, "--homedir", str(home), "--kill", "all"],
check=False,
capture_output=True,
)
if proton_pkg.sha256_file(package) != lock.sha256:
msg = "the downloaded package's sha256 differs from the pinned sha256"
raise PkgError(msg)
return package
def _stamp(tool: Path, version: str) -> None:
(tool / "VERSION").write_text(version + "\n")
def _swap(package: Path, tools_dir: Path, tool: Path, version: str) -> None:
new = tools_dir / f".{TOOL_NAME}.new"
old = tools_dir / f".{TOOL_NAME}.old"
for leftover in (new, old):
shutil.rmtree(leftover, ignore_errors=True)
new.mkdir()
proton_pkg.extract(package, new)
if tool.exists():
tool.rename(old)
new.rename(tool)
shutil.rmtree(old, ignore_errors=True)
_stamp(tool, version)
def ensure(
tools_dir: Path, lock: Lock, *, key: Path = KEY, fingerprint: str = FINGERPRINT
) -> str:
"""Make `tools_dir/proton-cachyos-native` the pinned build; return what happened."""
tool = tools_dir / TOOL_NAME
stamp = tool / "VERSION"
current = stamp.read_text().strip() if stamp.is_file() else None
if current == lock.version:
return f"current: {lock.version}"
tools_dir.mkdir(parents=True, exist_ok=True)
work = Path(tempfile.mkdtemp(dir=tools_dir, prefix=".proton-download."))
try:
package = _fetch_verified(lock, work, key, fingerprint)
checks = f"signature by {fingerprint} and sha256 both passed"
if not tool.exists():
verb = f"installed: {lock.version}"
elif current is not None:
verb = f"updated: {current} -> {lock.version}"
else:
entries = proton_pkg.read_mtree(package)
mismatch = proton_pkg.first_mismatch(tool, entries)
if mismatch is None:
_stamp(tool, lock.version)
count = sum(entry.kind != "dir" for entry in entries.values())
return f"adopted: all {count} files match .MTREE ({checks})"
verb = f"replaced: mismatch in {mismatch}"
busy = in_use(tool) if tool.exists() else []
if busy:
return (
"deferred: Steam or a game is running "
f"(pid {', '.join(map(str, busy))}); rerun with Steam closed"
)
_swap(package, tools_dir, tool, lock.version)
return f"{verb} ({checks})"
finally:
shutil.rmtree(work, ignore_errors=True)
def main(argv: list[str] | None = None) -> int:
"""Install the pinned build; with `--check`, only say whether it is in place."""
lock = proton_pkg.read_lock()
if os.environ.get("SGI_PROTON_REPO"):
lock = dataclasses.replace(lock, repo=os.environ["SGI_PROTON_REPO"])
try:
tools_dir = find_steam_root() / "compatibilitytools.d"
except FileNotFoundError:
sys.stdout.write("native Proton skipped: no Steam installation\n")
return 0
if argv and argv[0] == "--check":
stamp = tools_dir / TOOL_NAME / "VERSION"
return 0 if stamp.is_file() and stamp.read_text().strip() == lock.version else 1
try:
status = ensure(tools_dir, lock)
except (PkgError, OSError) as exc:
sys.stderr.write(f"error: native Proton: {exc}\n")
return 1
sys.stdout.write(f"native Proton {status}\n")
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))