Skip to content

Commit cff2a3b

Browse files
Merge pull request #95 from aboutcode-org/update-tar-extract
Support setting the tarfile extract filter
2 parents 24b07fa + c284fa4 commit cff2a3b

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Release notes
22
=============
33

4+
Version 32.4.2 - (2025-01-08)
5+
-----------------------------
6+
7+
- Support setting the tar archive filter in python3.14
8+
https://github.com/aboutcode-org/commoncode/issues/88
9+
410
Version 32.4.1 - (2025-01-07)
511
-----------------------------
612

src/commoncode/archive.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from os import path
1515

1616
from commoncode.system import on_windows
17+
from commoncode.system import py314
1718

1819
"""
1920
Mimimal tar and zip file handling, primarily for testing.
@@ -39,7 +40,7 @@ def _extract_tar_raw(test_path, target_dir, to_bytes, *args, **kwargs):
3940
extract_tar_uni = partial(_extract_tar_raw, to_bytes=False)
4041

4142

42-
def extract_tar(location, target_dir, verbatim=False, *args, **kwargs):
43+
def extract_tar(location, target_dir, verbatim=False, filter=None, *args, **kwargs):
4344
"""
4445
Extract a tar archive at location in the target_dir directory.
4546
If `verbatim` is True preserve the permissions.
@@ -58,7 +59,10 @@ def extract_tar(location, target_dir, verbatim=False, *args, **kwargs):
5859
if not verbatim:
5960
tarinfo.mode = 0o755
6061
to_extract.append(tarinfo)
61-
tar.extractall(target_dir, members=to_extract)
62+
if py314 and filter:
63+
tar.extractall(target_dir, members=to_extract, filter=filter)
64+
else:
65+
tar.extractall(target_dir, members=to_extract)
6266
finally:
6367
if tar:
6468
tar.close()

src/commoncode/testcase.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def remove_vcs(self, test_dir):
194194
for tf in tilde_files:
195195
os.remove(tf)
196196

197-
def __extract(self, test_path, extract_func=None, verbatim=False):
197+
def __extract(self, test_path, extract_func=None, verbatim=False, filter=None):
198198
"""
199199
Given an archive file identified by test_path relative
200200
to a test files directory, return a new temp directory where the
@@ -206,7 +206,7 @@ def __extract(self, test_path, extract_func=None, verbatim=False):
206206
target_path = path.basename(test_path)
207207
target_dir = self.get_temp_dir(target_path)
208208
original_archive = self.get_test_loc(test_path)
209-
extract_func(original_archive, target_dir, verbatim=verbatim)
209+
extract_func(original_archive, target_dir, verbatim=verbatim, filter=filter)
210210
return target_dir
211211

212212
def extract_test_zip(self, test_path, *args, **kwargs):
@@ -215,8 +215,8 @@ def extract_test_zip(self, test_path, *args, **kwargs):
215215
def extract_test_zip_raw(self, test_path, *args, **kwargs):
216216
return self.__extract(test_path, extract_zip_raw)
217217

218-
def extract_test_tar(self, test_path, verbatim=False):
219-
return self.__extract(test_path, extract_tar, verbatim)
218+
def extract_test_tar(self, test_path, verbatim=False, filter=None):
219+
return self.__extract(test_path, extract_tar, verbatim, filter)
220220

221221
def extract_test_tar_raw(self, test_path, *args, **kwargs):
222222
return self.__extract(test_path, extract_tar_raw)

tests/data/filetype/types.tar

-3.5 KB
Binary file not shown.

tests/test_filetype.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def test_get_size_on_directory(self):
3434
assert filetype.get_size(test_dir) == 12400
3535

3636
def test_get_type(self):
37-
test_dir = self.extract_test_tar("filetype/types.tar", verbatim=True)
37+
test_dir = self.extract_test_tar(
38+
"filetype/types.tar", verbatim=True, filter="fully_trusted"
39+
)
3840
results = []
3941
for root, dirs, files in os.walk(test_dir):
4042
for d in dirs:
@@ -61,6 +63,7 @@ def test_get_type(self):
6163
if on_posix:
6264
expected += [
6365
("2-SYMTYPE", "l"),
66+
("6-FIFOTYPE", "s"),
6467
]
6568

6669
try:

0 commit comments

Comments
 (0)