from pathlib import Path
import hashlib,json,tarfile,urllib.request
root=Path('/var/www/html/artcare');full=root/'storage/app/elderly-support-deploy-20261016';out=root/'storage/app/elderly-support-date-inputs-20261020'
archive=full/'elderly-support-runtime.tar.gz'
expected='9d3078433ea9415d9606d2eb1aebf6e53e955de782d18a5b585394b3c52ec191'
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
assert sha(archive)==expected
with tarfile.open(archive) as data:
    for member in data.getmembers():
        assert not member.name.startswith('/') and '..' not in Path(member.name).parts
    data.extractall(full/'stage')
manifest=json.loads((full/'stage/manifest.json').read_text())
for row in manifest:
    assert sha(root/row['path'])==row['sha256'],row['path']
    assert sha(full/'stage'/row['path'])==row['sha256'],row['path']
request=urllib.request.Request('http://127.0.0.1:8089/elderly-support/register',headers={'Host':'elderly.mam-app.org','X-Forwarded-Proto':'https'})
body=urllib.request.urlopen(request).read()
assert b'placeholder="DD-MM-YYYY"' in body and b'data-date-field' in body
assert b'v=date-inputs-20261020' in body and b'v=1.7-register9-followup4' in body
ready=urllib.request.urlopen('http://127.0.0.1:20249/ready').status
assert ready==200
result={'runtime_and_stage_files_verified':len(manifest),'archive_sha256':expected,'live_date_fields':True,'live_date_asset_versions':True,'live_dictionary_version':'1.7','tunnel_ready_status':ready}
(out/'backup-verification.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps(result,indent=2))
