"""Shared helpers for the ancillary-package build scripts (no absolute paths here;
every input path is passed on the command line)."""
import hashlib, json, sys, datetime

def sha256_file(path):
    h = hashlib.sha256()
    with open(path, 'rb') as f:
        for chunk in iter(lambda: f.read(1 << 20), b''):
            h.update(chunk)
    return h.hexdigest()

def source_entry(name, path):
    """Descriptive source name + sha256 of the input file (no path recorded)."""
    return {"name": name, "sha256": sha256_file(path)}

def utcnow():
    return datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')

def dump(obj, path):
    with open(path + '.tmp', 'w') as f:
        json.dump(obj, f, indent=1, ensure_ascii=False)
        f.write('\n')
    import os
    os.replace(path + '.tmp', path)
