Skip to content

Commit d0f4ed1

Browse files
committed
Move open_archive to archive_utils module
1 parent b39a608 commit d0f4ed1

File tree

6 files changed

+53
-54
lines changed

6 files changed

+53
-54
lines changed

autobuild/filetype.py renamed to autobuild/archive_utils.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
"""Utilities for detecting file types"""
1+
import multiprocessing
2+
import tarfile
3+
import zipfile
24

35
class ArchiveType:
46
GZ = "gz"
@@ -47,3 +49,39 @@ def detect_archive_type(filename: str):
4749
if f_type:
4850
return f_type
4951
return _archive_type_from_signature(filename)
52+
53+
54+
def open_archive(filename: str) -> tarfile.TarFile | zipfile.ZipFile:
55+
f_type = detect_archive_type(filename)
56+
57+
if f_type == ArchiveType.ZST:
58+
return ZstdTarFile(filename, "r")
59+
60+
if f_type == ArchiveType.ZIP:
61+
return zipfile.ZipFile(filename, "r")
62+
63+
return tarfile.open(filename, "r")
64+
65+
66+
class ZstdTarFile(tarfile.TarFile):
67+
def __init__(self, name, mode='r', *, level=4, zstd_dict=None, **kwargs):
68+
from pyzstd import CParameter, ZstdFile
69+
zstdoption = None
70+
if mode != 'r' and mode != 'rb':
71+
zstdoption = {CParameter.compressionLevel : level,
72+
CParameter.nbWorkers : multiprocessing.cpu_count(),
73+
CParameter.checksumFlag : 1}
74+
self.zstd_file = ZstdFile(name, mode,
75+
level_or_option=zstdoption,
76+
zstd_dict=zstd_dict)
77+
try:
78+
super().__init__(fileobj=self.zstd_file, mode=mode, **kwargs)
79+
except:
80+
self.zstd_file.close()
81+
raise
82+
83+
def close(self):
84+
try:
85+
super().close()
86+
finally:
87+
self.zstd_file.close()

autobuild/autobuild_tool_install.py

+3-17
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
import os
1515
import pprint
1616
import sys
17-
import tarfile
1817
import urllib.error
1918
import urllib.parse
2019
import urllib.request
21-
import zipfile
2220

2321
from autobuild import autobuild_base, common, configfile
2422
from autobuild.autobuild_tool_source_environment import get_enriched_environment
2523
from autobuild.hash_algorithms import verify_hash
26-
from autobuild import filetype
24+
from autobuild import archive_utils
2725

2826
logger = logging.getLogger('autobuild.install')
2927

@@ -405,7 +403,7 @@ def _install_binary(configured_name, platform, package, config_file, install_dir
405403

406404
def get_metadata_from_package(package_file) -> configfile.MetadataDescription:
407405
try:
408-
with open_archive(package_file) as archive:
406+
with archive_utils.open_archive(package_file) as archive:
409407
f = archive.extractfile(configfile.PACKAGE_METADATA_FILE)
410408
return configfile.MetadataDescription(stream=f)
411409
except (FileNotFoundError, KeyError):
@@ -439,18 +437,6 @@ def _default_metadata_for_package(package_file: str, package = None):
439437
return metadata
440438

441439

442-
def open_archive(filename: str) -> tarfile.TarFile | zipfile.ZipFile:
443-
f_type = filetype.detect_archive_type(filename)
444-
445-
if f_type == filetype.ArchiveType.ZST:
446-
return common.ZstdTarFile(filename, "r")
447-
448-
if f_type == filetype.ArchiveType.ZIP:
449-
return zipfile.ZipFile(filename, "r")
450-
451-
return tarfile.open(filename, "r")
452-
453-
454440
class ExtractPackageResults:
455441
files: list[str]
456442
conflicts: list[str]
@@ -468,7 +454,7 @@ def raise_conflicts(self):
468454

469455

470456
def extract_package(package_file: str, install_dir: str, dry_run: bool = False) -> ExtractPackageResults:
471-
with open_archive(package_file) as archive:
457+
with archive_utils.open_archive(package_file) as archive:
472458
results = ExtractPackageResults()
473459
for t in archive:
474460
if t.name == configfile.PACKAGE_METADATA_FILE:

autobuild/autobuild_tool_package.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import getpass
2424
import glob
25-
import hashlib
2625
import json
2726
import logging
2827
import os
@@ -32,7 +31,7 @@
3231
from collections import UserDict
3332
from zipfile import ZIP_DEFLATED, ZipFile
3433

35-
from autobuild import autobuild_base, common, configfile
34+
from autobuild import autobuild_base, common, configfile, archive_utils
3635
from autobuild.common import AutobuildError
3736

3837
logger = logging.getLogger('autobuild.package')
@@ -306,7 +305,7 @@ def _create_tarfile(tarfilename, format, build_directory, filelist, results: dic
306305
tfile = tarfile.open(tarfilename, 'w:gz')
307306
elif format == 'tzst':
308307
tarfilename = tarfilename + '.tar.zst'
309-
tfile = common.ZstdTarFile(tarfilename, 'w', level=22)
308+
tfile = archive_utils.ZstdTarFile(tarfilename, 'w', level=22)
310309
else:
311310
raise PackageError("unknown tar archive format: %s" % format)
312311

autobuild/common.py

-24
Original file line numberDiff line numberDiff line change
@@ -524,27 +524,3 @@ def has_cmd(name, subcmd: str = "help") -> bool:
524524
except OSError:
525525
return False
526526
return not p.returncode
527-
528-
529-
class ZstdTarFile(tarfile.TarFile):
530-
def __init__(self, name, mode='r', *, level=4, zstd_dict=None, **kwargs):
531-
from pyzstd import CParameter, ZstdFile
532-
zstdoption = None
533-
if mode != 'r' and mode != 'rb':
534-
zstdoption = {CParameter.compressionLevel : level,
535-
CParameter.nbWorkers : multiprocessing.cpu_count(),
536-
CParameter.checksumFlag : 1}
537-
self.zstd_file = ZstdFile(name, mode,
538-
level_or_option=zstdoption,
539-
zstd_dict=zstd_dict)
540-
try:
541-
super().__init__(fileobj=self.zstd_file, mode=mode, **kwargs)
542-
except:
543-
self.zstd_file.close()
544-
raise
545-
546-
def close(self):
547-
try:
548-
super().close()
549-
finally:
550-
self.zstd_file.close()

tests/test_filetype.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
from tests.basetest import temp_dir
55

66
import pytest
7-
from autobuild import filetype
7+
from autobuild import archive_utils
88

99

1010
_DATA_DIR = Path(__file__).parent / "data"
1111

1212
_ARCHIVE_TEST_CASES = (
13-
(path.join(_DATA_DIR, "archive.tar.bz2"), filetype.ArchiveType.BZ2),
14-
(path.join(_DATA_DIR, "archive.tar.gz"), filetype.ArchiveType.GZ),
15-
(path.join(_DATA_DIR, "archive.tar.zst"), filetype.ArchiveType.ZST),
16-
(path.join(_DATA_DIR, "archive.zip"), filetype.ArchiveType.ZIP),
13+
(path.join(_DATA_DIR, "archive.tar.bz2"), archive_utils.ArchiveType.BZ2),
14+
(path.join(_DATA_DIR, "archive.tar.gz"), archive_utils.ArchiveType.GZ),
15+
(path.join(_DATA_DIR, "archive.tar.zst"), archive_utils.ArchiveType.ZST),
16+
(path.join(_DATA_DIR, "archive.zip"), archive_utils.ArchiveType.ZIP),
1717
)
1818

1919

2020
@pytest.mark.parametrize("filename,expected_type", _ARCHIVE_TEST_CASES)
2121
def test_detect_from_extension(filename, expected_type):
22-
f_type = filetype.detect_archive_type(filename)
22+
f_type = archive_utils.detect_archive_type(filename)
2323
assert f_type == expected_type
2424

2525

@@ -28,5 +28,5 @@ def test_detect_from_signature(filename, expected_type):
2828
with temp_dir() as dir:
2929
filename_no_ext = str(Path(dir) / "archive")
3030
shutil.copyfile(filename, filename_no_ext)
31-
f_type = filetype.detect_archive_type(filename_no_ext)
31+
f_type = archive_utils.detect_archive_type(filename_no_ext)
3232
assert f_type == expected_type

tests/test_package.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from zipfile import ZipFile
1010

1111
import autobuild.autobuild_tool_package as package
12-
from autobuild import common, configfile
12+
from autobuild import common, configfile, archive_utils
1313
from tests.basetest import BaseTest, CaptureStdout, ExpectError, clean_dir, clean_file
1414

1515
# ****************************************************************************
@@ -76,7 +76,7 @@ def tearDown(self):
7676

7777
def tar_has_expected(self,tar):
7878
if 'tar.zst' in tar:
79-
tarball = common.ZstdTarFile(tar, 'r')
79+
tarball = archive_utils.ZstdTarFile(tar, 'r')
8080
else:
8181
tarball = tarfile.open(tar, 'r')
8282
packaged_files=tarball.getnames()

0 commit comments

Comments
 (0)