#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-2.0-or-later """Report the transfer and installed size of an OSTree commit tree. The image builder commits the composed tree twice: a dangling commit is measured here, then the published commit records the result as commit metadata. Clients get exact numbers from authenticated commit metadata, without downloading the image or trusting a separate size file. The transfer size counts every unique file object once (that is what a pull can retrieve), so hardlinked and repeated payload does not inflate it. The installed size sums every path of the checkout. """ import os import subprocess import sys def object_size(objects, checksum, kinds): for kind in kinds: try: return os.path.getsize(os.path.join(objects, checksum[:2], checksum[2:] + "." + kind)) except OSError: continue return 0 def main(repo, commit): listing = subprocess.run( ["ostree", "--repo=" + repo, "ls", "-R", "-C", commit], check=True, capture_output=True, text=True).stdout objects = os.path.join(repo, "objects") files, directories = set(), set() transfer = 0 installed = 0 for line in listing.splitlines(): # mode uid gid size checksum path; the path may contain spaces. fields = line.split(None, 5) if len(fields) < 6: raise SystemExit("Unexpected ostree ls record: " + line) mode, size, checksum = fields[0], int(fields[3]), fields[4] if mode.startswith("d"): if checksum not in directories: directories.add(checksum) transfer += object_size(objects, checksum, ("dirtree",)) continue installed += size if checksum not in files: files.add(checksum) transfer += object_size(objects, checksum, ("filez", "file")) print(transfer, installed) if __name__ == "__main__": if len(sys.argv) != 3: raise SystemExit("usage: image-size.py REPO COMMIT") main(sys.argv[1], sys.argv[2])