steam-backlog-enforcer/_installer_import/steam_installer/finalize.py
Krzysztof kuhy Rudnicki 15f647f639 Install any game from an appid, a URL or a name; adopt Steam's partial downloads
`./run.sh <anything>` is now the entry point. It installs its own dependencies
(steamcmd via yay, DepotDownloader from the SteamRE release, a local .venv) and
works out what the argument is: an appid, a Steam URL, an exact title, or part
of a title. Every route is confirmed through steamcmd's own app_info before
anything acts on it.

Name search moves to steamcommunity.com/actions/SearchApps, which works, with
the storefront API kept as a fallback -- it TLS-rejects scripted clients here,
so search had been effectively dead. DNS on this box fails transiently about one
lookup in three, so each backend is retried before giving up. An exact title now
wins over Steam's own ranking, which puts "Portal 2" above "Portal"; an
ambiguous name shows a picker, or exits 2 rather than guessing.

Two problems found while building this, both now handled:

Anonymous steamcmd has no licence for a paid app and does not say so. It
transfers nothing, prints "Success! App '284160' fully installed.", exits 0, and
writes StateFlags=4 with SizeOnDisk=0 and no depots. A running Steam client then
reads that, decides the app is installed but stale, and downloads the whole game
by itself. `ghost.py` detects that state -- validated read-only against all ten
manifests in a real library, where the nine genuine installs are untouched and
only the fabricated one matches -- and `--repair-ghosts` clears it away. Repair
uses rmdir, never rmtree, so a non-empty directory survives a wrong diagnosis.

And Steam's own in-progress downloads are no longer wasted. `takeover.py`
hardlinks `steamapps/downloading/<appid>/` into the install directory before
depotdownloader runs; depotdownloader hash-checks what is already on disk
(ContentDownloader.cs skips a file when neededChunks is empty) and fetches only
the remainder. Nothing is deleted up front -- the staging tree is cleared only
after the install is verified -- so a failed run still leaves Steam able to
resume. The trees share inodes, so an in-place chunk patch corrects both.

cli.py drops from 311 to 244 lines, back under the repo's 250-line cap, by
moving the install flow to flow.py and the manifest finish to finalize.py.

install.sh had a latent bug: `pip install -e .[dev]` only warns about a missing
extra and exits 0, so the `|| fallback` that installed pytest and coverage never
ran. A fresh venv had no test tools at all; the existing one only worked because
they had been installed by hand earlier. Now named outright and asserted.

246 tests, 100% statement and branch coverage. Verified live: all four input
forms resolve to 284160, an exact "Portal" resolves to 400 and not 620, an
ambiguous name exits 2, ghost detection flags nothing on nine real installs, and
a from-scratch venv bootstraps and runs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sgkb3wH6MU9fYquX328ovT
2026-09-11 21:38:16 +02:00

93 lines
3.5 KiB
Python

"""Hand-finish the appmanifest, but only when every precondition really holds.
Split out of cli.py so the entry point stays under the repo's 250-line cap.
The logic is unchanged: it patches a manifest steamcmd or Steam already
wrote, and refuses rather than fabricating one.
"""
from __future__ import annotations
import time
from pathlib import Path
from steam_installer import appinfo, recognize, runlog
from steam_installer.library import SteamLibrary
def finish(
log: runlog.RunLog,
library: SteamLibrary,
metadata: appinfo.AppMetadata,
target: Path,
depot_plan: appinfo.DepotPlan,
expected_build_id: str | None,
skipped_steamcmd: bool,
) -> int:
logger = log.logger
manifest_path = library.steamapps / f"appmanifest_{metadata.appid}.acf"
if not manifest_path.is_file():
# Reachable two ways, so don't blame steamcmd unconditionally: under
# --skip-steamcmd it never runs at all, and nothing else in this tool
# creates a manifest from scratch.
logger.error(
"No %s exists, so there is nothing to finish. This tool only ever "
"patches a manifest Steam or steamcmd already wrote; it never "
"fabricates one. %s",
manifest_path,
(
"Re-run without --skip-steamcmd so steamcmd creates it (it writes "
"one even when it crashes)."
if skipped_steamcmd
else "steamcmd writes one even when it crashes, so its absence "
"means steamcmd never ran far enough."
),
)
logger.info("Run log (send this): %s", log.path)
return 1
app_state = recognize.read_app_state(manifest_path)
problems = recognize.check_ready_to_finish(
install_dir=target,
app_state=app_state,
required_depots=depot_plan.required,
expected_build_id=expected_build_id,
)
if problems:
# Fail closed. Writing StateFlags=4 here is what previously turned a
# steamcmd run that did nothing at all into a reported success.
logger.error("Refusing to mark %s as installed:", metadata.name)
for problem in problems:
logger.error(" - %s", problem)
logger.info("Run log (send this): %s", log.path)
return 1
# Do this before writing the manifest: a StateFlags=4 manifest alongside a
# staging tree full of pending chunks is the same contradiction that made
# Steam resume the download last time. By this point depotdownloader has
# already filled common/, so the staged copies are genuinely redundant.
freed = recognize.clear_steam_staging(library, metadata.appid)
if freed:
logger.info("Cleared %.1f GB of now-redundant Steam staging data.", freed / 1e9)
written = recognize.finish_manifest_manually(
library=library,
appid=metadata.appid,
install_dir=target,
now_unix=int(time.time()),
)
log.file_contents("Manifest after manual finish", written)
# Re-read from disk: claiming success without checking the write landed is
# the same fail-open mistake in a different place.
if not recognize.manifest_reflects_real_install(library, metadata.appid, target):
logger.error(
"Wrote %s but it still does not read back as a real install.", written
)
logger.info("Run log (send this): %s", log.path)
return 1
logger.info("Wrote %s -- launch Steam and confirm Play works.", written)
logger.info("Run log: %s", log.path)
return 0