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 gzip 12import lzma 13import os 14import shutil 15import tarfile 16 17def archive_extract(archive, dest_dir, member=None): 18 with tarfile.open(archive) as tf: 19 if hasattr(tarfile, 'data_filter'): 20 tf.extraction_filter = getattr(tarfile, 'data_filter', 21 (lambda member, path: member)) 22 if member: 23 tf.extract(member=member, path=dest_dir) 24 else: 25 tf.extractall(path=dest_dir) 26 27def gzip_uncompress(gz_path, output_path): 28 if os.path.exists(output_path): 29 return 30 with gzip.open(gz_path, 'rb') as gz_in: 31 try: 32 with open(output_path, 'wb') as raw_out: 33 shutil.copyfileobj(gz_in, raw_out) 34 except: 35 os.remove(output_path) 36 raise 37 38def lzma_uncompress(xz_path, output_path): 39 if os.path.exists(output_path): 40 return 41 with lzma.open(xz_path, 'rb') as lzma_in: 42 try: 43 with open(output_path, 'wb') as raw_out: 44 shutil.copyfileobj(lzma_in, raw_out) 45 except: 46 os.remove(output_path) 47 raise 48