"""Download unchanged public CDISC Pilot inputs to internal storage."""
from pathlib import Path
import urllib.request, hashlib, json, datetime

ROOT = Path(__file__).resolve().parent
COMMIT = '667511d4b183871d74392ba691c935c38d431d39'
BASE = f'https://raw.githubusercontent.com/cdisc-org/sdtm-adam-pilot-project/{COMMIT}/'
PREFIX = 'updated-pilot-submission-package/900172/m5/datasets/cdiscpilot01/analysis/adam/datasets/'
dest = ROOT / 'inputs'
dest.mkdir(exist_ok=True)
manifest = []
for name in ['adsl.xpt','adae.xpt','adqsadas.xpt','adlbc.xpt','adtte.xpt','define.xml','README.md']:
    url = BASE + (name if name == 'README.md' else PREFIX + name)
    data = urllib.request.urlopen(url).read()
    (dest/name).write_bytes(data)
    manifest.append(dict(file=name,url=url,commit=COMMIT,downloaded_utc=datetime.datetime.now(datetime.timezone.utc).isoformat(),sha256=hashlib.sha256(data).hexdigest(),bytes=len(data)))
(ROOT/'provenance.json').write_text(json.dumps(manifest,indent=2)+'\n',encoding='utf-8')
print(json.dumps(manifest,indent=2))
