debian-forge/tools/inline-source.py
Tomáš Hozza 1dc2cdcc14 Tools/inline-source.py: support specifying data encoding
Extend the script to support specifying the data encoding. Keep
'base64' as the default encoding.

Add support for 'lzma+base64' encoding.

Also use the 'base64' module, instead of 'binascii' module for base64
encoding. This is consistent with what the actual source implementation
uses.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2025-05-13 10:27:36 +02:00

56 lines
1.2 KiB
Python
Executable file

#!/usr/bin/python3
"""
Encode binary file data within the manifest by using
the org.osbuild.inline source.
"""
import argparse
import base64
import hashlib
import json
import lzma
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument("FILE", help="The file to encode")
parser.add_argument(
"-e",
"--encoding",
choices=["base64", "lzma+base64"],
default="base64",
help="The encoding to use for the data (default: base64)"
)
args = parser.parse_args()
with open(args.FILE, "rb") as f:
raw = f.read()
m = hashlib.sha256()
m.update(raw)
checksum = "sha256:" + m.hexdigest()
if args.encoding == "lzma+base64":
raw = lzma.compress(raw)
data = base64.b64encode(raw).decode("ascii")
else:
# default to base64
data = base64.b64encode(raw).decode("ascii")
source = {
"org.osbuild.inline": {
"items": {
checksum: {
"encoding": args.encoding,
"data": data
}
}
}
}
json.dump(source, sys.stdout, indent=2)
if __name__ == "__main__":
main()