xref: /qemu/tests/functional/qemu_test/utils.py (revision ba32e50a1b2103e9f548c30ef5e96375c0002372)
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
13from .archive import tar_extract as archive_extract
14from .archive import cpio_extract
15from .uncompress import gzip_uncompress
16from .uncompress import lzma_uncompress
17
18"""
19Round up to next power of 2
20"""
21def pow2ceil(x):
22    return 1 if x == 0 else 2**(x - 1).bit_length()
23
24def file_truncate(path, size):
25    if size != os.path.getsize(path):
26        with open(path, 'ab+') as fd:
27            fd.truncate(size)
28
29"""
30Expand file size to next power of 2
31"""
32def image_pow2ceil_expand(path):
33        size = os.path.getsize(path)
34        size_aligned = pow2ceil(size)
35        if size != size_aligned:
36            with open(path, 'ab+') as fd:
37                fd.truncate(size_aligned)
38