prepare-library-cleanup.py 1.9 KB

123456789101112131415161718192021222324252627282930313233
  1. """Seal the reviewed removals, source bindings and protected records."""
  2. import hashlib
  3. import json
  4. from pathlib import Path
  5. import sys
  6. import os
  7. ROOT=Path(__file__).resolve().parents[1]
  8. sys.path.insert(0,str(ROOT/'server/voc-library'))
  9. import research_cleanup as cleanup
  10. WORK=ROOT/os.environ.get('KSF_CLEANUP_WORK','work/library-cleanup-20261111')
  11. inventory=json.loads((WORK/'inventory-result.json').read_bytes())['body']['result']
  12. audit=json.loads((WORK/'audit-proposals.json').read_bytes())
  13. protected=set()
  14. for b in ['five-topics-public-01','five-topics-public-02','five-topics-public-03']:
  15. protected.update(r['id'] for r in json.loads((ROOT/'work/database-enrichment-20261110/batches'/b/'expected-records.json').read_bytes()))
  16. social={r['id']:r for r in inventory['social']}
  17. def original(row):
  18. result={k:v for k,v in row.items() if k not in ('payload_sha256','source_payload','_ksf_source_rowid')}
  19. fields=next(t['columns'] for t in inventory['manifest']['tables'] if t['name']=='social_posts')
  20. for f in fields:
  21. if result.get(f['name']) is not None and 'INT' in f['type'].upper():result[f['name']]=int(result[f['name']])
  22. return result
  23. plan={'schema':cleanup.SCHEMA,'baseGeneration':inventory['generation'],
  24. 'removals':audit['proposedRemovals'],'repairs':audit['proposedRepairs'],
  25. 'protectedSocial':[{'id':id,'recordSha256':cleanup.digest(original(social[id]))} for id in sorted(protected)],
  26. 'reviewCriteria':audit['limits'],'scope':audit['scope']}
  27. plan_sha=cleanup.digest(plan)
  28. cleanup.validate(plan,plan_sha)
  29. out={'plan':plan,'planSha256':plan_sha}
  30. (WORK/'sealed-plan.json').write_text(json.dumps(out,ensure_ascii=False,indent=2),encoding='utf8')
  31. print(json.dumps({'planSha256':plan_sha,'removals':len(plan['removals']),'repairs':len(plan['repairs']),
  32. 'protected':len(protected),'implementationSha256':hashlib.sha256((ROOT/'server/voc-library/research_cleanup.py').read_bytes()).hexdigest()},ensure_ascii=False))