fetch_vcsl.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. """fetch_vcsl.py - downloads the CC0 reference notes LYNX Model is calibrated on (round 190ni) and writes their manifest.
  3. fetch_vcsl.py <tree.json> <LICENSE> <out-dir> <commit>
  4. <tree.json>: https://api.github.com/repos/sgossner/VCSL/git/trees/<commit>?recursive=1 ; <LICENSE>: the repository's
  5. LICENSE at that commit (read it: CC0 1.0 Universal). Fetches the six mallet folders below at the pinned commit from
  6. raw.githubusercontent.com into <out-dir>/<set>/, checks each file's git blob SHA-1 against the tree (a file that does
  7. not match fails the run) and writes <out-dir>/manifest.json: per file its path, size, blob SHA-1 and SHA-256, plus the
  8. commit, the licence and the licence file's SHA-256. calibrate.py copies those records into each table's provenance.
  9. Keep the downloads out of the repository: only the fitted parameters ship.
  10. """
  11. import hashlib, json, os, sys, urllib.parse, urllib.request
  12. tree, licence, out, commit = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
  13. licence_text = open(licence, 'rb').read()
  14. if b'CC0 1.0 Universal' not in licence_text:
  15. raise SystemExit('the LICENSE is not CC0 1.0: stop (the data policy is CC0 only)')
  16. SETS = {
  17. 'marimba': 'Idiophones/Struck Idiophones/Marimba/',
  18. 'vibraphone': 'Idiophones/Struck Idiophones/Vibraphone/Soft Mallets/',
  19. 'vibraphone-hard': 'Idiophones/Struck Idiophones/Vibraphone/Hard Mallets/',
  20. 'xylophone': 'Idiophones/Struck Idiophones/Xylophone/Medium Mallets/',
  21. 'xylophone-hard': 'Idiophones/Struck Idiophones/Xylophone/Hard Mallets/',
  22. 'glockenspiel': 'Idiophones/Struck Idiophones/Glockenspiel/',
  23. 'tubularbells': 'Idiophones/Struck Idiophones/Tubular Bells 2/',
  24. 'kalimba': 'Idiophones/Plucked Idiophones/Kalimba, Tanzania/',
  25. }
  26. t = json.load(open(tree))
  27. manifest = []
  28. for name, prefix in SETS.items():
  29. d = os.path.join(out, name)
  30. os.makedirs(d, exist_ok=True)
  31. for e in t['tree']:
  32. p = e['path']
  33. if e['type'] != 'blob' or not p.startswith(prefix) or '/' in p[len(prefix):] or not p.lower().endswith('.wav'):
  34. continue
  35. dst = os.path.join(d, p.split('/')[-1])
  36. if not os.path.exists(dst) or os.path.getsize(dst) != e['size']:
  37. url = 'https://raw.githubusercontent.com/sgossner/VCSL/%s/%s' % (commit, urllib.parse.quote(p))
  38. data = urllib.request.urlopen(url, timeout=120).read()
  39. open(dst, 'wb').write(data)
  40. data = open(dst, 'rb').read()
  41. blob = hashlib.sha1(b'blob %d\0' % len(data) + data).hexdigest()
  42. ok = blob == e['sha']
  43. if not ok:
  44. raise SystemExit('blob SHA-1 mismatch: %s' % p)
  45. manifest.append({'set': name, 'path': p, 'bytes': len(data), 'git_blob_sha1': e['sha'], 'verified': ok,
  46. 'sha256': hashlib.sha256(data).hexdigest()})
  47. print(name, ok, p.split('/')[-1], flush=True)
  48. json.dump({'source': 'https://github.com/sgossner/VCSL', 'commit': commit, 'licence': 'CC0 1.0 Universal (repository LICENSE)',
  49. 'licence_sha256': hashlib.sha256(licence_text).hexdigest(),
  50. 'licence_check': 'Repository LICENSE at commit %s: Creative Commons CC0 1.0 Universal; README: "This collection is under a '
  51. 'Creative Commons 0 license"; no other licence or notice file in the instrument folders' % commit,
  52. 'files': manifest}, open(os.path.join(out, 'manifest.json'), 'w'), indent=1)
  53. print('files', len(manifest), 'all verified', all(m['verified'] for m in manifest))