Files
T

29 lines
1.8 KiB
Python

from pathlib import Path
import datetime, hashlib, json, os, subprocess, sys
root=Path(__file__).resolve().parent
repo=root.parent/'traceability-view-20260923'
out=root/sys.argv[1]
out.mkdir(exist_ok=False)
def record(path):
data=path.read_bytes()
return {'path':str(path),'bytes':len(data),'sha256':hashlib.sha256(data).hexdigest()}
def save(name,value):
with (out/name).open('x',encoding='utf-8') as f: json.dump(value,f,indent=2);f.write('\n')
def git(*args): return subprocess.check_output(['git','-c','core.longpaths=true',*args],cwd=repo)
def inputs():
paths=git('ls-files','--cached','--others','--exclude-standard','-z').decode().split('\0')
return [record(repo/p) for p in paths if p and (repo/p).is_file()]
before=inputs();save('inputs-before.json',before)
context={'source_commit':git('rev-parse','HEAD').decode().strip(),'working_changes':git('status','--short').decode(),'cwd':str(repo),'argv':sys.argv[2:],'started_utc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'environment':{k:os.environ.get(k) for k in ['CARGO_HOME','RUSTUP_HOME','RUSTUP_TOOLCHAIN','CARGO_TARGET_DIR','CARGO_BUILD_JOBS','CARGO_INCREMENTAL','CARGO_TARGET_X86_64_PC_WINDOWS_GNULLVM_LINKER','RC']},'runner':record(Path(__file__))}
save('context.json',context)
(out/'changes.patch').write_bytes(git('diff','--binary'))
with (out/'output.log').open('xb') as log:
result=subprocess.run(sys.argv[2:],cwd=repo,stdout=log,stderr=subprocess.STDOUT)
after=inputs();save('inputs-after.json',after)
context.update(exit_code=result.returncode,inputs_unchanged=before==after,finished_utc=datetime.datetime.now(datetime.timezone.utc).isoformat(),log=record(out/'output.log'))
save('result.json',context)
print(json.dumps(context,indent=2))
print((out/'output.log').read_text(encoding='utf-8',errors='replace')[-16000:])
sys.exit(result.returncode or int(before!=after))