simrepairbench / scripts /verify_asset_release.py
chrisvoncsefalvay's picture
Publish CASTAIGNE25 repair tasks and native image case viewer
2dee2e9 verified
Raw History Blame Contribute Delete
3.67 kB
"""Check every published tree file against the pinned source or local release bytes."""
import argparse
import hashlib
import json
from pathlib import Path
from huggingface_hub import HfApi, hf_hub_download
def git_blob(path):
h = hashlib.sha1(b'blob ' + str(path.stat().st_size).encode() + b'\0')
with path.open('rb') as stream:
for block in iter(lambda: stream.read(1024 * 1024), b''):
h.update(block)
return h.hexdigest()
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--root', type=Path, default=Path(__file__).resolve().parents[1])
args = p.parse_args()
root = args.root.resolve()
receipt = json.loads((root / 'work/asset-publication-receipt.json').read_text())
operations = json.loads((root / 'work/asset-publication-plan.json').read_text())['operations']
source = {}
for inv_path in (root / 'records').glob('*/upstream-inventory.json'):
inv = json.loads(inv_path.read_text())
source.update({(inv['repository'], inv['revision'], inv['asset_id'] + '/' + row['path']): row for row in inv['files']})
paths = [op['path'] for op in operations]
api, checked, attributes_added = HfApi(), 0, 0
for start in range(0, len(paths), 200):
batch = operations[start:start + 200]
remote = {row.path: row for row in api.get_paths_info(receipt['repository'], [x['path'] for x in batch],
repo_type='dataset', revision=receipt['commit'])}
for op in batch:
row = remote[op['path']]
if op['kind'] == 'copy':
expected = source[(op['source_repository'], op['source_revision'], op['source_path'])]
digest = expected['lfs_sha256'] or expected['git_blob_id']
size = expected['size']
else:
local = root / op['local']
if op['path'] == '.gitattributes':
# Cross-repository LFS copies append explicit path rules on the Hub.
published = Path(hf_hub_download(receipt['repository'], '.gitattributes', repo_type='dataset', revision=receipt['commit'])).read_text()
authored = local.read_text()
allowed = {path + ' filter=lfs diff=lfs merge=lfs -text' for path in paths}
if not published.startswith(authored) or any(line not in allowed for line in published[len(authored):].splitlines()):
raise ValueError('Unexpected generated LFS attributes')
attributes_added = len(published[len(authored):].splitlines())
checked += 1
continue
digest = op['sha256'] if row.lfs else git_blob(local)
size = local.stat().st_size
observed = row.lfs.sha256 if row.lfs else row.blob_id
if observed != digest or row.size != size:
raise ValueError('Published file differs: ' + op['path'])
checked += 1
result = {'repository': receipt['repository'], 'commit': receipt['commit'], 'passed': True,
'checked_files': checked, 'whole_objects': 25, 'broken_objects': 25,
'hub_generated_lfs_rules': attributes_added,
'checks': ['Every published path exists', 'Every file byte size matches',
'Every LFS SHA256 or Git blob identity matches its verified release or source pin']}
(root / 'work/asset-publication-verification.json').write_text(json.dumps(result, indent=2) + '\n')
print(json.dumps(result), flush=True)
if __name__ == '__main__':
main()