from urllib import request
from urllib.parse import urlparse
import hashlib
import sys
import yaml
if len(sys.argv) > 1:
filename = sys.argv[1];
with open(filename) as f:
file_ref = yaml.load(f, Loader=yaml.FullLoader)
length = file_ref.get("length")
for part in file_ref.get("parts"):
for data in part.get("data"):
location = data.get("locations")[0]
location_url = urlparse(location)
if location_url.scheme == "":
with open(location, "rb") as f:
content = f.read()
else:
with request.urlopen(location) as f:
content = f.read()
known_hash = data.get("sha256")
content_hash = hashlib.sha256(content).hexdigest()
if known_hash != content_hash:
print("%s != %s" % (known_hash, content_hash), file=sys.stderr)
if length is not None:
if len(content) > length:
content = content[:length]
length -= len(content)
sys.stdout.buffer.write(content)
else:
print("chunky-bits.py <file>")