Files
rust-discovery/deploy/linux/apt-diagnostic.py
T
2026-11-13 13:49:11 +01:00

68 lines
3.5 KiB
Python

"""Exact dev/test RMM diagnostic payload: simulation only, no host APT writes."""
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
def digest(data):
return hashlib.sha256(data).hexdigest()
def preserved():
base = Path('/var/lib/rmm-apt-transaction')
files = [Path('/var/lib/dpkg/status'), base / 'bootstrap-attempt-rmm-apt-fixture-install-1.0.json']
files.extend(p for p in (base / 'active').rglob('*') if p.is_file())
return {str(p): digest(p.read_bytes()) for p in files}
def main():
os.umask(0o077)
base = Path('/var/lib/rmm-apt-transaction')
archive = base / 'archives/74036ae81223deab3aae994fd2a2a695af408741777e1ce17dcb4e32f1235867.deb'
assert archive.stat().st_size == 20672 and digest(archive.read_bytes()) == archive.stem
assert not Path('/usr/share/rmm-mvp-test/version').exists()
before = preserved()
config = (base / 'active/apt.conf').read_bytes()
results = []
with tempfile.TemporaryDirectory(prefix='rmm-apt-diagnostic-') as temp:
workspace = Path(temp)
for name in ('empty', 'lists', 'cache', 'log', 'state'):
(workspace / name).mkdir(mode=0o700)
# Preserve source/hook/solver configuration. Only diagnostic scratch
# directories move into PrivateTmp; host dpkg status is read-only.
copied = config.replace(str(base / 'active').encode(), str(workspace).encode())
copied = copied.replace(str(base / 'log').encode(), str(workspace / 'log').encode())
copied = copied.replace(str(base / 'state').encode(), str(workspace / 'state').encode())
path = workspace / 'apt.conf'; path.write_bytes(copied)
env = dict(PATH='/usr/sbin:/usr/bin:/sbin:/bin', LC_ALL='C', HOME='/root',
DEBIAN_FRONTEND='noninteractive', APT_CONFIG=str(path))
for name, delimiter in (('original_argv_simulated', ['--']), ('without_separator_simulated', [])):
command = ['/usr/bin/apt-get', '--simulate', '--yes', '--no-remove', '--no-install-recommends',
'--no-download', 'install'] + delimiter + [str(archive)]
with tempfile.TemporaryFile() as out, tempfile.TemporaryFile() as err:
process = subprocess.run(command, env=env, stdin=subprocess.DEVNULL,
stdout=out, stderr=err, timeout=20)
lengths = (out.tell(), err.tell()); out.seek(0); err.seek(0)
stdout = out.read(16384); stderr = err.read(16384)
messages = []
for line in stderr.decode('utf-8', errors='replace').splitlines():
if line.startswith(('E:', 'W:')):
line = re.sub(r'[A-Za-z][A-Za-z0-9+.-]*://\S+', '<uri>', line)
line = re.sub(r'/[^\s\x27\x22,;()]+', '<path>', line)
line = re.sub(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', '<address>', line)
messages.append(line[:512])
results.append(dict(case=name,exit_code=process.returncode,messages=messages[:12],
stdout_sha256=digest(stdout),stderr_sha256=digest(stderr),truncated=any(n>16384 for n in lengths),
solver_selected_fixture=b'rmm-mvp-test' in stdout))
assert preserved() == before and not Path('/usr/share/rmm-mvp-test/version').exists()
print(json.dumps(dict(schema_version=1,diagnostic_only=True,transaction_executed=False,
preserved_state=True,original_config_sha256=digest(config),results=results)))
if __name__ == '__main__':
main()