tools: add inline-source.py

Add a simple tool that will spit out a valid org.osbuild.inline
source entry that encodes the given file. Currently always uses
base64 as encoding and sha256 for the hashing.
This commit is contained in:
Christian Kellner 2021-04-29 11:21:44 +00:00 committed by Achilleas Koutsou
parent f75cb1d56d
commit 2dfb6a224b

42
tools/inline-source.py Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/python3
"""
Encode binary file data within the manifest by using
the org.osbuild.inline source.
"""
import argparse
import binascii
import hashlib
import json
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument("FILE", help="The file to encode")
args = parser.parse_args()
with open(args.FILE, "rb") as f:
raw = f.read()
m = hashlib.sha256()
m.update(raw)
checksum = "sha256:" + m.hexdigest()
data = binascii.b2a_base64(raw, newline=False).decode("ascii")
source = {
"org.osbuild.inline": {
"items": {
checksum: {
"encoding": "base64",
"data": data
}
}
}
}
json.dump(source, sys.stdout, indent=2)
if __name__ == "__main__":
main()