"""Fixed dev/test diagnostic: APT download-only, copied DB, private scratch only.""" 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') paths=[Path('/var/lib/dpkg/status'),base/'bootstrap-attempt-rmm-apt-fixture-install-1.0.json'] paths.extend(p for p in (base/'active').rglob('*') if p.is_file()) return {str(p):digest(p.read_bytes()) for p in paths} def main(): os.umask(0o077) base=Path('/var/lib/rmm-apt-transaction') archive=base/'archives/74036ae81223deab3aae994fd2a2a695af408741777e1ce17dcb4e32f1235867.deb' raw=archive.read_bytes();assert len(raw)==20672 and digest(raw)==archive.stem assert not Path('/usr/share/rmm-mvp-test/version').exists() before=preserved();original=(base/'active/apt.conf').read_bytes();results=[] for name,seeded in (('empty_cache',False),('seeded_cache',True)): with tempfile.TemporaryDirectory(prefix='rmm-apt-acquisition-') as temp: work=Path(temp) for directory in ('empty','lists','cache','log','state','dpkg'):(work/directory).mkdir(mode=0o700) (work/'dpkg/status').write_bytes(Path('/var/lib/dpkg/status').read_bytes()) config=original.replace(str(base/'active').encode(),str(work).encode()) for directory in ('log','state'): config=config.replace(str(base/directory).encode(),str(work/directory).encode()) config=config.replace(b'/var/lib/dpkg/status',str(work/'dpkg/status').encode()) (work/'apt.conf').write_bytes(config) if seeded:(work/'cache/rmm-mvp-test_1.0_all.deb').write_bytes(raw) env=dict(PATH='/usr/sbin:/usr/bin:/sbin:/bin',LC_ALL='C',HOME='/root',DEBIAN_FRONTEND='noninteractive',APT_CONFIG=str(work/'apt.conf')) # Mandatory --download-only prevents every dpkg/install/hook action. # --no-download is retained in both cases. Only scratch cache varies. command=['/usr/bin/apt-get','--download-only','--yes','--no-remove','--no-install-recommends', '--no-download','install','--',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) sizes=(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+','',line) line=re.sub(r'/[^\s\x27\x22,;()]+','',line) line=re.sub(r'\b(?:\d{1,3}\.){3}\d{1,3}\b','
',line) messages.append(line[:512]) results.append(dict(case=name,exit_code=process.returncode,messages=messages[:12],truncated=any(n>16384 for n in sizes), stdout_sha256=digest(stdout),stderr_sha256=digest(stderr),cached_files=sorted(p.name for p in (work/'cache').glob('*.deb')))) assert preserved()==before and not Path('/usr/share/rmm-mvp-test/version').exists() print(json.dumps(dict(schema_version=1,diagnostic_only=True,download_only=True,dpkg_status_copied=True, transaction_executed=False,preserved_state=True,original_config_sha256=digest(original),results=results))) if __name__=='__main__':main()