cyd0806's picture
Release all nine KC structure/function rounds and auditable dependencies
6f24982 verified
Raw History Blame Contribute Delete
4.73 kB
"""Publish only to the user-authorized dataset through Hugging Face's stored-auth client.
No raw tokens are accepted on the CLI, serialized or printed.
"""
from pathlib import Path
import argparse,json,subprocess,hashlib
import httpx
from huggingface_hub import HfApi,set_client_factory,CommitOperationAdd
P=Path(__file__).resolve().parents[1];R=P.parents[1]
REPO='cyd0806/neuronmimic-data';PREFIX='wu_20261104_structure_function'
def main():
parser=argparse.ArgumentParser();parser.add_argument('--publish',action='store_true');parser.add_argument('--check-write-with-card',action='store_true');args=parser.parse_args()
assert args.publish or args.check_write_with_card,'Use an explicit publication mode'
# Use only the user's configured proxy route and the SDK's existing credential broker.
proxy=subprocess.run(['git','config','--get','https.proxy'],capture_output=True,text=True).stdout.strip()
set_client_factory(lambda:httpx.Client(proxy=proxy or None,timeout=90,follow_redirects=True))
api=HfApi();status={'repo':REPO,'prefix':PREFIX,'auth':'HuggingFaceSDK_stored_credentials','uploaded':False}
try:
who=api.whoami();status['authenticated_account']=who.get('name')
before=api.repo_info(REPO,repo_type='dataset');status['previous_commit']=before.sha
if args.check_write_with_card:
c=api.create_commit(REPO,repo_type='dataset',operations=[CommitOperationAdd(path_in_repo=PREFIX+'/README.md',path_or_fileobj=str(P/'metadata/DATASET_CARD.md'))],commit_message='Document structure/function public-validation release scope')
status.update(uploaded=True,scope='dataset_card_only_not_scientific_payload',commit=c.oid)
else:
directory=R/'deliveries/wu_structure_function_data_20261105'
assert (directory/'release_file_manifest.json').exists()
before_upload=api.repo_info(REPO,repo_type='dataset')
assert before_upload.sha==before.sha,'Dataset changed during publication; refresh before retrying'
api.upload_folder(repo_id=REPO,repo_type='dataset',folder_path=directory,path_in_repo=PREFIX,commit_message='Release all nine KC structure/function rounds and auditable dependencies',ignore_patterns=['.cache/**'],parent_commit=before.sha)
status['payload_upload_returned_success']=True
after=api.repo_info(REPO,repo_type='dataset');status['commit']=after.sha
entries=list(api.list_repo_tree(REPO,repo_type='dataset',path_in_repo=PREFIX,recursive=True,revision=after.sha))
remote={x.path:x for x in entries if hasattr(x,'size')}
checks=[]
for a in json.loads((directory/'release_file_manifest.json').read_text()):
path=PREFIX+'/'+a['path'];v=remote[path];lfs=getattr(v,'lfs',None)
remote_sha=getattr(lfs,'sha256',None) if lfs else None
assert v.size==a['bytes']
if remote_sha:assert remote_sha==a['sha256'],(path,'remote SHA256 mismatch')
else:assert a['git_blob_sha1'] and getattr(v,'blob_id',None)==a['git_blob_sha1'],(path,'remote Git blob mismatch')
checks.append({'path':a['path'],'bytes':v.size,'remote_sha256':remote_sha,'verified':True})
status.update(uploaded=True,scope='all_included_scientific_payload',remote_file_checks=checks,n_remote_files=len(remote))
# Publish a separate status receipt only after scientific file verification.
# This mutable receipt is deliberately outside the frozen payload hash manifest.
receipt=json.loads((directory/'publication_status.json').read_text())
receipt['huggingface'].update(uploaded=True,payload_commit=after.sha,verified_payload_files=len(checks),write_http_status=None)
receipt['huggingface']['reason']='Published and checked against the frozen file manifest.'
receipt['status']='HF_PUBLISHED_GITHUB_STATUS_RECORDED_SEPARATELY'
receipt_bytes=json.dumps(receipt,ensure_ascii=False,indent=2).encode()
c=api.create_commit(REPO,repo_type='dataset',parent_commit=after.sha,operations=[CommitOperationAdd(path_in_repo=PREFIX+'/publication_status.json',path_or_fileobj=receipt_bytes)],commit_message='Record verified structure/function data publication receipt')
status['status_commit']=c.oid
(directory/'publication_status.json').write_bytes(receipt_bytes)
(P/'metadata/publication_status.json').write_bytes(receipt_bytes)
except Exception as e:
response=getattr(e,'response',None)
status.update(status='BLOCKED_AUTHORIZATION_OR_NETWORK',error_class=type(e).__name__,http_status=getattr(response,'status_code',None),reason='Current SDK account cannot publish to specified dataset, or endpoint is unavailable. No credentials are recorded.')
(P/'metadata/hf_publication_status.json').write_text(json.dumps(status,ensure_ascii=False,indent=2));print(json.dumps(status,ensure_ascii=False,indent=2))
if not status['uploaded']:raise SystemExit(2)
if __name__=='__main__':main()