50 lines
2.7 KiB
Python
50 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Import and publish the supplied, hash-verified development sample files locally."""
|
|
from datetime import datetime, timezone
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
from local_api import Api
|
|
import project
|
|
|
|
ROOT = project.ROOT
|
|
api = Api().login()
|
|
sources = api.call('/api/sources/scan', {})['items']
|
|
# Restrict this reproducible bootstrap to the delivered evidence set.
|
|
evidence = json.loads((ROOT/'verification/source_hashes.json').read_text())
|
|
allowed = set()
|
|
if isinstance(evidence, list):
|
|
allowed.update(x['sha256'] for x in evidence)
|
|
if isinstance(evidence, dict):
|
|
# Evidence formats carry paths as keys or as a files collection.
|
|
candidates = evidence.get('files', evidence)
|
|
if isinstance(candidates, dict):
|
|
for key, value in candidates.items():
|
|
allowed.add(value.get('sha256') if isinstance(value, dict) else value)
|
|
elif isinstance(candidates, list):
|
|
allowed.update(x.get('sha256') for x in candidates)
|
|
selected = [s for s in sources if s['sha256'] in allowed]
|
|
if len(selected) != 15:
|
|
raise RuntimeError('Expected exactly the 15 supplied source-period files; no data was imported')
|
|
ids = sorted(s['id'] for s in selected)
|
|
mapping = [x['id'] for x in api.call('/api/mappings')['items'] if x['status'] == 'ACTIVE']
|
|
rules = [x['id'] for x in api.call('/api/rules')['items'] if x['status'] == 'ACTIVE']
|
|
key = 'provided-samples-' + hashlib.sha256(json.dumps([ids, mapping, rules]).encode()).hexdigest()
|
|
releases = api.call('/api/releases')
|
|
run = api.call('/api/import-runs', {'sourceIds': ids, 'idempotencyKey': key,
|
|
'expectedCurrentReleaseId': releases.get('currentReleaseId')})['run']
|
|
if run['status'] != 'COMPLETED' or not run.get('resultReleaseId'):
|
|
raise RuntimeError('Sample import did not produce a completed candidate; inspect the import page')
|
|
release = run['resultReleaseId']
|
|
current = api.call('/api/releases').get('currentReleaseId')
|
|
if current != release:
|
|
api.call('/api/releases/'+release+'/publish', {'expectedCurrentReleaseId':current,
|
|
'idempotencyKey':key+'-publish', 'reason':'本机开发:已提供正常样本的独立基线验证与页面联调'})
|
|
summary = {'verifiedAt': datetime.now(timezone.utc).isoformat(), 'environment':'LOCAL_DEVELOPMENT_ONLY',
|
|
'sourcePeriods':len(selected), 'processedRows':run['processed'], 'releaseId':release,
|
|
'validation':run.get('validation'), 'status':'IMPORTED_AND_PUBLISHED'}
|
|
output = ROOT/'verification/local-import.json'
|
|
output.write_text(json.dumps(summary, ensure_ascii=False, indent=2)+'\n')
|
|
print('Imported and published locally:',len(selected),'source periods,',run['processed'],'rows')
|
|
print('Evidence:',output)
|