Datasets:
Download tools/fetch_source.py from momoya2000/SkelMo: direct link, hf CLI and curl.
- Browser
- Download file 3.02 kB
-
https://huggingface.co/datasets/momoya2000/SkelMo/resolve/main/tools/fetch_source.py
- Command line
-
hf download hf://datasets/momoya2000/SkelMo/tools/fetch_source.py
-
curl -L -o fetch_source.py https://huggingface.co/datasets/momoya2000/SkelMo/resolve/main/tools/fetch_source.py
3.02 kB
| #!/usr/bin/env python3 | |
| """Inspect a source asset, or explicitly download a commit-pinned GitHub asset.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import urllib.parse | |
| import urllib.request | |
| def github_raw_url(url): | |
| parsed = urllib.parse.urlsplit(url) | |
| parts = parsed.path.split('/') | |
| if parsed.hostname != 'github.com' or len(parts) < 6 or parts[3] != 'blob': | |
| raise ValueError('Expected a GitHub blob URL') | |
| owner, repo, revision = parts[1], parts[2], parts[4] | |
| if len(revision) != 40 or any(c not in '0123456789abcdef' for c in revision.lower()): | |
| raise ValueError('Source URL is not pinned to a full commit') | |
| raw_path = '/'.join([owner, repo, revision] + parts[5:]) | |
| return 'https://raw.githubusercontent.com/' + urllib.parse.quote(urllib.parse.unquote(raw_path), safe='/') | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('manifest', type=Path) | |
| parser.add_argument('sample_id') | |
| parser.add_argument('--download', action='store_true') | |
| parser.add_argument('--output', type=Path) | |
| args = parser.parse_args() | |
| record = next((r for r in map(json.loads, args.manifest.open()) if r['sample_id'] == args.sample_id), None) | |
| if record is None: | |
| parser.error('Sample ID not found') | |
| info = {k: record[k] for k in ['sample_id', 'source_asset_id', 'source_url', 'source_file_type', | |
| 'source_sha256', 'historical_license', 'observed_license_url', 'provenance_flags']} | |
| if record['source_group'] == 'github': | |
| info['download_url'] = github_raw_url(record['source_url']) | |
| else: | |
| info['download_method'] = 'Use the original Sketchfab page and its official download flow.' | |
| print(json.dumps(info, indent=2, ensure_ascii=False)) | |
| if not args.download: | |
| return | |
| if record['source_group'] != 'github': | |
| parser.error('Automatic Sketchfab downloads are not implemented; use the official source flow.') | |
| if args.output is None: | |
| parser.error('--download requires --output') | |
| if args.output.exists(): | |
| parser.error('Output already exists; select a new path') | |
| partial = args.output.with_name(args.output.name + '.partial') | |
| digest = hashlib.sha256() | |
| created = False | |
| try: | |
| with partial.open('xb') as out: | |
| created = True | |
| with urllib.request.urlopen(info['download_url'], timeout=60) as response: | |
| for chunk in iter(lambda: response.read(1024 * 1024), b''): | |
| out.write(chunk) | |
| digest.update(chunk) | |
| if digest.hexdigest() != record['source_sha256']: | |
| raise ValueError('Source SHA-256 mismatch; upstream content or a Git LFS pointer may differ') | |
| partial.rename(args.output) | |
| except Exception: | |
| if created and partial.exists(): | |
| partial.unlink() | |
| raise | |
| print('Verified source asset saved to ' + str(args.output)) | |
| if __name__ == '__main__': | |
| main() | |