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 15from .cmd import run_cmd 16 17 18def tar_extract(archive, dest_dir, member=None): 19 with tarfile.open(archive) as tf: 20 if hasattr(tarfile, 'data_filter'): 21 tf.extraction_filter = getattr(tarfile, 'data_filter', 22 (lambda member, path: member)) 23 if member: 24 tf.extract(member=member, path=dest_dir) 25 else: 26 tf.extractall(path=dest_dir) 27 28def cpio_extract(cpio_handle, output_path): 29 cwd = os.getcwd() 30 os.chdir(output_path) 31 subprocess.run(['cpio', '-i'], 32 input=cpio_handle.read(), 33 stderr=subprocess.DEVNULL) 34 os.chdir(cwd) 35 36def zip_extract(archive, dest_dir, member=None): 37 with zipfile.ZipFile(archive, 'r') as zf: 38 if member: 39 zf.extract(member=member, path=dest_dir) 40 else: 41 zf.extractall(path=dest_dir) 42 43def deb_extract(archive, dest_dir, member=None): 44 cwd = os.getcwd() 45 os.chdir(dest_dir) 46 try: 47 (stdout, stderr, ret) = run_cmd(['ar', 't', archive]) 48 file_path = stdout.split()[2] 49 run_cmd(['ar', 'x', archive, file_path]) 50 tar_extract(file_path, dest_dir, member) 51 finally: 52 os.chdir(cwd) 53