55 lines
2.6 KiB
Python
55 lines
2.6 KiB
Python
"""Initialize a fresh disposable comparison instance via the unchanged public API.
|
|
Never run on an existing Architecture A database/production deployment.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import pathlib
|
|
import ssl
|
|
import urllib.request
|
|
import urllib.error
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--url', required=True)
|
|
parser.add_argument('--ca', required=True)
|
|
parser.add_argument('--account-file', required=True, help='Owner-only JSON with accountId/password')
|
|
parser.add_argument('--dataset', default=str(pathlib.Path(__file__).with_name('dataset.json')))
|
|
parser.add_argument('--output', required=True, help='New manifest filename')
|
|
args = parser.parse_args()
|
|
output = pathlib.Path(args.output)
|
|
if output.exists():
|
|
raise SystemExit('Output manifest already exists; refusing overwrite.')
|
|
if not args.url.startswith('https://'):
|
|
raise SystemExit('Verified HTTPS is required.')
|
|
credentials_path = pathlib.Path(args.account_file)
|
|
if credentials_path.stat().st_mode & 0o077:
|
|
raise SystemExit('Credentials file must be owner-only.')
|
|
credentials = json.loads(credentials_path.read_text())
|
|
data_bytes = pathlib.Path(args.dataset).read_bytes()
|
|
data = json.loads(data_bytes)
|
|
context = ssl.create_default_context(cafile=args.ca)
|
|
token = None
|
|
|
|
def request(path, payload):
|
|
headers = {'Content-Type': 'application/json'}
|
|
if token:
|
|
headers['Authorization'] = 'Bearer ' + token
|
|
query = urllib.request.Request(args.url.rstrip('/') + path,
|
|
data=json.dumps(payload).encode(), headers=headers)
|
|
with urllib.request.urlopen(query, context=context, timeout=30) as response:
|
|
return json.load(response)
|
|
|
|
try:
|
|
token = request('/api/authentication/login', credentials)['accessToken']
|
|
patient = request('/api/patients', data['profile'])
|
|
observations = [request('/api/patients/' + patient['patientId'] + '/observations', item)
|
|
for item in data['observations']]
|
|
except urllib.error.HTTPError as error:
|
|
raise SystemExit('Initialization stopped at HTTP ' + str(error.code) +
|
|
'; inspect the fresh test instance. No mutation retry or automatic cleanup performed.')
|
|
output.write_text(json.dumps({'datasetVersion': data['version'],
|
|
'datasetSha256': hashlib.sha256(data_bytes).hexdigest(),
|
|
'patientId': patient['patientId'],
|
|
'observationIds': [item['observationId'] for item in observations]}, indent=2) + '\n')
|
|
print('Fresh synthetic dataset initialized; manifest written. No credentials recorded.')
|