import base64
import hashlib
import json
from pathlib import Path
from remote import ROOT, run, python

remote='/storage/contextra/maintenance/fight-r5-errors-20261101-r01'
shared=ROOT/'error-evidence/shared'
raw=run(['cat',remote+'/shared/trace_windows.json'],sudo=True)
expected=json.loads((ROOT/'error-evidence/trace_manifest.json').read_text())['manifest']['shared/trace_windows.json']['sha256']
assert hashlib.sha256(raw).hexdigest()==expected
(shared/'trace_windows.json').write_bytes(raw)
source=Path('/workspace/contextra-analytics-runtime-lifecycle-refactor/docs/profiles/ltb1021/topics/runtime-lifecycle/analysis/20261101T101310+0900-analysis-icerti-six-channel-errors.md')
report=source.read_text().replace(str(shared)+'/case_summary.json','case_summary.json').replace(str(shared),'.')
(shared/'00_README_ko.md').write_text(report)
files={p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(shared.iterdir()) if p.is_file()}
(shared/'SHA256SUMS').write_text(''.join(f'{h}  {name}\n' for name,h in files.items()))
payload={name:base64.b64encode((shared/name).read_bytes()).decode() for name in ('case_summary.json','00_README_ko.md','SHA256SUMS')}
code="import base64,json,os\nfrom pathlib import Path\nroot=Path("+repr(remote+'/shared')+")\nfiles="+repr(payload)+"\nfor name,value in files.items():\n p=root/name\n assert not p.exists()\n p.write_bytes(base64.b64decode(value));p.chmod(0o600)\nprint('Shared case report and checksums preserved.')"
print(python(code,sudo=True).decode().strip())
