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
This commit is contained in:
Krzysztof Rudnicki 2026-11-22 20:25:39 +01:00
parent 14fc070272
commit a76e726e36
135 changed files with 4847 additions and 3372 deletions

View File

@ -32,10 +32,11 @@ jobs:
# requirements.txt); pytest-cov merges the workers' coverage.
- name: Run tests with coverage
run: |
python -m pytest steam_backlog_enforcer/tests/ \
python -m pytest steam_backlog_enforcer/tests/ steam_installer/tests/ \
-n auto \
-p no:sugar \
--cov=steam_backlog_enforcer \
--cov=steam_installer \
--cov-branch \
--cov-fail-under=100 \
-q

View File

@ -18,6 +18,8 @@ repos:
hooks:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
# The pinned signing key is byte-for-byte what its source served.
exclude: ^steam_installer/data/
- id: end-of-file-fixer
- id: check-yaml
# pnpm 12 writes pnpm-lock.yaml as two YAML documents (the pinned
@ -33,7 +35,7 @@ repos:
- id: debug-statements
- id: name-tests-test
args: [--pytest-test-first]
exclude: ^steam_backlog_enforcer/tests/_[a-z_]+\.py$
exclude: ^steam_(backlog_enforcer|installer)/tests/_[a-z_]+\.py$
- id: check-ast
- id: check-builtin-literals
- id: check-docstring-first
@ -63,10 +65,16 @@ repos:
types: [text]
- id: requirements-sync
name: requirements.txt covers all package imports
entry: python3 scripts/check_requirements_sync.py
entry: python3 scripts/check_requirements_sync.py --package steam_backlog_enforcer
language: system
pass_filenames: false
files: ^(steam_backlog_enforcer/.*\.py|requirements\.txt)$
- id: requirements-sync-installer
name: requirements.txt covers the installer's imports
entry: python3 scripts/check_requirements_sync.py --package steam_installer
language: system
pass_filenames: false
files: ^(steam_installer/.*\.py|requirements\.txt)$
# ===========================================================================
# RUFF - Linter + formatter
@ -114,6 +122,7 @@ repos:
- --disable-error-code=list-item
additional_dependencies:
- types-requests
- kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit
- earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.23.0#subdirectory=earned_time
- leisure-watch @ git+https://github.com/kuhyx/leisure-watch@leisure-watch-v0.2.9
@ -145,6 +154,7 @@ repos:
# Python 3.15 (removed co_lnotab). Same reason as jobs = 1 in pyproject.
- --jobs=1
- steam_backlog_enforcer
- steam_installer
- scripts/check_requirements_sync.py
- scripts/pylint_symilar_cache.py
# pylint resolves imports in its OWN isolated env, so anything the
@ -157,6 +167,7 @@ repos:
- earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.23.0#subdirectory=earned_time
- leisure-watch @ git+https://github.com/kuhyx/leisure-watch@leisure-watch-v0.2.9
- howlongtobeatpy==1.0.24
- kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit
- mcp==2.3.0
- pytest
- requests
@ -219,7 +230,7 @@ repos:
types: [python]
require_serial: true
stages: [manual]
files: ^(steam_backlog_enforcer/(?!tests/).*|scripts/(check_requirements_sync|pylint_symilar_cache))\.py$
files: ^((steam_backlog_enforcer|steam_installer)/(?!tests/).*|scripts/(check_requirements_sync|pylint_symilar_cache))\.py$
args: &pylint_per_file
- --rcfile=pyproject.toml
- --fail-under=10.0
@ -228,15 +239,15 @@ repos:
- <<: *pylint_shard
id: ci-mirror-pylint-tests-1
name: CI mirror pylint (tests helpers + test_[a-h], per-file checks)
files: ^steam_backlog_enforcer/tests/(?!test_[i-z]).*\.py$
files: ^steam_(backlog_enforcer|installer)/tests/(?!test_[i-z]).*\.py$
- <<: *pylint_shard
id: ci-mirror-pylint-tests-2
name: CI mirror pylint (test_[i-r], per-file checks)
files: ^steam_backlog_enforcer/tests/test_[i-r][^/]*\.py$
files: ^steam_(backlog_enforcer|installer)/tests/test_[i-r][^/]*\.py$
- <<: *pylint_shard
id: ci-mirror-pylint-tests-3
name: CI mirror pylint (test_[s-z], per-file checks)
files: ^steam_backlog_enforcer/tests/test_[s-z][^/]*\.py$
files: ^steam_(backlog_enforcer|installer)/tests/test_[s-z][^/]*\.py$
- &pylint_whole_tree
<<: *pylint_shard
id: ci-mirror-pylint-dup
@ -253,6 +264,7 @@ repos:
# duplicate-code speed-up) loud instead of silently slow.
- --enable=duplicate-code,bad-plugin-value
- steam_backlog_enforcer
- steam_installer
- scripts/check_requirements_sync.py
- scripts/pylint_symilar_cache.py
- <<: *pylint_whole_tree
@ -265,11 +277,12 @@ repos:
- --disable=all
- --enable=cyclic-import
- steam_backlog_enforcer
- steam_installer
- scripts/check_requirements_sync.py
- scripts/pylint_symilar_cache.py
- id: ci-mirror-pytest
name: CI mirror (clean-venv pytest, xdist, 100% coverage)
entry: bash scripts/ci_mirror.sh pytest steam_backlog_enforcer/tests/
entry: bash scripts/ci_mirror.sh pytest steam_backlog_enforcer/tests/ steam_installer/tests/
language: system
pass_filenames: false
always_run: true
@ -302,7 +315,7 @@ repos:
# sha512 read as a misspelling of "view".
- --skip=*.json,*.lock,*-lock.yaml,.git,__pycache__,.venv,node_modules
# `lazer` is osu!lazer's own name, not a misspelling of laser.
- --ignore-words-list=lazer,als,ans,ect,nd,som,sur,te,nam,numer,lew,sie,wil,postion,clen,ther,folow,derrive,ony,tje,noe,theses,crate,doubleclick,wile,tabel,pary,blok,bloc,proces,serwer,parametr,adres,hart,dout,metod,tekst,synonim,grup,mosty,lokal,skalar,milion,nowe,tre,hel,alph
- --ignore-words-list=fpr,lazer,als,ans,ect,nd,som,sur,te,nam,numer,lew,sie,wil,postion,clen,ther,folow,derrive,ony,tje,noe,theses,crate,doubleclick,wile,tabel,pary,blok,bloc,proces,serwer,parametr,adres,hart,dout,metod,tekst,synonim,grup,mosty,lokal,skalar,milion,nowe,tre,hel,alph
# ===========================================================================
# SHELLCHECK - Shell script linting
@ -370,7 +383,8 @@ repos:
|package\.json|package-lock\.json|pnpm-lock\.yaml
|Cargo\.(toml|lock)|go\.(mod|sum)
|\.fvmrc|\.nvmrc|\.python-version
|\.github/workflows/.*\.ya?ml)$
|\.github/workflows/.*\.ya?ml
|steam_installer/data/proton-native\.lock)$
- repo: local
hooks:

View File

@ -24,4 +24,5 @@ After running tests fix all coverage gaps and issues, do not ignore unless speci
- coverage: `python3 -m pytest -q`
- coverage-gaps: `coverage-gaps coverage.lcov`
- deploy: `scripts/deploy.sh`
- install-game: `steam_installer/run.sh <appid|url|name>` (the daemon itself runs `python3 -m steam_installer.cli`; docs in `DOCS-steam-installer.md`)
- push: `scripts/check_gate_stamp.sh full && git push` (every manual-stage gate hook, ~3 min, then push). Agents (60 s cap): one call per id of `scripts/check_gate_stamp.sh full --list`: `CAP_MEM=4G CAP_CPU_PCT=20 ~/.claude/scripts/capped.sh timeout 55 scripts/check_gate_stamp.sh step <id>` (each < 55 s; the last stamps the tree), then `git push`.

View File

@ -1,27 +1,27 @@
# steam-game-installer
# Steam game installer
Download an owned Steam game with `depotdownloader` (which saturates a link far
better than the Steam client), then make the Steam client accept the result as
a real install so **Play** works.
```bash
./run.sh BeamNG.drive
steam_installer/run.sh BeamNG.drive
```
That is the whole thing. `run.sh` installs its own dependencies (`libibus` via
pacman, DepotDownloader from the SteamRE release, a local `.venv` and the native
That is the whole thing. `steam_installer/run.sh` installs its own dependencies (`libibus` via
pacman, DepotDownloader from the SteamRE release and the native
Proton described below) on first use, then works out what you meant. It never
runs `steamcmd`: app info comes from Steam's own cache and the tool writes the
manifest itself. All four of these install the same game:
```bash
./run.sh 284160 # an AppID
./run.sh https://steamcommunity.com/app/284160/guides/ # any Steam URL
./run.sh BeamNG.drive # the exact name
./run.sh BeamNG # part of the name
steam_installer/run.sh 284160 # an AppID
steam_installer/run.sh https://steamcommunity.com/app/284160/guides/ # any Steam URL
steam_installer/run.sh BeamNG.drive # the exact name
steam_installer/run.sh BeamNG # part of the name
```
An exact title always wins, so `./run.sh Portal` installs Portal and not
An exact title always wins, so `steam_installer/run.sh Portal` installs Portal and not
Portal 2 — even though Steam's own search ranks the sequel first. An ambiguous
name shows a numbered picker, and in a non-interactive run it lists the
candidates and exits 2 rather than guessing.
@ -40,7 +40,7 @@ it with no Steam Linux Runtime container, and Proton Experimental, Steam Linux
Runtime 4.0 and the Steamworks Redistributables are not needed while only such
games are installed (steam-backlog-enforcer removes them then).
- **The tool.** `install.sh` downloads the exact build pinned in
- **The tool.** `steam_installer/install_deps.sh` downloads the exact build pinned in
`steam_installer/data/proton-native.lock` from the CachyOS mirror. It checks
the package's `.sig` against the CachyOS key pinned in
`steam_installer/data/cachyos_signing_key.asc` (fingerprint and sources in
@ -61,7 +61,7 @@ games are installed (steam-backlog-enforcer removes them then).
entry in `config/config.vdf` (`"name" "proton-cachyos-native"`, priority
250). The file is edited as text, and none of its other lines is ever logged.
An entry Valve chose (priority 100) is replaced; one you picked is kept.
- **Games already installed.** `./run.sh --map-installed` prints the decision
- **Games already installed.** `steam_installer/run.sh --map-installed` prints the decision
for every installed game and writes nothing; add `--apply` to close Steam and
write the safe ones. `--appids 730,1201270` decides any cached app instead.
@ -110,7 +110,7 @@ Steam records it.
## Which depotdownloader
Always `~/.local/bin/depotdownloader` (the copy `install.sh` manages) when it
Always `~/.local/bin/depotdownloader` (the copy `steam_installer/install_deps.sh` manages) when it
exists, never "whatever is first on PATH". Each depotdownloader binary keeps
its remembered login in its own storage slot, keyed by its path; with an AUR
copy in `/usr/bin` too, a terminal run and a `sudo`'d run (whose PATH lacks
@ -129,7 +129,7 @@ That manifest is not harmless: a running Steam client reads it, concludes the
app is installed but out of date, and downloads the entire game by itself.
```bash
./run.sh --repair-ghosts
steam_installer/run.sh --repair-ghosts
```
finds them across every library and clears them away — but only the *cold* ones.
@ -219,16 +219,14 @@ not game content and overstate the total by gigabytes.
## Development
The installer shares this repo's gate (ruff, mypy, pylint, bandit, 250-line cap)
and its test run, which enforces **100% of statements and branches** for both
packages (`fail_under = 100`):
```bash
./install.sh # also prepares the test env
.venv/bin/python -m pytest tests/ -q
ruff check steam_installer/ tests/
ruff format --check steam_installer/ tests/
python3 -m pytest -q # both suites, coverage gated
scripts/test_changed.sh # only the tests of changed files
```
Coverage is enforced at **100% of statements and branches**
(`fail_under = 100`), so an uncovered line fails the suite rather than quietly
lowering a number. One test runs `python -m steam_installer.cli` in a
subprocess to prove the `__main__` entry point works; recording coverage from
a child process needs a `.pth` that calls `coverage.process_startup()`, which
`install.sh` writes into the venv. Skip `install.sh` and coverage reports 99%.
The steam-backlog-enforcer daemon runs the installer from this checkout as
`python3 -m steam_installer.cli`; it needs no virtualenv.

View File

@ -1,5 +1,8 @@
# Steam Backlog Enforcer
This repo is also an alternative Steam frontend: it installs games faster than the
Steam client (`steam_installer`, see [DOCS-steam-installer.md](DOCS-steam-installer.md)).
Forces you to play one Steam game at a time: earn at least one new
achievement in the assigned game before moving on to the next.
@ -84,3 +87,11 @@ Claude Code spawns:
Registration lives in the checked-in [`.mcp.json`](./.mcp.json) (project scope).
Restart Claude Code in this repo and approve the project MCP-server prompt for it
to load. Verify with `claude mcp list`.
## Steam game installer
`steam_installer` (merged in from steam-game-installer) downloads an owned game
with depotdownloader and makes Steam accept it as installed. The daemon runs it
as `python3 -m steam_installer.cli`; by hand use `steam_installer/run.sh <appid|url|name>`.
Dependencies: `steam_installer/install_deps.sh` (run by `install.sh`). Details in
[DOCS-steam-installer.md](DOCS-steam-installer.md).

View File

@ -1,35 +0,0 @@
name: file length
# The shared 250-line cap, enforced on push as well as at commit time: the
# pre-commit hook only sees staged files and a fresh clone may never have run
# `pre-commit install`. This checks the whole tree.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
jobs:
file-length:
name: files at most 250 lines
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# The gate and its exemption list live in the shared utils repo.
- name: Check out the shared gate
uses: actions/checkout@v4
with:
repository: kuhyx/utils
path: .utils
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
# Tracked files only: --all would also walk the .utils checkout above,
# which is another repo's code and not this one's to split.
- name: Every file is at most 250 lines
run: git ls-files -z | xargs -0 bash .utils/scripts/check_file_length.sh

View File

@ -1,25 +0,0 @@
name: repo-contract
# The local pre-commit hook can be skipped (a fresh clone has no hooks), so the
# contract is enforced on push too. Mirrors md-naming.yml.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
jobs:
repo-contract:
name: repo contract
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check out the shared gate
uses: actions/checkout@v4
with:
repository: kuhyx/utils
path: .utils
- name: Enforce the repo contract
run: bash .utils/scripts/check_repo_contract.sh

View File

@ -1,8 +0,0 @@
.venv/
__pycache__/
*.pyc
.pytest_cache/
*.egg-info/
.coverage
.coverage.*
coverage.xml

View File

@ -1,84 +0,0 @@
# Pre-commit hooks for this repo.
#
# Created when the shared markdown-naming gate was rolled out. The checks
# are `language: system` because they are plain bash already on this
# machine; pre-commit must not build an isolated env for them.
default_install_hook_types: [pre-commit, post-commit, post-merge, post-rewrite]
repos:
- repo: local
hooks:
# Four markdown namespaces (README / CLAUDE* / DOCS* / TODO*), plus the
# rule that a TODO carries "REMOVE ME AFTER FINISH" and nothing else may.
# Shared gate in ~/src/utils owns the namespaces and the exemption list, so
# one repo's idea of a valid name cannot drift from every other repo's.
- id: md-naming
name: markdown naming convention
entry: scripts/check_md_naming.sh
language: system
files: \.(md|markdown)$
- id: todo-closer
name: close Todo trailer tasks
entry: scripts/todo_hooks.sh closer
language: system
stages: [post-commit]
always_run: true
pass_filenames: false
- id: todo-import-pr-merge
name: import PR Todo sections (pull)
entry: scripts/todo_hooks.sh import-pr post-merge
language: system
stages: [post-merge]
always_run: true
pass_filenames: false
- id: todo-import-pr-rewrite
name: import PR Todo sections (pull --rebase)
entry: scripts/todo_hooks.sh import-pr post-rewrite
language: system
stages: [post-rewrite]
always_run: true
pass_filenames: false
# Shared 250-line cap on every file in the commit, code and prose alike.
# Checks only what is staged, so an unrelated commit never goes red, and
# it fails rather than warns: no baseline, no allowlist.
- id: file-length
name: files at most 250 lines
entry: scripts/check_file_length.sh
language: system
types: [text]
# Every dependency on its ecosystem's newest STABLE release, exact-pinned
# so the question is mechanically answerable at all. That includes the
# native Proton build, pinned in proton-native.lock and checked against
# cachyos.db by the shared gate's pacman-repo adapter.
#
# `files:` is what keeps this cheap: a commit touching only source makes
# ZERO network requests, and manifest commits mostly hit a 6h disk cache.
#
# Runs WITHOUT --strict on purpose. Offline it degrades to the cached
# answer and passes with a loud banner; `--no-verify` is banned here, so
# a hook that hard-failed on a flaky connection would leave no way to
# commit at all. The strict run is local only (CI does not check
# freshness): scripts/check_dependency_freshness.sh --all --strict
- id: dependency-freshness
name: dependencies on newest stable
entry: scripts/check_dependency_freshness.sh
language: system
pass_filenames: true
files: >-
(pubspec\.(yaml|lock)|pyproject\.toml|requirements.*\.txt|uv\.lock
|package\.json|package-lock\.json|pnpm-lock\.yaml
|Cargo\.(toml|lock)|go\.(mod|sum)
|\.fvmrc|\.nvmrc|\.python-version
|\.github/workflows/.*\.ya?ml
|steam_installer/data/proton-native\.lock)$
- id: repo-contract
name: repo contract (CLAUDE.md Commands + test_changed)
entry: scripts/check_repo_contract.sh
language: system
pass_filenames: false
files: ^(CLAUDE\.md|AGENTS\.md|scripts/test_changed\.sh)$

View File

@ -1,9 +0,0 @@
## Commands
- run: `./run.sh <appid|url|name>`
- test: `python3 -m pytest -q`
- test-changed: `scripts/test_changed.sh`
- lint: `pre-commit run --all-files`
- coverage: `python3 -m pytest -q --cov --cov-branch --cov-report=xml`
- coverage-gaps: `coverage-gaps coverage.xml`
- deploy: `scripts/deploy.sh` (fast-forwards kuhy's checkout `~/src/steam-game-installer` to origin/main, refusing a dirty tree, reruns `./run.sh --deps-only`, then checks the CLI starts; idempotent, non-zero unless at origin/main and healthy; test: `tests/test_deploy.py`)

View File

@ -1,34 +0,0 @@
[project]
name = "steam-game-installer"
version = "0.1.0"
description = "Fast Steam game installs: depotdownloader for speed, a hand-written manifest for recognition, a FOSS Proton to run them."
requires-python = ">=3.14"
# kuhy's shared "Created by kuhy" credit, shown at the end of --help.
dependencies = [
"kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit",
]
[project.scripts]
steam-game-installer = "steam_installer.cli:main"
[build-system]
requires = ["setuptools>=68"]
build-backend = "setuptools.build_meta"
[tool.pytest.ini_options]
testpaths = ["tests"]
[tool.coverage.run]
source = ["steam_installer"]
branch = true
# The `__main__` guard is exercised by running the module as a script in a
# subprocess; without this its coverage would be recorded in a child process
# and thrown away.
parallel = true
concurrency = ["multiprocessing"]
[tool.coverage.report]
# The suite covers every line and branch. Anything less is a regression, so
# make it fail rather than merely reporting a lower number.
fail_under = 100
show_missing = true

View File

@ -1,89 +0,0 @@
#!/bin/bash
# ============================================================================
# Fail if any dependency in the commit is behind its ecosystem's newest stable.
#
# Thin delegate to the shared gate in ~/src/utils, which owns the registry
# adapters, the pre-release rules and the allowlist semantics. Copying that
# logic here is what lets one repo's idea of "current" drift from every other
# repo's -- so this script only locates the shared checker and forwards its
# arguments.
#
# Usage:
# scripts/check_dependency_freshness.sh <file> [<file> ...] # pre-commit
# scripts/check_dependency_freshness.sh --all --strict # full local run
#
# Exit: 0 current | 1 behind | 2 allowlist wrong | 3 undeterminable (offline)
# ============================================================================
set -euo pipefail
readonly UTILS="${UTILS_ROOT:-$HOME/src/utils}"
readonly SHARED_GATE="$UTILS/scripts/check_dependency_freshness.sh"
readonly PACMAN_PARSER="$UTILS/dep_freshness/parsers/pacman_lock.py"
# True when this run must judge the native-Proton lock (a full run, or the lock staged).
needs_pacman_adapter() {
local arg
for arg in "$@"; do
[[ "$arg" == "--all" || "$arg" == *proton-native.lock ]] && return 0
done
return 1
}
# Print a gate directory that can read the Proton lock, or nothing.
#
# kuhy's ~/src/utils can lag origin/main (local commits not yet pushed, so it
# cannot fast-forward). Then the gate runs a read-only snapshot of utils'
# origin/main from the cache instead: the checkout itself is never touched.
gate_root_for_lock() {
if [[ -f "$PACMAN_PARSER" ]]; then
echo "$UTILS"
return
fi
local sha snapshot
local -a paths=(scripts/check_dependency_freshness.sh dep_freshness)
git -C "$UTILS" cat-file -e origin/main:dep_freshness/parsers/pacman_lock.py 2>/dev/null ||
return 0
sha="$(git -C "$UTILS" rev-parse origin/main)"
snapshot="${XDG_CACHE_HOME:-$HOME/.cache}/steam-game-installer/utils-gate/$sha"
if [[ ! -f "$snapshot/dep_freshness/parsers/pacman_lock.py" ]]; then
rm -rf "$snapshot.tmp"
mkdir -p "$snapshot.tmp"
# The fleet allowlist lives beside the gate, when utils has one.
if git -C "$UTILS" cat-file -e origin/main:dependency-freshness.allowlist.yaml 2>/dev/null; then
paths+=(dependency-freshness.allowlist.yaml)
fi
git -C "$UTILS" archive origin/main "${paths[@]}" | tar -x -C "$snapshot.tmp"
rm -rf "$snapshot"
mv "$snapshot.tmp" "$snapshot"
fi
echo "dependency-freshness: $UTILS lacks the pacman adapter; using its origin/main ${sha:0:12}" >&2
echo "$snapshot"
}
main() {
if [[ ! -x "$SHARED_GATE" ]]; then
echo "Error: shared dependency-freshness gate not found at $SHARED_GATE" >&2
echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2
echo " UTILS_ROOT to where it lives." >&2
exit 1
fi
local root="$UTILS"
# An older utils checkout skips a manifest it has no parser for and exits
# 0, which would wave any Proton pin through. Fail closed instead.
if needs_pacman_adapter "$@"; then
root="$(gate_root_for_lock)"
if [[ -z "$root" ]]; then
echo "Error: $UTILS has no dep_freshness/parsers/pacman_lock.py, so it" >&2
echo " cannot check steam_installer/data/proton-native.lock." >&2
echo " Update it: git -C $UTILS pull --ff-only" >&2
exit 1
fi
fi
exec bash "$root/scripts/check_dependency_freshness.sh" "$@"
}
main "$@"

View File

@ -1,31 +0,0 @@
#!/bin/bash
# ============================================================================
# Fail if any file in the commit exceeds the shared 250-line cap.
#
# Thin delegate to the shared gate in ~/src/utils, which owns the cap and the
# exemption list (generated files, markup, data). Copying that logic here is
# how one repo's idea of "too long" drifts from every other repo's -- so this
# script only locates the shared checker and forwards its arguments.
#
# Usage:
# scripts/check_file_length.sh <file> [<file> ...] # pre-commit passes these
# scripts/check_file_length.sh --all # whole tree, from cwd
# ============================================================================
set -euo pipefail
readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_file_length.sh"
main() {
if [[ ! -x "$SHARED_GATE" ]]; then
echo "Error: shared file-length gate not found at $SHARED_GATE" >&2
echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2
echo " UTILS_ROOT to where it lives." >&2
exit 1
fi
exec bash "$SHARED_GATE" "$@"
}
main "$@"

View File

@ -1,32 +0,0 @@
#!/bin/bash
# ============================================================================
# Fail if any markdown file breaks the shared naming convention.
#
# Thin delegate to the shared gate in ~/src/utils, which owns the four namespaces
# (README / CLAUDE* / DOCS* / TODO*), the "REMOVE ME AFTER FINISH" marker rule
# and the exemption list. Copying that logic here is what lets one repo's idea
# of a valid name drift from every other repo's -- so this script only locates
# the shared checker and forwards its arguments.
#
# Usage:
# scripts/check_md_naming.sh <file> [<file> ...] # pre-commit passes these
# scripts/check_md_naming.sh --all # whole tree, from cwd
# ============================================================================
set -euo pipefail
readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_md_naming.sh"
main() {
if [[ ! -x "$SHARED_GATE" ]]; then
echo "Error: shared markdown-naming gate not found at $SHARED_GATE" >&2
echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2
echo " UTILS_ROOT to where it lives." >&2
exit 1
fi
exec bash "$SHARED_GATE" "$@"
}
main "$@"

View File

@ -1,26 +0,0 @@
#!/bin/bash
# ============================================================================
# Fail if this repo breaks the shared repo contract.
#
# Thin delegate to the shared gate in ~/src/utils, which owns the contract
# text. Copy this file to scripts/check_repo_contract.sh in a repo; never copy
# the shared gate itself, or the shim would exec itself forever.
# ============================================================================
set -euo pipefail
readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_repo_contract.sh"
main() {
if [[ ! -x "$SHARED_GATE" ]]; then
echo "Error: shared repo-contract gate not found at $SHARED_GATE" >&2
echo " Clone github.com/kuhyx/utils to ~/src/utils, or set" >&2
echo " UTILS_ROOT to where it lives." >&2
exit 1
fi
exec bash "$SHARED_GATE" "$@"
}
main "$@"

View File

@ -1,38 +0,0 @@
#!/bin/bash
# ============================================================================
# Deploy origin/main to where this tool runs: kuhy's checkout
# (~/src/steam-game-installer, used through ./run.sh). Refuses a dirty tree,
# fast-forwards it, reruns ./run.sh --deps-only (libibus, depotdownloader, the native Proton,
# editable .venv), then starts the CLI as the health check. Nothing is
# published elsewhere: no package, no service, no release.
# Idempotent; non-zero unless the checkout is at origin/main and the CLI starts.
# scripts/deploy.sh
# SGI_DEPLOY_DIR=/path another checkout (the test uses a fixture)
# ============================================================================
set -euo pipefail
readonly DIR="${SGI_DEPLOY_DIR:-$HOME/src/steam-game-installer}"
die() {
echo "deploy: FAILED ($1)" >&2
exit 1
}
main() {
[[ -d "$DIR/.git" ]] || die "no checkout at $DIR"
[[ -z "$(git -C "$DIR" status --porcelain)" ]] || die "$DIR has local changes"
git -C "$DIR" checkout -q main || die "cannot switch $DIR to main"
git -C "$DIR" pull -q --ff-only origin main || die "cannot fast-forward $DIR"
local head want
head="$(git -C "$DIR" rev-parse HEAD)"
want="$(git -C "$DIR" rev-parse origin/main)"
[[ "$head" == "$want" ]] || die "$DIR is at ${head:0:7}, origin/main is ${want:0:7}"
(cd "$DIR" && ./run.sh --deps-only) || die "dependency install failed"
"$DIR/.venv/bin/python" -m steam_installer.cli --help >/dev/null ||
die "health check: the CLI does not start"
echo "deploy: $DIR at ${head:0:7}, deps ok, CLI starts"
}
main "$@"

View File

@ -1,50 +0,0 @@
#!/bin/bash
# ============================================================================
# Run only the tests related to files changed vs HEAD (staged, unstaged and
# untracked). Quiet: failures plus a one-line summary. Falls back to the full
# suite when a change cannot be mapped to tests. Generated by repo_contract.
# ============================================================================
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"
CHANGED=()
while IFS= read -r f; do
[[ -n "$f" && -e "$f" ]] && CHANGED+=("$f")
done < <({ git diff --name-only HEAD 2>/dev/null || true; git ls-files --others --exclude-standard; } | sort -u)
pytest_run() {
if [[ -f uv.lock ]]; then uv run pytest "$@"
elif [[ -f poetry.lock ]]; then poetry run pytest "$@"
else python3 -m pytest "$@"; fi
}
if grep -qs testmon pyproject.toml requirements*.txt; then
pytest_run --testmon -q --tb=short; exit $?
fi
full=0
tests=()
for f in "${CHANGED[@]}"; do
case "$f" in
*/conftest.py | conftest.py | pyproject.toml | requirements*.txt | pytest.ini | setup.cfg) full=1 ;;
*.py)
base="$(basename "$f" .py)"
if [[ "$base" == test_* || "$base" == *_test ]]; then
tests+=("$f")
else
while IFS= read -r t; do tests+=("$t"); done \
< <(git ls-files "*test_${base}.py" "*${base}_test.py")
fi
;;
esac
done
if [[ $full -eq 0 && ${#CHANGED[@]} -eq 0 ]]; then echo "no changes vs HEAD: nothing to test"; exit 0; fi
if [[ $full -eq 0 && ${#tests[@]} -gt 0 ]]; then pytest_run -q --tb=short "${tests[@]}"; exit $?; fi
if [[ $full -eq 0 ]] && ! printf '%s\n' "${CHANGED[@]}" | grep -q '\.py$'; then
echo "no python changes: nothing to test"; exit 0
fi
echo "no mapped tests: running full suite"
pytest_run -q --tb=short

View File

@ -1,12 +0,0 @@
#!/bin/bash
# Shim: forward to the shared todo git hooks in ~/src/utils (silent if absent).
set -euo pipefail
readonly SHARED="${UTILS_ROOT:-$HOME/src/utils}/scripts/todo_hooks.sh"
main() {
[[ -f "$SHARED" ]] || exit 0
exec bash "$SHARED" "$@"
}
main "$@"

View File

@ -1,9 +0,0 @@
"""Stubs shared by the CLI flow tests (plain functions, not fixtures)."""
from steam_installer import depotdl
ARGV = ["--appid", "1771300", "--username", "u", "--language", "english"]
def _stub_download(monkeypatch, returncode=0):
monkeypatch.setattr(depotdl, "download", lambda **kw: returncode)

View File

@ -1,97 +0,0 @@
"""End-to-end-ish tests for the CLI's decision logic.
The bug that motivated all of this lived here, not in the manifest writer:
a step died instantly having done nothing, and main() still wrote
StateFlags=4, printed "launch Steam and confirm Play works" and returned 0.
These tests pin the fail-closed behaviour.
"""
import pytest
from steam_installer import (
appinfo,
cli,
depotdl,
recognize,
)
from _cli_helpers import ARGV, _stub_download
pytestmark = pytest.mark.usefixtures("cli_logs")
def test_downloaded_content_finishes_the_manifest(steam, monkeypatch):
"""The content is what matters, and the manifest must come out consistent,
not merely flagged installed."""
library, install_dir = steam
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf")
assert app_state["StateFlags"] == "4"
for key in ("BytesToDownload", "BytesDownloaded", "BytesToStage", "BytesStaged"):
assert app_state[key] == "0", key
assert recognize.download_counters_settled(app_state)
def test_refuses_when_a_required_depot_never_downloaded(steam, monkeypatch):
"""The exact fail-open case: nothing was really downloaded, so declaring the
app installed would be a fabricated success."""
library, install_dir = steam
_stub_download(monkeypatch)
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: appinfo.DepotPlan({"1771303", "9999999"}, set()),
)
assert cli.main(ARGV) == 1
app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf")
assert app_state["StateFlags"] == "1026"
def test_refuses_when_depotdownloader_fails(steam, monkeypatch):
library, _ = steam
_stub_download(monkeypatch, returncode=1)
assert cli.main(ARGV) == 1
app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf")
assert app_state["StateFlags"] == "1026"
def test_declining_to_close_steam_aborts_before_downloading(steam, monkeypatch):
"""Steam owns the manifest while it runs. The tool offers to close it, but a
"no" must stop the run rather than download for an hour and lose the write."""
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [4242])
monkeypatch.setattr(
"builtins.input", lambda *a: "n" if "Steam is running" in a[0] else "y"
)
called = []
monkeypatch.setattr(depotdl, "download", lambda **kw: called.append(1) or 0)
assert cli.main(ARGV) == 1
assert called == []
def test_user_declining_confirmation_aborts(steam, monkeypatch):
monkeypatch.setattr("builtins.input", lambda *a: "n")
called = []
monkeypatch.setattr(depotdl, "download", lambda **kw: called.append(1) or 0)
assert cli.main(ARGV) == 1
assert called == []
def test_unhandled_error_returns_nonzero_and_names_the_log(steam, monkeypatch, capsys):
def boom(**kwargs):
raise RuntimeError("depotdownloader exploded")
monkeypatch.setattr(depotdl, "download", boom)
assert cli.main(ARGV) == 1
assert "Run log" in capsys.readouterr().out
def test_requires_a_name_or_appid():
with pytest.raises(SystemExit):
cli.main(["--username", "u"])

View File

@ -1,228 +0,0 @@
"""CLI flow tests, part 2: the manifest-finishing end of a run."""
import subprocess
import sys
import pytest
from _cli_helpers import ARGV, _stub_download
from steam_installer import (
appinfo,
cli,
recognize,
vdf,
)
pytestmark = pytest.mark.usefixtures("cli_logs")
# The 15 keys Steam accepted on 2026-11-22 (A Space for the Unbound): it
# logged "Fully Installed,App Running", queued no download, and added its own
# LastOwner, UserConfig, MountedConfig, ... on exit.
FROM_SCRATCH_KEYS = [
"appid", "universe", "name", "StateFlags", "installdir", "LastUpdated",
"SizeOnDisk", "StagingSize", "buildid", "UpdateResult", "BytesToDownload",
"BytesDownloaded", "BytesToStage", "BytesStaged", "InstalledDepots",
]
def _proven_plan(monkeypatch, install_dir, shared_owner=None):
(gid,) = recognize.discover_cached_depot_manifests(install_dir).values()
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: appinfo.DepotPlan(
{"1771303"}, set(shared_owner or ()), manifests={"1771303": gid},
shared_owner=dict(shared_owner or {}),
),
)
return gid
def test_missing_manifest_is_written_from_scratch(steam, monkeypatch):
"""No steamcmd any more, so nothing else creates the manifest."""
library, install_dir = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.unlink()
gid = _proven_plan(monkeypatch, install_dir)
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = vdf.parse(manifest.read_text())["AppState"]
assert list(app_state) == FROM_SCRATCH_KEYS
assert app_state["appid"] == "1771300"
assert app_state["universe"] == "1"
assert app_state["name"] == "KCD2"
assert app_state["StateFlags"] == "4"
assert app_state["installdir"] == "KingdomComeDeliverance2"
assert app_state["SizeOnDisk"] == "1000"
assert app_state["buildid"] == "23914554"
for key in ("StagingSize", "UpdateResult", "BytesToDownload",
"BytesDownloaded", "BytesToStage", "BytesStaged"):
assert app_state[key] == "0", key
assert app_state["InstalledDepots"] == {"1771303": {"manifest": gid, "size": "0"}}
# Nothing to back up: there was no manifest before.
assert not manifest.with_suffix(".acf.bak").exists()
def test_a_shared_depot_is_recorded_under_its_owner_from_scratch(steam, monkeypatch):
"""Fixture-only: no shared-depot game was tested live. Steam records a
depot installed under another app as SharedDepots {depot: owner}."""
library, install_dir = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.unlink()
_proven_plan(monkeypatch, install_dir, shared_owner={"228989": "228980"})
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = vdf.parse(manifest.read_text())["AppState"]
assert app_state["SharedDepots"] == {"228989": "228980"}
assert "228989" not in app_state["InstalledDepots"]
def test_unproven_content_writes_no_manifest_from_scratch(steam, monkeypatch, capsys):
"""Fail closed: without a proven build there is no buildid to write, and a
half-true StateFlags=4 manifest is what made Steam redownload games."""
library, _ = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.unlink()
_stub_download(monkeypatch)
assert cli.main(ARGV) == 1
assert not manifest.exists()
assert "no usable buildid" in capsys.readouterr().out
def test_language_defaults_to_english_when_prompt_is_empty(steam, monkeypatch):
seen = {}
monkeypatch.setattr("builtins.input", lambda *a: "y" if "Proceed" in a[0] else "")
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: (
seen.setdefault("language", language)
and None
or appinfo.DepotPlan({"1771303"}, set())
),
)
_stub_download(monkeypatch)
cli.main(["--appid", "1771300", "--username", "u"])
assert seen["language"] == "english"
def test_adopts_staged_content_then_clears_it(steam, monkeypatch, capsys):
"""Steam's staging tree is adopted into the install dir before downloading,
and only cleared once the install is finished -- never discarded up front."""
library, install_dir = steam
staged = library.steamapps / "downloading" / "1771300"
staged.mkdir(parents=True)
(staged / "big.pak").write_bytes(b"x" * 2_000_000)
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
out = capsys.readouterr().out
assert "Hardlinking it" in out
assert "now-redundant Steam staging data" in out
# Adopted, not copied: the file is one inode with two names.
assert (install_dir / "big.pak").stat().st_size == 2_000_000
def test_fails_when_written_manifest_does_not_read_back_as_installed(
steam, monkeypatch, capsys
):
"""Claiming success without re-reading the write is the same fail-open
mistake in a different place."""
_stub_download(monkeypatch)
monkeypatch.setattr(
recognize, "manifest_reflects_real_install", lambda *a, **kw: False
)
assert cli.main(ARGV) == 1
assert "does not read back as a real install" in capsys.readouterr().out
def test_module_is_runnable_as_a_script(coverage_subprocess_env):
"""`python -m steam_installer.cli` is a real entry point (the __main__
guard), so it must not be dead code."""
result = subprocess.run(
[sys.executable, "-m", "steam_installer.cli", "--help"],
capture_output=True,
text=True,
timeout=60,
env=coverage_subprocess_env,
)
assert result.returncode == 0
assert "--skip-steamcmd" not in result.stdout
def test_a_stub_manifest_is_finished_with_the_proven_build(steam, monkeypatch):
"""The enforcer's stub says build 0 and Steam never ran to fill it in. The
cached depot manifest matching the public branch proves the current build,
which is what the finished manifest must record (BG3, 2026-10-23)."""
library, install_dir = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.write_text(
vdf.dumps("AppState", {"appid": "1771300", "StateFlags": "1026", "buildid": "0"})
)
(gid,) = recognize.discover_cached_depot_manifests(install_dir).values()
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: appinfo.DepotPlan(
{"1771303"}, {"228989"}, manifests={"1771303": gid}
),
)
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = vdf.parse(manifest.read_text())["AppState"]
assert (app_state["StateFlags"], app_state["buildid"]) == ("4", "23914554")
def test_the_install_folder_is_corrected_even_when_the_download_fails(
steam, monkeypatch
):
"""The enforcer's stub names the folder after the display name ("Baldur's
Gate 3", not "Baldurs Gate 3"). Steam resumes onto whatever the manifest
says, so it is fixed as soon as Steam is closed, not only on success."""
library, _ = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
state = recognize.read_app_state(manifest)
state["installdir"] = "Kingdom Come: Deliverance II"
manifest.write_text(vdf.dumps("AppState", state))
_stub_download(monkeypatch, returncode=2)
assert cli.main(ARGV) == 1
assert recognize.read_app_state(manifest)["installdir"] == "KingdomComeDeliverance2"
def test_wrong_installdir_and_mismatched_counters_end_correct_and_zeroed(
steam, monkeypatch
):
"""C18: the live repro (installdir "... WRONG", BytesToDownload 1000,
BytesDownloaded 5) once came out as 1000/1000. The written manifest must
name the real folder, keep its files, and carry every counter at 0."""
library, install_dir = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
state = recognize.read_app_state(manifest)
state.update(
installdir="KingdomComeDeliverance2 WRONG",
BytesToDownload="1000",
BytesDownloaded="5",
BytesToStage="1000",
BytesStaged="5",
)
manifest.write_text(vdf.dumps("AppState", state))
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = recognize.read_app_state(manifest)
assert app_state["installdir"] == "KingdomComeDeliverance2"
assert (install_dir / "game.pak").stat().st_size == 1000
for key in ("BytesToDownload", "BytesDownloaded", "BytesToStage", "BytesStaged"):
assert app_state[key] == "0", key

View File

@ -1,205 +0,0 @@
"""What the CLI does around a real install: ghosts, usernames, the Steam client.
Split from test_cli_modes.py to keep both files under the repo's 250-line cap.
"""
import pytest
from steam_installer import appinfo, cli, config, depotdl, ghost, recognize
from tests.conftest import GHOST_ACF
def test_a_cold_ghost_blocks_an_install_and_names_the_fix(cli_steam, capsys):
"""Installing over a fabricated manifest is how Steam was tricked into
re-downloading in the first place; stop and say what to run."""
(cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF)
assert cli.main(["--yes", "284160", "--username", "u"]) == 1
assert "./run.sh --repair-ghosts" in capsys.readouterr().out
def test_a_warm_ghost_does_not_block_an_install(cli_steam, monkeypatch):
"""Staged bytes are the whole reason to run this tool, not a blocker."""
(cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF)
staged = cli_steam.steamapps / "downloading" / "284160"
staged.mkdir(parents=True)
(staged / "part.pak").write_bytes(b"x" * 4096)
reached = []
monkeypatch.setattr(depotdl, "download", lambda **kw: reached.append(1) or 1)
monkeypatch.setattr(
appinfo, "get_depot_plan", lambda a, l: appinfo.DepotPlan(set(), set())
)
monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1")
cli.main(["--yes", "284160", "--username", "u"])
assert reached == [1]
def test_repair_ghosts_clears_a_cold_ghost(cli_steam, capsys):
(cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF)
assert cli.main(["--repair-ghosts"]) == 0
assert not (cli_steam.steamapps / "appmanifest_284160.acf").exists()
assert "Ghost: appid 284160" in capsys.readouterr().out
def test_repair_ghosts_refuses_to_discard_a_warm_ghosts_download(cli_steam, capsys):
(cli_steam.steamapps / "appmanifest_284160.acf").write_text(GHOST_ACF)
staged = cli_steam.steamapps / "downloading" / "284160"
staged.mkdir(parents=True)
(staged / "part.pak").write_bytes(b"x" * 4096)
assert cli.main(["--repair-ghosts"]) == 0
assert (cli_steam.steamapps / "appmanifest_284160.acf").exists()
assert (staged / "part.pak").exists()
assert "nothing is deleted" in capsys.readouterr().out
def test_repair_ghosts_says_so_when_there_is_nothing_to_do(cli_steam, capsys):
assert cli.main(["--repair-ghosts"]) == 0
assert "No ghost installs found" in capsys.readouterr().out
def test_repair_ghosts_needs_no_target(cli_steam):
"""It is the one mode that runs without naming a game."""
assert cli.main(["--repair-ghosts"]) == 0
def test_a_saved_username_removes_the_need_for_the_flag(cli_steam, monkeypatch):
config.save_username("saved-user")
seen = {}
monkeypatch.setattr(
depotdl,
"download",
lambda **kw: seen.setdefault("username", kw["username"]) or 1,
)
monkeypatch.setattr(
appinfo, "get_depot_plan", lambda a, l: appinfo.DepotPlan(set(), set())
)
monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1")
cli.main(["--yes", "284160"])
assert seen["username"] == "saved-user"
def test_no_username_anywhere_fails_with_an_explanation(cli_steam, capsys):
assert cli.main(["--yes", "284160"]) == 1
assert "No Steam username available" in capsys.readouterr().out
def test_a_non_interactive_run_without_yes_does_not_start_a_download(cli_steam, capsys):
"""A piped invocation never asked for a 50 GB download; refuse rather than
assume."""
assert cli.main(["284160", "--username", "u"]) == 1
assert "use --yes" in capsys.readouterr().out
def test_language_defaults_to_english_when_not_interactive(cli_steam, monkeypatch):
seen = {}
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda a, language: (
seen.setdefault("language", language) or appinfo.DepotPlan(set(), set())
),
)
monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1")
monkeypatch.setattr(depotdl, "download", lambda **kw: 1)
cli.main(["--yes", "284160", "--username", "u"])
assert seen["language"] == "english"
def test_nothing_to_do_at_all_is_a_usage_error():
with pytest.raises(SystemExit):
cli.main(["--username", "u"])
def test_a_ghost_for_a_different_app_does_not_block_this_one(cli_steam, monkeypatch):
(cli_steam.steamapps / "appmanifest_999.acf").write_text(
GHOST_ACF.replace('"appid"\t\t"284160"', '"appid"\t\t"999"')
)
monkeypatch.setattr(depotdl, "download", lambda **kw: 1)
monkeypatch.setattr(
appinfo, "get_depot_plan", lambda a, l: appinfo.DepotPlan(set(), set())
)
monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1")
assert cli.main(["--yes", "284160", "--username", "u"]) == 1
def _installable(monkeypatch, library):
monkeypatch.setattr(
appinfo, "get_depot_plan", lambda a, l: appinfo.DepotPlan(set(), set())
)
monkeypatch.setattr(appinfo, "get_build_id", lambda a: "1")
monkeypatch.setattr(depotdl, "download", lambda **kw: 1)
def test_declining_the_shutdown_prompt_stops_the_run(cli_steam, monkeypatch):
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42])
monkeypatch.setattr(cli, "_is_interactive", lambda args: True)
monkeypatch.setattr(
"builtins.input", lambda *a: "n" if "Steam is running" in a[0] else "y"
)
monkeypatch.setattr(
depotdl, "download", lambda **kw: pytest.fail("must not download")
)
assert cli.main(["284160", "--username", "u", "--language", "english"]) == 1
def test_steam_is_closed_and_reopened_around_the_install(cli_steam, monkeypatch):
"""It was running before, so it is running after -- whatever the outcome."""
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42])
monkeypatch.setattr(ghost.recognize, "steam_client_pids", lambda: [42])
events = []
monkeypatch.setattr(
cli.flow.steamctl, "stop", lambda logger: events.append("stop") or True
)
monkeypatch.setattr(
cli.flow.steamctl, "start", lambda logger: events.append("start")
)
_installable(monkeypatch, cli_steam)
assert cli.main(["--yes", "284160", "--username", "u"]) == 1
assert events == ["stop", "start"]
def test_a_steam_that_refuses_to_exit_aborts_the_run(cli_steam, monkeypatch, capsys):
"""Never escalate to a signal: a hung client is the user's to deal with."""
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42])
def refuse(logger):
raise cli.flow.steamctl.SteamStillRunning("Steam is still running after 60s")
monkeypatch.setattr(cli.flow.steamctl, "stop", refuse)
monkeypatch.setattr(
depotdl, "download", lambda **kw: pytest.fail("must not download")
)
assert cli.main(["--yes", "284160", "--username", "u"]) == 1
assert "still running" in capsys.readouterr().out
def test_accepting_the_shutdown_prompt_closes_and_reopens_steam(cli_steam, monkeypatch):
"""The interactive path: asked, answered yes, Steam cycled around the run."""
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [42])
monkeypatch.setattr(cli, "_is_interactive", lambda args: True)
monkeypatch.setattr("builtins.input", lambda *a: "y")
events = []
monkeypatch.setattr(
cli.flow.steamctl, "stop", lambda logger: events.append("stop") or True
)
monkeypatch.setattr(
cli.flow.steamctl, "start", lambda logger: events.append("start")
)
_installable(monkeypatch, cli_steam)
assert cli.main(["284160", "--username", "u", "--language", "english"]) == 1
assert events == ["stop", "start"]

View File

@ -1,94 +0,0 @@
"""scripts/deploy.sh against a fixture checkout instead of kuhy's real one."""
from __future__ import annotations
import os
from pathlib import Path
import subprocess
import pytest
DEPLOY = Path(__file__).resolve().parent.parent / "scripts" / "deploy.sh"
# Stubs stand in for the real run.sh / venv so the test installs nothing.
RUN_SH = '#!/bin/bash\n[[ "$1" == --deps-only ]] && touch .deps-ran\n'
VENV_PY = '#!/bin/bash\n[[ -f "$(dirname "$0")/healthy" ]]\n'
def _git(cwd: Path, *args: str) -> str:
"""Run git quietly in cwd and return stdout."""
return subprocess.run(
["git", "-C", str(cwd), *args], check=True, capture_output=True, text=True
).stdout.strip()
def _commit(repo: Path, name: str, body: str, mode: int = 0o644) -> None:
"""Write one file into repo and commit it."""
path = repo / name
path.write_text(body)
path.chmod(mode)
_git(repo, "add", name)
_git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", name)
@pytest.fixture
def checkout(tmp_path: Path) -> tuple[Path, Path]:
"""An origin with run.sh and a clone of it that is one commit behind."""
origin = tmp_path / "origin"
origin.mkdir()
_git(origin, "init", "-q", "-b", "main")
_commit(origin, ".gitignore", ".venv/\n.deps-ran\n")
_commit(origin, "run.sh", RUN_SH, 0o755)
clone = tmp_path / "clone"
subprocess.run(["git", "clone", "-q", str(origin), str(clone)], check=True)
_commit(origin, "new.txt", "new\n")
venv_bin = clone / ".venv" / "bin"
venv_bin.mkdir(parents=True)
(venv_bin / "python").write_text(VENV_PY)
(venv_bin / "python").chmod(0o755)
(venv_bin / "healthy").touch()
return origin, clone
def _deploy(clone: Path) -> subprocess.CompletedProcess[str]:
"""Run deploy.sh pointed at the fixture clone."""
env = {**os.environ, "SGI_DEPLOY_DIR": str(clone)}
return subprocess.run(
["bash", str(DEPLOY)], env=env, capture_output=True, text=True, check=False
)
def test_deploy_fast_forwards_installs_and_checks(
checkout: tuple[Path, Path],
) -> None:
"""A clean checkout ends at origin/main with deps run and exit 0."""
origin, clone = checkout
result = _deploy(clone)
assert result.returncode == 0, result.stderr
assert _git(clone, "rev-parse", "HEAD") == _git(origin, "rev-parse", "HEAD")
assert (clone / ".deps-ran").exists()
assert _deploy(clone).returncode == 0 # idempotent
def test_deploy_refuses_dirty_checkout(checkout: tuple[Path, Path]) -> None:
"""Local edits in kuhy's checkout are never overwritten."""
_, clone = checkout
(clone / "run.sh").write_text(RUN_SH + "# local edit\n")
result = _deploy(clone)
assert result.returncode != 0
assert "local changes" in result.stderr
def test_deploy_fails_when_health_check_fails(checkout: tuple[Path, Path]) -> None:
"""A CLI that cannot start makes deploy exit non-zero."""
_, clone = checkout
(clone / ".venv" / "bin" / "healthy").unlink()
result = _deploy(clone)
assert result.returncode != 0
assert "health" in result.stderr
def test_claude_md_declares_deploy() -> None:
"""CLAUDE.md names scripts/deploy.sh as the deploy command."""
claude = DEPLOY.parent.parent / "CLAUDE.md"
assert "- deploy: `scripts/deploy.sh`" in claude.read_text()

View File

@ -1,80 +0,0 @@
import logging
from steam_installer import depotdl
def _capture(monkeypatch):
"""Intercept the subprocess call and hand back the argv it was given."""
seen = {}
def fake_run_logged(cmd, logger, timeout=None):
seen["cmd"] = cmd
return seen.get("rc", 0), ""
monkeypatch.setattr(depotdl.runlog, "run_logged", fake_run_logged)
return seen
def test_download_builds_expected_argv(tmp_path, monkeypatch):
seen = _capture(monkeypatch)
depotdl.download(
appid=1771300,
install_dir=tmp_path / "Game",
username="krzys2000",
logger=logging.getLogger("t"),
language="english",
max_downloads=32,
)
cmd = seen["cmd"]
assert cmd[0] == "depotdownloader"
pairs = dict(zip(cmd[1::2], cmd[2::2]))
assert pairs["-app"] == "1771300"
assert pairs["-language"] == "english"
assert pairs["-max-downloads"] == "32"
assert pairs["-username"] == "krzys2000"
def test_download_always_passes_os_windows(tmp_path, monkeypatch):
"""Without -os, depotdownloader defaults to the host OS and can filter out
every real (Windows-tagged) depot -- the original "0 bytes from 0 depots"."""
seen = _capture(monkeypatch)
depotdl.download(
appid=1771300,
install_dir=tmp_path / "Game",
username="u",
logger=logging.getLogger("t"),
)
cmd = seen["cmd"]
assert cmd[cmd.index("-os") + 1] == "windows"
assert "-remember-password" in cmd
def test_download_creates_the_install_dir(tmp_path, monkeypatch):
_capture(monkeypatch)
target = tmp_path / "nested" / "Game"
depotdl.download(
appid=1, install_dir=target, username="u", logger=logging.getLogger("t")
)
assert target.is_dir()
def test_download_returns_the_exit_code_instead_of_raising(tmp_path, monkeypatch):
"""The caller must distinguish "download failed" from "manifest step failed"
to decide whether touching the manifest is safe at all."""
seen = _capture(monkeypatch)
seen["rc"] = 1
returncode = depotdl.download(
appid=1,
install_dir=tmp_path / "Game",
username="u",
logger=logging.getLogger("t"),
)
assert returncode == 1

View File

@ -1,199 +0,0 @@
"""DLC that ships as its own app: discovery, download, and crediting it in the manifest.
Shapes are taken from the real `app_info_print` of Cyberpunk 2077 (1091500) and
its DLC, 2025-09-25: Phantom Liberty (2138330) keeps its ~25 GB depot under its
own app id; 2060310 is listed in the game's own depots; 2441600 has none.
"""
import logging
import pytest
from _cli_helpers import ARGV, _stub_download
from steam_installer import appinfo, cli, depotdl, deps, dlc, recognize, runlog, vdf
pytestmark = pytest.mark.usefixtures("cli_logs")
# Every real depot that has content ships a manifest on the public branch.
_PUB = {"manifests": {"public": {"gid": "1"}}}
GAME = {
"extended": {"listofdlc": "2060310,2138330,2441600"},
"depots": {
"1091501": {"config": {"oslist": "windows"}, **_PUB},
"2060310": {"dlcappid": "2060310", "config": {"oslist": "windows"}, **_PUB},
},
}
DLC_BLOCKS = {
# Listed in the game's own depots: the base run already covers it.
2060310: {"depots": {"2060310": {"dlcappid": "2060310"}}},
# Its own depot, tagged with its own id -- exactly what it installs.
2138330: {
"depots": {
"2138330": {"dlcappid": "2138330", "config": {"oslist": "windows"}, **_PUB},
"2138331": {"config": {"oslist": "windows", "language": "german"}, **_PUB},
}
},
2441600: {"common": {"name": "Pre-Order Bonus"}},
}
def _app_info(monkeypatch, game=GAME, dlc_blocks=DLC_BLOCKS):
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: game)
monkeypatch.setattr(
appinfo, "_cached_blocks", lambda ids: {i: dlc_blocks[i] for i in ids}
)
def test_only_dlc_with_depots_of_its_own_needs_a_run(monkeypatch):
_app_info(monkeypatch)
assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}}
@pytest.mark.parametrize("game", [{}, {"extended": "scalar"}, {"extended": {}}])
def test_a_game_without_dlc_needs_no_lookup(monkeypatch, game):
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: game)
monkeypatch.setattr(appinfo, "_cached_blocks", lambda ids: pytest.fail())
assert appinfo.get_dlc_plans(1) == {}
def test_a_game_with_no_depot_list_still_finds_its_dlc(monkeypatch):
_app_info(monkeypatch, game={"extended": {"listofdlc": "2138330"}})
assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}}
def test_depot_plan_tags_the_games_own_dlc_depots(monkeypatch):
_app_info(monkeypatch)
plan = appinfo.get_depot_plan(1091500)
assert plan.required == {"1091501"}
assert plan.dlc == {"2060310": "2060310"}
def _logged(monkeypatch, returncode, output=""):
monkeypatch.setattr(
depotdl.runlog, "run_logged", lambda cmd, logger: (returncode, output)
)
@pytest.mark.parametrize(
("returncode", "output", "expected"),
[
(0, "", True),
(1, "App 2441600 (Bonus) is not available from this account.", None),
(1, "Connection lost", False),
],
)
def test_download_dlc_separates_not_owned_from_failed(
tmp_path, monkeypatch, returncode, output, expected
):
_logged(monkeypatch, returncode, output)
result = depotdl.download_dlc(2441600, tmp_path, "u", logging.getLogger("t"))
assert result is expected
def test_the_managed_depotdownloader_wins_over_path(tmp_path, monkeypatch):
managed = tmp_path / "depotdownloader"
managed.write_text("")
managed.chmod(0o755)
monkeypatch.setattr(deps, "MANAGED_DEPOTDOWNLOADER", managed)
assert deps.depotdownloader_bin() == str(managed)
@pytest.fixture
def run_log(tmp_path, monkeypatch):
monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state")
log = runlog.setup(1091500)
yield log
for handler in list(log.logger.handlers):
log.logger.removeHandler(handler)
handler.close()
def _download_owned(monkeypatch, run_log, tmp_path, outcomes):
monkeypatch.setattr(
appinfo,
"get_dlc_plans",
lambda appid, language: {7: {"70": "g70"}, 8: {"80": "g80"}},
)
monkeypatch.setattr(
depotdl, "download_dlc", lambda dlc_appid, **kw: outcomes[dlc_appid]
)
return dlc.download_owned(1, tmp_path, "u", "english", 8, run_log)
def test_owned_dlc_is_returned_and_unowned_skipped(monkeypatch, run_log, tmp_path):
plan = _download_owned(monkeypatch, run_log, tmp_path, {7: True, 8: None})
assert (plan.required, plan.dlc) == ({"70"}, {"70": "7"})
# Its public gid too, so the finish check can prove the DLC's build.
assert plan.manifests == {"70": "g70"}
def test_a_failed_dlc_download_aborts(monkeypatch, run_log, tmp_path):
assert _download_owned(monkeypatch, run_log, tmp_path, {7: False, 8: True}) is None
def test_downloaded_dlc_is_required_and_credited_to_its_app(steam, monkeypatch):
library, install_dir = steam
(install_dir / ".DepotDownloader" / "2138330_100389.manifest").write_bytes(b"x")
manifest = library.steamapps / "appmanifest_1771300.acf"
state = recognize.read_app_state(manifest)
# Steam's own progress for a DLC download it had started itself.
state["DlcDownloads"] = {
"2138330": {"BytesDownloaded": "1", "BytesToDownload": "9"}
}
manifest.write_text(vdf.dumps("AppState", state))
_stub_download(monkeypatch)
monkeypatch.setattr(dlc, "download_owned", lambda *a: _dlc_plan("100389"))
assert cli.main(ARGV) == 0
state = recognize.read_app_state(manifest)
assert state["InstalledDepots"]["2138330"]["dlcappid"] == "2138330"
assert "dlcappid" not in state["InstalledDepots"]["1771303"]
assert "DlcDownloads" not in state
def _dlc_plan(gid):
return appinfo.DepotPlan(
{"2138330"}, set(), {"2138330": "2138330"}, {"2138330": gid}
)
def test_a_stub_with_own_app_dlc_finishes_with_the_proven_build(steam, monkeypatch):
"""Base game and its own-app DLC both match the public branch: a build-0
stub is finished, the DLC's gid coming from the DLC's own app info."""
library, install_dir = steam
(install_dir / ".DepotDownloader" / "2138330_100389.manifest").write_bytes(b"x")
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.write_text(vdf.dumps("AppState", {"appid": "1771300", "buildid": "0"}))
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: appinfo.DepotPlan(
{"1771303"}, set(), manifests={"1771303": "1999516201078544527"}
),
)
_stub_download(monkeypatch)
monkeypatch.setattr(dlc, "download_owned", lambda *a: _dlc_plan("100389"))
assert cli.main(ARGV) == 0
assert recognize.read_app_state(manifest)["buildid"] == "23914554"
def test_a_failed_dlc_download_leaves_the_manifest_alone(steam, monkeypatch):
library, _ = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
before = recognize.read_app_state(manifest)
_stub_download(monkeypatch)
monkeypatch.setattr(dlc, "download_owned", lambda *a: None)
assert cli.main(ARGV) == 1
# Only the folder name is fixed up front; nothing claims an install.
after = recognize.read_app_state(manifest)
assert after.pop("installdir") == "KingdomComeDeliverance2"
assert after == before
def test_a_dlc_with_a_malformed_depot_section_is_skipped(monkeypatch):
_app_info(monkeypatch, dlc_blocks={**DLC_BLOCKS, 2441600: {"depots": "scalar"}})
assert appinfo.get_dlc_plans(1091500) == {2138330: {"2138330": "1"}}

View File

@ -1,45 +0,0 @@
"""install.sh --check-only: what a run needs, with steamcmd no longer on the list."""
import os
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
def _stub(bin_dir: Path, name: str, script: str) -> None:
path = bin_dir / name
path.write_text(f"#!/bin/bash\n{script}\n")
path.chmod(0o755)
def _check_only(tmp_path, libibus_installed: bool):
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
_stub(bin_dir, "depotdownloader", "exit 0")
_stub(bin_dir, "pacman", f'[[ "$*" == "-Q libibus" ]] && exit {0 if libibus_installed else 1}; exit 0')
env = {**os.environ, "PATH": f"{bin_dir}:/usr/bin:/bin", "HOME": str(tmp_path)}
return subprocess.run(
["bash", str(ROOT / "install.sh"), "--check-only"],
capture_output=True, text=True, env=env, timeout=30, check=False,
)
def test_missing_libibus_is_reported_and_steamcmd_is_not_needed(tmp_path):
result = _check_only(tmp_path, libibus_installed=False)
assert result.returncode == 1
assert "missing: libibus" in result.stderr
assert "steamcmd" not in result.stdout + result.stderr
def test_all_present_without_steamcmd(tmp_path):
result = _check_only(tmp_path, libibus_installed=True)
assert result.returncode == 0, result.stderr
assert "All dependencies present." in result.stdout
def test_run_sh_rechecks_when_the_proton_pin_changes():
"""A stale stamp must not hide a lock bump or a deleted tool folder."""
text = (ROOT / "run.sh").read_text()
assert "proton-native.lock" in text
assert "steam_installer.proton_native --check" in text

View File

@ -1,197 +0,0 @@
"""Mapping games to the native Proton: after an install, and for the whole library."""
import pytest
from _appinfo_blob import encode
from _cli_helpers import ARGV, _stub_download
from steam_installer import (
anticheat,
cli,
compat_apply,
compat_map,
proton_pkg,
recognize,
steamctl,
vdf,
)
pytestmark = pytest.mark.usefixtures("cli_logs")
LOCKED = proton_pkg.read_lock().version
CONFIG = '"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n\t\t\t"Steam"\n\t\t\t{\n\t\t\t}\n\t\t}\n\t}\n}\n'
PLAIN = {"common": {"name": "Plain", "oslist": "windows", "type": "Game"},
"depots": {"11": {"config": {"oslist": "windows"}}}}
NATIVE = {"common": {"name": "Native", "oslist": "windows,linux", "type": "Game"}}
SHARED = {"common": {"name": "Shared", "oslist": "windows", "type": "Game"},
"depots": {"31": {"depotfromapp": "228980"}}}
CS = {"common": {"name": "CS", "oslist": "windows,linux", "type": "Game"}}
TOOL = {"common": {"name": "Proton Experimental", "type": "Tool"}}
@pytest.fixture
def home(tmp_path, monkeypatch):
"""A Steam root with config.vdf, the stamped tool, an app cache and anti-cheat list."""
root = tmp_path / "Steam"
(root / "config").mkdir(parents=True)
(root / "config" / "config.vdf").write_text(CONFIG)
tool = root / "compatibilitytools.d" / "proton-cachyos-native"
tool.mkdir(parents=True)
(tool / "toolmanifest.vdf").write_text("x")
(tool / "VERSION").write_text(LOCKED + "\n")
cache = tmp_path / "appinfo.vdf"
cache.write_bytes(encode({10: PLAIN, 20: NATIVE, 30: SHARED, 730: CS, 1493710: TOOL}))
monkeypatch.setattr(compat_apply.appinfo.appinfo_cache, "default_path", lambda: cache)
monkeypatch.setattr(anticheat, "_download", lambda: b'[{"storeIds": {"steam": "730"}}]')
steamapps = root / "steamapps"
steamapps.mkdir()
for appid in (10, 20, 1493710):
(steamapps / f"appmanifest_{appid}.acf").write_text(
vdf.dumps("AppState", {"appid": str(appid), "InstalledDepots": {}}))
monkeypatch.setattr(cli, "find_steam_root", lambda: root)
return root
def _mapped(root, appid):
return compat_map.current(root / "config" / "config.vdf", appid)
def test_dry_run_lists_decisions_and_writes_nothing(home, capsys):
before = (home / "config" / "config.vdf").read_bytes()
assert cli.main(["--map-installed"]) == 0
out = capsys.readouterr().out
assert "10 Plain: map" in out
assert "20 Native: Valve default: native" in out
assert "1493710" not in out # a tool, not a game
assert "Dry run" in out
assert (home / "config" / "config.vdf").read_bytes() == before
def test_appids_decides_any_cached_app(home, capsys):
assert cli.main(["--map-installed", "--appids", "730,20,30,10,999"]) == 0
out = capsys.readouterr().out
assert "730 CS: Valve default: anti-cheat, native" in out
assert "30 Shared: Valve default: shared depots" in out
assert "10 Plain: map" in out
assert "999: not in Steam's app cache" in out
def test_apply_maps_the_safe_ones_and_a_rerun_changes_nothing(home, capsys):
assert cli.main(["--map-installed", "--apply", "--yes"]) == 0
assert _mapped(home, 10) == ("proton-cachyos-native", "250")
assert _mapped(home, 20) is None
once = (home / "config" / "config.vdf").read_bytes()
capsys.readouterr()
assert cli.main(["--map-installed", "--apply", "--yes"]) == 0
assert "10 Plain: map -- already mapped" in capsys.readouterr().out
assert (home / "config" / "config.vdf").read_bytes() == once
def test_apply_closes_steam_first_and_restarts_it(home, monkeypatch):
events = []
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [] if events else [5])
monkeypatch.setattr(steamctl, "stop", lambda logger: events.append("stop") or True)
monkeypatch.setattr(steamctl, "start", lambda logger: events.append("start"))
assert cli.main(["--map-installed", "--apply", "--yes"]) == 0
assert events == ["stop", "start"]
assert _mapped(home, 10) is not None
def test_apply_declined_writes_nothing(home, monkeypatch):
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [5])
monkeypatch.setattr("builtins.input", lambda *a: "n")
assert cli.main(["--map-installed", "--apply"]) == 1
assert _mapped(home, 10) is None
def test_a_kept_choice_and_a_write_error_are_reported(home, monkeypatch, capsys):
config = home / "config" / "config.vdf"
compat_map.map_app(config, 10)
config.write_text(config.read_text().replace("proton-cachyos-native", "GE-Proton10"))
assert cli.main(["--map-installed", "--apply", "--yes"]) == 0
assert "kept: already mapped to GE-Proton10" in capsys.readouterr().out
config.write_text("garbage {")
assert cli.main(["--map-installed", "--apply", "--yes"]) == 1
def test_owned_dlc_in_the_manifest_keeps_valves_default(home, capsys):
manifest = home / "steamapps" / "appmanifest_10.acf"
manifest.write_text(vdf.dumps("AppState", {"appid": "10", "InstalledDepots": {
"11": {"manifest": "1"}, "12": {"manifest": "2", "dlcappid": "13"}}}))
assert cli.main(["--map-installed"]) == 0
assert "10 Plain: Valve default: owned DLC" in capsys.readouterr().out
def test_valve_proton_only_goes_with_an_install(home):
with pytest.raises(SystemExit):
cli.main(["--map-installed", "--valve-proton"])
def test_an_install_maps_the_game_while_steam_is_closed(steam, monkeypatch, capsys, tmp_path):
library, install_dir = steam
_with_compat_home(library.path, monkeypatch, tmp_path)
_stub_download(monkeypatch)
monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN)
_proven(monkeypatch, install_dir)
assert cli.main(ARGV) == 0
assert _mapped(library.path, 1771300) == ("proton-cachyos-native", "250")
assert "mapped to proton-cachyos-native" in capsys.readouterr().out
def test_valve_proton_keeps_valves_default_for_one_run(steam, monkeypatch, capsys, tmp_path):
library, install_dir = steam
_with_compat_home(library.path, monkeypatch, tmp_path)
_stub_download(monkeypatch)
monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN)
_proven(monkeypatch, install_dir)
assert cli.main([*ARGV, "--valve-proton"]) == 0
assert _mapped(library.path, 1771300) is None
assert "Valve default: override: --valve-proton" in capsys.readouterr().out
def test_a_mapping_failure_leaves_the_install_on_valves_default(steam, monkeypatch, capsys, tmp_path):
"""The install itself succeeded; the game still runs on Valve's Proton."""
library, install_dir = steam
_with_compat_home(library.path, monkeypatch, tmp_path)
(library.path / "config" / "config.vdf").write_text("broken {")
_stub_download(monkeypatch)
monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN)
_proven(monkeypatch, install_dir)
assert cli.main(ARGV) == 0
assert "stays on Valve's default" in capsys.readouterr().out
def test_an_owned_dlc_download_keeps_valves_default(steam, monkeypatch, capsys, tmp_path):
library, install_dir = steam
_with_compat_home(library.path, monkeypatch, tmp_path)
_stub_download(monkeypatch)
monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN)
_proven(monkeypatch, install_dir, dlc={"1771303": "9"})
assert cli.main(ARGV) == 0
assert "Valve default: owned DLC" in capsys.readouterr().out
def _with_compat_home(root, monkeypatch, tmp_path):
(root / "config").mkdir(parents=True, exist_ok=True)
(root / "config" / "config.vdf").write_text(CONFIG)
tool = root / "compatibilitytools.d" / "proton-cachyos-native"
tool.mkdir(parents=True)
(tool / "toolmanifest.vdf").write_text("x")
(tool / "VERSION").write_text(LOCKED + "\n")
monkeypatch.setattr(anticheat, "_download", lambda: b"[]")
def _proven(monkeypatch, install_dir, dlc=None):
(gid,) = recognize.discover_cached_depot_manifests(install_dir).values()
plan = compat_apply.appinfo.DepotPlan({"1771303"}, set(), manifests={"1771303": gid},
dlc=dict(dlc or {}))
monkeypatch.setattr(compat_apply.appinfo, "get_depot_plan", lambda appid, language: plan)
def test_a_failed_write_stops_the_run(home, monkeypatch, capsys):
def boom(config, appid):
raise compat_map.CompatMapError("config.vdf line 3: unexpected layout")
monkeypatch.setattr(compat_map, "map_app", boom)
(home / "steamapps" / "appmanifest_notanid.acf").write_text("x")
assert cli.main(["--map-installed", "--apply", "--yes"]) == 1
assert "Mapping stopped: config.vdf line 3" in capsys.readouterr().out

View File

@ -1,211 +0,0 @@
"""Fetch, verify and install the native Proton, never running its .INSTALL."""
import os
import shutil
import pytest
from _proton_fixture import (
FILES,
VERSION,
build_db,
build_package,
build_repo,
gpg,
stop_agent,
)
from steam_installer import proton_native, proton_pkg
@pytest.fixture(scope="module")
def repo(tmp_path_factory):
built = build_repo(tmp_path_factory.mktemp("repo"))
yield built
stop_agent(built.gpg_home)
@pytest.fixture
def mirror(repo, tmp_path):
"""A private copy of the fake mirror a test may tamper with."""
root = tmp_path / "mirror"
shutil.copytree(repo.root, root)
return root
@pytest.fixture
def setup(repo, mirror, tmp_path, monkeypatch):
tools = tmp_path / "compatibilitytools.d"
tools.mkdir()
lock = proton_pkg.Lock(
"proton-cachyos-native", (mirror / "cachyos.db").as_uri(), VERSION, repo.sha256
)
monkeypatch.setattr(proton_native, "in_use", lambda tool: [])
def run(**kw):
return proton_native.ensure(
tools, lock, key=repo.key, fingerprint=repo.fingerprint, **kw
)
return tools, lock, run
def _tree(tool):
return sorted(str(p.relative_to(tool)) for p in tool.rglob("*"))
def test_a_fresh_install_extracts_only_the_tool_and_stamps_it(setup):
tools, _, run = setup
status = run()
tool = tools / "proton-cachyos-native"
assert status.startswith("installed")
assert "signature" in status and "sha256" in status
assert (tool / "VERSION").read_text() == VERSION + "\n"
assert (tool / "files/bin/wine").read_bytes() == FILES["files/bin/wine"]
assert os.readlink(tool / "files/bin/msidb") == "wine"
assert not (tools / ".INSTALL").exists()
assert not any(".INSTALL" in p or "stray" in p for p in _tree(tools))
assert sorted(os.listdir(tools)) == ["proton-cachyos-native"]
def test_a_current_stamp_needs_no_network(setup, monkeypatch):
_, _, run = setup
run()
monkeypatch.setattr(proton_native, "_download", lambda url, dest: pytest.fail())
assert run() == f"current: {VERSION}"
def _assert_fails_untouched(run, tools, match):
old = tools / "proton-cachyos-native"
old.mkdir(exist_ok=True)
(old / "keep").write_text("old build")
with pytest.raises(proton_pkg.PkgError, match=match):
run()
assert (old / "keep").read_text() == "old build"
assert sorted(os.listdir(tools)) == ["proton-cachyos-native"]
def test_a_tampered_signature_fails_closed(setup, mirror):
tools, _, run = setup
sig = next(mirror.glob("*.sig"))
data = bytearray(sig.read_bytes())
data[-5] ^= 0xFF
sig.write_bytes(bytes(data))
_assert_fails_untouched(run, tools, "signature")
def test_a_signature_from_another_key_fails_closed(setup, mirror, tmp_path):
tools, _, run = setup
other = tmp_path / "other"
other.mkdir(mode=0o700)
gpg(other, "--quick-gen-key", "Mallory <m@example.invalid>", "ed25519", "sign", "never")
package = next(mirror.glob("*.pkg.tar.zst"))
(mirror / f"{package.name}.sig").unlink()
gpg(other, "--output", f"{package}.sig", "--detach-sign", str(package))
stop_agent(other)
_assert_fails_untouched(run, tools, "signature")
def test_a_package_that_differs_from_the_pinned_sha256_fails_closed(setup, mirror, repo):
tools, _, run = setup
package = next(mirror.glob("*.pkg.tar.zst"))
build_package(package, {**FILES, "proton": b"evil"})
gpg(repo.gpg_home, "--yes", "--output", f"{package}.sig", "--detach-sign", str(package))
_assert_fails_untouched(run, tools, "sha256")
def test_a_db_sha256_that_disagrees_with_the_lock_fails_closed(setup, mirror):
tools, _, run = setup
build_db(mirror / "cachyos.db", VERSION, "0" * 64)
_assert_fails_untouched(run, tools, "sha256")
def test_a_newer_package_in_the_repo_asks_for_a_lock_bump(setup, mirror, repo):
tools, _, run = setup
build_db(mirror / "cachyos.db", "1:11.0.20261101-1", repo.sha256)
_assert_fails_untouched(run, tools, "bump")
def test_a_repo_without_the_package_fails(setup, mirror, repo):
tools, _, run = setup
build_db(mirror / "cachyos.db", VERSION, repo.sha256, name="something-else")
_assert_fails_untouched(run, tools, "no proton-cachyos-native")
def _existing_copy(tools):
tool = tools / "proton-cachyos-native"
for name, data in FILES.items():
(tool / name).parent.mkdir(parents=True, exist_ok=True)
(tool / name).write_bytes(data)
os.symlink("wine", tool / "files/bin/msidb")
(tool / "__pycache__").mkdir()
(tool / "__pycache__" / "x.pyc").write_bytes(b"made by running it")
return tool
def test_an_identical_copy_is_adopted_without_extracting(setup, monkeypatch):
tools, _, run = setup
tool = _existing_copy(tools)
monkeypatch.setattr(proton_pkg, "extract", lambda *a: pytest.fail("extracted"))
status = run()
assert status.startswith(f"adopted: all {len(FILES) + 1} files match .MTREE (signature")
assert (tool / "VERSION").read_text() == VERSION + "\n"
@pytest.mark.parametrize(
("change", "where"),
[
(lambda t: (t / "files/bin/wine").write_bytes(b"patched"), "files/bin/wine"),
(lambda t: (t / "user_settings.py").write_text("x"), "user_settings.py"),
(lambda t: (t / "proton").unlink(), "proton"),
(lambda t: (t / "files/bin/msidb").unlink() or os.symlink("x", t / "files/bin/msidb"), "msidb"),
(lambda t: (t / "files/bin/msidb").unlink(), "msidb"),
],
)
def test_a_differing_copy_is_replaced(setup, change, where):
tools, _, run = setup
tool = _existing_copy(tools)
change(tool)
status = run()
assert status.startswith("replaced: mismatch in") and where in status
assert (tool / "files/bin/wine").read_bytes() == FILES["files/bin/wine"]
assert not (tool / "user_settings.py").exists()
assert sorted(os.listdir(tools)) == ["proton-cachyos-native"]
def test_replacing_is_deferred_while_steam_or_a_game_runs(setup, monkeypatch):
tools, _, run = setup
tool = _existing_copy(tools)
(tool / "proton").write_text("old")
monkeypatch.setattr(proton_native, "in_use", lambda t: [4242])
status = run()
assert status.startswith("deferred")
assert (tool / "proton").read_text() == "old"
assert not (tool / "VERSION").exists()
def test_an_older_stamp_is_updated(setup):
tools, _, run = setup
tool = _existing_copy(tools)
(tool / "VERSION").write_text("1:10.0-1\n")
assert run().startswith("updated: 1:10.0-1 ->")
assert (tool / "VERSION").read_text() == VERSION + "\n"
def test_in_use_sees_steam_and_processes_running_from_the_tool(tmp_path, monkeypatch):
proc = tmp_path / "proc"
tool = tmp_path / "tool"
(tool / "files/bin").mkdir(parents=True)
(tool / "files/bin/wine").write_text("")
(tmp_path / "other").write_text("")
for pid, target in (
("10", tool / "files/bin/wine"),
("11", tmp_path / "other"),
("13", tmp_path / "gone"),
):
(proc / pid).mkdir(parents=True)
os.symlink(target, proc / pid / "exe")
(proc / "self").mkdir()
(proc / "12").mkdir()
monkeypatch.setattr(proton_native.recognize, "PROC_DIR", proc)
monkeypatch.setattr(proton_native.recognize, "steam_client_pids", lambda: [99])
assert proton_native.in_use(tool) == [99, 10]

View File

@ -1,81 +0,0 @@
"""The proton_native entry point: what install.sh and run.sh call."""
import subprocess
import sys
from steam_installer import proton_native, proton_pkg
def test_main_reports_and_exits(monkeypatch, capsys, tmp_path):
monkeypatch.setattr(proton_native, "ensure", lambda *a, **kw: "current: x")
monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path)
assert proton_native.main([]) == 0
assert "current: x" in capsys.readouterr().out
def boom(*a, **kw):
raise proton_pkg.PkgError("bad signature")
monkeypatch.setattr(proton_native, "ensure", boom)
assert proton_native.main([]) == 1
assert "bad signature" in capsys.readouterr().err
def test_check_reports_whether_the_stamp_matches_the_lock(tmp_path, monkeypatch):
monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path)
assert proton_native.main(["--check"]) == 1
tool = tmp_path / "compatibilitytools.d" / "proton-cachyos-native"
tool.mkdir(parents=True)
(tool / "VERSION").write_text(proton_pkg.read_lock().version + "\n")
assert proton_native.main(["--check"]) == 0
def test_no_steam_installation_means_nothing_to_install(monkeypatch, capsys):
def absent():
raise FileNotFoundError("No Steam installation found")
monkeypatch.setattr(proton_native, "find_steam_root", absent)
assert proton_native.main([]) == 0
assert proton_native.main(["--check"]) == 0
assert "skipped" in capsys.readouterr().out
def test_the_repo_can_be_pointed_at_a_test_mirror(monkeypatch, tmp_path, capsys):
seen = {}
monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path)
monkeypatch.setenv("SGI_PROTON_REPO", "file:///mirror/cachyos.db")
monkeypatch.setattr(proton_native, "ensure", lambda d, lock: seen.setdefault("repo", lock.repo))
assert proton_native.main([]) == 0
assert seen["repo"] == "file:///mirror/cachyos.db"
def test_module_runs_as_a_script(tmp_path, coverage_subprocess_env):
env = {**coverage_subprocess_env, "HOME": str(tmp_path)}
result = subprocess.run(
[sys.executable, "-m", "steam_installer.proton_native", "--check"],
capture_output=True, text=True, env=env, timeout=30, check=False,
)
assert result.returncode == 0
assert "skipped" in result.stdout
def test_downloads_send_a_user_agent(monkeypatch, tmp_path):
"""The CachyOS mirror answers 403 to urllib's default agent (2026-11-22)."""
import io
seen = {}
class _Response(io.BytesIO):
def __enter__(self):
return self
def __exit__(self, *a):
return False
def fake_urlopen(request, timeout):
seen["agent"] = request.get_header("User-agent")
return _Response(b"data")
monkeypatch.setattr(proton_native.urllib.request, "urlopen", fake_urlopen)
proton_native._download("https://m/x", tmp_path / "x")
assert (tmp_path / "x").read_bytes() == b"data"
assert seen["agent"] == "steam-game-installer"

View File

@ -32,6 +32,21 @@ else
echo "(The rest of this tool is unaffected.)"
fi
# The Steam game installer (steam_installer, in this repo) fast-installs the
# games the daemon assigns: depotdownloader, kuhy-credit and the native Proton.
# Runs as the desktop user (it writes ~/.local and Steam's compatibilitytools.d)
# and is not fatal: without it the daemon falls back to Steam's own download.
echo
echo "Installing the Steam game installer's dependencies..."
INSTALLER_DEPS="$SCRIPT_DIR/steam_installer/install_deps.sh"
if [[ $EUID -eq 0 && -n ${SUDO_USER:-} ]]; then
sudo -u "$SUDO_USER" bash "$INSTALLER_DEPS" \
|| echo "Warning: installer dependencies incomplete (Steam's own download is used)."
else
bash "$INSTALLER_DEPS" \
|| echo "Warning: installer dependencies incomplete (Steam's own download is used)."
fi
# Install the web-UI user unit. No root needed: it serves read-only views and
# needs no privilege. Automated rather than documented, because the whole
# reason this unit exists is that a hand-started server outlived its own code.

View File

@ -1,7 +1,7 @@
[project]
name = "steam-backlog-enforcer"
version = "1.0.0"
description = "Enforce your Steam backlog: one game at a time, tracked with HowLongToBeat data"
description = "Enforce your Steam backlog: one game at a time, tracked with HowLongToBeat data. Also ships steam_installer, a fast Steam game installer."
# 3.11 floor: websockets 17 (and its successors) drop 3.10, and this repo
# tracks newest stable rather than the oldest interpreter still alive.
requires-python = ">=3.14"
@ -21,8 +21,16 @@ dependencies = [
# contract C4); stdlib-only, imported by root from the user site like
# earned-time.
"leisure-watch @ git+https://github.com/kuhyx/leisure-watch@leisure-watch-v0.2.9",
# kuhy's shared "Created by kuhy" credit, shown at the end of the
# installer's --help (steam_installer).
"kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit",
]
# The installer's console script; the daemon itself runs
# `python3 -m steam_installer.cli` from the checkout.
[project.scripts]
steam-game-installer = "steam_installer.cli:main"
# Optional extras. The `mcp` group is ONLY needed by the MCP server
# (steam_backlog_enforcer._mcp), which Claude Code spawns via a dedicated
# venv (see scripts/setup_mcp.sh / .mcp.json). It is deliberately NOT a core
@ -37,7 +45,12 @@ mcp = ["mcp==2.3.0"]
# to guess. Scoping discovery to the Python package fixes `pip install -e .`
# for the CLI, systemd, AND the dedicated MCP venv.
[tool.setuptools.packages.find]
include = ["steam_backlog_enforcer*"]
include = ["steam_backlog_enforcer*", "steam_installer*"]
[tool.setuptools.package-data]
# The pinned Proton lock, its signing key and the compat overrides are read
# at run time through `Path(__file__).parent / "data"`.
steam_installer = ["data/*"]
# ============================================================================
# RUFF - Fast Python linter and formatter
@ -79,6 +92,13 @@ unfixable = []
# libX11 puts local display sockets in /tmp/.X11-unix; the path is not ours to
# move to a private temp dir (and `# noqa` is banned by pre-commit).
"steam_backlog_enforcer/_desktop_env.py" = ["S108"]
# open_https() allows only https URLs: the scheme is checked just above the
# urlopen call, so the S310 "file:/custom scheme" worry cannot occur there.
"steam_installer/_http.py" = ["S310"]
# The pinned Proton package is fetched from lock.repo or SGI_PROTON_REPO (an
# operator-set local mirror, file:// or http://); the db is sha256-pinned and
# the package sha256- and gpg-verified, so the transport needs no restriction.
"steam_installer/proton_native.py" = ["S310"]
[tool.ruff.lint.pydocstyle]
convention = "google"
@ -86,7 +106,7 @@ convention = "google"
[tool.ruff.lint.isort]
force-single-line = false
force-sort-within-sections = true
known-first-party = ["steam_backlog_enforcer"]
known-first-party = ["steam_backlog_enforcer", "steam_installer"]
[tool.ruff.lint.flake8-quotes]
docstring-quotes = "double"
@ -205,7 +225,7 @@ exclude_dirs = ["tests", ".venv"]
# PYTEST
# ============================================================================
[tool.pytest.ini_options]
testpaths = ["steam_backlog_enforcer/tests"]
testpaths = ["steam_backlog_enforcer/tests", "steam_installer/tests"]
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
@ -215,6 +235,7 @@ addopts = [
"--strict-config",
"-ra",
"--cov=steam_backlog_enforcer",
"--cov=steam_installer",
"--cov-branch",
"--cov-report=term-missing",
"--cov-report=lcov",
@ -229,7 +250,7 @@ filterwarnings = [
# COVERAGE
# ============================================================================
[tool.coverage.run]
source = ["steam_backlog_enforcer"]
source = ["steam_backlog_enforcer", "steam_installer"]
branch = true
omit = ["*/__pycache__/*", "*/tests/*", "*/.venv/*"]

View File

@ -1,4 +1,4 @@
# Steam Backlog Enforcer — runtime + development dependencies
# Steam Backlog Enforcer (and steam_installer) — runtime + development dependencies
# Install with: pip install -r requirements.txt
aiohttp==3.14.5
@ -7,6 +7,7 @@ codespell==2.4.3
coverage==7.17.4
earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.23.0#subdirectory=earned_time
howlongtobeatpy==1.0.24
kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit
leisure-watch @ git+https://github.com/kuhyx/leisure-watch@leisure-watch-v0.2.9
mcp==2.3.0
mypy==2.4.1

View File

@ -18,7 +18,57 @@
set -euo pipefail
readonly SHARED_GATE="${UTILS_ROOT:-$HOME/src/utils}/scripts/check_dependency_freshness.sh"
readonly UTILS="${UTILS_ROOT:-$HOME/src/utils}"
readonly SHARED_GATE="$UTILS/scripts/check_dependency_freshness.sh"
readonly PACMAN_PARSER="$UTILS/dep_freshness/parsers/pacman_lock.py"
# True when this run must judge the native-Proton lock (a full run, or the lock staged).
needs_pacman_adapter() {
local arg
for arg in "$@"; do
[[ "$arg" == "--all" || "$arg" == *proton-native.lock ]] && return 0
done
return 1
}
# git against the utils checkout. A pre-commit run inherits the committing
# repo's git location variables, which would point these commands at the wrong
# repository, so they are cleared first.
utils_git() {
env -u GIT_DIR -u GIT_WORK_TREE -u GIT_INDEX_FILE -u GIT_COMMON_DIR \
-u GIT_PREFIX git -C "$UTILS" "$@"
}
# Print a gate directory that can read the Proton lock, or nothing.
#
# kuhy's ~/src/utils can lag origin/main (local commits not yet pushed, so it
# cannot fast-forward). Then the gate runs a read-only snapshot of utils'
# origin/main from the cache instead: the checkout itself is never touched.
gate_root_for_lock() {
if [[ -f "$PACMAN_PARSER" ]]; then
echo "$UTILS"
return
fi
local sha snapshot
local -a paths=(scripts/check_dependency_freshness.sh dep_freshness)
utils_git cat-file -e origin/main:dep_freshness/parsers/pacman_lock.py 2>/dev/null ||
return 0
sha="$(utils_git rev-parse origin/main)"
snapshot="${XDG_CACHE_HOME:-$HOME/.cache}/steam-game-installer/utils-gate/$sha"
if [[ ! -f "$snapshot/dep_freshness/parsers/pacman_lock.py" ]]; then
rm -rf "$snapshot.tmp"
mkdir -p "$snapshot.tmp"
# The fleet allowlist lives beside the gate, when utils has one.
if utils_git cat-file -e origin/main:dependency-freshness.allowlist.yaml 2>/dev/null; then
paths+=(dependency-freshness.allowlist.yaml)
fi
utils_git archive origin/main "${paths[@]}" | tar -x -C "$snapshot.tmp"
rm -rf "$snapshot"
mv "$snapshot.tmp" "$snapshot"
fi
echo "dependency-freshness: $UTILS lacks the pacman adapter; using its origin/main ${sha:0:12}" >&2
echo "$snapshot"
}
main() {
if [[ ! -x "$SHARED_GATE" ]]; then
@ -28,7 +78,20 @@ main() {
exit 1
fi
exec bash "$SHARED_GATE" "$@"
local root="$UTILS"
# An older utils checkout skips a manifest it has no parser for and exits
# 0, which would wave any Proton pin through. Fail closed instead.
if needs_pacman_adapter "$@"; then
root="$(gate_root_for_lock)"
if [[ -z "$root" ]]; then
echo "Error: $UTILS has no dep_freshness/parsers/pacman_lock.py, so it" >&2
echo " cannot check steam_installer/data/proton-native.lock." >&2
echo " Update it: git -C $UTILS pull --ff-only" >&2
exit 1
fi
fi
exec bash "$root/scripts/check_dependency_freshness.sh" "$@"
}
main "$@"

View File

@ -96,14 +96,21 @@ def _top_level_imports(module: Path) -> set[str]:
def _third_party_imports(package_dir: Path, package_name: str) -> set[str]:
"""Return third-party top-level imports across the whole package.
Standard-library modules (via ``sys.stdlib_module_names``) and the package's
own name are filtered out, leaving only external dependencies.
Standard-library modules (via ``sys.stdlib_module_names``), the package's own
name and the repo's sibling packages (``steam_backlog_enforcer`` imports
``steam_installer`` from the same checkout) are filtered out, leaving only
external dependencies.
"""
stdlib = sys.stdlib_module_names
first_party = {
sibling.name
for sibling in package_dir.parent.iterdir()
if (sibling / "__init__.py").is_file()
} | {package_name}
external: set[str] = set()
for module in _iter_package_modules(package_dir):
for name in _top_level_imports(module):
if name != package_name and name not in stdlib:
if name not in first_party and name not in stdlib:
external.add(name)
return external

View File

@ -16,23 +16,17 @@ from __future__ import annotations
import struct
from typing import TYPE_CHECKING, Final
from steam_installer.appinfo_kv import HEADER, MAGIC, read_block, string_table
if TYPE_CHECKING:
from collections.abc import Iterable
from pathlib import Path
from steam_backlog_enforcer._vdf_text import VdfNode
MAGIC: Final = 0x07564429
# "SteamPlay 2.0 Manifests": its extended/app_mappings is Valve's per-game
# tool pick (e.g. a broken native port sent to Proton).
STEAM_PLAY_APP: Final = 891390
_HEADER: Final = 60
# KV type bytes: a nested block, a NUL-terminated string, end of block.
_BLOCK: Final = 0
_STRING: Final = 1
_END: Final = 8
# Fixed-width value types: KV type byte -> struct format.
_SCALARS: Final = {2: "<i", 3: "<f", 4: "<i", 6: "<i", 7: "<Q", 10: "<q"}
def default_path(steam_root: Path) -> Path:
@ -60,7 +54,7 @@ def _parse(buf: bytes, wanted: set[int]) -> dict[int, dict[str, VdfNode]]:
if magic != MAGIC:
msg = f"unsupported appinfo.vdf format {magic:#010x}"
raise ValueError(msg)
strings = _string_table(buf, table_offset)
strings = string_table(buf, table_offset)
apps: dict[int, dict[str, VdfNode]] = {}
pos = 16
while True:
@ -68,44 +62,7 @@ def _parse(buf: bytes, wanted: set[int]) -> dict[int, dict[str, VdfNode]]:
if app_id == 0:
return apps
if app_id in wanted:
data, _ = _kv(buf, pos + 8 + _HEADER, strings)
data, _ = read_block(buf, pos + 8 + HEADER, strings)
inner = data.get("appinfo")
apps[app_id] = inner if isinstance(inner, dict) else data
pos += 8 + size
def _string_table(buf: bytes, offset: int) -> list[str]:
(count,) = struct.unpack_from("<I", buf, offset)
pos = offset + 4
out: list[str] = []
for _ in range(count):
end = buf.index(b"\0", pos)
out.append(buf[pos:end].decode("utf-8", "replace"))
pos = end + 1
return out
def _kv(buf: bytes, pos: int, strings: list[str]) -> tuple[dict[str, VdfNode], int]:
out: dict[str, VdfNode] = {}
while True:
kind = buf[pos]
pos += 1
if kind == _END:
return out, pos
(index,) = struct.unpack_from("<I", buf, pos)
pos += 4
key = strings[index]
if kind == _BLOCK:
out[key], pos = _kv(buf, pos, strings)
elif kind == _STRING:
end = buf.index(b"\0", pos)
out[key] = buf[pos:end].decode("utf-8", "replace")
pos = end + 1
elif kind in _SCALARS:
fmt = _SCALARS[kind]
(value,) = struct.unpack_from(fmt, buf, pos)
out[key] = str(value)
pos += struct.calcsize(fmt)
else:
msg = f"unsupported value type {kind} in appinfo.vdf"
raise ValueError(msg)

View File

@ -1,7 +1,7 @@
"""Installing games through steam-game-installer, for download speed.
depotdownloader saturates the link far better than the Steam client, and
steam-game-installer (``~/src/steam-game-installer``) wraps it so Steam accepts
steam-game-installer (``steam_installer``, in this repo) wraps it so Steam accepts
the result as a real install. It also adopts whatever Steam already staged, so
taking over a slow client download loses nothing.
@ -204,7 +204,7 @@ def fast_install(
if app_id in _FAILED:
return False
user = resolve_desktop_user()
installer = installer_path(user)
installer = installer_path()
if installer is None:
logger.warning("steam-game-installer not found; using Steam's download.")
return False

View File

@ -1,4 +1,4 @@
"""How steam-game-installer is invoked: where it lives, as whom, with what argv.
"""How the installer (``steam_installer``) is invoked: where, as whom, what argv.
Split out of :mod:`_fast_install`, which runs and reaps the installer; this
half only decides the command line, every choice it would otherwise prompt for
@ -8,31 +8,32 @@ pinned in advance.
from __future__ import annotations
from pathlib import Path
import pwd
import re
from steam_backlog_enforcer._desktop_env import desktop_user_cmd
from steam_backlog_enforcer._steam_state import STEAMAPPS_PATH
_INSTALLER = Path("src", "steam-game-installer", "run.sh")
# The installer ships in this repo (``steam_installer``, a sibling package).
# Tests repoint this at a missing file: it always exists here, and a real run
# closes the user's Steam.
_INSTALLER = Path(__file__).resolve().parents[1] / "steam_installer" / "cli.py"
# The interpreter the daemon itself runs under (see the unit's ExecStart). The
# installer needs ``kuhy_credit``, which lives in the desktop user's site.
_PYTHON = "/usr/bin/python3"
# Depot content only: the app itself is ours to choose, the language is not
# worth a prompt nobody is there to answer.
_LANGUAGE = "english"
def installer_path(user: str | None) -> Path | None:
"""steam-game-installer's ``run.sh`` in the desktop user's home, if present.
def installer_path() -> Path | None:
"""The installer's ``cli.py`` inside this checkout, if present.
Resolved from the passwd entry rather than ``Path.home()``: the daemon runs
as root, and the installer, its venv and its credentials are per-user.
A checkout is the same for every user, so unlike the old external clone
there is no per-user lookup.
"""
try:
home = Path(pwd.getpwnam(user).pw_dir) if user else Path.home()
except KeyError:
return None
path = home / _INSTALLER
return path if path.is_file() else None
return _INSTALLER if _INSTALLER.is_file() else None
def steam_account_name() -> str | None:
@ -51,11 +52,17 @@ def steam_account_name() -> str | None:
def build_cmd(installer: Path, app_id: int, user: str | None) -> list[str]:
"""The installer invocation, dropped to the desktop user when root."""
"""The installer invocation, dropped to the desktop user when root.
Launched as ``python3 -m steam_installer.cli`` with the checkout on
``PYTHONPATH``: ``sudo`` resets the environment, so it is named in the argv.
"""
cmd = [
str(installer),
# install.sh reaches for yay; dependencies are installed once, by hand.
"--no-deps",
"env",
f"PYTHONPATH={installer.parents[1]}",
_PYTHON,
"-m",
"steam_installer.cli",
"--appid",
str(app_id),
"--yes",

View File

@ -70,10 +70,11 @@ def assert_safe_to_restart() -> None:
# Matched against /proc cmdlines, so a run started by hand counts exactly like
# one the enforcer spawned. run.sh as well as the module it execs: the sudo
# and bash wrappers exist the moment Popen returns, the Python process only a
# moment later, and a Steam launch in that gap would race the shutdown.
_INSTALLER_MARKERS = (b"steam-game-installer/run.sh", b"steam_installer.cli")
# one the enforcer spawned. The module name is in the argv of every wrapper
# too (``sudo``, ``env``), which exist the moment Popen returns while the
# Python process follows a moment later, so a Steam launch cannot slip into
# that gap. ``run.sh`` is the by-hand launcher that execs the same module.
_INSTALLER_MARKERS = (b"steam_installer/run.sh", b"steam_installer.cli")
def fast_install_running() -> bool:

View File

@ -46,9 +46,10 @@ def _no_fast_install(tmp_path: Path) -> Iterator[None]:
_FAILURES.clear()
patches = [patch(name, return_value=False) for name in _RUNNING_BINDINGS]
patches += [
# Absolute, so `home / _INSTALLER` resolves here and never under ~.
# The installer is in this repo now, so it always exists: point the
# constant at a file that does not, or a test could close real Steam.
patch.object(
_fast_install_cmd, "_INSTALLER", tmp_path / "no-installer" / "run.sh"
_fast_install_cmd, "_INSTALLER", tmp_path / "no-installer" / "cli.py"
),
patch.object(_fast_install_cmd, "STEAMAPPS_PATH", fake_steamapps),
]

View File

@ -101,7 +101,7 @@ def _drives_steam(argv: Sequence[str]) -> bool:
for arg in argv:
if PurePath(arg).name in _DENIED_TOOLS:
return True
if arg.startswith("steam://") or arg == "-shutdown":
if arg.startswith("steam://") or arg in {"-shutdown", "steam_installer.cli"}:
return True
return False

View File

@ -60,12 +60,15 @@ class TestFastInstallRunning:
@pytest.mark.parametrize(
"cmdline",
[
b"bash\0/home/u/src/steam-game-installer/run.sh\0--appid\0001",
(
b"sudo\0-u\0u\0env\0PYTHONPATH=/r\0/usr/bin/python3\0"
b"-m\0steam_installer.cli\0--appid\0001"
),
b"python\0-m\0steam_installer.cli\0--appid\0001",
],
)
def test_detects_installer(self, tmp_path: Path, cmdline: bytes) -> None:
"""Both the run.sh wrapper and the Python module count as the installer."""
"""The sudo wrapper and the Python module both count as the installer."""
proc = self._proc(tmp_path, {"self": b"", "10": None, "11": cmdline})
with patch(f"{GUARD}.Path", return_value=proc):
assert fast_install_running() is True

View File

@ -7,7 +7,6 @@ to keep both files under the 250-line cap.
from __future__ import annotations
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from steam_backlog_enforcer import _fast_install, _fast_install_cmd, _steam_state
@ -17,41 +16,6 @@ PKG = "steam_backlog_enforcer._fast_install"
CMD = "steam_backlog_enforcer._fast_install_cmd"
class TestInstallerPath:
"""run.sh is resolved from the desktop user's passwd home."""
def test_found_in_user_home(self, tmp_path: Path) -> None:
"""run.sh is found under the desktop user's passwd home."""
installer = tmp_path / "src" / "steam-game-installer" / "run.sh"
installer.parent.mkdir(parents=True)
installer.write_text("#!/bin/bash\n")
# The autouse guard points _INSTALLER at a dead absolute path; put the
# real relative one back so the home join itself is what is tested.
relative = Path("src", "steam-game-installer", "run.sh")
with (
patch.object(_fast_install_cmd, "_INSTALLER", relative),
patch(f"{CMD}.pwd.getpwnam", return_value=SimpleNamespace(pw_dir=tmp_path)),
):
assert _fast_install_cmd.installer_path("kuhy") == installer
def test_missing_file_is_none(self, tmp_path: Path) -> None:
"""A home without run.sh yields no installer."""
with patch(
f"{CMD}.pwd.getpwnam", return_value=SimpleNamespace(pw_dir=tmp_path)
):
assert _fast_install_cmd.installer_path("kuhy") is None
def test_unknown_user_is_none(self) -> None:
"""A user unknown to passwd yields no installer."""
with patch(f"{CMD}.pwd.getpwnam", side_effect=KeyError("nobody")):
assert _fast_install_cmd.installer_path("nobody") is None
def test_no_user_uses_own_home(self, tmp_path: Path) -> None:
"""With no user the process's own home is searched."""
with patch(f"{CMD}.Path.home", return_value=tmp_path):
assert _fast_install_cmd.installer_path(None) is None
class TestSteamAccountName:
"""The username comes from Steam's own loginusers.vdf."""
@ -105,10 +69,15 @@ class TestBuildCmd:
patch(f"{CMD}.steam_account_name", return_value="krzys"),
patch(f"{CMD}.desktop_user_cmd", side_effect=lambda cmd, _u: cmd),
):
cmd = _fast_install_cmd.build_cmd(Path("/i/run.sh"), 42, "kuhy")
cmd = _fast_install_cmd.build_cmd(
Path("/r/steam_installer/cli.py"), 42, "kuhy"
)
assert cmd == [
"/i/run.sh",
"--no-deps",
"env",
"PYTHONPATH=/r",
"/usr/bin/python3",
"-m",
"steam_installer.cli",
"--appid",
"42",
"--yes",
@ -124,9 +93,9 @@ class TestBuildCmd:
patch(f"{CMD}.steam_account_name", return_value=None),
patch(f"{CMD}.desktop_user_cmd", return_value=["wrapped"]) as wrap,
):
assert _fast_install_cmd.build_cmd(Path("/i/run.sh"), 42, "kuhy") == [
"wrapped"
]
assert _fast_install_cmd.build_cmd(
Path("/r/steam_installer/cli.py"), 42, "kuhy"
) == ["wrapped"]
argv, user = wrap.call_args.args
assert "--username" not in argv
assert user == "kuhy"

View File

@ -0,0 +1,87 @@
"""The installer ships in this repo: where it is, and the argv that starts it.
The daemon is root and the installer runs as the desktop user, so every piece
of the command line must survive ``sudo``: the interpreter, ``PYTHONPATH`` and
a flag set the installer's own parser accepts. A flag the parser rejects makes
argparse exit 2 and every fast install silently fall back to Steam's download.
"""
from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
import pytest
from steam_backlog_enforcer import _fast_install_cmd
from steam_backlog_enforcer._fast_install_cmd import build_cmd, installer_path
from steam_backlog_enforcer._steam_restart_guard import _INSTALLER_MARKERS
from steam_installer import cli_args
CMD = "steam_backlog_enforcer._fast_install_cmd"
# Read at import: the autouse guard swaps the attribute out before each test.
REAL_INSTALLER: Path = vars(_fast_install_cmd)["_INSTALLER"]
REPO = Path(__file__).resolve().parents[2]
def _argv_after_launcher(cmd: list[str]) -> list[str]:
"""The installer's own arguments: everything after ``-m steam_installer.cli``."""
return cmd[cmd.index("steam_installer.cli") + 1 :]
class TestInstallerLocation:
"""The installer is found in this checkout, never under ~/src."""
def test_real_path_is_inside_this_repo(self) -> None:
"""Unpatched, the installer is this repo's own cli module, and exists."""
assert REAL_INSTALLER == REPO / "steam_installer" / "cli.py"
assert REAL_INSTALLER.is_file()
assert "steam-game-installer" not in REAL_INSTALLER.parts
def test_found_when_present(self) -> None:
"""installer_path returns the repo's cli module."""
with patch.object(_fast_install_cmd, "_INSTALLER", REAL_INSTALLER):
assert installer_path() == REAL_INSTALLER
def test_missing_is_none(self, tmp_path: Path) -> None:
"""A checkout without the installer falls back to Steam's download."""
with patch.object(_fast_install_cmd, "_INSTALLER", tmp_path / "gone.py"):
assert installer_path() is None
class TestBuiltArgv:
"""The exact argv the daemon hands to the desktop user."""
@pytest.fixture(name="argv")
def _argv(self) -> list[str]:
with patch(f"{CMD}.steam_account_name", return_value="krzys"):
return build_cmd(REAL_INSTALLER, 284160, None)
def test_installer_parser_accepts_it(self, argv: list[str]) -> None:
"""The installer's argparse takes the argv as built, with no exit 2."""
args = cli_args.build_parser().parse_args(_argv_after_launcher(argv))
assert args.appid == 284160
assert args.yes is True
assert args.language == "english"
assert args.username == "krzys"
def test_parser_accepts_it_without_account(self) -> None:
"""No signed-in account means no ``--username``, still parseable."""
with patch(f"{CMD}.steam_account_name", return_value=None):
argv = build_cmd(REAL_INSTALLER, 7, None)
cli_args.build_parser().parse_args(_argv_after_launcher(argv))
assert "--username" not in argv
def test_no_run_sh_only_flag(self, argv: list[str]) -> None:
"""``--no-deps`` belongs to run.sh; the module's parser has no such flag."""
assert "--no-deps" not in argv
def test_imports_from_this_checkout(self, argv: list[str]) -> None:
"""sudo resets the environment, so PYTHONPATH is named in the argv."""
assert f"PYTHONPATH={REPO}" in argv
assert argv[argv.index("steam_installer.cli") - 1] == "-m"
def test_scan_marker_matches_the_real_cmdline(self, argv: list[str]) -> None:
"""The /proc guard sees the process this argv starts, sudo wrapper too."""
cmdline = b"\0".join(arg.encode() for arg in argv)
assert any(marker in cmdline for marker in _INSTALLER_MARKERS)

View File

@ -63,6 +63,7 @@ class TestDrivesSteam:
["sudo", "-u", "kuhy", "steam.sh"],
["xdg-open", "https://example.com"],
["runuser", "steam-game-installer", "620"],
["python3", "-m", "steam_installer.cli", "--appid", "620"],
["pkill", "-f", "x"],
["killall", "x"],
["/bin/kill", "-TERM", "42"],

View File

@ -0,0 +1 @@
"""Fast Steam game installer (depotdownloader plus a hand-written manifest)."""

26
steam_installer/_http.py Normal file
View File

@ -0,0 +1,26 @@
"""One https-only way to open a URL, shared by the network callers."""
from __future__ import annotations
import contextlib
from typing import TYPE_CHECKING
import urllib.request
if TYPE_CHECKING:
from collections.abc import Iterator
from http.client import HTTPResponse
@contextlib.contextmanager
def open_https(
url: str, *, timeout: float, user_agent: str | None = None
) -> Iterator[HTTPResponse]:
"""Open `url` (https only) with `user_agent`; a context manager."""
if not url.startswith("https://"):
msg = f"refusing a non-https URL: {url}"
raise ValueError(msg)
# No header when none is given: urllib then sends its own default agent.
headers = {} if user_agent is None else {"User-Agent": user_agent}
request = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(request, timeout=timeout) as response:
yield response

View File

@ -11,8 +11,10 @@ from __future__ import annotations
import json
import os
import urllib.request
from pathlib import Path
import urllib.error
from steam_installer import _http
URL = (
"https://raw.githubusercontent.com/AreWeAntiCheatYet/AreWeAntiCheatYet/"
@ -22,7 +24,7 @@ CACHE = Path.home() / ".cache" / "steam-game-installer" / "anticheat-games.json"
def _download() -> bytes:
with urllib.request.urlopen(URL, timeout=20) as response:
with _http.open_https(URL, timeout=20) as response:
return response.read()
@ -30,7 +32,8 @@ def _ids(raw: bytes | str) -> set[int]:
"""Steam ids in a games.json; ValueError/TypeError when it is not one."""
games = json.loads(raw)
if not isinstance(games, list):
raise TypeError("games.json is not a list")
msg = "games.json is not a list"
raise TypeError(msg)
ids: set[int] = set()
for game in games:
stores = game.get("storeIds") if isinstance(game, dict) else None
@ -45,13 +48,14 @@ def steam_ids(cache: Path = CACHE) -> set[int] | None:
try:
raw = _download()
ids = _ids(raw)
except (OSError, ValueError, TypeError):
# URLError is an OSError; named so the handler reads as network failure.
except urllib.error.URLError, OSError, ValueError, TypeError:
try:
return _ids(cache.read_bytes())
except (OSError, ValueError, TypeError):
except OSError, ValueError, TypeError:
return None
cache.parent.mkdir(parents=True, exist_ok=True)
tmp = cache.with_name(f"{cache.name}.tmp{os.getpid()}")
tmp.write_bytes(raw)
os.replace(tmp, cache)
tmp.replace(cache)
return ids

View File

@ -3,19 +3,22 @@
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
from steam_installer import appinfo_cache
from steam_installer import appinfo_cache, errors
# depotdownloader is never given -branch, so it always fetches this one.
PUBLIC_BRANCH = "public"
class AppInfoError(Exception):
pass
class AppInfoError(errors.InstallerError):
"""Steam's app info is missing or unreadable."""
@dataclass
class AppMetadata:
"""What the installer needs to know about one app, from Steam's cache."""
appid: int
name: str
installdir: str
@ -44,10 +47,11 @@ class DepotPlan:
@property
def all_expected(self) -> set[str]:
"""Every depot the install should end up with."""
return self.required | self.shared
def _cached_blocks(appids: list[int]) -> dict[int, dict]:
def cached_blocks(appids: list[int]) -> dict[int, dict[str, Any]]:
"""App info for those of `appids` Steam's local cache holds.
Read from `appcache/appinfo.vdf` (see :mod:`appinfo_cache`), not from
@ -60,46 +64,51 @@ def _cached_blocks(appids: list[int]) -> dict[int, dict]:
return {appid: apps[appid] for appid in appids if appid in apps}
def _fetch_app_blocks(appids: list[int]) -> dict[int, dict]:
def _fetch_app_blocks(appids: list[int]) -> dict[int, dict[str, Any]]:
"""App info for every id in `appids`; a missing one is an error with the fix."""
blocks = _cached_blocks(appids)
blocks = cached_blocks(appids)
missing = [str(appid) for appid in appids if appid not in blocks]
if missing:
raise AppInfoError(
msg = (
f"app {', '.join(missing)} is not in Steam's local app cache: "
f"{appinfo_cache.FIX}"
)
raise AppInfoError(msg)
return blocks
def _fetch_app_block(appid: int) -> dict:
def fetch_app_block(appid: int) -> dict[str, Any]:
"""App info for one id; a missing one is an error with the fix."""
return _fetch_app_blocks([appid])[appid]
def get_metadata(appid: int) -> AppMetadata:
"""Fetch the fields this project actually needs: display name and installdir."""
app_block = _fetch_app_block(appid)
app_block = fetch_app_block(appid)
common = app_block.get("common", {})
if not isinstance(common, dict):
raise AppInfoError(f"no 'common' section in app info for {appid}")
msg = f"no 'common' section in app info for {appid}"
raise AppInfoError(msg)
name = common.get("name")
if not isinstance(name, str):
raise AppInfoError(f"no name field found for app {appid}")
msg = f"no name field found for app {appid}"
raise AppInfoError(msg)
installdir = common.get("installdir")
if not isinstance(installdir, str):
config = app_block.get("config", {})
installdir = config.get("installdir") if isinstance(config, dict) else None
if not isinstance(installdir, str):
raise AppInfoError(f"no installdir field found for app {appid}")
msg = f"no installdir field found for app {appid}"
raise AppInfoError(msg)
return AppMetadata(appid=appid, name=name, installdir=installdir)
def get_build_id(appid: int, branch: str = "public") -> str | None:
"""Current buildid for `branch`, used to sanity-check the manifest we write."""
app_block = _fetch_app_block(appid)
app_block = fetch_app_block(appid)
depots = app_block.get("depots", {})
if not isinstance(depots, dict):
return None
@ -122,10 +131,11 @@ def get_depot_plan(
or marked `optional` -- none of those are fetched by a default install, so
demanding them would make a healthy download look incomplete.
"""
app_block = _fetch_app_block(appid)
app_block = fetch_app_block(appid)
depots = app_block.get("depots", {})
if not isinstance(depots, dict):
raise AppInfoError(f"no 'depots' section in app info for {appid}")
msg = f"no 'depots' section in app info for {appid}"
raise AppInfoError(msg)
plan = _plan_from_depots(appid, depots, language, os_name)
# Tag the game's own list's DLC depots too: depotdownloader fetches the
# owned ones in the base run, and Steam records each with its dlcappid.
@ -149,15 +159,16 @@ def _public_gid(entry: object) -> str | None:
def get_dlc_plans(
appid: int, language: str = "english", os_name: str = "windows"
) -> dict[int, dict[str, str]]:
"""DLC app id -> {depot: public gid} it installs itself, for DLC with depots
of its own.
"""Map each DLC app id to the {depot: public gid} it installs itself.
Only DLC with depots of its own appear.
Some DLC (Cyberpunk 2077's Phantom Liberty, app 2138330, ~25 GB) keeps its
content under its own app id rather than in the game's depot list, so a
depotdownloader run for the game never sees it. DLC with no depots of its
own, or whose depots the game already lists, needs no run of its own.
"""
app_block = _fetch_app_block(appid)
app_block = fetch_app_block(appid)
extended = app_block.get("extended", {})
listed = extended.get("listofdlc", "") if isinstance(extended, dict) else ""
dlc_ids = [int(x) for x in str(listed).split(",") if x.strip().isdigit()]
@ -169,7 +180,7 @@ def get_dlc_plans(
# A DLC the cache lacks is not owned: Steam caches app info for every app
# the account owns (1828 of 7102 listed DLC ids on 2026-11-22), and
# depotdownloader would refuse an unowned one anyway.
for dlc_id, dlc_block in _cached_blocks(dlc_ids).items():
for dlc_id, dlc_block in cached_blocks(dlc_ids).items():
depots = dlc_block.get("depots", {})
if not isinstance(depots, dict):
continue
@ -182,7 +193,7 @@ def get_dlc_plans(
def _plan_from_depots(
appid: int, depots: dict, language: str, os_name: str
appid: int, depots: dict[str, Any], language: str, os_name: str
) -> DepotPlan:
"""The depots of one app's `depots` section that an (os, language) install gets."""
required: set[str] = set()

View File

@ -20,29 +20,36 @@ would silently change which depots count.
from __future__ import annotations
import struct
from pathlib import Path
from typing import TYPE_CHECKING, Any
from steam_installer import errors
from steam_installer.appinfo_kv import (
HEADER,
MAGIC,
AppinfoFormatError,
read_block,
string_table,
)
from steam_installer.library import find_steam_root
MAGIC = 0x07564429
_HEADER = 60
if TYPE_CHECKING:
from pathlib import Path
FIX = "start Steam once, let it sync, rerun"
# Fixed-width value types: KV type byte -> struct format.
_SCALARS = {2: "<i", 3: "<f", 4: "<i", 6: "<i", 7: "<Q", 10: "<q"}
_memo: dict[Path, tuple[tuple[int, int], dict[int, dict]]] = {}
_memo: dict[Path, tuple[tuple[int, int], dict[int, dict[str, Any]]]] = {}
class CacheError(Exception):
pass
class CacheError(errors.InstallerError):
"""Steam's appinfo cache cannot be read."""
def default_path() -> Path:
"""Where Steam keeps its binary appinfo cache."""
return find_steam_root() / "appcache" / "appinfo.vdf"
def load(path: Path | None = None) -> dict[int, dict]:
def load(path: Path | None = None) -> dict[int, dict[str, Any]]:
"""Every app in the cache, appid -> its `appinfo` block.
Parsed once per file version: four lookups in one run would otherwise
@ -52,7 +59,8 @@ def load(path: Path | None = None) -> dict[int, dict]:
try:
stat = path.stat()
except OSError as exc:
raise CacheError(f"cannot read Steam's app cache {path}: {FIX}") from exc
msg = f"cannot read Steam's app cache {path}: {FIX}"
raise CacheError(msg) from exc
key = (stat.st_mtime_ns, stat.st_size)
cached = _memo.get(path)
if cached is not None and cached[0] == key:
@ -62,54 +70,22 @@ def load(path: Path | None = None) -> dict[int, dict]:
return apps
def _parse(buf: bytes) -> dict[int, dict]:
def _parse(buf: bytes) -> dict[int, dict[str, Any]]:
magic, _universe, table_offset = struct.unpack_from("<IIq", buf, 0)
if magic != MAGIC:
raise CacheError(f"unsupported appinfo.vdf format {magic:#010x}")
strings = _string_table(buf, table_offset)
apps: dict[int, dict] = {}
msg = f"unsupported appinfo.vdf format {magic:#010x}"
raise CacheError(msg)
strings = string_table(buf, table_offset)
apps: dict[int, dict[str, Any]] = {}
pos = 16
while True:
appid, size = struct.unpack_from("<II", buf, pos)
if appid == 0:
return apps
data, _ = _kv(buf, pos + 8 + _HEADER, strings)
try:
data, _ = read_block(buf, pos + 8 + HEADER, strings)
except AppinfoFormatError as exc:
raise CacheError(str(exc)) from exc
inner = data.get("appinfo")
apps[appid] = inner if isinstance(inner, dict) else data
pos += 8 + size
def _string_table(buf: bytes, offset: int) -> list[str]:
(count,) = struct.unpack_from("<I", buf, offset)
pos = offset + 4
out: list[str] = []
for _ in range(count):
end = buf.index(b"\0", pos)
out.append(buf[pos:end].decode("utf-8", "replace"))
pos = end + 1
return out
def _kv(buf: bytes, pos: int, strings: list[str]) -> tuple[dict, int]:
out: dict = {}
while True:
kind = buf[pos]
pos += 1
if kind == 8:
return out, pos
(index,) = struct.unpack_from("<I", buf, pos)
pos += 4
key = strings[index]
if kind == 0:
out[key], pos = _kv(buf, pos, strings)
elif kind == 1:
end = buf.index(b"\0", pos)
out[key] = buf[pos:end].decode("utf-8", "replace")
pos = end + 1
elif kind in _SCALARS:
fmt = _SCALARS[kind]
(value,) = struct.unpack_from(fmt, buf, pos)
out[key] = str(value)
pos += struct.calcsize(fmt)
else:
raise CacheError(f"unsupported value type {kind} in appinfo.vdf")

View File

@ -0,0 +1,68 @@
"""Binary key-value decoding shared by both readers of ``appinfo.vdf``.
``steam_installer.appinfo_cache`` (every app) and
``steam_backlog_enforcer._appinfo_cache`` (selected apps) read the same v29
layout; this module holds the parts they have in common: the string table and
the nested key-value blocks. It raises ``AppinfoFormatError`` (a ``ValueError``)
and each reader keeps the exception type its callers already handle.
"""
from __future__ import annotations
import struct
from typing import Any, Final
MAGIC: Final = 0x07564429
HEADER: Final = 60
# KV type bytes: a nested block, a NUL-terminated string, end of block.
_BLOCK: Final = 0
_STRING: Final = 1
_END: Final = 8
# Fixed-width value types: KV type byte -> struct format.
_SCALARS: Final = {2: "<i", 3: "<f", 4: "<i", 6: "<i", 7: "<Q", 10: "<q"}
class AppinfoFormatError(ValueError):
"""A key-value block holds a value type this decoder does not know."""
def string_table(buf: bytes, offset: int) -> list[str]:
"""The NUL-terminated strings (u32 count first) that keys index into."""
(count,) = struct.unpack_from("<I", buf, offset)
pos = offset + 4
out: list[str] = []
for _ in range(count):
end = buf.index(b"\0", pos)
out.append(buf[pos:end].decode("utf-8", "replace"))
pos = end + 1
return out
def read_block(buf: bytes, pos: int, strings: list[str]) -> tuple[dict[str, Any], int]:
"""Decode one key-value block at *pos*; return it and the next position.
Every scalar comes back as a string, as Steam's text VDF prints it.
"""
out: dict[str, Any] = {}
while True:
kind = buf[pos]
pos += 1
if kind == _END:
return out, pos
(index,) = struct.unpack_from("<I", buf, pos)
pos += 4
key = strings[index]
if kind == _BLOCK:
out[key], pos = read_block(buf, pos, strings)
elif kind == _STRING:
end = buf.index(b"\0", pos)
out[key] = buf[pos:end].decode("utf-8", "replace")
pos = end + 1
elif kind in _SCALARS:
fmt = _SCALARS[kind]
(value,) = struct.unpack_from(fmt, buf, pos)
out[key] = str(value)
pos += struct.calcsize(fmt)
else:
msg = f"unsupported value type {kind} in appinfo.vdf"
raise AppinfoFormatError(msg)

View File

@ -1,16 +1,16 @@
"""Entry point: resolve -> confirm -> adopt what exists -> fast download -> recognize."""
"""Entry point: resolve, confirm, adopt what exists, download, recognize."""
from __future__ import annotations
import argparse
import logging
import sys
from kuhy_credit import credit_line
from typing import TYPE_CHECKING
from steam_installer import (
cli_args,
config,
depotdl,
deps,
errors,
flow,
ghost,
map_installed,
@ -19,98 +19,49 @@ from steam_installer import (
takeover,
target,
)
from steam_installer.job import Job
from steam_installer.library import find_steam_root, list_libraries, pick_install_target
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Download an owned Steam game fast, then make Steam recognize it.",
# kuhy's shared credit line (kuhy_credit, kuhyx/utils).
epilog=credit_line(),
)
parser.add_argument(
"target",
nargs="?",
default=None,
metavar="TARGET",
help=(
"AppID (284160), a Steam URL "
"(https://steamcommunity.com/app/284160/guides/), or a game name "
"(BeamNG.drive, or just BeamNG)"
),
)
parser.add_argument(
"--appid", type=int, default=None, help="Steam AppID (skips detection)"
)
parser.add_argument(
"--username",
default=None,
help="Steam account username (default: $STEAM_USERNAME, then the saved one)",
)
parser.add_argument(
"--language", default=None, help="Language depot to fetch (default: prompts)"
)
parser.add_argument(
"--max-downloads", type=int, default=depotdl.DEFAULT_MAX_DOWNLOADS
)
parser.add_argument(
"--yes",
action="store_true",
help="Assume yes for every prompt. Ambiguous names still fail rather than guess.",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Resolve the target and report what would happen. Changes nothing.",
)
parser.add_argument(
"--repair-ghosts",
action="store_true",
help=(
"Scan every library for the fake 'installed' manifests anonymous "
"steamcmd leaves behind, and clear them away."
),
)
map_installed.add_arguments(parser)
parser.add_argument(
"--verbose",
action="store_true",
help="Show on the console everything that goes to the run log.",
)
return parser
if TYPE_CHECKING:
import argparse
from pathlib import Path
def main(argv: list[str] | None = None) -> int:
parser = _build_parser()
"""Run the installer; return the process exit code."""
parser = cli_args.build_parser()
args = parser.parse_args(argv)
if not (args.repair_ghosts or args.map_installed) and not (args.appid or args.target):
if not (args.repair_ghosts or args.map_installed) and not (
args.appid or args.target
):
parser.error(
"provide an AppID, a Steam URL, a game name, --repair-ghosts or --map-installed"
"provide an AppID, a Steam URL, a game name, "
"--repair-ghosts or --map-installed"
)
map_installed.check_arguments(parser, args)
literal = target.parse_literal(args.target) if args.target else None
log = runlog.setup(args.appid or (literal[0] if literal else None), args.verbose)
log = runlog.setup(
args.appid or (literal[0] if literal else None), verbose=args.verbose
)
logger = log.logger
# Printed up front as well as at the end: if the run dies badly, the path
# still has to be on screen to copy-paste.
logger.info("Run log: %s", log.path)
logger.debug("argv: %s", sys.argv)
try:
# Any Exception ends in "log it, exit 1" plus the run-log path, as before.
with errors.CrashGuard(logger, log.path):
return _run(args, log)
except Exception:
logger.exception("Run failed with an unhandled error")
logger.info("Run log (send this): %s", log.path)
return 1
return 1
def _is_interactive(args: argparse.Namespace) -> bool:
return sys.stdin.isatty() and not args.yes
def _ask(args: argparse.Namespace, interactive: bool, question: str) -> bool:
def _ask(args: argparse.Namespace, question: str, *, interactive: bool) -> bool:
if args.yes:
return True
if not interactive:
@ -143,7 +94,12 @@ def _repair_ghosts(log: runlog.RunLog) -> int:
return 0
def _describe(logger, metadata, install_target, report, staged) -> int:
def _describe(
logger: logging.Logger,
install_target: Path,
report: ghost.GhostReport,
staged: takeover.Staged | None,
) -> int:
"""--dry-run output: everything the real run would act on, and nothing done."""
logger.info("Install target: %s", install_target)
if report.is_ghost:
@ -162,26 +118,51 @@ def _describe(logger, metadata, install_target, report, staged) -> int:
return 0
def _run(args: argparse.Namespace, log: runlog.RunLog) -> int:
logger = log.logger
interactive = _is_interactive(args)
def _log_error(logger: logging.Logger, exc: Exception) -> None:
"""Log the message alone, with no traceback: these are user errors."""
logger.log(logging.ERROR, "%s", exc)
def _resolve(
args: argparse.Namespace, logger: logging.Logger, *, interactive: bool
) -> target.Resolved | int:
"""Resolve the target, or return the exit code for a failure already logged."""
try:
deps.require()
return target.resolve_target(args.target, args.appid, interactive=interactive)
except deps.DependencyError as exc:
_log_error(logger, exc)
return 1
except (target.TargetError, resolve.ResolveError) as exc:
_log_error(logger, exc)
return 2
def _pick_username(
args: argparse.Namespace, logger: logging.Logger, *, interactive: bool
) -> str | None:
"""Return the Steam username, or None after logging why there is none."""
try:
return config.resolve_username(args.username, interactive=interactive)
except config.ConfigError as exc:
_log_error(logger, exc)
return None
def _run(args: argparse.Namespace, log: runlog.RunLog) -> int:
if args.repair_ghosts:
return _repair_ghosts(log)
if args.map_installed:
return map_installed.run(args, log, find_steam_root())
return _run_install(args, log)
try:
deps.require()
resolved = target.resolve_target(
args.target, args.appid, interactive=interactive
)
except deps.DependencyError as exc:
logger.error("%s", exc)
return 1
except (target.TargetError, resolve.ResolveError) as exc:
logger.error("%s", exc)
return 2
def _run_install(args: argparse.Namespace, log: runlog.RunLog) -> int:
logger = log.logger
interactive = _is_interactive(args)
resolved = _resolve(args, logger, interactive=interactive)
if isinstance(resolved, int):
return resolved
metadata = resolved.metadata
logger.info(
@ -199,24 +180,22 @@ def _run(args: argparse.Namespace, log: runlog.RunLog) -> int:
staged = takeover.find_staged(library, metadata.appid, install_target)
if args.dry_run:
return _describe(logger, metadata, install_target, report, staged)
return _describe(logger, install_target, report, staged)
if report.is_ghost and not report.is_warm:
logger.error(
"appid %s has a ghost manifest (a fake 'installed' state with nothing "
"behind it). Clear it first: ./run.sh --repair-ghosts",
"behind it). Clear it first: steam_installer/run.sh --repair-ghosts",
metadata.appid,
)
return 1
try:
username = config.resolve_username(args.username, interactive=interactive)
except config.ConfigError as exc:
logger.error("%s", exc)
username = _pick_username(args, logger, interactive=interactive)
if username is None:
return 1
logger.info("Install target: %s", install_target)
if not _ask(args, interactive, "Proceed? [y/N]: "):
if not _ask(args, "Proceed? [y/N]: ", interactive=interactive):
logger.info("Cancelled (use --yes for a non-interactive run).")
return 1
@ -232,12 +211,14 @@ def _run(args: argparse.Namespace, log: runlog.RunLog) -> int:
args=args,
log=log,
steam_root=steam_root,
library=library,
metadata=metadata,
install_target=install_target,
staged=staged,
username=username,
language=language,
job=Job(
library=library,
metadata=metadata,
install_target=install_target,
staged=staged,
username=username,
language=language,
),
)

View File

@ -0,0 +1,70 @@
"""Command-line argument definitions for the installer."""
from __future__ import annotations
import argparse
from kuhy_credit import credit_line
from steam_installer import depotdl, map_installed
def build_parser() -> argparse.ArgumentParser:
"""Build the installer's argument parser."""
parser = argparse.ArgumentParser(
description="Download an owned Steam game fast, then make Steam recognize it.",
# kuhy's shared credit line (kuhy_credit, kuhyx/utils).
epilog=credit_line(),
)
parser.add_argument(
"target",
nargs="?",
default=None,
metavar="TARGET",
help=(
"AppID (284160), a Steam URL "
"(https://steamcommunity.com/app/284160/guides/), or a game name "
"(BeamNG.drive, or just BeamNG)"
),
)
parser.add_argument(
"--appid", type=int, default=None, help="Steam AppID (skips detection)"
)
parser.add_argument(
"--username",
default=None,
help="Steam account username (default: $STEAM_USERNAME, then the saved one)",
)
parser.add_argument(
"--language", default=None, help="Language depot to fetch (default: prompts)"
)
parser.add_argument(
"--max-downloads", type=int, default=depotdl.DEFAULT_MAX_DOWNLOADS
)
parser.add_argument(
"--yes",
action="store_true",
help=(
"Assume yes for every prompt. Ambiguous names still fail rather than guess."
),
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Resolve the target and report what would happen. Changes nothing.",
)
parser.add_argument(
"--repair-ghosts",
action="store_true",
help=(
"Scan every library for the fake 'installed' manifests anonymous "
"steamcmd leaves behind, and clear them away."
),
)
map_installed.add_arguments(parser)
parser.add_argument(
"--verbose",
action="store_true",
help="Show on the console everything that goes to the run log.",
)
return parser

View File

@ -7,11 +7,14 @@ is still closed) and by `--map-installed`. The decision rules live in
from __future__ import annotations
import logging
from pathlib import Path
from typing import TYPE_CHECKING, Any
from steam_installer import anticheat, appinfo, compat_decide, compat_map, proton_pkg
if TYPE_CHECKING:
import logging
from pathlib import Path
VALVE_PROTON = "--valve-proton"
TOOL_DIR = "compatibilitytools.d/proton-cachyos-native"
@ -19,7 +22,10 @@ TOOL_DIR = "compatibilitytools.d/proton-cachyos-native"
class Context:
"""What every decision in one run shares: fetched once, not per game."""
def __init__(self, steam_root: Path, extra_overrides: dict[str, str] | None = None):
def __init__(
self, steam_root: Path, extra_overrides: dict[str, str] | None = None
) -> None:
"""Remember what every decision in this run shares."""
self.steam_root = steam_root
self.config = compat_map.config_path(steam_root)
self.anticheat = anticheat.steam_ids()
@ -28,20 +34,22 @@ class Context:
steam_root / TOOL_DIR, proton_pkg.read_lock().version
)
def decide(self, appid: int, block: dict, owned_dlc: bool) -> compat_decide.Decision:
return compat_decide.decide(
appid,
block,
def decide(
self, appid: int, block: dict[str, Any], *, owned_dlc: bool
) -> compat_decide.Decision:
"""Decide whether *appid* maps to the native Proton."""
facts = compat_decide.Facts(
anticheat=self.anticheat,
overrides=self.overrides,
owned_dlc=owned_dlc,
tool_ready=self.tool_ready,
)
return compat_decide.decide(appid, block, facts, owned_dlc=owned_dlc)
def after_install(
steam_root: Path,
appid: int,
*,
owned_dlc: bool,
valve_proton: bool,
logger: logging.Logger,
@ -54,18 +62,28 @@ def after_install(
extra = {str(appid): VALVE_PROTON} if valve_proton else {}
try:
context = Context(steam_root, extra)
decision = context.decide(appid, appinfo._fetch_app_block(appid), owned_dlc)
decision = context.decide(
appid, appinfo.fetch_app_block(appid), owned_dlc=owned_dlc
)
if not decision.map:
logger.info("Compat tool: %s", decision.describe())
return
outcome = compat_map.map_app(context.config, appid)
except (appinfo.AppInfoError, compat_map.CompatMapError, OSError, ValueError) as exc:
logger.warning("Compat tool not set (%s); the game stays on Valve's default.", exc)
except (
appinfo.AppInfoError,
compat_map.CompatMapError,
OSError,
ValueError,
) as exc:
logger.warning(
"Compat tool not set (%s); the game stays on Valve's default.", exc
)
return
report(logger, appid, outcome)
def report(logger: logging.Logger, appid: int, outcome: compat_map.Outcome) -> None:
"""Log what happened to *appid*'s compat mapping."""
if outcome.action == "kept":
logger.info("Compat tool for %s kept: %s", appid, outcome.detail)
elif outcome.action == "already":

View File

@ -9,8 +9,8 @@ playable. When unsure, the answer is no.
from __future__ import annotations
import json
from dataclasses import dataclass, field
import json
from pathlib import Path
OVERRIDES = Path(__file__).parent / "data" / "compat_overrides.json"
@ -18,14 +18,18 @@ OVERRIDES = Path(__file__).parent / "data" / "compat_overrides.json"
@dataclass
class Decision:
"""Whether one game goes on the native Proton, and why."""
appid: int
reasons: list[str] = field(default_factory=list)
@property
def map(self) -> bool:
"""Whether the game gets the native Proton."""
return not self.reasons
def describe(self) -> str:
"""One line naming the verdict and its reasons."""
return "map" if self.map else f"Valve default: {', '.join(self.reasons)}"
@ -35,7 +39,8 @@ def load_overrides(path: Path = OVERRIDES) -> dict[str, str]:
if not isinstance(data, dict) or not all(
key.isdigit() and isinstance(reason, str) for key, reason in data.items()
):
raise ValueError(f"{path.name}: compat_overrides must map app ids to reasons")
msg = f"{path.name}: compat_overrides must map app ids to reasons"
raise ValueError(msg)
return data
@ -83,26 +88,27 @@ def _platform_reasons(block: dict) -> list[str]:
return reasons
def decide(
appid: int,
block: dict,
*,
anticheat: set[int] | None,
overrides: dict[str, str],
owned_dlc: bool,
tool_ready: bool,
) -> Decision:
@dataclass(frozen=True)
class Facts:
"""What every decision in one run shares: fetched once, not per game."""
anticheat: set[int] | None
overrides: dict[str, str]
tool_ready: bool
def decide(appid: int, block: dict, facts: Facts, *, owned_dlc: bool) -> Decision:
"""Every reason `appid` must keep Valve's default; none means map it."""
reasons: list[str] = []
if anticheat is None:
if facts.anticheat is None:
reasons.append("anti-cheat list unavailable")
elif appid in anticheat:
elif appid in facts.anticheat:
reasons.append("anti-cheat")
reasons += _platform_reasons(block)
if owned_dlc:
reasons.append("owned DLC")
if str(appid) in overrides:
reasons.append(f"override: {overrides[str(appid)]}")
if not tool_ready:
if str(appid) in facts.overrides:
reasons.append(f"override: {facts.overrides[str(appid)]}")
if not facts.tool_ready:
reasons.append("native Proton not installed")
return Decision(appid, reasons)

View File

@ -18,13 +18,13 @@ write while it runs would be lost; it is refused.
from __future__ import annotations
from dataclasses import dataclass, field
import os
from pathlib import Path
import re
import tempfile
from dataclasses import dataclass, field
from pathlib import Path
from steam_installer import recognize
from steam_installer import errors, recognize
TOOL = "proton-cachyos-native"
PRIORITY = "250"
@ -35,12 +35,14 @@ _MAPPING = (*_STEAM, "compattoolmapping")
_TOKEN = re.compile(r'"(?:[^"\\]|\\.)*"|[{}]|[^\s"{}]+')
class CompatMapError(Exception):
pass
class CompatMapError(errors.InstallerError):
"""Steam's config.vdf cannot be edited safely."""
@dataclass
class Outcome:
"""What editing the compat mapping did."""
action: str # "mapped", "already" or "kept"
detail: str = ""
@ -61,6 +63,7 @@ class _Layout:
def config_path(steam_root: Path) -> Path:
"""Path of Steam's ``config.vdf`` under *steam_root*."""
return steam_root / "config" / "config.vdf"
@ -76,9 +79,10 @@ def map_app(config: Path, appid: int) -> Outcome:
"""Point `appid` at the native Proton unless kuhy chose another tool."""
pids = recognize.steam_client_pids()
if pids:
raise CompatMapError(
msg = (
"Steam is running and would overwrite config.vdf on exit -- close it first"
)
raise CompatMapError(msg)
layout = _scan(_read(config), str(appid))
lines = layout.lines
entry = layout.entry
@ -90,8 +94,12 @@ def map_app(config: Path, appid: int) -> Outcome:
if name and priority != VALVE_PRIORITY:
return Outcome("kept", f"already mapped to {name} (priority {priority})")
key_line = entry.open_line - 1
if lines[key_line].strip() != f'"{appid}"' or lines[entry.close_line].strip() != "}":
raise CompatMapError(f"config.vdf line {key_line + 1}: unexpected layout")
if (
lines[key_line].strip() != f'"{appid}"'
or lines[entry.close_line].strip() != "}"
):
msg = f"config.vdf line {key_line + 1}: unexpected layout"
raise CompatMapError(msg)
indent = _indent(lines[entry.open_line])
lines[key_line : entry.close_line + 1] = _entry(appid, indent)
_write(config, lines)
@ -112,7 +120,8 @@ def _read(config: Path) -> list[str]:
try:
return config.read_text(encoding="utf-8").splitlines(keepends=True)
except OSError as exc:
raise CompatMapError(f"cannot read {config}") from exc
msg = f"cannot read {config}"
raise CompatMapError(msg) from exc
def _tokens(lines: list[str]) -> list[tuple[str, int, bool]]:
@ -126,13 +135,14 @@ def _tokens(lines: list[str]) -> list[tuple[str, int, bool]]:
for match in _TOKEN.finditer(text):
while starts[line_no + 1] <= match.start():
line_no += 1
token = match.group(0)
if token[0] == '"':
out.append((token[1:-1], line_no, True))
elif token in "{}":
out.append((token, line_no, False))
piece = match.group(0)
if piece[0] == '"':
out.append((piece[1:-1], line_no, True))
elif piece in "{}":
out.append((piece, line_no, False))
else:
raise CompatMapError(f"config.vdf line {line_no + 1}: unexpected layout")
msg = f"config.vdf line {line_no + 1}: unexpected layout"
raise CompatMapError(msg)
return out
@ -142,34 +152,41 @@ def _scan(lines: list[str], appid: str) -> _Layout:
stack: list[str] = []
pending: str | None = None
for text, index, quoted in _tokens(lines):
in_entry = layout.entry is not None and tuple(stack) == (*_MAPPING, appid)
entry = layout.entry if tuple(stack) == (*_MAPPING, appid) else None
if quoted:
if pending is None:
pending = text
continue
if in_entry:
layout.entry.values[pending.lower()] = text
if entry is not None:
entry.values[pending.lower()] = text
pending = None
elif text == "{":
if pending is None:
raise CompatMapError(f"config.vdf line {index + 1}: block without a key")
msg = f"config.vdf line {index + 1}: block without a key"
raise CompatMapError(msg)
stack.append(pending.lower())
pending = None
_note_open(layout, tuple(stack), appid, index)
else:
if not stack or pending is not None:
raise CompatMapError(f"config.vdf line {index + 1}: unbalanced brace")
if in_entry:
layout.entry.close_line = index
msg = f"config.vdf line {index + 1}: unbalanced brace"
raise CompatMapError(msg)
if entry is not None:
entry.close_line = index
stack.pop()
if layout.steam_open < 0:
raise CompatMapError("config.vdf has no InstallConfigStore/Software/Valve/Steam")
msg = "config.vdf has no InstallConfigStore/Software/Valve/Steam"
raise CompatMapError(msg)
return layout
def _note_open(layout: _Layout, path: tuple[str, ...], appid: str, index: int) -> None:
if path in (_STEAM, _MAPPING, (*_MAPPING, appid)) and layout.lines[index].strip() != "{":
raise CompatMapError(f"config.vdf line {index + 1}: unexpected layout")
if (
path in (_STEAM, _MAPPING, (*_MAPPING, appid))
and layout.lines[index].strip() != "{"
):
msg = f"config.vdf line {index + 1}: unexpected layout"
raise CompatMapError(msg)
if path == _STEAM:
layout.steam_open = index
elif path == _MAPPING:
@ -201,8 +218,8 @@ def _write(config: Path, lines: list[str]) -> None:
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
handle.write("".join(lines))
os.chmod(tmp, mode)
os.replace(tmp, config)
Path(tmp).chmod(mode)
Path(tmp).replace(config)
except BaseException:
Path(tmp).unlink(missing_ok=True)
raise

View File

@ -2,8 +2,8 @@
Only the username is ever persisted. Passwords and Steam Guard codes stay
where they belong: with depotdownloader's `-remember-password` login token
(prompted through the pty in `runlog.run_logged`). Writing them to a dotfile would be a downgrade in security over
what Steam's own tooling already does.
(prompted through the pty in `runlog.run_logged`). Writing them to a dotfile
would be a downgrade in security over what Steam's own tooling already does.
"""
from __future__ import annotations
@ -11,14 +11,16 @@ from __future__ import annotations
import os
from pathlib import Path
from steam_installer import errors
CONFIG_DIR = Path.home() / ".config" / "steam-game-installer"
CONFIG_PATH = CONFIG_DIR / "config"
USERNAME_KEY = "username"
ENV_VAR = "STEAM_USERNAME"
class ConfigError(Exception):
pass
class ConfigError(errors.InstallerError):
"""The saved config is unusable or a username is missing."""
def load_username() -> str | None:
@ -51,12 +53,14 @@ def resolve_username(cli_value: str | None, *, interactive: bool) -> str:
if saved:
return saved
if not interactive:
raise ConfigError(
msg = (
"No Steam username available. Pass --username, set "
f"${ENV_VAR}, or run once interactively to save one."
)
raise ConfigError(msg)
entered = input("Steam username: ").strip()
if not entered:
raise ConfigError("No Steam username entered.")
msg = "No Steam username entered."
raise ConfigError(msg)
save_username(entered)
return entered

View File

@ -11,82 +11,74 @@ built to avoid.
from __future__ import annotations
import logging
from pathlib import Path
from dataclasses import dataclass
from typing import TYPE_CHECKING
from steam_installer import deps, runlog
if TYPE_CHECKING:
import logging
from pathlib import Path
DEFAULT_MAX_DOWNLOADS = 32
# Windows depots only: a game this tool can map to Proton ships no other kind.
OS_NAME = "windows"
# depotdownloader's licence check refusing a DLC app. The one failure that is
# expected: most games list DLC the account never bought.
NOT_OWNED_MARKER = "is not available from this account"
def _command(
appid: int,
install_dir: Path,
username: str,
language: str,
os_name: str,
max_downloads: int,
) -> list[str]:
@dataclass(frozen=True)
class Pull:
"""Where and as whom depotdownloader pulls content, and in which language."""
install_dir: Path
username: str
language: str = "english"
max_downloads: int = DEFAULT_MAX_DOWNLOADS
def _command(appid: int, pull: Pull) -> list[str]:
return [
deps.depotdownloader_bin(),
"-app",
str(appid),
"-os",
os_name,
OS_NAME,
"-language",
language,
pull.language,
"-max-downloads",
str(max_downloads),
str(pull.max_downloads),
"-username",
username,
pull.username,
"-remember-password",
"-dir",
str(install_dir),
str(pull.install_dir),
]
def download(
appid: int,
install_dir: Path,
username: str,
logger: logging.Logger,
language: str = "english",
os_name: str = "windows",
max_downloads: int = DEFAULT_MAX_DOWNLOADS,
) -> int:
"""Run depotdownloader against `install_dir`, streaming its own progress output live.
def download(appid: int, pull: Pull, logger: logging.Logger) -> int:
"""Run depotdownloader against the install dir, streaming its progress live.
Returns the exit code rather than raising: the caller has to distinguish
"download failed" from "download fine, manifest step failed" to decide
whether touching the manifest is safe at all.
"""
install_dir.mkdir(parents=True, exist_ok=True)
cmd = _command(appid, install_dir, username, language, os_name, max_downloads)
returncode, _ = runlog.run_logged(cmd, logger)
pull.install_dir.mkdir(parents=True, exist_ok=True)
returncode, _ = runlog.run_logged(_command(appid, pull), logger)
return returncode
def download_dlc(
dlc_appid: int,
install_dir: Path,
username: str,
logger: logging.Logger,
language: str = "english",
os_name: str = "windows",
max_downloads: int = DEFAULT_MAX_DOWNLOADS,
) -> bool | None:
"""Fetch one DLC app into the game's own `install_dir`.
def download_dlc(dlc_appid: int, pull: Pull, logger: logging.Logger) -> bool | None:
"""Fetch one DLC app into the game's own install dir.
Returns True when downloaded, None when the account does not own it, and
False for any other failure -- which the caller must treat as fatal.
"""
cmd = _command(dlc_appid, install_dir, username, language, os_name, max_downloads)
returncode, output = runlog.run_logged(cmd, logger)
returncode, output = runlog.run_logged(_command(dlc_appid, pull), logger)
if returncode == 0:
return True
return None if NOT_OWNED_MARKER in output else False

View File

@ -3,15 +3,20 @@
from __future__ import annotations
import os
import shutil
from pathlib import Path
import shutil
from steam_installer import errors
TOOL_HINTS = {
"depotdownloader": "Run ./install.sh, which fetches it from the SteamRE release.",
"depotdownloader": (
"Run steam_installer/install_deps.sh, "
"which fetches it from the SteamRE release."
),
}
# The copy install.sh manages. Preferred over whatever PATH finds first: each
# The copy install_deps.sh manages. Preferred over whatever PATH finds first: each
# depotdownloader binary keeps its remembered login in its own IsolatedStorage
# slot, keyed by its path. With an AUR copy in /usr/bin as well, a terminal run
# (PATH has ~/.local/bin) and a sudo'd run (secure_path does not) used two
@ -20,12 +25,12 @@ TOOL_HINTS = {
MANAGED_DEPOTDOWNLOADER = Path.home() / ".local" / "bin" / "depotdownloader"
class DependencyError(Exception):
pass
class DependencyError(errors.InstallerError):
"""A tool this installer shells out to is missing."""
def depotdownloader_bin() -> str:
"""The depotdownloader to run: install.sh's own copy, else PATH's."""
"""The depotdownloader to run: install_deps.sh's own copy, else PATH's."""
if os.access(MANAGED_DEPOTDOWNLOADER, os.X_OK):
return str(MANAGED_DEPOTDOWNLOADER)
return "depotdownloader"
@ -43,6 +48,7 @@ def missing_tools() -> list[str]:
def require() -> None:
"""Fail with the fix when a required tool is missing."""
missing = missing_tools()
if missing:
details = "; ".join(

View File

@ -14,18 +14,11 @@ the install before the manifest is touched, like a failed base download.
from __future__ import annotations
from pathlib import Path
from steam_installer import appinfo, depotdl, runlog
def download_owned(
appid: int,
install_dir: Path,
username: str,
language: str,
max_downloads: int,
log: runlog.RunLog,
appid: int, pull: depotdl.Pull, log: runlog.RunLog
) -> appinfo.DepotPlan | None:
"""Download every owned DLC of `appid` that has depots of its own.
@ -35,18 +28,11 @@ def download_owned(
than not owning it.
"""
logger = log.logger
plans = appinfo.get_dlc_plans(appid, language)
plans = appinfo.get_dlc_plans(appid, pull.language)
downloaded = appinfo.DepotPlan(required=set(), shared=set())
for dlc_appid, depots in sorted(plans.items()):
log.section(f"depotdownloader DLC {dlc_appid}")
outcome = depotdl.download_dlc(
dlc_appid=dlc_appid,
install_dir=install_dir,
username=username,
language=language,
max_downloads=max_downloads,
logger=logger,
)
outcome = depotdl.download_dlc(dlc_appid=dlc_appid, pull=pull, logger=logger)
if outcome is None:
logger.info("DLC %d is not owned by this account -- skipped.", dlc_appid)
continue

45
steam_installer/errors.py Normal file
View File

@ -0,0 +1,45 @@
"""Base class of every error this package raises on purpose, and the crash guard."""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import logging
from pathlib import Path
from types import TracebackType
class InstallerError(Exception):
"""A failure the installer reports and exits on, as opposed to a bug."""
class CrashGuard:
"""Context manager: log any ``Exception`` with the run-log path, swallow it.
``try/except Exception`` would say the same, but pylint's broad-exception
check rejects it. ``KeyboardInterrupt`` and other ``BaseException`` pass.
"""
def __init__(self, logger: logging.Logger, log_path: Path) -> None:
"""Remember where a crash is reported."""
self.logger = logger
self.log_path = log_path
def __enter__(self) -> None:
"""Nothing to set up."""
def __exit__(
self,
exc_type: type[BaseException] | None,
exc: BaseException | None,
tb: TracebackType | None,
) -> bool:
"""Report an ``Exception`` and swallow it; let everything else pass."""
if not isinstance(exc, Exception):
return False
self.logger.error(
"Run failed with an unhandled error", exc_info=(exc_type, exc, tb)
)
self.logger.info("Run log (send this): %s", self.log_path)
return True

View File

@ -9,10 +9,15 @@ it, is no longer run. Either way every precondition is checked first.
from __future__ import annotations
import time
from pathlib import Path
from typing import TYPE_CHECKING
from steam_installer import appinfo, finish_check, manifest_write, recognize, runlog
from steam_installer.library import SteamLibrary
from steam_installer.manifest_depots import Proven
if TYPE_CHECKING:
from pathlib import Path
from steam_installer.library import SteamLibrary
def finish(
@ -21,9 +26,10 @@ def finish(
metadata: appinfo.AppMetadata,
target: Path,
depot_plan: appinfo.DepotPlan,
expected_build_id: str | None,
) -> int:
"""Finish the manifest and close out the run."""
logger = log.logger
expected_build_id = appinfo.get_build_id(metadata.appid)
manifest_path = library.steamapps / f"appmanifest_{metadata.appid}.acf"
seed = None
@ -62,22 +68,21 @@ def finish(
if freed:
logger.info("Cleared %.1f GB of now-redundant Steam staging data.", freed / 1e9)
build_proven = finish_check.build_proven(
recognize.discover_cached_depot_manifests(target),
depot_plan.required,
depot_plan.manifests,
)
written = manifest_write.finish_manifest_manually(
library=library,
appid=metadata.appid,
install_dir=target,
now_unix=int(time.time()),
dlc_depots=depot_plan.dlc,
seed=seed,
# Proven depot by depot, so it outranks whatever the manifest says.
build_id=(
expected_build_id
if finish_check.build_proven(
recognize.discover_cached_depot_manifests(target),
depot_plan.required,
depot_plan.manifests,
)
else None
proven=Proven(
dlc_depots=depot_plan.dlc,
seed=seed,
# Proven depot by depot, so it outranks whatever the manifest says.
build_id=expected_build_id if build_proven else None,
),
)
log.file_contents("Manifest after manual finish", written)

View File

@ -6,14 +6,17 @@ write it gates lives in :mod:`manifest_write`.
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Any
from steam_installer import manifest_write, recognize
if TYPE_CHECKING:
from pathlib import Path
def check_ready_to_finish(
install_dir: Path,
app_state: dict,
app_state: dict[str, Any],
required_depots: set[str],
expected_build_id: str | None,
expected_manifests: dict[str, str] | None = None,
@ -45,7 +48,7 @@ def check_ready_to_finish(
# refusing is still free.
shared = app_state.get("SharedDepots")
shared_ids = set(shared) if isinstance(shared, dict) else set()
if cached and not (set(cached) - shared_ids):
if cached and set(cached) <= shared_ids:
problems.append(
"every cached depot is listed under SharedDepots in the existing "
"manifest, so there would be nothing to record as installed"
@ -55,7 +58,8 @@ def check_ready_to_finish(
if missing:
problems.append(
f"depot(s) {', '.join(missing)} have no cached manifest under "
f"{install_dir}/{recognize.DEPOTDOWNLOADER_DIR} -- the download did not finish"
f"{install_dir}/{recognize.DEPOTDOWNLOADER_DIR} -- "
"the download did not finish"
)
expected_manifests = expected_manifests or {}

View File

@ -6,7 +6,8 @@ The manifest-finishing half lives in `finalize.py`.
from __future__ import annotations
from pathlib import Path
import logging
from typing import TYPE_CHECKING
from steam_installer import (
appinfo,
@ -20,10 +21,15 @@ from steam_installer import (
takeover,
)
from steam_installer.finalize import finish
from steam_installer.library import SteamLibrary
if TYPE_CHECKING:
import argparse
from pathlib import Path
from steam_installer.job import Job
def _prepare_steam(args, logger) -> bool | None:
def prepare_steam(args: argparse.Namespace, logger: logging.Logger) -> bool | None:
"""Close Steam if it's running. Returns whether it was, or None to abort.
Steam owns the manifest and is actively writing the staging tree, so it has
@ -42,38 +48,32 @@ def _prepare_steam(args, logger) -> bool | None:
return None
try:
return steamctl.stop(logger)
except steamctl.SteamStillRunning as exc:
logger.error("%s", exc)
except steamctl.SteamStillRunningError as exc:
logger.log(logging.ERROR, "%s", exc)
return None
def install(
*,
args,
args: argparse.Namespace,
log: runlog.RunLog,
steam_root: Path,
library: SteamLibrary,
metadata: appinfo.AppMetadata,
install_target: Path,
staged: takeover.Staged | None,
username: str,
language: str,
job: Job,
) -> int:
"""Adopt anything Steam already fetched, download the rest, make it count."""
logger = log.logger
metadata = job.metadata
steam_was_running = _prepare_steam(args, logger)
steam_was_running = prepare_steam(args, logger)
if steam_was_running is None:
return 1
try:
returncode = _download_and_recognize(
args, log, library, metadata, install_target, staged, username, language
)
returncode = _download_and_recognize(args, log, job)
if returncode == 0:
# Still inside the window where Steam is closed: config.vdf written
# under a running client is lost when it exits.
cached = recognize.discover_cached_depot_manifests(install_target)
dlc_depots = appinfo.get_depot_plan(metadata.appid, language).dlc
cached = recognize.discover_cached_depot_manifests(job.install_target)
dlc_depots = appinfo.get_depot_plan(metadata.appid, job.language).dlc
compat_apply.after_install(
steam_root,
metadata.appid,
@ -87,55 +87,62 @@ def install(
steamctl.start(logger)
def _adopt_staged(log: runlog.RunLog, job: Job) -> None:
"""Hardlink whatever Steam already staged into the install dir."""
staged = job.staged
if staged is None:
return
logger = log.logger
log.section("Adopting Steam's staged download")
logger.info(
"Steam already staged %.2f GB for this app (%d files). Hardlinking it "
"into place so depotdownloader verifies instead of refetching.",
staged.bytes_staged / 1e9,
staged.file_count,
)
job.install_target.mkdir(parents=True, exist_ok=True)
takeover.adopt(staged, logger)
def _log_plan(
log: runlog.RunLog, job: Job, depot_plan: appinfo.DepotPlan, max_downloads: int
) -> None:
"""Record what this run is about to fetch, for the run log."""
logger = log.logger
log.section("Plan")
logger.debug("language=%s max_downloads=%s", job.language, max_downloads)
logger.debug("required depots: %s", sorted(depot_plan.required))
logger.debug("shared depots: %s", sorted(depot_plan.shared))
logger.debug("expected buildid: %s", appinfo.get_build_id(job.metadata.appid))
manifest_path = job.library.steamapps / f"appmanifest_{job.metadata.appid}.acf"
log.file_contents("Manifest before run", manifest_path)
def _download_and_recognize(
args,
log: runlog.RunLog,
library: SteamLibrary,
metadata: appinfo.AppMetadata,
install_target: Path,
staged: takeover.Staged | None,
username: str,
language: str,
args: argparse.Namespace, log: runlog.RunLog, job: Job
) -> int:
logger = log.logger
metadata, install_target = job.metadata, job.install_target
# First thing with Steam closed: whatever happens next, Steam resumes onto
# this field, and a stub written from the display name ("Baldur's Gate 3")
# would point it at a folder the download never fills.
manifest_write.ensure_installdir_correct(library, metadata.appid, install_target)
if staged is not None:
log.section("Adopting Steam's staged download")
logger.info(
"Steam already staged %.2f GB for this app (%d files). Hardlinking it "
"into place so depotdownloader verifies instead of refetching.",
staged.bytes_staged / 1e9,
staged.file_count,
)
install_target.mkdir(parents=True, exist_ok=True)
takeover.adopt(staged, logger)
depot_plan = appinfo.get_depot_plan(metadata.appid, language)
expected_build_id = appinfo.get_build_id(metadata.appid)
log.section("Plan")
logger.debug("language=%s max_downloads=%s", language, args.max_downloads)
logger.debug("required depots: %s", sorted(depot_plan.required))
logger.debug("shared depots: %s", sorted(depot_plan.shared))
logger.debug("expected buildid: %s", expected_build_id)
manifest_path = library.steamapps / f"appmanifest_{metadata.appid}.acf"
log.file_contents("Manifest before run", manifest_path)
manifest_write.ensure_installdir_correct(
job.library, metadata.appid, install_target
)
_adopt_staged(log, job)
depot_plan = appinfo.get_depot_plan(metadata.appid, job.language)
_log_plan(log, job, depot_plan, args.max_downloads)
logger.info("Downloading via depotdownloader (fast path)...")
log.section("depotdownloader")
returncode = depotdl.download(
appid=metadata.appid,
pull = depotdl.Pull(
install_dir=install_target,
username=username,
language=language,
username=job.username,
language=job.language,
max_downloads=args.max_downloads,
logger=logger,
)
returncode = depotdl.download(metadata.appid, pull, logger)
if returncode != 0:
logger.error(
"depotdownloader failed (exit %d) -- not touching the manifest.", returncode
@ -144,9 +151,7 @@ def _download_and_recognize(
return 1
logger.info("Checking for owned DLC with depots of its own...")
dlc_plan = dlc.download_owned(
metadata.appid, install_target, username, language, args.max_downloads, log
)
dlc_plan = dlc.download_owned(metadata.appid, pull, log)
if dlc_plan is None:
logger.info("Run log (send this): %s", log.path)
return 1
@ -163,11 +168,4 @@ def _download_and_recognize(
):
logger.debug("depot %s -> manifest %s", depot_id, mid)
return finish(
log,
library,
metadata,
install_target,
depot_plan,
expected_build_id,
)
return finish(log, job.library, metadata, install_target, depot_plan)

View File

@ -25,19 +25,25 @@ Two outcomes, and only one of them deletes anything:
from __future__ import annotations
import logging
import time
from dataclasses import dataclass
from pathlib import Path
import time
from typing import TYPE_CHECKING, Any
from steam_installer import manifest_write, recognize, vdf
from steam_installer.library import SteamLibrary
if TYPE_CHECKING:
import logging
from pathlib import Path
from steam_installer.library import SteamLibrary
INSTALLED_STATE_FLAG = 4
@dataclass(frozen=True)
class GhostReport:
"""What the scan found about one app's manifest."""
appid: int
manifest_path: Path
install_dir: Path | None
@ -47,6 +53,7 @@ class GhostReport:
@property
def is_ghost(self) -> bool:
"""Whether the manifest claims an install with nothing behind it."""
return bool(self.reasons)
@property
@ -56,19 +63,52 @@ class GhostReport:
def staging_size(library: SteamLibrary, appid: int) -> int:
"""Bytes of staged content for *appid* in *library*."""
staged = library.steamapps / "downloading" / str(appid)
if not staged.is_dir():
return 0
return sum(f.stat().st_size for f in staged.rglob("*") if f.is_file())
def _install_dir(library: SteamLibrary, app_state: dict) -> Path | None:
def _install_dir(library: SteamLibrary, app_state: dict[str, Any]) -> Path | None:
installdir = app_state.get("installdir")
if not isinstance(installdir, str) or not installdir:
return None
return library.common / installdir
def _load_state(manifest_path: Path, appid: int) -> dict[str, Any] | None:
"""The manifest's AppState, or None when it is absent, unparsable or foreign."""
if not manifest_path.is_file():
return None
try:
app_state = recognize.read_app_state(manifest_path)
except vdf.VdfParseError:
return None
if str(app_state.get("appid", "")) != str(appid):
return None
return app_state
def _looks_installed_but_empty(
app_state: dict[str, Any], content_bytes: int, install_dir: Path | None
) -> bool:
"""Every ghost condition at once; one failing condition rules a ghost out."""
state_flags = app_state.get("StateFlags", "")
if not (str(state_flags).isdigit() and int(state_flags) & INSTALLED_STATE_FLAG):
return False
if app_state.get("InstalledDepots"):
return False
if str(app_state.get("SizeOnDisk", "0")) != "0" or content_bytes:
return False
# Our own downloader's cache means a real run is in progress against this
# directory; an empty install dir mid-download is not a ghost.
return not (
install_dir is not None
and recognize.discover_cached_depot_manifests(install_dir)
)
def inspect(library: SteamLibrary, appid: int) -> GhostReport:
"""Classify `appid` in `library`. An empty `reasons` means it is not a ghost.
@ -78,16 +118,9 @@ def inspect(library: SteamLibrary, appid: int) -> GhostReport:
game.
"""
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
empty = GhostReport(appid, manifest_path, None, 0, 0, ())
if not manifest_path.is_file():
return empty
try:
app_state = recognize.read_app_state(manifest_path)
except vdf.VdfParseError:
return empty
if str(app_state.get("appid", "")) != str(appid):
return empty
app_state = _load_state(manifest_path, appid)
if app_state is None:
return GhostReport(appid, manifest_path, None, 0, 0, ())
install_dir = _install_dir(library, app_state)
content_bytes = (
@ -96,24 +129,8 @@ def inspect(library: SteamLibrary, appid: int) -> GhostReport:
else 0
)
staged = staging_size(library, appid)
known = GhostReport(appid, manifest_path, install_dir, content_bytes, staged, ())
state_flags = app_state.get("StateFlags", "")
if not (str(state_flags).isdigit() and int(state_flags) & INSTALLED_STATE_FLAG):
return known
depots = app_state.get("InstalledDepots")
if depots:
return known
if str(app_state.get("SizeOnDisk", "0")) != "0":
return known
if content_bytes:
return known
# Our own downloader's cache means a real run is in progress against this
# directory; an empty install dir mid-download is not a ghost.
if install_dir is not None and recognize.discover_cached_depot_manifests(
install_dir
):
return known
if not _looks_installed_but_empty(app_state, content_bytes, install_dir):
return GhostReport(appid, manifest_path, install_dir, content_bytes, staged, ())
return GhostReport(
appid,
@ -122,7 +139,7 @@ def inspect(library: SteamLibrary, appid: int) -> GhostReport:
content_bytes,
staged,
(
f"manifest claims StateFlags={state_flags} (installed)",
f"manifest claims StateFlags={app_state.get('StateFlags')} (installed)",
"InstalledDepots is empty",
"SizeOnDisk is 0",
"no game content on disk",
@ -154,13 +171,15 @@ def repair(
Refuses a warm ghost outright: those bytes belong to `takeover`.
"""
if not report.is_ghost:
raise ValueError(f"appid {report.appid} is not a ghost; refusing to touch it")
msg = f"appid {report.appid} is not a ghost; refusing to touch it"
raise ValueError(msg)
if report.is_warm:
raise ValueError(
msg = (
f"appid {report.appid} has {report.staging_bytes / 1e9:.1f} GB of staged "
"Steam content. That is a partial download, not a ghost to delete -- "
"install it normally so the bytes are reused."
)
raise ValueError(msg)
stamp = int(time.time()) if now_unix is None else now_unix
touched: list[Path] = []

View File

@ -1,12 +1,13 @@
#!/bin/bash
# ============================================================================
# Install everything this tool shells out to, then set up the dev environment.
# Install everything the Steam game installer shells out to.
#
# libibus the one runtime library of proton-cachyos-native's 63
# package dependencies a plain Arch desktop may lack (pacman)
# depotdownloader official self-contained release from SteamRE/DepotDownloader
# .venv local virtualenv with the package installed editable
# kuhy-credit the installer's one third-party import (user site, pinned in
# requirements.txt)
# native Proton proton-cachyos-native, pinned in
# steam_installer/data/proton-native.lock, signature- and
# sha256-checked, into Steam's compatibilitytools.d
@ -14,8 +15,8 @@
# steamcmd is not needed any more: app info comes from Steam's own cache and
# the manifest is written by the tool itself.
#
# Idempotent: every step no-ops when its target is already in place, so run.sh
# can call it on every run. Installing libibus runs `sudo -n pacman`; when sudo
# Idempotent: every step no-ops when its target is already in place, so
# steam_installer/run.sh can call it on every run. Installing libibus runs `sudo -n pacman`; when sudo
# would prompt, it stops and prints the one command to type.
# ============================================================================
@ -23,8 +24,10 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
readonly VENV="${SCRIPT_DIR}/.venv"
readonly STAMP="${VENV}/.deps-ok"
readonly REPO_DIR="${SCRIPT_DIR%/*}"
readonly PYTHON="${SGI_PYTHON:-/usr/bin/python3}"
readonly STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/steam-game-installer"
readonly STAMP="${STATE_DIR}/.deps-ok"
readonly DD_REPO="SteamRE/DepotDownloader"
readonly DD_HOME="${XDG_DATA_HOME:-$HOME/.local/share}/steam-game-installer/depotdownloader"
readonly BIN_DIR="${HOME}/.local/bin"
@ -67,17 +70,34 @@ ensure_libibus() {
die "pacman could not install libibus (a stale package db 404s). Run: sudo pacman -Syu libibus"
}
# The installer's one third-party import. The daemon launches it with the
# system interpreter as the desktop user, so it must live in that user's site.
ensure_kuhy_credit() {
if "$PYTHON" -c 'import kuhy_credit' >/dev/null 2>&1; then
return 0
fi
if [[ "$CHECK_ONLY" -eq 1 ]]; then
echo "missing: kuhy-credit -- run steam_installer/install_deps.sh" >&2
return 1
fi
local pin
pin="$(grep -m1 '^kuhy-credit' "${REPO_DIR}/requirements.txt")" ||
die "no kuhy-credit pin in ${REPO_DIR}/requirements.txt"
"$PYTHON" -m pip install --user --break-system-packages "$pin" ||
die "could not install kuhy-credit"
}
# Never runs the package's .INSTALL; see steam_installer/proton_native.py.
ensure_native_proton() {
local python="${VENV}/bin/python"
export PYTHONPATH="${REPO_DIR}${PYTHONPATH:+:$PYTHONPATH}"
if [[ "$CHECK_ONLY" -eq 1 ]]; then
if [[ -x "$python" ]] && ! "$python" -m steam_installer.proton_native --check; then
echo "missing: proton-cachyos-native at the pinned version -- run ./install.sh" >&2
if ! "$PYTHON" -m steam_installer.proton_native --check; then
echo "missing: proton-cachyos-native at the pinned version -- run steam_installer/install_deps.sh" >&2
return 1
fi
return 0
fi
"$python" -m steam_installer.proton_native || die "native Proton install failed"
"$PYTHON" -m steam_installer.proton_native || die "native Proton install failed"
}
# Build from source only if the published binary can't be fetched. The upstream
@ -99,7 +119,7 @@ ensure_depotdownloader() {
return 0
fi
if [[ "$CHECK_ONLY" -eq 1 ]]; then
echo "missing: depotdownloader -- run ./install.sh" >&2
echo "missing: depotdownloader -- run steam_installer/install_deps.sh" >&2
return 1
fi
@ -127,38 +147,11 @@ ensure_depotdownloader() {
die "depotdownloader installed to ${BIN_DIR} but is not on PATH. Add ${BIN_DIR} to PATH."
}
# The suite requires 100% coverage (pyproject: fail_under = 100), and one test
# runs `python -m steam_installer.cli` in a subprocess. Coverage in a child is
# only recorded if it calls coverage.process_startup() at interpreter start,
# which is what this .pth does. Without it a clean clone reports 99% and fails.
setup_dev_env() {
if [[ ! -x "${VENV}/bin/python" ]]; then
echo "Creating ${VENV}..."
python3 -m venv "$VENV"
fi
local python="${VENV}/bin/python"
"$python" -m pip install -q --upgrade pip
# Named outright rather than via an extra: pyproject declares none, and pip
# only *warns* about a missing extra before exiting 0 -- so the old
# `[dev] || fallback` form silently installed no test tools at all on a
# fresh venv, and the .pth below then failed to import coverage.
"$python" -m pip install -q -e "$SCRIPT_DIR" pytest pytest-cov coverage
local site_packages
site_packages="$("$python" -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')"
cat >"${site_packages}/steam_installer_coverage.pth" <<-'PTH'
import os, coverage; os.environ.get("COVERAGE_PROCESS_START") and coverage.process_startup()
PTH
"$python" -c 'import pytest, coverage' ||
die "dev environment incomplete: pytest/coverage did not install"
echo "Dev environment ready (subprocess coverage enabled)."
}
main() {
local missing=0
ensure_libibus || missing=1
ensure_depotdownloader || missing=1
ensure_kuhy_credit || missing=1
if [[ "$CHECK_ONLY" -eq 1 ]]; then
ensure_native_proton || missing=1
@ -166,9 +159,10 @@ main() {
exit "$missing"
fi
setup_dev_env
ensure_kuhy_credit
ensure_native_proton
echo "All dependencies present."
mkdir -p "$STATE_DIR"
: >"$STAMP"
}

24
steam_installer/job.py Normal file
View File

@ -0,0 +1,24 @@
"""Everything one install run is about, bundled so it travels as one argument."""
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
from steam_installer import appinfo, takeover
from steam_installer.library import SteamLibrary
@dataclass(frozen=True)
class Job:
"""The app, where it goes, what Steam already staged, and as whom."""
library: SteamLibrary
metadata: appinfo.AppMetadata
install_target: Path
staged: takeover.Staged | None
username: str
language: str

View File

@ -12,14 +12,18 @@ DEFAULT_STEAM_ROOT = Path.home() / ".local/share/Steam"
@dataclass
class SteamLibrary:
"""One Steam library folder."""
path: Path
@property
def common(self) -> Path:
"""Folder holding installed games."""
return self.path / "steamapps" / "common"
@property
def steamapps(self) -> Path:
"""The library's ``steamapps`` folder."""
return self.path / "steamapps"
@ -28,13 +32,12 @@ def find_steam_root() -> Path:
for candidate in (DEFAULT_STEAM_ROOT, Path.home() / ".steam/steam"):
if (candidate / "steamapps").is_dir():
return candidate
raise FileNotFoundError(
"No Steam installation found at ~/.local/share/Steam or ~/.steam/steam"
)
msg = "No Steam installation found at ~/.local/share/Steam or ~/.steam/steam"
raise FileNotFoundError(msg)
def list_libraries(steam_root: Path) -> list[SteamLibrary]:
"""Every Steam library folder, parsed from libraryfolders.vdf (plus steam_root itself)."""
"""Every Steam library folder in libraryfolders.vdf, plus steam_root itself."""
libraries = [SteamLibrary(steam_root)]
vdf_path = steam_root / "steamapps" / "libraryfolders.vdf"
if not vdf_path.is_file():
@ -69,7 +72,7 @@ def find_existing_install(
def pick_install_target(libraries: list[SteamLibrary], installdir: str) -> Path:
"""Where to install: an existing partial install if one exists, else the primary library."""
"""Where to install: an existing partial install, else the primary library."""
existing = find_existing_install(libraries, installdir)
if existing is not None:
return existing

View File

@ -0,0 +1,58 @@
"""What a manifest finish records about depots and builds, apart from file I/O.
Split out of manifest_write.py (250-line cap).
"""
from __future__ import annotations
from dataclasses import dataclass, field
@dataclass(frozen=True)
class Proven:
"""The extras a finish may add beyond what the manifest on disk already says.
``dlc_depots`` maps a DLC depot to its DLC app id. Steam's own manifests
record each DLC depot with that ``dlcappid``, so it is written the same way.
``build_id``, when given, is a build the caller proved from the cached depot
manifests; it replaces the one Steam recorded. ``seed`` (from
:func:`seed_app_state`) is written instead when there is no manifest.
"""
dlc_depots: dict[str, str] = field(default_factory=dict)
build_id: str | None = None
seed: dict | None = None
def pick_buildid(app_state: dict, proven_build: str | None) -> str:
"""The build to record: the proven one, else Steam's, else "0".
"0" means "no real build installed yet" (Steam uses it as a sentinel) -- a
bare `or` treats that non-empty string as truthy and never falls through to
TargetBuildID, leaving a stale/wrong buildid.
"""
existing = app_state.get("buildid")
recorded = existing if existing and existing != "0" else None
return proven_build or recorded or app_state.get("TargetBuildID") or "0"
def installed_depots(
app_state: dict, depot_manifests: dict[str, str], dlc_depots: dict[str, str]
) -> dict:
"""InstalledDepots for the cached depots, minus the shared ones.
A shared depot (e.g. Steamworks Common Redistributables) is installed once
under its owning app and only referenced here. Listing it in
InstalledDepots as though this app owned a copy contradicts SharedDepots.
"""
shared = app_state.get("SharedDepots")
shared_ids = set(shared) if isinstance(shared, dict) else set()
installed: dict = {}
for depot_id, manifest_id in depot_manifests.items():
if depot_id in shared_ids:
continue
entry = {"manifest": manifest_id, "size": "0"}
if depot_id in dlc_depots:
entry["dlcappid"] = dlc_depots[depot_id]
installed[depot_id] = entry
return installed

View File

@ -17,10 +17,15 @@ in the run that prompted this comment.
from __future__ import annotations
import os
from pathlib import Path
from typing import TYPE_CHECKING
from steam_installer import recognize, vdf
from steam_installer.library import SteamLibrary
from steam_installer.manifest_depots import Proven, installed_depots, pick_buildid
if TYPE_CHECKING:
from pathlib import Path
from steam_installer.library import SteamLibrary
def ensure_installdir_correct(
@ -103,75 +108,48 @@ def finish_manifest_manually(
appid: int,
install_dir: Path,
now_unix: int,
dlc_depots: dict[str, str] | None = None,
build_id: str | None = None,
seed: dict | None = None,
proven: Proven | None = None,
) -> Path:
"""Patch appmanifest_<appid>.acf to StateFlags=4 using only verified-known values.
Patches the manifest on disk, or -- when there is none -- writes `seed`
(from :func:`seed_app_state`) instead. With neither it refuses.
`dlc_depots` maps a DLC depot to its DLC app id. Steam's own manifests
record each DLC depot with that `dlcappid`, so it is written the same way.
`build_id`, when given, is a build the caller proved from the cached depot
manifests; it replaces the one Steam recorded.
Patches the manifest on disk, or -- when there is none -- writes
``proven.seed`` instead. With neither it refuses.
"""
proven = proven or Proven()
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
existed = manifest_path.is_file()
if not existed and seed is None:
raise recognize.RecognizeError(
f"No existing {manifest_path.name} to finish and no seed to write"
)
if not existed and proven.seed is None:
msg = f"No existing {manifest_path.name} to finish and no seed to write"
raise recognize.RecognizeError(msg)
app_state = (
vdf.parse(manifest_path.read_text()).get("AppState", {}) if existed else seed
vdf.parse(manifest_path.read_text()).get("AppState", {})
if existed
else proven.seed
)
if not isinstance(app_state, dict):
raise recognize.RecognizeError(
f"{manifest_path} is not a valid AppState manifest"
)
msg = f"{manifest_path} is not a valid AppState manifest"
raise recognize.RecognizeError(msg)
depot_manifests = recognize.discover_cached_depot_manifests(install_dir)
if not depot_manifests:
raise recognize.RecognizeError(
f"No cached depot manifests found under {install_dir}/{recognize.DEPOTDOWNLOADER_DIR}"
)
size_on_disk = measure_content_size(install_dir)
# "0" means "no real build installed yet" (Steam uses it as a
# sentinel) -- a bare `or` treats that non-empty string as truthy and
# never falls through to TargetBuildID, leaving a stale/wrong buildid.
existing_buildid = app_state.get("buildid")
buildid = existing_buildid if existing_buildid and existing_buildid != "0" else None
buildid = build_id or buildid or app_state.get("TargetBuildID") or "0"
# A shared depot (e.g. Steamworks Common Redistributables) is installed once
# under its owning app and only referenced here. Listing it in
# InstalledDepots as though this app owned a copy contradicts SharedDepots.
shared = app_state.get("SharedDepots")
shared_ids = set(shared) if isinstance(shared, dict) else set()
dlc_depots = dlc_depots or {}
installed_depots: dict = {}
for depot_id, manifest_id in depot_manifests.items():
if depot_id in shared_ids:
continue
entry = {"manifest": manifest_id, "size": "0"}
if depot_id in dlc_depots:
entry["dlcappid"] = dlc_depots[depot_id]
installed_depots[depot_id] = entry
if not installed_depots:
raise recognize.RecognizeError(
f"Every cached depot for {appid} is a shared depot -- nothing to install"
msg = (
"No cached depot manifests found under "
f"{install_dir}/{recognize.DEPOTDOWNLOADER_DIR}"
)
raise recognize.RecognizeError(msg)
depots = installed_depots(app_state, depot_manifests, proven.dlc_depots)
if not depots:
msg = f"Every cached depot for {appid} is a shared depot -- nothing to install"
raise recognize.RecognizeError(msg)
app_state["StateFlags"] = "4"
app_state["installdir"] = install_dir.name
app_state["LastUpdated"] = str(now_unix)
app_state["SizeOnDisk"] = str(size_on_disk)
app_state["SizeOnDisk"] = str(measure_content_size(install_dir))
app_state["StagingSize"] = "0"
app_state["buildid"] = buildid
app_state["buildid"] = pick_buildid(app_state, proven.build_id)
app_state["UpdateResult"] = "0"
app_state["InstalledDepots"] = installed_depots
app_state["InstalledDepots"] = depots
app_state.pop("StagedDepots", None)
# Steam's own per-DLC progress for a download it started: left in place it
# contradicts the settled counters and Steam resumes fetching the DLC.
@ -199,14 +177,14 @@ def backup_once(manifest_path: Path) -> None:
def write_atomic(path: Path, text: str) -> None:
"""Write via a temp file + rename so a crash can't truncate the manifest.
A partial write leaves an unparseable manifest, which Steam and this tool
both then choke on. `os.replace` is atomic within a filesystem, and the
A partial write leaves an unparsable manifest, which Steam and this tool
both then choke on. `Path.replace` is atomic within a filesystem, and the
temp file is created alongside the target to stay on the same one.
"""
tmp = path.with_name(f"{path.name}.tmp{os.getpid()}")
try:
tmp.write_text(text)
os.replace(tmp, path)
tmp.replace(path)
except BaseException:
tmp.unlink(missing_ok=True)
raise

View File

@ -1,8 +1,8 @@
"""`--map-installed`: run the compat decision over every installed game.
./run.sh --map-installed # dry run: print, write nothing
./run.sh --map-installed --apply # close Steam, map the safe ones
./run.sh --map-installed --appids 730,10 # decide any cached app (read-only)
steam_installer/run.sh --map-installed # dry run: write nothing
steam_installer/run.sh --map-installed --apply # close Steam, map the safe ones
steam_installer/run.sh --map-installed --appids 730,10 # any cached app, read-only
The dry run only reads, so it works with Steam open. `--apply` closes Steam
first (it rewrites config.vdf on exit) and restarts it afterwards.
@ -10,8 +10,8 @@ first (it rewrites config.vdf on exit) and restarts it afterwards.
from __future__ import annotations
import argparse
from pathlib import Path
import logging
from typing import TYPE_CHECKING, Any
from steam_installer import (
appinfo,
@ -24,11 +24,16 @@ from steam_installer import (
)
from steam_installer.library import SteamLibrary, list_libraries
if TYPE_CHECKING:
import argparse
from pathlib import Path
# Steam's own compat tools and runtimes are apps too, but not games.
TOOL_TYPES = {"tool", "config"}
def add_arguments(parser: argparse.ArgumentParser) -> None:
"""Register the ``--map-installed`` options on *parser*."""
parser.add_argument(
"--valve-proton",
action="store_true",
@ -50,13 +55,14 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
def check_arguments(parser: argparse.ArgumentParser, args: argparse.Namespace) -> None:
"""Reject option combinations that make no sense."""
if args.map_installed and args.valve_proton:
parser.error("--valve-proton goes with an install, not --map-installed")
def _installed(libraries: list[SteamLibrary]) -> dict[int, dict]:
"""appid -> AppState for every manifest in every library."""
found: dict[int, dict] = {}
def _installed(libraries: list[SteamLibrary]) -> dict[int, dict[str, Any]]:
"""Appid -> AppState for every manifest in every library."""
found: dict[int, dict[str, Any]] = {}
for library in libraries:
for manifest in sorted(library.steamapps.glob("appmanifest_*.acf")):
appid = manifest.stem.removeprefix("appmanifest_")
@ -65,19 +71,20 @@ def _installed(libraries: list[SteamLibrary]) -> dict[int, dict]:
return found
def _owned_dlc(app_state: dict) -> bool:
def _owned_dlc(app_state: dict[str, Any]) -> bool:
depots = app_state.get("InstalledDepots")
entries = depots.values() if isinstance(depots, dict) else []
return any(isinstance(entry, dict) and "dlcappid" in entry for entry in entries)
def _is_tool(block: dict) -> bool:
def _is_tool(block: dict[str, Any]) -> bool:
common = block.get("common")
kind = common.get("type", "") if isinstance(common, dict) else ""
return isinstance(kind, str) and kind.lower() in TOOL_TYPES
def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int:
"""Decide, and with ``--apply`` write, mappings for installed games."""
logger = log.logger
installed = _installed(list_libraries(steam_root))
if args.appids:
@ -85,7 +92,7 @@ def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int:
else:
appids = sorted(installed)
context = compat_apply.Context(steam_root)
blocks = appinfo._cached_blocks(appids)
blocks = appinfo.cached_blocks(appids)
to_map: list[int] = []
for appid in appids:
block = blocks.get(appid)
@ -95,7 +102,9 @@ def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int:
if _is_tool(block) and not args.appids:
continue
name = block.get("common", {}).get("name", "?")
decision = context.decide(appid, block, _owned_dlc(installed.get(appid, {})))
decision = context.decide(
appid, block, owned_dlc=_owned_dlc(installed.get(appid, {}))
)
line = f"{appid} {name}: {decision.describe()}"
if decision.map:
to_map.append(appid)
@ -109,15 +118,17 @@ def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int:
return _apply(args, logger, context.config, to_map)
def _apply(args, logger, config, to_map: list[int]) -> int:
was_running = flow._prepare_steam(args, logger)
def _apply(
args: argparse.Namespace, logger: logging.Logger, config: Path, to_map: list[int]
) -> int:
was_running = flow.prepare_steam(args, logger)
if was_running is None:
return 1
try:
for appid in to_map:
compat_apply.report(logger, appid, compat_map.map_app(config, appid))
except compat_map.CompatMapError as exc:
logger.error("Mapping stopped: %s", exc)
logger.log(logging.ERROR, "Mapping stopped: %s", exc)
return 1
finally:
if was_running:

View File

@ -1,6 +1,6 @@
"""Install the pinned proton-cachyos-native build into Steam's compatibilitytools.d.
python -m steam_installer.proton_native # what install.sh runs
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`
@ -24,18 +24,22 @@ from __future__ import annotations
import dataclasses
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import urllib.request
from pathlib import Path
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]:
@ -56,8 +60,11 @@ def in_use(tool: Path) -> list[int]:
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": "steam-game-installer"})
with urllib.request.urlopen(request, timeout=60) as response, dest.open("wb") as out:
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)
@ -67,13 +74,15 @@ def _fetch_verified(lock: Lock, work: Path, key: Path, fingerprint: str) -> Path
_download(lock.repo, db)
entry = proton_pkg.find_in_db(db, lock.name)
if entry.version != lock.version:
raise PkgError(
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:
raise PkgError("the repo's sha256 for the pinned version differs from the lock")
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)
@ -85,11 +94,13 @@ def _fetch_verified(lock: Lock, work: Path, key: Path, fingerprint: str) -> Path
proton_pkg.verify_signature(package, sig, key, fingerprint, home)
finally:
subprocess.run(
["gpgconf", "--homedir", str(home), "--kill", "all"],
check=False, capture_output=True,
[GPGCONF, "--homedir", str(home), "--kill", "all"],
check=False,
capture_output=True,
)
if proton_pkg.sha256_file(package) != lock.sha256:
raise PkgError("the downloaded package's sha256 differs from the pinned sha256")
msg = "the downloaded package's sha256 differs from the pinned sha256"
raise PkgError(msg)
return package
@ -140,8 +151,8 @@ def ensure(
busy = in_use(tool) if tool.exists() else []
if busy:
return (
f"deferred: Steam or a game is running (pid {', '.join(map(str, busy))}); "
"rerun with Steam closed"
"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})"
@ -157,7 +168,7 @@ def main(argv: list[str] | None = None) -> int:
try:
tools_dir = find_steam_root() / "compatibilitytools.d"
except FileNotFoundError:
print("native Proton skipped: no Steam installation")
sys.stdout.write("native Proton skipped: no Steam installation\n")
return 0
if argv and argv[0] == "--check":
stamp = tools_dir / TOOL_NAME / "VERSION"
@ -165,9 +176,9 @@ def main(argv: list[str] | None = None) -> int:
try:
status = ensure(tools_dir, lock)
except (PkgError, OSError) as exc:
print(f"error: native Proton: {exc}", file=sys.stderr)
sys.stderr.write(f"error: native Proton: {exc}\n")
return 1
print(f"native Proton {status}")
sys.stdout.write(f"native Proton {status}\n")
return 0

View File

@ -12,20 +12,25 @@ Pinned key: 882DCFE48E2051D48E2562ABF3B607488DB35A47 ("CachyOS
for the pin. Taken on 2026-11-22 from two other sources instead:
https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x882DCFE48E2051D48E2562ABF3B607488DB35A47
(the key file) and the long id F3B607488DB35A47 that CachyOS's own installer
imports, https://github.com/CachyOS/cachyos-repo-add-script/blob/5b27dc1eff276e104077cfaf75798b4c290eaef4/cachyos-repo.sh#L156
imports:
https://github.com/CachyOS/cachyos-repo-add-script/blob/5b27dc1eff276e104077cfaf75798b4c290eaef4/cachyos-repo.sh#L156
"""
from __future__ import annotations
from dataclasses import dataclass
import gzip
import hashlib
from pathlib import Path
import re
import subprocess
import tarfile
from dataclasses import dataclass
from pathlib import Path
from urllib.parse import quote
from steam_installer import errors
# On PATH, as ever (see steamctl.STEAM on why a name and not a literal).
GPG = "gpg"
DATA = Path(__file__).parent / "data"
LOCK = DATA / "proton-native.lock"
KEY = DATA / "cachyos_signing_key.asc"
@ -33,12 +38,14 @@ FINGERPRINT = "882DCFE48E2051D48E2562ABF3B607488DB35A47"
PREFIX = "usr/share/steam/compatibilitytools.d/proton-cachyos-native/"
class PkgError(Exception):
pass
class PkgError(errors.InstallerError):
"""A package failed a check and must not be installed."""
@dataclass(frozen=True)
class Lock:
"""The pinned package: name, version and checksum."""
name: str
repo: str # URL of the <repo>.db
version: str
@ -47,6 +54,8 @@ class Lock:
@dataclass(frozen=True)
class DbEntry:
"""One package's entry in the repo database."""
filename: str
version: str
sha256: str
@ -54,12 +63,15 @@ class DbEntry:
@dataclass(frozen=True)
class MtreeEntry:
"""One file's entry in a package's ``.MTREE``."""
kind: str # "file", "link" or "dir"
sha256: str = ""
link: str = ""
def read_lock(path: Path = LOCK) -> Lock:
"""Read the pinned package lock at *path*."""
fields = {}
for line in path.read_text().splitlines():
if line.strip() and not line.lstrip().startswith("#"):
@ -68,7 +80,8 @@ def read_lock(path: Path = LOCK) -> Lock:
try:
return Lock(fields["name"], fields["repo"], fields["version"], fields["sha256"])
except KeyError as exc:
raise PkgError(f"{path.name} has no {exc.args[0]}=") from exc
msg = f"{path.name} has no {exc.args[0]}="
raise PkgError(msg) from exc
def package_url(lock: Lock, filename: str) -> str:
@ -84,8 +97,11 @@ def find_in_db(db: Path, name: str) -> DbEntry:
continue
fields = _desc(tar.extractfile(member).read().decode())
if fields.get("NAME") == name:
return DbEntry(fields["FILENAME"], fields["VERSION"], fields["SHA256SUM"])
raise PkgError(f"the repo database lists no {name}")
return DbEntry(
fields["FILENAME"], fields["VERSION"], fields["SHA256SUM"]
)
msg = f"the repo database lists no {name}"
raise PkgError(msg)
def _desc(text: str) -> dict[str, str]:
@ -97,6 +113,7 @@ def _desc(text: str) -> dict[str, str]:
def sha256_file(path: Path) -> str:
"""Hex sha256 of the file at *path*."""
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1 << 20), b""):
@ -104,10 +121,13 @@ def sha256_file(path: Path) -> str:
return digest.hexdigest()
def _gpg(home: Path, *args: str) -> subprocess.CompletedProcess:
def _gpg(home: Path, *args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
["gpg", "--homedir", str(home), "--batch", "--no-tty", *args],
capture_output=True, text=True, check=False, timeout=60,
[GPG, "--homedir", str(home), "--batch", "--no-tty", *args],
capture_output=True,
text=True,
check=False,
timeout=60,
)
@ -125,10 +145,13 @@ def key_fingerprints(key: Path, home: Path) -> list[str]:
return out
def verify_signature(package: Path, sig: Path, key: Path, fingerprint: str, home: Path) -> None:
def verify_signature(
package: Path, sig: Path, key: Path, fingerprint: str, home: Path
) -> None:
"""Fail unless `sig` is a good signature by the pinned primary key."""
if fingerprint not in key_fingerprints(key, home):
raise PkgError("the pinned key file does not hold the pinned fingerprint")
msg = "the pinned key file does not hold the pinned fingerprint"
raise PkgError(msg)
result = _gpg(home, "--status-fd", "1", "--verify", str(sig), str(package))
signers = [
line.split()[-1]
@ -136,7 +159,8 @@ def verify_signature(package: Path, sig: Path, key: Path, fingerprint: str, home
if line.startswith("[GNUPG:] VALIDSIG ")
]
if result.returncode != 0 or signers != [fingerprint]:
raise PkgError(f"package signature does not verify against {fingerprint}")
msg = f"package signature does not verify against {fingerprint}"
raise PkgError(msg)
def _unescape(path: str) -> str:
@ -151,7 +175,8 @@ def read_mtree(package: Path) -> dict[str, MtreeEntry]:
text = gzip.decompress(tar.extractfile(member).read()).decode()
break
else:
raise PkgError("package has no .MTREE")
msg = "package has no .MTREE"
raise PkgError(msg)
entries: dict[str, MtreeEntry] = {}
defaults: dict[str, str] = {}
for line in text.splitlines():
@ -166,8 +191,10 @@ def read_mtree(package: Path) -> dict[str, MtreeEntry]:
if not path.startswith(PREFIX) or path == PREFIX:
continue
values = {**defaults, **values}
entries[path[len(PREFIX):]] = MtreeEntry(
values.get("type", "file"), values.get("sha256digest", ""), values.get("link", "")
entries[path[len(PREFIX) :]] = MtreeEntry(
values.get("type", "file"),
values.get("sha256digest", ""),
values.get("link", ""),
)
return entries
@ -203,6 +230,6 @@ def extract(package: Path, dest: Path) -> None:
members = []
for member in tar.getmembers():
if member.name.startswith(PREFIX) and member.name != PREFIX:
member.name = member.name[len(PREFIX):]
member.name = member.name[len(PREFIX) :]
members.append(member)
tar.extractall(dest, members=members, filter="data")

View File

@ -9,12 +9,15 @@ and client process, and whether a manifest's bookkeeping says "done".
from __future__ import annotations
from pathlib import Path
import re
import shutil
from pathlib import Path
from typing import TYPE_CHECKING, Any
from steam_installer import vdf
from steam_installer.library import SteamLibrary
from steam_installer import errors, vdf
if TYPE_CHECKING:
from steam_installer.library import SteamLibrary
MANIFEST_CACHE_RE = re.compile(r"^(\d+)_(\d+)\.manifest$")
@ -26,20 +29,22 @@ DEPOTDOWNLOADER_DIR = ".DepotDownloader"
PROC_DIR = Path("/proc")
class RecognizeError(Exception):
pass
class RecognizeError(errors.InstallerError):
"""The manifest does not match the install."""
def manifest_reflects_real_install(
library: SteamLibrary, appid: int, install_dir: Path
) -> bool:
"""Don't trust steamcmd's exit code alone -- verify the manifest actually matches
reality. Confirmed necessary the hard way: steamcmd exited 0 while leaving
`installdir` pointed at a stale, unrelated folder name from an earlier corrupted
manifest state, `InstalledDepots` empty, and `StateFlags` still "update needed" --
a "clean" exit that hadn't actually recognized anything. Relaunching the Steam
client then treated the real, correctly-downloaded folder as orphaned and wiped
it while starting a fresh (slow, wrong-folder) download.
"""Whether the manifest matches the real install, not just steamcmd's exit code.
Don't trust steamcmd's exit code alone. Confirmed necessary the hard way:
steamcmd exited 0 while leaving `installdir` pointed at a stale, unrelated
folder name from an earlier corrupted manifest state, `InstalledDepots`
empty, and `StateFlags` still "update needed" -- a "clean" exit that hadn't
actually recognized anything. Relaunching the Steam client then treated the
real, correctly-downloaded folder as orphaned and wiped it while starting a
fresh (slow, wrong-folder) download.
"""
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
if not manifest_path.is_file():
@ -56,7 +61,7 @@ def manifest_reflects_real_install(
)
def download_counters_settled(app_state: dict) -> bool:
def download_counters_settled(app_state: dict[str, Any]) -> bool:
"""Whether the download bookkeeping agrees that nothing is left to fetch.
This is the check that was missing when Steam threw away a StateFlags=4
@ -73,11 +78,11 @@ def download_counters_settled(app_state: dict) -> bool:
# ones are not.
if total is not None and done is not None and total != done:
return False
return app_state.get("StagingSize", "0") == "0"
return bool(app_state.get("StagingSize", "0") == "0")
def discover_cached_depot_manifests(install_dir: Path) -> dict[str, str]:
"""Depot ID -> manifest ID, read from depotdownloader's own local cache filenames."""
"""Depot ID -> manifest ID, from depotdownloader's local cache filenames."""
cache_dir = install_dir / DEPOTDOWNLOADER_DIR
found: dict[str, str] = {}
if not cache_dir.is_dir():
@ -116,7 +121,7 @@ def clear_steam_staging(library: SteamLibrary, appid: int) -> int:
return freed
def read_app_state(manifest_path: Path) -> dict:
def read_app_state(manifest_path: Path) -> dict[str, Any]:
"""The `AppState` block of a manifest, or an empty dict if unreadable."""
if not manifest_path.is_file():
return {}

View File

@ -20,12 +20,14 @@ asking for a numeric AppID rather than treating a search failure as fatal.
from __future__ import annotations
from dataclasses import dataclass
import json
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from dataclasses import dataclass
from steam_installer import _http, errors
COMMUNITY_SEARCH_URL = "https://steamcommunity.com/actions/SearchApps/{term}"
STORE_SEARCH_URL = "https://store.steampowered.com/api/storesearch/?term={term}&cc=us"
@ -45,24 +47,27 @@ RETRY_DELAY = 1.0
@dataclass
class SearchResult:
"""One candidate from a name search."""
appid: int
name: str
class ResolveError(Exception):
pass
class ResolveError(errors.InstallerError):
"""The target could not be resolved to a single app."""
class SearchUnavailable(Exception):
class SearchUnavailableError(errors.InstallerError):
"""Neither search endpoint could be reached (network/TLS block, not a code bug)."""
def _fetch_json(url: str, timeout: float, *, attempts: int = ATTEMPTS) -> object:
request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
last: Exception = RuntimeError("no attempts made")
for attempt in range(attempts):
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
with _http.open_https(
url, timeout=timeout, user_agent=USER_AGENT
) as response:
return json.loads(response.read())
except (urllib.error.URLError, TimeoutError, OSError) as exc:
last = exc
@ -77,7 +82,8 @@ def _search_community(term: str, timeout: float) -> list[SearchResult]:
"""Community endpoint. Returns a bare JSON list; appids arrive as strings."""
payload = _fetch_json(COMMUNITY_SEARCH_URL.format(term=term), timeout)
if not isinstance(payload, list):
raise TypeError(f"unexpected community search payload: {type(payload)}")
msg = f"unexpected community search payload: {type(payload)}"
raise TypeError(msg)
return [
SearchResult(appid=int(item["appid"]), name=item["name"])
for item in payload
@ -89,7 +95,8 @@ def _search_store(term: str, timeout: float) -> list[SearchResult]:
"""Storefront endpoint. Returns {"items": [{"id": int, "name": str}, ...]}."""
payload = _fetch_json(STORE_SEARCH_URL.format(term=term), timeout)
if not isinstance(payload, dict):
raise TypeError(f"unexpected store search payload: {type(payload)}")
msg = f"unexpected store search payload: {type(payload)}"
raise TypeError(msg)
return [
SearchResult(appid=int(item["id"]), name=item["name"])
for item in payload.get("items", [])
@ -115,7 +122,7 @@ def search(name: str, *, timeout: float = DEFAULT_TIMEOUT) -> list[SearchResult]
KeyError,
) as exc:
failures.append(f"{label}: {exc}")
raise SearchUnavailable("; ".join(failures))
raise SearchUnavailableError("; ".join(failures))
def exact_match(results: list[SearchResult], name: str) -> SearchResult | None:
@ -133,14 +140,16 @@ def exact_match(results: list[SearchResult], name: str) -> SearchResult | None:
def prompt_confirm(results: list[SearchResult]) -> SearchResult:
"""Show numbered candidates and require an explicit choice. Never guesses."""
if not results:
raise ResolveError("No matches found on Steam.")
print("Matches found:")
msg = "No matches found on Steam."
raise ResolveError(msg)
sys.stdout.write("Matches found:\n")
for i, result in enumerate(results, start=1):
print(f" {i}. {result.name} (appid {result.appid})")
sys.stdout.write(f" {i}. {result.name} (appid {result.appid})\n")
while True:
choice = input(f"Which one? [1-{len(results)}, or 0 to cancel]: ").strip()
if choice == "0":
raise ResolveError("Cancelled by user.")
msg = "Cancelled by user."
raise ResolveError(msg)
if choice.isdigit() and 1 <= int(choice) <= len(results):
return results[int(choice) - 1]
print("Invalid choice, try again.")
sys.stdout.write("Invalid choice, try again.\n")

View File

@ -2,25 +2,31 @@
# ============================================================================
# Install a Steam game from whatever you happen to have: an AppID, a Steam URL,
# or the game's name.
# or the game's name. Part of steam-backlog-enforcer; the root daemon launches
# `python3 -m steam_installer.cli` itself, so this is the by-hand entry point.
#
# ./run.sh 284160
# ./run.sh https://steamcommunity.com/app/284160/guides/
# ./run.sh BeamNG.drive
# ./run.sh BeamNG
# steam_installer/run.sh 284160
# steam_installer/run.sh https://steamcommunity.com/app/284160/guides/
# steam_installer/run.sh BeamNG.drive
# steam_installer/run.sh BeamNG
#
# Dependencies install themselves on first run. Every other argument is passed
# straight through to the CLI, so `./run.sh --help` lists the full set.
# straight through to the CLI, so `run.sh --help` lists the full set.
# ============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
readonly VENV_PY="${SCRIPT_DIR}/.venv/bin/python"
readonly STAMP="${SCRIPT_DIR}/.venv/.deps-ok"
readonly INSTALLER="${SCRIPT_DIR}/install.sh"
readonly PROTON_LOCK="${SCRIPT_DIR}/steam_installer/data/proton-native.lock"
readonly REPO_DIR="${SCRIPT_DIR%/*}"
readonly PYTHON="${SGI_PYTHON:-/usr/bin/python3}"
readonly STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/steam-game-installer"
readonly STAMP="${STATE_DIR}/.deps-ok"
readonly INSTALLER="${SCRIPT_DIR}/install_deps.sh"
readonly PROTON_LOCK="${SCRIPT_DIR}/data/proton-native.lock"
# The package lives in this checkout, not in site-packages.
export PYTHONPATH="${REPO_DIR}${PYTHONPATH:+:$PYTHONPATH}"
SKIP_DEPS="${SGI_SKIP_DEPS:-0}"
DEPS_ONLY=0
@ -40,22 +46,19 @@ usage() {
echo " --deps-only Install dependencies and exit"
echo " -h, --help Show this help, then the installer's own options"
echo
if [[ -x "$VENV_PY" ]]; then
"$VENV_PY" -m steam_installer.cli --help
fi
"$PYTHON" -m steam_installer.cli --help
exit 0
}
# Dependencies are only re-checked when the stamp is missing or older than the
# files that decide what gets installed, so a normal run adds no startup cost.
deps_are_current() {
[[ -x "$VENV_PY" ]] || return 1
[[ -f "$STAMP" ]] || return 1
[[ "$STAMP" -nt "$INSTALLER" ]] || return 1
[[ "$STAMP" -nt "${SCRIPT_DIR}/pyproject.toml" ]] || return 1
[[ "$STAMP" -nt "${REPO_DIR}/pyproject.toml" ]] || return 1
[[ "$STAMP" -nt "$PROTON_LOCK" ]] || return 1
# A bumped pin or a deleted tool folder: the stamp alone would miss both.
"$VENV_PY" -m steam_installer.proton_native --check >/dev/null 2>&1
"$PYTHON" -m steam_installer.proton_native --check >/dev/null 2>&1
}
main() {
@ -67,14 +70,9 @@ main() {
exit 0
fi
[[ -x "$VENV_PY" ]] || {
echo "error: no virtualenv at ${VENV_PY}. Run ./install.sh" >&2
exit 1
}
# `-m` rather than the console script, so a stale .venv shim can never
# shadow the working tree.
exec "$VENV_PY" -m steam_installer.cli "${ARGS[@]}"
# `-m` rather than a console script, so a stale install can never shadow
# the working tree.
exec "$PYTHON" -m steam_installer.cli "${ARGS[@]}"
}
while [[ $# -gt 0 ]]; do

View File

@ -17,18 +17,18 @@ mirror every byte to both the terminal and the log.
from __future__ import annotations
from dataclasses import dataclass
import errno
import io
import logging
import os
from pathlib import Path
import pty
import select
import shlex
import subprocess
import sys
import time
from dataclasses import dataclass
from pathlib import Path
LOG_DIR = Path.home() / ".local/state/steam-game-installer"
LOGGER_NAME = "steam_installer"
@ -63,7 +63,7 @@ class RunLog:
self.logger.debug("(unreadable: %s)", exc)
def setup(appid: int | None, verbose: bool = False) -> RunLog:
def setup(appid: int | None, *, verbose: bool = False) -> RunLog:
"""Start a run log. Console stays terse; the file gets everything at DEBUG."""
LOG_DIR.mkdir(parents=True, exist_ok=True)
stamp = time.strftime("%Y%m%d-%H%M%S")
@ -102,6 +102,24 @@ def _point_latest_at(path: Path) -> None:
pass
def _spawn_on_pty(
cmd: list[str], master_fd: int, slave_fd: int
) -> subprocess.Popen[bytes]:
"""Start `cmd` with its output on the pty; close both ends if it won't start."""
try:
return subprocess.Popen(
cmd,
stdout=slave_fd,
stderr=slave_fd,
stdin=_stdin_for_child(slave_fd),
close_fds=True,
)
except OSError:
os.close(master_fd)
os.close(slave_fd)
raise
def run_logged(
cmd: list[str],
logger: logging.Logger,
@ -118,18 +136,7 @@ def run_logged(
master_fd, slave_fd = pty.openpty()
chunks: list[bytes] = []
try:
process = subprocess.Popen(
cmd,
stdout=slave_fd,
stderr=slave_fd,
stdin=_stdin_for_child(slave_fd),
close_fds=True,
)
except OSError:
os.close(master_fd)
os.close(slave_fd)
raise
process = _spawn_on_pty(cmd, master_fd, slave_fd)
# The child owns the slave end now; holding it open here would keep the
# master readable forever and hang the relay after the child exits.
os.close(slave_fd)
@ -169,8 +176,11 @@ def _log_output(
def _read_pty(fd: int, size: int) -> bytes:
"""Indirection so tests can fault-inject the pty read without patching the
global `os.read`, which subprocess itself uses during spawn."""
"""Read from the pty; an indirection so tests can fault-inject it.
Tests use it instead of patching the global `os.read`, which subprocess
itself uses during spawn.
"""
return os.read(fd, size)
@ -184,14 +194,14 @@ def _stdin_for_child(slave_fd: int) -> int:
"""
try:
return sys.stdin.fileno()
except (AttributeError, ValueError, OSError, io.UnsupportedOperation):
except AttributeError, ValueError, OSError, io.UnsupportedOperation:
return slave_fd
def _relay(
master_fd: int,
chunks: list[bytes],
process: subprocess.Popen,
process: subprocess.Popen[bytes],
timeout: float | None,
started: float,
) -> None:

View File

@ -13,18 +13,24 @@ escalate.
from __future__ import annotations
import logging
import subprocess
import time
from collections.abc import Callable
from typing import TYPE_CHECKING
from steam_installer import recognize
from steam_installer import errors, recognize
if TYPE_CHECKING:
from collections.abc import Callable
import logging
# Looked up on PATH by the OS, as `steam` always was; a name, not a literal in
# the argv list, because ruff S607 objects to partial executable paths there.
STEAM = "steam"
SHUTDOWN_TIMEOUT = 60.0
POLL_INTERVAL = 0.5
class SteamStillRunning(Exception):
class SteamStillRunningError(errors.InstallerError):
"""Steam was asked to exit and did not."""
@ -36,14 +42,14 @@ def stop(
) -> bool:
"""Shut Steam down. Returns True if it had been running, False if it wasn't.
Raises SteamStillRunning if it refuses to exit, so callers can abort
Raises SteamStillRunningError if it refuses to exit, so callers can abort
without having touched anything.
"""
if not recognize.steam_client_pids():
return False
logger.info("Shutting down the Steam client...")
subprocess.run(["steam", "-shutdown"], check=False, capture_output=True)
subprocess.run([STEAM, "-shutdown"], check=False, capture_output=True)
waited = 0.0
while waited < timeout:
@ -54,19 +60,25 @@ def stop(
waited += POLL_INTERVAL
pids = recognize.steam_client_pids()
raise SteamStillRunning(
msg = (
f"Steam is still running after {timeout:.0f}s (pid "
f"{', '.join(str(p) for p in pids)}). Close it by hand and re-run."
)
raise SteamStillRunningError(msg)
def _spawn_detached(cmd: list[str]) -> subprocess.Popen[bytes]:
"""Start `cmd` in its own session with no output; the caller never waits."""
return subprocess.Popen(
cmd,
start_new_session=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
def start(logger: logging.Logger) -> None:
"""Relaunch Steam, detached, so it outlives this process."""
logger.info("Restarting the Steam client...")
# Detached on purpose: Steam outlives this process and is never waited on.
subprocess.Popen(
["steam"],
start_new_session=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
_spawn_detached([STEAM])

View File

@ -32,16 +32,21 @@ but they are not frozen either -- on ext4 there is no reflink to get both.
from __future__ import annotations
import logging
import os
from dataclasses import dataclass
from pathlib import Path
import os
from typing import TYPE_CHECKING
from steam_installer.library import SteamLibrary
if TYPE_CHECKING:
import logging
from pathlib import Path
from steam_installer.library import SteamLibrary
@dataclass(frozen=True)
class Staged:
"""Content Steam staged for a download in progress."""
appid: int
staging_dir: Path
install_dir: Path
@ -51,6 +56,8 @@ class Staged:
@dataclass(frozen=True)
class Adoption:
"""What taking over staged content achieved."""
linked: int
skipped: int
failed: int

View File

@ -9,18 +9,18 @@ Four accepted forms, all resolving to the same app:
BeamNG
Whatever the input, the appid is always confirmed against Steam's own app
cache (`appcache/appinfo.vdf`) before anything acts on it, so the name and installdir used
downstream come from Steam rather than from a search index that ranks
cache (`appcache/appinfo.vdf`) before anything acts on it, so the name and
installdir used downstream come from Steam rather than from a search index that ranks
"Portal 2" above "Portal".
"""
from __future__ import annotations
from dataclasses import dataclass
import re
import sys
from dataclasses import dataclass
from steam_installer import appinfo, resolve
from steam_installer import appinfo, errors, resolve
# `app/<digits>` preceded by a delimiter: matches both store and community
# URLs, any trailing path or query, and `steam://` links. Not anchored to a
@ -28,12 +28,14 @@ from steam_installer import appinfo, resolve
APP_URL_RE = re.compile(r"(?:^|[/?&])app/(\d+)(?:[/?#]|$)")
class TargetError(Exception):
class TargetError(errors.InstallerError):
"""The input could not be resolved to exactly one Steam app."""
@dataclass(frozen=True)
class Resolved:
"""A target resolved to an app and its metadata."""
metadata: appinfo.AppMetadata
source: str # "flag" | "appid" | "url" | "exact" | "picked"
@ -53,19 +55,20 @@ def _confirm(appid: int, source: str) -> Resolved:
try:
return Resolved(appinfo.get_metadata(appid), source)
except appinfo.AppInfoError as exc:
raise TargetError(f"appid {appid} is not a known Steam app ({exc})") from exc
msg = f"appid {appid} is not a known Steam app ({exc})"
raise TargetError(msg) from exc
def _by_name(text: str, *, interactive: bool) -> Resolved:
try:
results = resolve.search(text)
except resolve.SearchUnavailable as exc:
raise TargetError(
f"Steam search is unavailable ({exc}). Pass the numeric AppID instead."
) from exc
except resolve.SearchUnavailableError as exc:
msg = f"Steam search is unavailable ({exc}). Pass the numeric AppID instead."
raise TargetError(msg) from exc
if not results:
raise TargetError(f"No Steam app matches {text!r}.")
msg = f"No Steam app matches {text!r}."
raise TargetError(msg)
if len(results) == 1:
return _confirm(results[0].appid, "only match")
@ -77,13 +80,14 @@ def _by_name(text: str, *, interactive: bool) -> Resolved:
if not interactive:
# Fail closed. Guessing here is how the wrong 90 GB gets downloaded.
print(f"{text!r} is ambiguous. Candidates:", file=sys.stderr)
sys.stderr.write(f"{text!r} is ambiguous. Candidates:\n")
for result in results:
print(f" {result.appid} {result.name}", file=sys.stderr)
raise TargetError(
sys.stderr.write(f" {result.appid} {result.name}\n")
msg = (
"Ambiguous name in a non-interactive run. Re-run with the exact "
"title or the numeric AppID."
)
raise TargetError(msg)
return _confirm(resolve.prompt_confirm(results).appid, "picked")
@ -95,7 +99,8 @@ def resolve_target(
if appid is not None:
return _confirm(appid, "flag")
if text is None:
raise TargetError("No target given.")
msg = "No target given."
raise TargetError(msg)
literal = parse_literal(text)
if literal is not None:

View File

@ -0,0 +1 @@
"""Installer tests."""

View File

@ -0,0 +1,79 @@
"""Manifest helpers for tests that need a Steam library on disk."""
from __future__ import annotations
from typing import TYPE_CHECKING
from steam_installer import vdf
if TYPE_CHECKING:
from pathlib import Path
from steam_installer.library import SteamLibrary
GHOST_ACF = """"AppState"
{
\t"appid"\t\t"284160"
\t"universe"\t\t"1"
\t"name"\t\t"BeamNG.drive"
\t"StateFlags"\t\t"4"
\t"installdir"\t\t"BeamNG.drive"
\t"LastUpdated"\t\t"0"
\t"SizeOnDisk"\t\t"0"
\t"StagingSize"\t\t"0"
\t"buildid"\t\t"24617469"
\t"DownloadType"\t\t"1"
\t"BytesToDownload"\t\t"0"
\t"BytesDownloaded"\t\t"0"
\t"TargetBuildID"\t\t"24617469"
\t"InstalledDepots"
\t{
\t}
\t"UserConfig"
\t{
\t\t"language"\t\t"english"
\t}
}
"""
def _write(library: SteamLibrary, appid: int, text: str) -> Path:
path = library.steamapps / f"appmanifest_{appid}.acf"
path.write_text(text)
return path
def _real_install(
library: SteamLibrary,
appid: int = 475150,
installdir: str = "RealGame",
size: str = "13908166765",
) -> Path:
"""A manifest shaped like the genuinely installed apps in a live library."""
_write(
library,
appid,
vdf.dumps(
"AppState",
{
"appid": str(appid),
"StateFlags": "4",
"installdir": installdir,
"SizeOnDisk": size,
"InstalledDepots": {"475151": {"manifest": "123"}},
},
),
)
content = library.common / installdir
content.mkdir(parents=True, exist_ok=True)
(content / "game.pak").write_bytes(b"x" * 4096)
return content
def stamp_native_tool(root: Path, version: str) -> None:
"""Put a ready native Proton (manifest and version stamp) under *root*."""
tool = root / "compatibilitytools.d" / "proton-cachyos-native"
tool.mkdir(parents=True)
(tool / "toolmanifest.vdf").write_text("x")
(tool / "VERSION").write_text(version + "\n")

View File

@ -37,8 +37,9 @@ def encode(apps: dict[int, dict], change_number: int = 41062472) -> bytes:
body = bytearray()
for appid, data in apps.items():
kv = _kv({"appinfo": data}, strings)
header = struct.pack("<IIQ20sI20s", 2, 1795000000, 0, b"\0" * 20,
change_number, b"\0" * 20)
header = struct.pack(
"<IIQ20sI20s", 2, 1795000000, 0, b"\0" * 20, change_number, b"\0" * 20
)
payload = header + kv
body += struct.pack("<II", appid, len(payload)) + payload
body += struct.pack("<I", 0)

View File

@ -0,0 +1,43 @@
"""Stubs shared by the CLI flow tests (plain functions, not fixtures)."""
from __future__ import annotations
from typing import TYPE_CHECKING
from steam_installer import appinfo, depotdl, recognize
if TYPE_CHECKING:
from pathlib import Path
import pytest
ARGV = ["--appid", "1771300", "--username", "u", "--language", "english"]
def _stub_download(monkeypatch: pytest.MonkeyPatch, returncode: int = 0) -> None:
monkeypatch.setattr(depotdl, "download", lambda appid, pull, logger: returncode)
def proven_plan(
monkeypatch: pytest.MonkeyPatch,
install_dir: Path,
shared_owner: dict[str, str] | None = None,
shared: set[str] | None = None,
) -> str:
"""Stub the depot plan so the cached manifest proves the public build.
*shared* defaults to the depots *shared_owner* names. Returns the gid.
"""
gid = next(iter(recognize.discover_cached_depot_manifests(install_dir).values()))
shared_depots = set(shared_owner or ()) if shared is None else shared
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: appinfo.DepotPlan(
{"1771303"},
shared_depots,
manifests={"1771303": gid},
shared_owner=dict(shared_owner or {}),
),
)
return gid

View File

@ -0,0 +1,31 @@
"""Shared data for the map-installed tests."""
from __future__ import annotations
from typing import TYPE_CHECKING
from steam_installer import compat_map, proton_pkg
if TYPE_CHECKING:
from pathlib import Path
LOCKED = proton_pkg.read_lock().version
CONFIG = (
'"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n'
'\t\t\t"Steam"\n\t\t\t{\n\t\t\t}\n\t\t}\n\t}\n}\n'
)
PLAIN = {
"common": {"name": "Plain", "oslist": "windows", "type": "Game"},
"depots": {"11": {"config": {"oslist": "windows"}}},
}
NATIVE = {"common": {"name": "Native", "oslist": "windows,linux", "type": "Game"}}
SHARED = {
"common": {"name": "Shared", "oslist": "windows", "type": "Game"},
"depots": {"31": {"depotfromapp": "228980"}},
}
CS = {"common": {"name": "CS", "oslist": "windows,linux", "type": "Game"}}
TOOL = {"common": {"name": "Proton Experimental", "type": "Tool"}}
def _mapped(root: Path, appid: int) -> tuple[str, str] | None:
return compat_map.current(root / "config" / "config.vdf", appid)

View File

@ -7,13 +7,22 @@ the real mirror serves.
from __future__ import annotations
from dataclasses import dataclass
import gzip
import hashlib
import io
import shutil
import subprocess
import tarfile
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path
from steam_installer.proton_pkg import Lock
type Setup = tuple[Path, Lock, Callable[..., str]]
PREFIX = "usr/share/steam/compatibilitytools.d/proton-cachyos-native/"
VERSION = "1:11.0.20261015-1"
@ -27,8 +36,14 @@ FILES = {
LINKS = {"files/bin/msidb": "wine"}
GPG = shutil.which("gpg") or "gpg"
GPGCONF = shutil.which("gpgconf") or "gpgconf"
@dataclass
class Repo:
"""Repo."""
root: Path # served as the mirror: holds cachyos.db, the package, the .sig
key: Path
fingerprint: str
@ -37,10 +52,12 @@ class Repo:
@property
def db_url(self) -> str:
"""Db url."""
return (self.root / "cachyos.db").as_uri()
@property
def package(self) -> Path:
"""Package."""
return self.root / FILENAME
@ -63,7 +80,9 @@ def _add(tar: tarfile.TarFile, name: str, data: bytes) -> None:
tar.addfile(info, io.BytesIO(data))
def build_package(path: Path, files: dict[str, bytes] = FILES) -> None:
def build_package(path: Path, files: dict[str, bytes] | None = None) -> None:
"""Build package."""
files = FILES if files is None else files
with tarfile.open(path, "w:zst") as tar:
_add(tar, ".INSTALL", b"rm -rf /\n") # never run, never extracted
_add(tar, ".MTREE", _mtree())
@ -77,7 +96,10 @@ def build_package(path: Path, files: dict[str, bytes] = FILES) -> None:
tar.addfile(info)
def build_db(path: Path, version: str, sha256: str, name: str = "proton-cachyos-native") -> None:
def build_db(
path: Path, version: str, sha256: str, name: str = "proton-cachyos-native"
) -> None:
"""Build db."""
desc = (
f"%FILENAME%\n{name}-{version}-x86_64.pkg.tar.zst\n\n%NAME%\n{name}\n\n"
f"%VERSION%\n{version}\n\n%SHA256SUM%\n{sha256}\n\n"
@ -89,24 +111,37 @@ def build_db(path: Path, version: str, sha256: str, name: str = "proton-cachyos-
def gpg(home: Path, *args: str) -> None:
"""Gpg."""
subprocess.run(
["gpg", "--homedir", str(home), "--batch", "--passphrase", "", *args],
[GPG, "--homedir", str(home), "--batch", "--passphrase", "", *args],
check=True,
capture_output=True,
)
def build_repo(tmp: Path) -> Repo:
"""Build repo."""
root = tmp / "mirror"
root.mkdir()
home = tmp / "signer"
home.mkdir(mode=0o700)
gpg(home, "--quick-gen-key", "Fake CachyOS <fake@example.invalid>", "ed25519", "sign", "never")
gpg(
home,
"--quick-gen-key",
"Fake CachyOS <fake@example.invalid>",
"ed25519",
"sign",
"never",
)
listing = subprocess.run(
["gpg", "--homedir", str(home), "--with-colons", "--fingerprint"],
check=True, capture_output=True, text=True,
[GPG, "--homedir", str(home), "--with-colons", "--fingerprint"],
check=True,
capture_output=True,
text=True,
).stdout
fingerprint = next(line.split(":")[9] for line in listing.splitlines() if line.startswith("fpr"))
fingerprint = next(
line.split(":")[9] for line in listing.splitlines() if line.startswith("fpr")
)
key = tmp / "key.asc"
gpg(home, "--armor", "--output", str(key), "--export", fingerprint)
package = root / FILENAME
@ -118,4 +153,5 @@ def build_repo(tmp: Path) -> Repo:
def stop_agent(home: Path) -> None:
subprocess.run(["gpgconf", "--homedir", str(home), "--kill", "all"], check=False)
"""Stop agent."""
subprocess.run([GPGCONF, "--homedir", str(home), "--kill", "all"], check=False)

View File

@ -1,9 +1,14 @@
"""Fixtures-as-functions shared by the recognize/manifest test files."""
from pathlib import Path
from __future__ import annotations
from typing import TYPE_CHECKING
from steam_installer import vdf
if TYPE_CHECKING:
from pathlib import Path
def _write_partial_manifest(steamapps: Path, appid: int) -> None:
steamapps.mkdir(parents=True, exist_ok=True)
@ -16,7 +21,7 @@ def _write_partial_manifest(steamapps: Path, appid: int) -> None:
(steamapps / f"appmanifest_{appid}.acf").write_text(vdf.dumps("AppState", data))
def _fake_proc(tmp_path, entries):
def _fake_proc(tmp_path: Path, entries: dict[str, str | None]) -> Path:
"""Build a /proc-shaped tree: {pid: exe_target_path_or_None}."""
proc = tmp_path / "proc"
proc.mkdir()

View File

@ -0,0 +1,19 @@
"""Run a command in a test and capture its text output."""
from __future__ import annotations
import subprocess
def run_captured(
argv: list[str], env: dict[str, str], timeout: float = 30
) -> subprocess.CompletedProcess[str]:
"""Run *argv* under *env*, text output captured, never raising on exit code."""
return subprocess.run(
argv,
capture_output=True,
text=True,
env=env,
timeout=timeout,
check=False,
)

View File

@ -9,113 +9,81 @@ several files need them, and importing a fixture across test modules confuses
both pytest and the linter.
"""
from __future__ import annotations
import logging
import os
import pathlib
from pathlib import Path
import shutil
from typing import TYPE_CHECKING, Never
import pytest
from steam_installer import (
anticheat,
appinfo,
appinfo_cache,
cli,
config,
depotdl,
deps,
proton_native,
proton_pkg,
recognize,
runlog,
steamctl,
vdf,
)
from steam_installer.library import SteamLibrary
from steam_installer.tests._proton_fixture import VERSION, Repo, build_repo, stop_agent
if TYPE_CHECKING:
from collections.abc import Iterator
from steam_installer.tests._proton_fixture import Setup
@pytest.fixture
def coverage_subprocess_env():
def coverage_subprocess_env() -> dict[str, str]:
"""Env for a child process that should also record coverage."""
env = os.environ.copy()
root = pathlib.Path(__file__).resolve().parent.parent
root = pathlib.Path(__file__).resolve().parents[2]
env["COVERAGE_PROCESS_START"] = str(root / "pyproject.toml")
env["PYTHONPATH"] = str(root) + os.pathsep + env.get("PYTHONPATH", "")
return env
GHOST_ACF = """"AppState"
{
\t"appid"\t\t"284160"
\t"universe"\t\t"1"
\t"name"\t\t"BeamNG.drive"
\t"StateFlags"\t\t"4"
\t"installdir"\t\t"BeamNG.drive"
\t"LastUpdated"\t\t"0"
\t"SizeOnDisk"\t\t"0"
\t"StagingSize"\t\t"0"
\t"buildid"\t\t"24617469"
\t"DownloadType"\t\t"1"
\t"BytesToDownload"\t\t"0"
\t"BytesDownloaded"\t\t"0"
\t"TargetBuildID"\t\t"24617469"
\t"InstalledDepots"
\t{
\t}
\t"UserConfig"
\t{
\t\t"language"\t\t"english"
\t}
}
"""
@pytest.fixture
def library(tmp_path):
@pytest.fixture(name="library")
def _library(tmp_path: Path) -> SteamLibrary:
"""The library fixture."""
lib = SteamLibrary(tmp_path / "Steam")
lib.steamapps.mkdir(parents=True)
lib.common.mkdir(parents=True)
return lib
def _write(library, appid, text):
path = library.steamapps / f"appmanifest_{appid}.acf"
path.write_text(text)
return path
def _real_install(library, appid=475150, installdir="RealGame", size="13908166765"):
"""A manifest shaped like the genuinely installed apps in a live library."""
_write(
library,
appid,
vdf.dumps(
"AppState",
{
"appid": str(appid),
"StateFlags": "4",
"installdir": installdir,
"SizeOnDisk": size,
"InstalledDepots": {"475151": {"manifest": "123"}},
},
),
)
content = library.common / installdir
content.mkdir(parents=True, exist_ok=True)
(content / "game.pak").write_bytes(b"x" * 4096)
return content
@pytest.fixture
def isolated_cli(tmp_path, monkeypatch):
monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state")
def isolated_cli(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest
) -> None:
"""The isolated cli fixture."""
# Requested by name: pylint rejects an argument that is only there for its effect.
request.getfixturevalue("cli_logs")
monkeypatch.setattr(config, "CONFIG_DIR", tmp_path / "cfg")
monkeypatch.setattr(config, "CONFIG_PATH", tmp_path / "cfg" / "config")
monkeypatch.delenv(config.ENV_VAR, raising=False)
monkeypatch.setattr(deps.shutil, "which", lambda name: f"/usr/bin/{name}")
monkeypatch.setattr(recognize, "steam_client_pids", list)
yield
logger = logging.getLogger(runlog.LOGGER_NAME)
for handler in list(logger.handlers):
logger.removeHandler(handler)
handler.close()
@pytest.fixture
def cli_steam(tmp_path, monkeypatch, isolated_cli):
def cli_steam(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest
) -> SteamLibrary:
"""A fake Steam library with the cli isolated from this machine's state."""
# Requested by name: pylint rejects an argument that is only there for its effect.
request.getfixturevalue("isolated_cli")
library = SteamLibrary(tmp_path / "Steam")
library.steamapps.mkdir(parents=True)
library.common.mkdir(parents=True)
@ -127,19 +95,20 @@ def cli_steam(tmp_path, monkeypatch, isolated_cli):
lambda appid: appinfo.AppMetadata(appid, "BeamNG.drive", "BeamNG.drive"),
)
monkeypatch.setattr(
depotdl, "download", lambda **kw: pytest.fail("must not download")
depotdl, "download", lambda *a, **kw: pytest.fail("must not download")
)
return library
@pytest.fixture(autouse=True)
def _no_host_depotdownloader(tmp_path, monkeypatch):
def _no_host_depotdownloader(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Never let this machine's ~/.local/bin decide which binary a test sees."""
monkeypatch.setattr(deps, "MANAGED_DEPOTDOWNLOADER", tmp_path / "absent")
@pytest.fixture
def cli_logs(tmp_path, monkeypatch):
def cli_logs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
"""The cli logs fixture."""
monkeypatch.setattr(runlog, "LOG_DIR", tmp_path / "state")
yield
logger = logging.getLogger(runlog.LOGGER_NAME)
@ -149,7 +118,7 @@ def cli_logs(tmp_path, monkeypatch):
@pytest.fixture
def steam(tmp_path, monkeypatch):
def steam(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[SteamLibrary, Path]:
"""A Steam tree with KCD2 downloaded but its manifest not yet finished."""
root = tmp_path / "Steam"
library = SteamLibrary(root)
@ -189,7 +158,7 @@ def steam(tmp_path, monkeypatch):
monkeypatch.setattr(appinfo, "get_build_id", lambda appid: "23914554")
# No DLC unless a test says so: the real lookup is a steamcmd call.
monkeypatch.setattr(appinfo, "get_dlc_plans", lambda appid, language: {})
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [])
monkeypatch.setattr(recognize, "steam_client_pids", list)
monkeypatch.setattr("builtins.input", lambda *a: "y")
# Prompting is tty-gated now, and pytest's stdin is not a tty.
monkeypatch.setattr(cli, "_is_interactive", lambda args: True)
@ -197,18 +166,20 @@ def steam(tmp_path, monkeypatch):
@pytest.fixture(autouse=True)
def _no_live_compat_side_effects(request, tmp_path, monkeypatch):
def _no_live_compat_side_effects(
request: pytest.FixtureRequest, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""No test may fetch the anti-cheat list or read the real app cache.
The modules that test those seams themselves (with their own fakes) are
exempt from the part they cover.
"""
from steam_installer import anticheat, appinfo_cache
def offline():
raise OSError("tests are offline")
def offline() -> Never:
msg = "tests are offline"
raise OSError(msg)
module = request.node.module.__name__
module = request.node.module.__name__.rsplit(".", 1)[-1]
if not request.node.name.startswith("test_anticheat_download"):
monkeypatch.setattr(anticheat, "_download", offline)
monkeypatch.setattr(anticheat, "CACHE", tmp_path / "no-anticheat-cache.json")
@ -218,10 +189,48 @@ def _no_live_compat_side_effects(request, tmp_path, monkeypatch):
)
# The live Steam client is never seen, stopped or started by a test: an
# unstubbed --apply run once shut down and relaunched kuhy's real Steam.
from steam_installer import steamctl
if module in ("test_steamctl", "test_recognize"):
return # these fake subprocess and /proc themselves
monkeypatch.setattr(recognize, "steam_client_pids", list)
monkeypatch.setattr(steamctl, "stop", lambda *a, **k: pytest.fail("real steam stop"))
monkeypatch.setattr(steamctl, "start", lambda *a, **k: pytest.fail("real steam start"))
monkeypatch.setattr(
steamctl, "stop", lambda *a, **k: pytest.fail("real steam stop")
)
monkeypatch.setattr(
steamctl, "start", lambda *a, **k: pytest.fail("real steam start")
)
@pytest.fixture(name="repo", scope="module")
def _repo(tmp_path_factory: pytest.TempPathFactory) -> Iterator[Repo]:
"""The repo fixture."""
built = build_repo(tmp_path_factory.mktemp("repo"))
yield built
stop_agent(built.gpg_home)
@pytest.fixture(name="mirror")
def _mirror(repo: Repo, tmp_path: Path) -> Path:
"""A private copy of the fake mirror a test may tamper with."""
root = tmp_path / "mirror"
shutil.copytree(repo.root, root)
return root
@pytest.fixture
def setup(
repo: Repo, mirror: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> Setup:
"""The setup fixture."""
tools = tmp_path / "compatibilitytools.d"
tools.mkdir()
lock = proton_pkg.Lock(
"proton-cachyos-native", (mirror / "cachyos.db").as_uri(), VERSION, repo.sha256
)
monkeypatch.setattr(proton_native, "in_use", lambda tool: [])
def run(**kw: object) -> str:
return proton_native.ensure(
tools, lock, key=repo.key, fingerprint=repo.fingerprint, **kw
)
return tools, lock, run

View File

@ -1,11 +1,14 @@
"""Tests for appinfo."""
from __future__ import annotations
import pytest
from _appinfo_blob import encode
from steam_installer import appinfo
def _app_block(monkeypatch, block):
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: block)
def _app_block(monkeypatch: pytest.MonkeyPatch, block: dict[str, object]) -> None:
monkeypatch.setattr(appinfo, "fetch_app_block", lambda appid: block)
# Every real depot that has content ships a manifest on the public branch.
@ -26,7 +29,10 @@ KCD2_DEPOTS = {
}
def test_get_depot_plan_selects_only_this_language_and_os(monkeypatch):
def test_get_depot_plan_selects_only_this_language_and_os(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get depot plan selects only this language and os."""
_app_block(monkeypatch, {"depots": KCD2_DEPOTS})
plan = appinfo.get_depot_plan(1771300, language="english", os_name="windows")
@ -36,34 +42,47 @@ def test_get_depot_plan_selects_only_this_language_and_os(monkeypatch):
assert plan.all_expected == {"1771302", "1771303", "1771306", "228989"}
def test_get_depot_plan_switches_language(monkeypatch):
def test_get_depot_plan_switches_language(monkeypatch: pytest.MonkeyPatch) -> None:
"""Get depot plan switches language."""
_app_block(monkeypatch, {"depots": KCD2_DEPOTS})
plan = appinfo.get_depot_plan(1771300, language="czech")
assert "1771305" in plan.required
assert "1771306" not in plan.required
def test_get_depot_plan_excludes_other_os(monkeypatch):
def test_get_depot_plan_excludes_other_os(monkeypatch: pytest.MonkeyPatch) -> None:
"""Get depot plan excludes other os."""
_app_block(
monkeypatch,
{"depots": {"10": {"config": {"oslist": "linux"}, **_PUB}, "11": {"config": {}, **_PUB}}},
{
"depots": {
"10": {"config": {"oslist": "linux"}, **_PUB},
"11": {"config": {}, **_PUB},
}
},
)
plan = appinfo.get_depot_plan(1771300, os_name="windows")
assert plan.required == {"11"}
def test_get_depot_plan_skips_optional_depots(monkeypatch):
def test_get_depot_plan_skips_optional_depots(monkeypatch: pytest.MonkeyPatch) -> None:
"""Get depot plan skips optional depots."""
_app_block(monkeypatch, {"depots": {"10": {"optional": "1", **_PUB}, "11": _PUB}})
assert appinfo.get_depot_plan(1771300).required == {"11"}
def test_get_depot_plan_rejects_malformed_depots_section(monkeypatch):
def test_get_depot_plan_rejects_malformed_depots_section(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get depot plan rejects malformed depots section."""
_app_block(monkeypatch, {"depots": "not-a-dict"})
with pytest.raises(appinfo.AppInfoError, match="depots"):
appinfo.get_depot_plan(1771300)
def test_get_depot_plan_with_no_depots_is_empty(monkeypatch):
def test_get_depot_plan_with_no_depots_is_empty(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""An app with no depot list yields nothing required -- callers must not
read that as "everything downloaded fine"."""
_app_block(monkeypatch, {})
@ -72,22 +91,32 @@ def test_get_depot_plan_with_no_depots_is_empty(monkeypatch):
assert plan.shared == set()
def test_get_build_id_reads_public_branch(monkeypatch):
def test_get_build_id_reads_public_branch(monkeypatch: pytest.MonkeyPatch) -> None:
"""Get build id reads public branch."""
_app_block(monkeypatch, {"depots": KCD2_DEPOTS})
assert appinfo.get_build_id(1771300) == "23914554"
def test_get_build_id_missing_branch_returns_none(monkeypatch):
def test_get_build_id_missing_branch_returns_none(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get build id missing branch returns none."""
_app_block(monkeypatch, {"depots": {"branches": {}}})
assert appinfo.get_build_id(1771300) is None
def test_get_build_id_without_depots_returns_none(monkeypatch):
def test_get_build_id_without_depots_returns_none(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get build id without depots returns none."""
_app_block(monkeypatch, {})
assert appinfo.get_build_id(1771300) is None
def test_get_metadata_reads_name_and_installdir(monkeypatch):
def test_get_metadata_reads_name_and_installdir(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get metadata reads name and installdir."""
_app_block(
monkeypatch,
{
@ -102,7 +131,10 @@ def test_get_metadata_reads_name_and_installdir(monkeypatch):
assert metadata.installdir == "KingdomComeDeliverance2"
def test_get_metadata_falls_back_to_config_installdir(monkeypatch):
def test_get_metadata_falls_back_to_config_installdir(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get metadata falls back to config installdir."""
_app_block(
monkeypatch,
{"common": {"name": "Game"}, "config": {"installdir": "GameDir"}},
@ -110,86 +142,33 @@ def test_get_metadata_falls_back_to_config_installdir(monkeypatch):
assert appinfo.get_metadata(1).installdir == "GameDir"
def test_get_metadata_requires_name(monkeypatch):
def test_get_metadata_requires_name(monkeypatch: pytest.MonkeyPatch) -> None:
"""Get metadata requires name."""
_app_block(monkeypatch, {"common": {"installdir": "GameDir"}})
with pytest.raises(appinfo.AppInfoError, match="no name field"):
appinfo.get_metadata(1)
def test_get_metadata_requires_installdir(monkeypatch):
def test_get_metadata_requires_installdir(monkeypatch: pytest.MonkeyPatch) -> None:
"""Get metadata requires installdir."""
_app_block(monkeypatch, {"common": {"name": "Game"}})
with pytest.raises(appinfo.AppInfoError, match="no installdir"):
appinfo.get_metadata(1)
def test_get_metadata_rejects_malformed_common_section(monkeypatch):
def test_get_metadata_rejects_malformed_common_section(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get metadata rejects malformed common section."""
_app_block(monkeypatch, {"common": "not-a-dict"})
with pytest.raises(appinfo.AppInfoError, match="no 'common' section"):
appinfo.get_metadata(1)
# The block steamcmd's app_info_print used to return for KCD2, now as Steam
# keeps it in its binary cache. The differential tests below pin that the
# cache gives the same answers the steamcmd text did.
KCD2 = {
"common": {"name": "Kingdom Come: Deliverance II", "installdir": "KingdomComeDeliverance2"},
"depots": {
"1771303": {"config": {"oslist": "windows"}},
"branches": {"public": {"buildid": 23914554}},
},
}
@pytest.fixture
def cache(tmp_path, monkeypatch):
"""Point appinfo at a fixture cache; returns a writer for its apps."""
path = tmp_path / "appinfo.vdf"
monkeypatch.setattr(appinfo.appinfo_cache, "default_path", lambda: path)
def write(apps):
path.write_bytes(encode(apps))
return write
def test_metadata_comes_from_steams_cache(cache):
cache({1771300: KCD2})
metadata = appinfo.get_metadata(1771300)
assert metadata.name == "Kingdom Come: Deliverance II"
assert metadata.installdir == "KingdomComeDeliverance2"
def test_get_build_id_through_the_real_parser(cache):
# Stored as an int32 in the binary cache; still a string like steamcmd's.
cache({1771300: KCD2})
assert appinfo.get_build_id(1771300) == "23914554"
def test_app_missing_from_the_cache_fails_with_the_fix(cache):
cache({1771300: KCD2})
with pytest.raises(appinfo.AppInfoError, match="start Steam once, let it sync, rerun"):
appinfo.get_metadata(999999999)
def test_unreadable_cache_fails_with_the_fix(tmp_path, monkeypatch):
monkeypatch.setattr(
appinfo.appinfo_cache, "default_path", lambda: tmp_path / "missing.vdf"
)
with pytest.raises(appinfo.AppInfoError, match="start Steam once"):
appinfo.get_metadata(1)
def test_dlc_missing_from_the_cache_counts_as_not_owned(cache):
"""Steam caches app info for what the account owns; on 2026-11-22 only 1828
of 7102 listed DLC ids were in the cache. Refusing on a missing one would
block installs that worked with steamcmd."""
game = {**KCD2, "extended": {"listofdlc": "500,600"}}
dlc = {"depots": {"501": {"manifests": {"public": {"gid": "9"}}}}}
cache({1771300: game, 500: dlc})
assert appinfo.get_dlc_plans(1771300) == {500: {"501": "9"}}
def test_get_build_id_handles_non_dict_sections(monkeypatch):
def test_get_build_id_handles_non_dict_sections(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get build id handles non dict sections."""
for block in (
{"depots": "scalar"},
{"depots": {"branches": "scalar"}},
@ -199,13 +178,22 @@ def test_get_build_id_handles_non_dict_sections(monkeypatch):
assert appinfo.get_build_id(1) is None
def test_get_build_id_returns_none_when_buildid_not_a_string(monkeypatch):
def test_get_build_id_returns_none_when_buildid_not_a_string(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get build id returns none when buildid not a string."""
_app_block(monkeypatch, {"depots": {"branches": {"public": {"buildid": {}}}}})
assert appinfo.get_build_id(1) is None
def test_a_shared_install_without_an_owner_is_still_shared(monkeypatch):
_app_block(monkeypatch, {"depots": {"5": {"sharedinstall": "1"}, "6": {"depotfromapp": "228980"}}})
def test_a_shared_install_without_an_owner_is_still_shared(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A shared install without an owner is still shared."""
_app_block(
monkeypatch,
{"depots": {"5": {"sharedinstall": "1"}, "6": {"depotfromapp": "228980"}}},
)
plan = appinfo.get_depot_plan(1)
assert plan.shared == {"5", "6"}
assert plan.shared_owner == {"6": "228980"}

View File

@ -1,42 +1,71 @@
"""Steam's local binary app-info cache, read without steamcmd."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
import pytest
from _appinfo_blob import encode
from steam_installer import appinfo_cache
from steam_installer.appinfo_cache import _parse
from steam_installer.tests._appinfo_blob import encode
if TYPE_CHECKING:
from pathlib import Path
def _write(tmp_path, apps):
def _write(tmp_path: Path, apps: dict[int, Any]) -> Path:
path = tmp_path / "appinfo.vdf"
path.write_bytes(encode(apps))
return path
def test_reads_every_app_and_unwraps_appinfo(tmp_path):
def test_reads_every_app_and_unwraps_appinfo(tmp_path: Path) -> None:
"""Reads every app and unwraps appinfo."""
path = _write(tmp_path, {10: {"common": {"name": "A"}}, 20: {"common": {}}})
apps = appinfo_cache.load(path)
assert set(apps) == {10, 20}
assert apps[10] == {"common": {"name": "A"}}
def test_scalars_come_back_as_strings_like_app_info_print(tmp_path):
def test_scalars_come_back_as_strings_like_app_info_print(tmp_path: Path) -> None:
"""The rest of the tool compares `== "1"` and `!= str(appid)`; an int here
would silently drop a DLC app's own depots."""
data = {"depots": {"5": {"dlcappid": 7, "optional": 1, "f": 1.5,
"gid": (7, 2**63 + 5), "neg": (10, -3)}}}
data = {
"depots": {
"5": {
"dlcappid": 7,
"optional": 1,
"f": 1.5,
"gid": (7, 2**63 + 5),
"neg": (10, -3),
}
}
}
apps = appinfo_cache.load(_write(tmp_path, {7: data}))
assert apps[7]["depots"]["5"] == {
"dlcappid": "7", "optional": "1", "f": "1.5",
"gid": str(2**63 + 5), "neg": "-3",
"dlcappid": "7",
"optional": "1",
"f": "1.5",
"gid": str(2**63 + 5),
"neg": "-3",
}
def test_parse_is_cached_until_the_file_changes(tmp_path, monkeypatch):
def test_parse_is_cached_until_the_file_changes(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Parse is cached until the file changes."""
path = _write(tmp_path, {1: {"common": {"name": "old"}}})
assert appinfo_cache.load(path)[1]["common"]["name"] == "old"
calls = []
real = appinfo_cache._parse
monkeypatch.setattr(appinfo_cache, "_parse", lambda b: calls.append(1) or real(b))
real = _parse
def counting(buf: bytes) -> dict[int, dict[str, Any]]:
calls.append(1)
return real(buf)
monkeypatch.setattr(appinfo_cache, "_parse", counting)
appinfo_cache.load(path)
assert calls == []
path.write_bytes(encode({1: {"common": {"name": "newer"}}}))
@ -44,14 +73,16 @@ def test_parse_is_cached_until_the_file_changes(tmp_path, monkeypatch):
assert calls == [1]
def test_wrong_magic_is_refused(tmp_path):
def test_wrong_magic_is_refused(tmp_path: Path) -> None:
"""Wrong magic is refused."""
path = tmp_path / "appinfo.vdf"
path.write_bytes(b"\x28\x44\x56\x07" + b"\0" * 20)
with pytest.raises(appinfo_cache.CacheError, match="unsupported"):
appinfo_cache.load(path)
def test_unknown_value_type_is_refused(tmp_path):
def test_unknown_value_type_is_refused(tmp_path: Path) -> None:
"""Unknown value type is refused."""
blob = bytearray(encode({1: {"common": {"name": "A"}}}))
blob[16 + 8 + 60] = 9 # first KV type byte
path = tmp_path / "appinfo.vdf"
@ -60,15 +91,15 @@ def test_unknown_value_type_is_refused(tmp_path):
appinfo_cache.load(path)
def test_missing_file_names_the_fix(tmp_path):
def test_missing_file_names_the_fix(tmp_path: Path) -> None:
"""Missing file names the fix."""
with pytest.raises(appinfo_cache.CacheError, match="start Steam once"):
appinfo_cache.load(tmp_path / "nope.vdf")
def test_entry_without_appinfo_wrapper_is_taken_as_is(tmp_path):
import _appinfo_blob
blob = _appinfo_blob.encode({3: {"x": "y"}})
def test_entry_without_appinfo_wrapper_is_taken_as_is(tmp_path: Path) -> None:
"""Entry without appinfo wrapper is taken as is."""
blob = encode({3: {"x": "y"}})
# Rename the "appinfo" wrapper key in the string table.
blob = blob.replace(b"appinfo\x00", b"appinfX\x00")
path = tmp_path / "appinfo.vdf"
@ -76,6 +107,9 @@ def test_entry_without_appinfo_wrapper_is_taken_as_is(tmp_path):
assert appinfo_cache.load(path)[3] == {"appinfX": {"x": "y"}}
def test_default_path_is_under_the_steam_root(monkeypatch, tmp_path):
def test_default_path_is_under_the_steam_root(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""Default path is under the steam root."""
monkeypatch.setattr(appinfo_cache, "find_steam_root", lambda: tmp_path)
assert appinfo_cache.default_path() == tmp_path / "appcache" / "appinfo.vdf"

View File

@ -0,0 +1,96 @@
"""get_metadata/get_build_id/get_dlc_plans answered from Steam's binary cache."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
import pytest
from steam_installer import appinfo
from steam_installer.tests._appinfo_blob import encode
if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path
# The block steamcmd's app_info_print used to return for KCD2, now as Steam
# keeps it in its binary cache. The differential tests below pin that the
# cache gives the same answers the steamcmd text did.
KCD2: dict[str, Any] = {
"common": {
"name": "Kingdom Come: Deliverance II",
"installdir": "KingdomComeDeliverance2",
},
"depots": {
"1771303": {"config": {"oslist": "windows"}},
"branches": {"public": {"buildid": 23914554}},
},
}
@pytest.fixture(name="cache")
def _cache(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> Callable[[dict[int, Any]], None]:
"""Point appinfo at a fixture cache; returns a writer for its apps."""
path = tmp_path / "appinfo.vdf"
monkeypatch.setattr(appinfo.appinfo_cache, "default_path", lambda: path)
def write(apps: dict[int, Any]) -> None:
path.write_bytes(encode(apps))
return write
def test_metadata_comes_from_steams_cache(
cache: Callable[[dict[int, Any]], None],
) -> None:
"""Metadata comes from Steam's cache."""
cache({1771300: KCD2})
metadata = appinfo.get_metadata(1771300)
assert metadata.name == "Kingdom Come: Deliverance II"
assert metadata.installdir == "KingdomComeDeliverance2"
def test_get_build_id_through_the_real_parser(
cache: Callable[[dict[int, Any]], None],
) -> None:
# Stored as an int32 in the binary cache; still a string like steamcmd's.
"""Get build id through the real parser."""
cache({1771300: KCD2})
assert appinfo.get_build_id(1771300) == "23914554"
def test_app_missing_from_the_cache_fails_with_the_fix(
cache: Callable[[dict[int, Any]], None],
) -> None:
"""App missing from the cache fails with the fix."""
cache({1771300: KCD2})
with pytest.raises(
appinfo.AppInfoError, match="start Steam once, let it sync, rerun"
):
appinfo.get_metadata(999999999)
def test_unreadable_cache_fails_with_the_fix(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Unreadable cache fails with the fix."""
monkeypatch.setattr(
appinfo.appinfo_cache, "default_path", lambda: tmp_path / "missing.vdf"
)
with pytest.raises(appinfo.AppInfoError, match="start Steam once"):
appinfo.get_metadata(1)
def test_dlc_missing_from_the_cache_counts_as_not_owned(
cache: Callable[[dict[int, Any]], None],
) -> None:
"""Steam caches app info for what the account owns; on 2026-11-22 only 1828
of 7102 listed DLC ids were in the cache. Refusing on a missing one would
block installs that worked with steamcmd."""
game = {**KCD2, "extended": {"listofdlc": "500,600"}}
dlc = {"depots": {"501": {"manifests": {"public": {"gid": "9"}}}}}
cache({1771300: game, 500: dlc})
assert appinfo.get_dlc_plans(1771300) == {500: {"501": "9"}}

View File

@ -4,11 +4,18 @@ Split from test_appinfo.py (250-line cap): these are the depots-without-a-
manifest cases that made a finished BG3 download read as incomplete.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from steam_installer import appinfo
if TYPE_CHECKING:
import pytest
def _app_block(monkeypatch, block):
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: block)
def _app_block(monkeypatch: pytest.MonkeyPatch, block: dict[str, object]) -> None:
monkeypatch.setattr(appinfo, "fetch_app_block", lambda appid: block)
# Shape taken from a real `app_info_print 1086940` (Baldur's Gate 3, build
@ -30,12 +37,17 @@ BG3_DEPOTS = {
}
def test_get_depot_plan_skips_depots_without_a_public_manifest(monkeypatch):
def test_get_depot_plan_skips_depots_without_a_public_manifest(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Get depot plan skips depots without a public manifest."""
_app_block(monkeypatch, {"depots": BG3_DEPOTS})
assert appinfo.get_depot_plan(1086940).required == {"1086941"}
def test_get_depot_plan_records_every_public_manifest(monkeypatch):
def test_get_depot_plan_records_every_public_manifest(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""DLC depots too: the finish check proves the build from these gids."""
_app_block(monkeypatch, {"depots": BG3_DEPOTS})
assert appinfo.get_depot_plan(1086940).manifests == {

View File

@ -0,0 +1,168 @@
"""End-to-end-ish tests for the CLI's decision logic.
The bug that motivated all of this lived here, not in the manifest writer:
a step died instantly having done nothing, and main() still wrote
StateFlags=4, printed "launch Steam and confirm Play works" and returned 0.
These tests pin the fail-closed behaviour.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Never
import pytest
from steam_installer import (
appinfo,
cli,
depotdl,
recognize,
)
from steam_installer.tests._cli_helpers import ARGV, _stub_download
if TYPE_CHECKING:
from pathlib import Path
from steam_installer.library import SteamLibrary
pytestmark = pytest.mark.usefixtures("cli_logs")
def test_downloaded_content_finishes_the_manifest(
steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch
) -> None:
"""The content is what matters, and the manifest must come out consistent,
not merely flagged installed."""
library, _install_dir = steam
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf")
assert app_state["StateFlags"] == "4"
for key in ("BytesToDownload", "BytesDownloaded", "BytesToStage", "BytesStaged"):
assert app_state[key] == "0", key
assert recognize.download_counters_settled(app_state)
def test_refuses_when_a_required_depot_never_downloaded(
steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch
) -> None:
"""The exact fail-open case: nothing was really downloaded, so declaring the
app installed would be a fabricated success."""
library, _install_dir = steam
_stub_download(monkeypatch)
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: appinfo.DepotPlan({"1771303", "9999999"}, set()),
)
assert cli.main(ARGV) == 1
app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf")
assert app_state["StateFlags"] == "1026"
def test_refuses_when_depotdownloader_fails(
steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch
) -> None:
"""Refuses when depotdownloader fails."""
library, _ = steam
_stub_download(monkeypatch, returncode=1)
assert cli.main(ARGV) == 1
app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf")
assert app_state["StateFlags"] == "1026"
@pytest.mark.usefixtures("steam")
def test_declining_to_close_steam_aborts_before_downloading(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Steam owns the manifest while it runs. The tool offers to close it, but a
"no" must stop the run rather than download for an hour and lose the write."""
monkeypatch.setattr(recognize, "steam_client_pids", lambda: [4242])
monkeypatch.setattr(
"builtins.input", lambda *a: "n" if "Steam is running" in a[0] else "y"
)
called = []
def download(*_a: object, **_kw: object) -> int:
called.append(1)
return 0
monkeypatch.setattr(depotdl, "download", download)
assert cli.main(ARGV) == 1
assert called == []
@pytest.mark.usefixtures("steam")
def test_user_declining_confirmation_aborts(monkeypatch: pytest.MonkeyPatch) -> None:
"""User declining confirmation aborts."""
monkeypatch.setattr("builtins.input", lambda *a: "n")
called = []
def download(*_a: object, **_kw: object) -> int:
called.append(1)
return 0
monkeypatch.setattr(depotdl, "download", download)
assert cli.main(ARGV) == 1
assert called == []
@pytest.mark.usefixtures("steam")
def test_unhandled_error_returns_nonzero_and_names_the_log(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Unhandled error returns nonzero and names the log."""
def boom(*_args: object, **_kwargs: object) -> Never:
msg = "depotdownloader exploded"
raise RuntimeError(msg)
monkeypatch.setattr(depotdl, "download", boom)
assert cli.main(ARGV) == 1
assert "Run log" in capsys.readouterr().out
@pytest.mark.usefixtures("steam")
@pytest.mark.parametrize("error", [AttributeError, KeyError, ZeroDivisionError])
def test_any_crash_is_logged_with_the_log_path(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
error: type[Exception],
) -> None:
"""A bug outside the expected errors is still logged, not left to a traceback."""
def boom(*_args: object, **_kwargs: object) -> Never:
msg = "bug"
raise error(msg)
monkeypatch.setattr(depotdl, "download", boom)
assert cli.main(ARGV) == 1
assert "Run log (send this)" in capsys.readouterr().out
@pytest.mark.usefixtures("steam")
def test_ctrl_c_still_interrupts(monkeypatch: pytest.MonkeyPatch) -> None:
"""Ctrl c still interrupts."""
def boom(*_args: object, **_kwargs: object) -> Never:
raise KeyboardInterrupt
monkeypatch.setattr(depotdl, "download", boom)
with pytest.raises(KeyboardInterrupt):
cli.main(ARGV)
def test_requires_a_name_or_appid() -> None:
"""Requires a name or appid."""
with pytest.raises(SystemExit):
cli.main(["--username", "u"])

View File

@ -1,11 +1,15 @@
"""``--help`` ends on kuhy's shared credit line (kuhy_credit, kuhyx/utils)."""
import pytest
from __future__ import annotations
from kuhy_credit import URL, credit_line
import pytest
from steam_installer import cli
def test_help_ends_with_the_kuhy_credit(capsys):
def test_help_ends_with_the_kuhy_credit(capsys: pytest.CaptureFixture[str]) -> None:
"""Help ends with the kuhy credit."""
with pytest.raises(SystemExit) as exc:
cli.main(["--help"])
assert exc.value.code == 0

View File

@ -0,0 +1,173 @@
"""CLI flow tests, part 2: the manifest-finishing end of a run."""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from steam_installer import (
appinfo,
cli,
recognize,
vdf,
)
from steam_installer.tests._cli_helpers import ARGV, _stub_download, proven_plan
if TYPE_CHECKING:
from pathlib import Path
from steam_installer.library import SteamLibrary
pytestmark = pytest.mark.usefixtures("cli_logs")
# The 15 keys Steam accepted on 2026-11-22 (A Space for the Unbound): it
# logged "Fully Installed,App Running", queued no download, and added its own
# LastOwner, UserConfig, MountedConfig, ... on exit.
FROM_SCRATCH_KEYS = [
"appid",
"universe",
"name",
"StateFlags",
"installdir",
"LastUpdated",
"SizeOnDisk",
"StagingSize",
"buildid",
"UpdateResult",
"BytesToDownload",
"BytesDownloaded",
"BytesToStage",
"BytesStaged",
"InstalledDepots",
]
def test_missing_manifest_is_written_from_scratch(
steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch
) -> None:
"""No steamcmd any more, so nothing else creates the manifest."""
library, install_dir = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.unlink()
gid = proven_plan(monkeypatch, install_dir)
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = vdf.parse(manifest.read_text())["AppState"]
assert list(app_state) == FROM_SCRATCH_KEYS
assert app_state["appid"] == "1771300"
assert app_state["universe"] == "1"
assert app_state["name"] == "KCD2"
assert app_state["StateFlags"] == "4"
assert app_state["installdir"] == "KingdomComeDeliverance2"
assert app_state["SizeOnDisk"] == "1000"
assert app_state["buildid"] == "23914554"
for key in (
"StagingSize",
"UpdateResult",
"BytesToDownload",
"BytesDownloaded",
"BytesToStage",
"BytesStaged",
):
assert app_state[key] == "0", key
assert app_state["InstalledDepots"] == {"1771303": {"manifest": gid, "size": "0"}}
# Nothing to back up: there was no manifest before.
assert not manifest.with_suffix(".acf.bak").exists()
def test_a_shared_depot_is_recorded_under_its_owner_from_scratch(
steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch
) -> None:
"""Fixture-only: no shared-depot game was tested live. Steam records a
depot installed under another app as SharedDepots {depot: owner}."""
library, install_dir = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.unlink()
proven_plan(monkeypatch, install_dir, shared_owner={"228989": "228980"})
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = vdf.parse(manifest.read_text())["AppState"]
assert app_state["SharedDepots"] == {"228989": "228980"}
assert "228989" not in app_state["InstalledDepots"]
def test_unproven_content_writes_no_manifest_from_scratch(
steam: tuple[SteamLibrary, Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Fail closed: without a proven build there is no buildid to write, and a
half-true StateFlags=4 manifest is what made Steam redownload games."""
library, _ = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.unlink()
_stub_download(monkeypatch)
assert cli.main(ARGV) == 1
assert not manifest.exists()
assert "no usable buildid" in capsys.readouterr().out
@pytest.mark.usefixtures("steam")
def test_language_defaults_to_english_when_prompt_is_empty(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Language defaults to english when prompt is empty."""
seen = {}
monkeypatch.setattr("builtins.input", lambda *a: "y" if "Proceed" in a[0] else "")
monkeypatch.setattr(
appinfo,
"get_depot_plan",
lambda appid, language: (
(seen.setdefault("language", language) and None)
or appinfo.DepotPlan({"1771303"}, set())
),
)
_stub_download(monkeypatch)
cli.main(["--appid", "1771300", "--username", "u"])
assert seen["language"] == "english"
def test_adopts_staged_content_then_clears_it(
steam: tuple[SteamLibrary, Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Steam's staging tree is adopted into the install dir before downloading,
and only cleared once the install is finished -- never discarded up front."""
library, install_dir = steam
staged = library.steamapps / "downloading" / "1771300"
staged.mkdir(parents=True)
(staged / "big.pak").write_bytes(b"x" * 2_000_000)
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
out = capsys.readouterr().out
assert "Hardlinking it" in out
assert "now-redundant Steam staging data" in out
# Adopted, not copied: the file is one inode with two names.
assert (install_dir / "big.pak").stat().st_size == 2_000_000
@pytest.mark.usefixtures("steam")
def test_fails_when_written_manifest_does_not_read_back_as_installed(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Claiming success without re-reading the write is the same fail-open
mistake in a different place."""
_stub_download(monkeypatch)
monkeypatch.setattr(
recognize, "manifest_reflects_real_install", lambda *a, **kw: False
)
assert cli.main(ARGV) == 1
assert "does not read back as a real install" in capsys.readouterr().out

View File

@ -0,0 +1,106 @@
"""CLI flow tests, part 3: stub manifests and the script entry point."""
from __future__ import annotations
import subprocess
import sys
from typing import TYPE_CHECKING
import pytest
from steam_installer import (
cli,
recognize,
vdf,
)
from steam_installer.tests._cli_helpers import ARGV, _stub_download, proven_plan
if TYPE_CHECKING:
from pathlib import Path
from steam_installer.library import SteamLibrary
pytestmark = pytest.mark.usefixtures("cli_logs")
def test_module_is_runnable_as_a_script(
coverage_subprocess_env: dict[str, str],
) -> None:
"""`python -m steam_installer.cli` is a real entry point (the __main__
guard), so it must not be dead code."""
result = subprocess.run(
[sys.executable, "-m", "steam_installer.cli", "--help"],
capture_output=True,
text=True,
timeout=60,
env=coverage_subprocess_env,
check=False,
)
assert result.returncode == 0
assert "--skip-steamcmd" not in result.stdout
def test_a_stub_manifest_is_finished_with_the_proven_build(
steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch
) -> None:
"""The enforcer's stub says build 0 and Steam never ran to fill it in. The
cached depot manifest matching the public branch proves the current build,
which is what the finished manifest must record (BG3, 2026-10-23)."""
library, install_dir = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
manifest.write_text(
vdf.dumps(
"AppState", {"appid": "1771300", "StateFlags": "1026", "buildid": "0"}
)
)
proven_plan(monkeypatch, install_dir, shared={"228989"})
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = vdf.parse(manifest.read_text())["AppState"]
assert (app_state["StateFlags"], app_state["buildid"]) == ("4", "23914554")
def test_the_install_folder_is_corrected_even_when_the_download_fails(
steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch
) -> None:
"""The enforcer's stub names the folder after the display name ("Baldur's
Gate 3", not "Baldurs Gate 3"). Steam resumes onto whatever the manifest
says, so it is fixed as soon as Steam is closed, not only on success."""
library, _ = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
state = recognize.read_app_state(manifest)
state["installdir"] = "Kingdom Come: Deliverance II"
manifest.write_text(vdf.dumps("AppState", state))
_stub_download(monkeypatch, returncode=2)
assert cli.main(ARGV) == 1
assert recognize.read_app_state(manifest)["installdir"] == "KingdomComeDeliverance2"
def test_wrong_installdir_and_mismatched_counters_end_correct_and_zeroed(
steam: tuple[SteamLibrary, Path], monkeypatch: pytest.MonkeyPatch
) -> None:
"""C18: the live repro (installdir "... WRONG", BytesToDownload 1000,
BytesDownloaded 5) once came out as 1000/1000. The written manifest must
name the real folder, keep its files, and carry every counter at 0."""
library, install_dir = steam
manifest = library.steamapps / "appmanifest_1771300.acf"
state = recognize.read_app_state(manifest)
state.update(
installdir="KingdomComeDeliverance2 WRONG",
BytesToDownload="1000",
BytesDownloaded="5",
BytesToStage="1000",
BytesStaged="5",
)
manifest.write_text(vdf.dumps("AppState", state))
_stub_download(monkeypatch)
assert cli.main(ARGV) == 0
app_state = recognize.read_app_state(manifest)
assert app_state["installdir"] == "KingdomComeDeliverance2"
assert (install_dir / "game.pak").stat().st_size == 1000
for key in ("BytesToDownload", "BytesDownloaded", "BytesToStage", "BytesStaged"):
assert app_state[key] == "0", key

Some files were not shown because too many files have changed in this diff Show More