xref: /qemu/tests/functional/qemu_test/archive.py (revision 379ee839f9ae374302c4b9f444c9f804ec7a2796)
1# SPDX-License-Identifier: GPL-2.0-or-later
2#
3# Utilities for python-based QEMU tests
4#
5# Copyright 2024 Red Hat, Inc.
6#
7# Authors:
8#  Thomas Huth <thuth@redhat.com>
9
10import os
11import subprocess
12import tarfile
13import zipfile
14
15
16def tar_extract(archive, dest_dir, member=None):
17    with tarfile.open(archive) as tf:
18        if hasattr(tarfile, 'data_filter'):
19            tf.extraction_filter = getattr(tarfile, 'data_filter',
20                                           (lambda member, path: member))
21        if member:
22            tf.extract(member=member, path=dest_dir)
23        else:
24            tf.extractall(path=dest_dir)
25
26def cpio_extract(cpio_handle, output_path):
27    cwd = os.getcwd()
28    os.chdir(output_path)
29    subprocess.run(['cpio', '-i'],
30                   input=cpio_handle.read(),
31                   stderr=subprocess.DEVNULL)
32    os.chdir(cwd)
33
34def zip_extract(archive, dest_dir, member=None):
35    with zipfile.ZipFile(archive, 'r') as zf:
36        if member:
37            zf.extract(member=member, path=dest_dir)
38        else:
39            zf.extractall(path=dest_dir)
40