Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 deletions msys2_devtools/db.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
import io

from pyzstd import ZstdFile, ZstdError
import tarfile


class ExtTarFile(tarfile.TarFile):
"""Extends TarFile to support zstandard"""

@classmethod
def zstdopen(cls, name, mode="r", fileobj=None, **kwargs): # type: ignore
"""Open zstd compressed tar archive"""

if mode not in ("r", "w", "x", "a"):
raise ValueError("mode must be 'r', 'w' or 'x' or 'a'")

zstfileobj = None
try:
zstfileobj = ZstdFile(fileobj or name, mode)
if "r" in mode:
zstfileobj.peek(1) # raises ZstdError if not a zstd file
except (ZstdError, EOFError) as e:
if zstfileobj is not None:
zstfileobj.close()
raise tarfile.ReadError("not a zstd file") from e

try:
t = cls.taropen(name, mode, zstfileobj, **kwargs)
except Exception:
zstfileobj.close()
raise

t._extfileobj = False
return t

OPEN_METH = {"zstd": "zstdopen", **tarfile.TarFile.OPEN_METH}
from .exttarfile import ExtTarFile


def parse_desc(t: str) -> dict[str, list[str]]:
Expand Down
48 changes: 48 additions & 0 deletions msys2_devtools/exttarfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import tarfile

HAS_ZSTD = True
try:
from compression import zstd
except ImportError:
HAS_ZSTD = False

ExtTarFile: type[tarfile.TarFile]

if not HAS_ZSTD:
from pyzstd import ZstdFile, ZstdError

class ZstTarFile(tarfile.TarFile):
"""Extends TarFile to support zstandard"""

@classmethod
def zstdopen(cls, name, mode="r", fileobj=None, **kwargs): # type: ignore
"""Open zstd compressed tar archive"""

if mode not in ("r", "w", "x", "a"):
raise ValueError("mode must be 'r', 'w' or 'x' or 'a'")

zstfileobj = None
try:
zstfileobj = ZstdFile(fileobj or name, mode)
if "r" in mode:
zstfileobj.peek(1) # raises ZstdError if not a zstd file
except (ZstdError, EOFError) as e:
if zstfileobj is not None:
zstfileobj.close()
raise tarfile.ReadError("not a zstd file") from e

try:
t = cls.taropen(name, mode, zstfileobj, **kwargs)
except Exception:
zstfileobj.close()
raise

t._extfileobj = False
return t

OPEN_METH = {"zst": "zstdopen", **tarfile.TarFile.OPEN_METH}

ExtTarFile = ZstTarFile
else:
assert zstd
ExtTarFile = tarfile.TarFile
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies = [
"tabulate>=0.9.0,<0.10",
"requests>=2.28.2,<3",
"pydantic>=2.0,<3",
"pyzstd>=0.18.0,<0.19",
"pyzstd>=0.18.0,<0.19; python_version < '3.14'",
]

[project.optional-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_parse_desc():

def test_zstd():
fileobj = io.BytesIO()
with ExtTarFile.open(fileobj=fileobj, mode='w:zstd') as tar:
with ExtTarFile.open(fileobj=fileobj, mode='w:zst') as tar:
data = "Hello world!".encode('utf-8')
info = tarfile.TarInfo("test.txt")
info.size = len(data)
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.