xref: /qemu/tests/functional/qemu_test/archive.py (revision cfcb4484fc78cbbd835e2880add561e1fbef6796)
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
13
14
15def tar_extract(archive, dest_dir, member=None):
16    with tarfile.open(archive) as tf:
17        if hasattr(tarfile, 'data_filter'):
18            tf.extraction_filter = getattr(tarfile, 'data_filter',
19                                           (lambda member, path: member))
20        if member:
21            tf.extract(member=member, path=dest_dir)
22        else:
23            tf.extractall(path=dest_dir)
24
25def cpio_extract(cpio_handle, output_path):
26    cwd = os.getcwd()
27    os.chdir(output_path)
28    subprocess.run(['cpio', '-i'],
29                   input=cpio_handle.read(),
30                   stderr=subprocess.DEVNULL)
31    os.chdir(cwd)
32