xref: /openbmc/qemu/tests/functional/qemu_test/utils.py (revision 3bb4c8b6134df5367675c4ade4f5c177d29fe903)
1# Utilities for python-based QEMU tests
2#
3# Copyright 2024 Red Hat, Inc.
4#
5# Authors:
6#  Thomas Huth <thuth@redhat.com>
7#
8# This work is licensed under the terms of the GNU GPL, version 2 or
9# later.  See the COPYING file in the top-level directory.
10
11import os
12
13"""
14Round up to next power of 2
15"""
16def pow2ceil(x):
17    return 1 if x == 0 else 2**(x - 1).bit_length()
18
19def file_truncate(path, size):
20    if size != os.path.getsize(path):
21        with open(path, 'ab+') as fd:
22            fd.truncate(size)
23
24"""
25Expand file size to next power of 2
26"""
27def image_pow2ceil_expand(path):
28        size = os.path.getsize(path)
29        size_aligned = pow2ceil(size)
30        if size != size_aligned:
31            with open(path, 'ab+') as fd:
32                fd.truncate(size_aligned)
33