From 0f681873404bd542c104b4908e174aa84f11b507 Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Tue, 10 Oct 2023 19:28:45 +0200 Subject: [PATCH] Add filter argument for extracting tar in python >=3.12 (#144) --- src/sinol_make/oiejq/__init__.py | 2 +- src/sinol_make/util.py | 8 ++++++++ tests/commands/export/test_integration.py | 7 ++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/sinol_make/oiejq/__init__.py b/src/sinol_make/oiejq/__init__.py index fccaf552..981e6c7b 100644 --- a/src/sinol_make/oiejq/__init__.py +++ b/src/sinol_make/oiejq/__init__.py @@ -73,7 +73,7 @@ def install_oiejq(): oiejq_file.write(request.content) with tarfile.open(oiejq_path) as tar: - tar.extractall(path=tmpdir) + util.extract_tar(tar, tmpdir) shutil.copy(os.path.join(tmpdir, 'oiejq', 'oiejq.sh'), os.path.expanduser('~/.local/bin/oiejq')) shutil.copy(os.path.join(tmpdir, 'oiejq', 'sio2jail'), os.path.expanduser('~/.local/bin/')) diff --git a/src/sinol_make/util.py b/src/sinol_make/util.py index 1bfbb3aa..a7a38511 100644 --- a/src/sinol_make/util.py +++ b/src/sinol_make/util.py @@ -1,5 +1,6 @@ import glob, importlib, os, sys, requests, yaml import platform +import tarfile import tempfile import shutil import hashlib @@ -362,6 +363,13 @@ def try_fix_config(config): return config +def extract_tar(tar: tarfile.TarFile, destination: str): + if sys.version_info.major == 3 and sys.version_info.minor >= 12: + tar.extractall(destination, filter='tar') + else: + tar.extractall(destination) + + def color_red(text): return "\033[91m{}\033[00m".format(text) def color_green(text): return "\033[92m{}\033[00m".format(text) def color_yellow(text): return "\033[93m{}\033[00m".format(text) diff --git a/tests/commands/export/test_integration.py b/tests/commands/export/test_integration.py index cfd46e30..f9f94e64 100644 --- a/tests/commands/export/test_integration.py +++ b/tests/commands/export/test_integration.py @@ -6,6 +6,7 @@ import tempfile from sinol_make import configure_parsers +from sinol_make import util as sinol_util from sinol_make.commands.doc import Command as DocCommand from tests import util from tests.fixtures import create_package @@ -19,7 +20,7 @@ def _test_archive(package_path, out, tar): with tempfile.TemporaryDirectory() as tmpdir: with tarfile.open(tar, "r") as tar: - tar.extractall(tmpdir) + sinol_util.extract_tar(tar, tmpdir) extracted = os.path.join(tmpdir, task_id) assert os.path.exists(extracted) @@ -75,7 +76,7 @@ def test_doc_cleared(create_package): with tempfile.TemporaryDirectory() as tmpdir: with tarfile.open(f'{package_util.get_task_id()}.tgz', "r") as tar: - tar.extractall(tmpdir) + sinol_util.extract_tar(tar, tmpdir) extracted = os.path.join(tmpdir, package_util.get_task_id()) assert os.path.exists(extracted) @@ -100,7 +101,7 @@ def test_correct_permissions(create_package, capsys): with tempfile.TemporaryDirectory() as tmpdir: with tarfile.open(f'{task_id}.tgz', "r") as tar: - tar.extractall(tmpdir) + sinol_util.extract_tar(tar, tmpdir) shell_ingen = os.path.join(tmpdir, task_id, 'prog', f'{task_id}ingen.sh') assert os.path.exists(shell_ingen)