#!/usr/bin/env python3 """Record reproducibility metadata, with an allowlist excluding all secrets.""" import hashlib,json,platform,subprocess,time from pathlib import Path root=Path(__file__).resolve().parents[1] def cmd(*args):return subprocess.check_output(args,cwd=root,text=True).strip() paths=[p for d in ['service','infra','scripts','tests','contracts','student-tests'] for p in (root/d).rglob('*') if p.is_file() and '__pycache__' not in p.parts] paths += [root/x for x in ['Dockerfile','compose.yaml','compose.faults.yaml','Makefile']] h=hashlib.sha256() for p in sorted(paths):h.update(str(p.relative_to(root)).encode());h.update(p.read_bytes()) containers=[json.loads(x) for x in cmd('docker','compose','--env-file','.env.lab6','ps','--format','json').splitlines()] ids=[c['ID'] for c in containers] inspect=json.loads(cmd('docker','inspect',*ids)) rows=[{'service':c['Config']['Labels']['com.docker.compose.service'],'image':c['Config']['Image'],'image_id':c['Image'],'started_at':c['State']['StartedAt'],'memory_limit':c['HostConfig']['Memory'],'nano_cpus':c['HostConfig']['NanoCpus'],'mounts':[{'type':m['Type'],'name':m.get('Name'),'destination':m['Destination']} for m in c['Mounts'] if m['Type']=='volume']} for c in inspect] info=json.loads(cmd('docker','info','--format','{{json .}}')) out={'timestamp':time.time(),'revision':cmd('git','rev-parse','HEAD'),'working_tree_dirty':bool(cmd('git','status','--porcelain')),'source_sha256':h.hexdigest(),'platform':platform.platform(),'go':cmd('go','version'),'docker':{'server_version':info['ServerVersion'],'cpus':info['NCPU'],'memory':info['MemTotal'],'architecture':info['Architecture']},'containers':rows} if platform.system()=='Darwin':out['host']={'cpu':cmd('sysctl','-n','machdep.cpu.brand_string'),'memory':cmd('sysctl','-n','hw.memsize')} (root/'evidence/environment.json').write_text(json.dumps(out,indent=2)+'\n') print('Saved sanitized environment and source fingerprint')