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