From 1dc2cdcc14ac006d2ff477e210fbbb553f19d245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Mon, 12 May 2025 13:37:57 +0200 Subject: [PATCH] Tools/inline-source.py: support specifying data encoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- tools/inline-source.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tools/inline-source.py b/tools/inline-source.py index 5a28b4e9..6fc481e2 100755 --- a/tools/inline-source.py +++ b/tools/inline-source.py @@ -5,15 +5,23 @@ the org.osbuild.inline source. """ import argparse -import binascii +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: @@ -22,13 +30,19 @@ def main(): m = hashlib.sha256() m.update(raw) checksum = "sha256:" + m.hexdigest() - data = binascii.b2a_base64(raw, newline=False).decode("ascii") + + 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": "base64", + "encoding": args.encoding, "data": data } }