Skip to content

Commit e8c1010

Browse files
authored
SCANPY-181 Only use the the filter parameter of tarfile.extractall if supported (#201)
1 parent b3fa61a commit e8c1010

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

.cirrus.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,14 @@ analysis_linux_task:
194194
qa_task:
195195
alias: qa
196196
matrix:
197-
- name: "Test Python 3.9"
197+
- name: "Test Python 3.9.18"
198198
eks_container:
199199
docker_arguments:
200200
PYTHON_VERSION: 3.9.18
201+
- name: "Test Python 3.9.6"
202+
eks_container:
203+
docker_arguments:
204+
PYTHON_VERSION: 3.9.6
201205
- name: "Test Python 3.10"
202206
eks_container:
203207
docker_arguments:

src/pysonar_scanner/jre.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ def __extract_jre(self, file_path: pathlib.Path, unzip_dir: pathlib.Path):
135135
with zipfile.ZipFile(file_path, "r") as zip_ref:
136136
zip_ref.extractall(unzip_dir)
137137
elif file_path.suffix in [".gz", ".tgz"]:
138-
with tarfile.open(file_path, "r:gz") as tar_ref:
139-
tar_ref.extractall(unzip_dir, filter="data")
138+
utils.extract_tar(file_path, unzip_dir)
140139
else:
141140
raise UnsupportedArchiveFormat(
142141
f"Received JRE is packaged as an unsupported archive format: {file_path.suffix}"

src/pysonar_scanner/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import hashlib
2121
import pathlib
2222
import platform
23+
import sys
24+
import tarfile
2325
import typing
2426
from enum import Enum
2527

@@ -89,3 +91,11 @@ def get_arch() -> Arch:
8991

9092
def filter_none_values(dictionary: dict) -> dict:
9193
return {k: v for k, v in dictionary.items() if v is not None}
94+
95+
96+
def extract_tar(path: pathlib.Path, target_dir: pathlib.Path):
97+
with tarfile.open(path, "r:gz") as tar_ref:
98+
if sys.version_info >= (3, 12):
99+
tar_ref.extractall(target_dir, filter="data")
100+
else:
101+
tar_ref.extractall(target_dir)

tests/unit/test_utils.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import unittest.mock
2424
import pyfakefs.fake_filesystem_unittest as pyfakefs
2525

26-
from pysonar_scanner.utils import Arch, Os, get_arch, get_os, remove_trailing_slash, calculate_checksum
26+
from pysonar_scanner.utils import Arch, Os, get_arch, get_os, remove_trailing_slash, calculate_checksum, extract_tar
2727

2828

2929
class TestUtils(unittest.TestCase):
@@ -132,3 +132,31 @@ def test_calculate_checksum(self):
132132
calculate_checksum(BytesIO(b"test test")),
133133
"03ffdf45276dd38ffac79b0e9c6c14d89d9113ad783d5922580f4c66a3305591",
134134
)
135+
136+
137+
class TestExtractTar(unittest.TestCase):
138+
def setUp(self):
139+
self.test_path = pathlib.Path("/fake/path/archive.tar.gz")
140+
self.test_target_dir = pathlib.Path("/fake/target/dir")
141+
142+
@unittest.mock.patch("tarfile.open")
143+
@unittest.mock.patch("sys.version_info", (3, 12, 0))
144+
def test_extract_tar_python_3_12_or_higher(self, mock_open):
145+
mock_tar = unittest.mock.MagicMock()
146+
mock_open.return_value.__enter__.return_value = mock_tar
147+
148+
extract_tar(self.test_path, self.test_target_dir)
149+
150+
mock_open.assert_called_once_with(self.test_path, "r:gz")
151+
mock_tar.extractall.assert_called_once_with(self.test_target_dir, filter="data")
152+
153+
@unittest.mock.patch("tarfile.open")
154+
@unittest.mock.patch("sys.version_info", (3, 11, 0))
155+
def test_extract_tar_python_older_than_3_12(self, mock_open):
156+
mock_tar = unittest.mock.MagicMock()
157+
mock_open.return_value.__enter__.return_value = mock_tar
158+
159+
extract_tar(self.test_path, self.test_target_dir)
160+
161+
mock_open.assert_called_once_with(self.test_path, "r:gz")
162+
mock_tar.extractall.assert_called_once_with(self.test_target_dir)

0 commit comments

Comments
 (0)