aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArthur Zamarin <arthurzam@gentoo.org>2022-10-10 15:42:26 +0300
committerArthur Zamarin <arthurzam@gentoo.org>2022-10-10 18:55:00 +0300
commit7c1d50adc0f82f6fc19f857930eb70c2ba7a84f2 (patch)
tree2a5f080d3eb241fe22d1ceb5266962a84402a80a /src
parentcompression/_bzip2: add tests (diff)
downloadsnakeoil-7c1d50adc0f82f6fc19f857930eb70c2ba7a84f2.tar.gz
snakeoil-7c1d50adc0f82f6fc19f857930eb70c2ba7a84f2.tar.bz2
snakeoil-7c1d50adc0f82f6fc19f857930eb70c2ba7a84f2.zip
compression.__init__: add tests
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'src')
-rw-r--r--src/snakeoil/compression/__init__.py27
-rw-r--r--src/snakeoil/compression/_util.py24
2 files changed, 21 insertions, 30 deletions
diff --git a/src/snakeoil/compression/__init__.py b/src/snakeoil/compression/__init__.py
index b250b1af..4b4a437c 100644
--- a/src/snakeoil/compression/__init__.py
+++ b/src/snakeoil/compression/__init__.py
@@ -1,9 +1,9 @@
import shlex
+from functools import cached_property
from importlib import import_module
-from .. import klass
+from .. import process
from ..cli.exceptions import UserException
-from ..process import CommandNotFound, find_binary
from ..process.spawn import spawn_get_output
@@ -12,14 +12,10 @@ class _transform_source:
def __init__(self, name):
self.name = name
- @klass.jit_attr
+ @cached_property
def module(self):
return import_module(f'snakeoil.compression._{self.name}')
- @klass.jit_attr
- def parallelizable(self):
- return bool(getattr(self.module, 'parallelizable', False))
-
def compress_data(self, data, level, parallelize=False):
parallelize = parallelize and self.module.parallelizable
return self.module.compress_data(data, level, parallelize=parallelize)
@@ -81,7 +77,7 @@ class ArComp:
def __init_subclass__(cls, **kwargs):
"""Initialize result subclasses and register archive extensions."""
super().__init_subclass__(**kwargs)
- if not all((cls.binary, cls.default_unpack_cmd, cls.exts)):
+ if not all((cls.binary, cls.default_unpack_cmd, cls.exts)): # pragma: no cover
raise ValueError(f'class missing required attrs: {cls!r}')
for ext in cls.exts:
cls.known_exts[ext] = cls
@@ -89,13 +85,13 @@ class ArComp:
def __init__(self, path, ext=None):
self.path = path
- @klass.jit_attr
+ @cached_property
def _unpack_cmd(self):
for b in self.binary:
try:
- binary = find_binary(b)
+ binary = process.find_binary(b)
break
- except CommandNotFound:
+ except process.CommandNotFound:
continue
else:
choices = ', '.join(self.binary)
@@ -107,9 +103,6 @@ class ArComp:
def unpack(self, dest=None, **kwargs):
raise NotImplementedError
- def create(self, dest):
- raise NotImplementedError
-
class _Archive:
"""Generic archive format support."""
@@ -155,16 +148,16 @@ class _Tar(_Archive, ArComp):
compress_binary = None
default_unpack_cmd = '{binary} xf "{path}"'
- @klass.jit_attr
+ @cached_property
def _unpack_cmd(self):
cmd = super()._unpack_cmd
if self.compress_binary is not None:
for b in self.compress_binary:
try:
- find_binary(b)
+ process.find_binary(b)
cmd += f' --use-compress-program={b}'
break
- except CommandNotFound:
+ except process.CommandNotFound:
pass
else:
choices = ', '.join(self.compress_binary)
diff --git a/src/snakeoil/compression/_util.py b/src/snakeoil/compression/_util.py
index 6a3c7258..e1af5aef 100644
--- a/src/snakeoil/compression/_util.py
+++ b/src/snakeoil/compression/_util.py
@@ -22,7 +22,7 @@ def _drive_process(args, mode, data):
def compress_data(binary, data, compresslevel=9, extra_args=()):
- args = [binary, '-%ic' % compresslevel]
+ args = [binary, f'-{compresslevel}c']
args.extend(extra_args)
return _drive_process(args, 'compression', data)
@@ -36,9 +36,7 @@ def decompress_data(binary, data, extra_args=()):
class _process_handle:
def __init__(self, handle, args, is_read=False):
- self.mode = 'wb'
- if is_read:
- self.mode = 'rb'
+ self.mode = 'rb' if is_read else 'wb'
self.args = tuple(args)
self.is_read = is_read
@@ -55,8 +53,7 @@ class _process_handle:
elif not isinstance(handle, int):
if not hasattr(handle, 'fileno'):
raise TypeError(
- "handle %r isn't a string, integer, and lacks a fileno "
- "method" % (handle,))
+ f"handle {handle!r} isn't a string, integer, and lacks a fileno method")
handle = handle.fileno()
try:
@@ -108,8 +105,8 @@ class _process_handle:
if fwd_seek < 0:
if self._allow_reopen is None:
raise TypeError(
- "instance %s can't do negative seeks: asked for %i, "
- "was at %i" % (self, position, self.position))
+ f"instance {self} can't do negative seeks: "
+ f"asked for {position}, was at {self.position}")
self._terminate()
self._open_handle(self._allow_reopen)
return self.seek(position)
@@ -142,16 +139,17 @@ class _process_handle:
def _terminate(self):
try:
self._process.terminate()
- except EnvironmentError as e:
+ except EnvironmentError as exc:
# allow no such process only.
- if e.errno != errno.ESRCH:
+ if exc.errno != errno.ESRCH:
raise
def close(self):
+ if not hasattr(self, '_process'):
+ return
if self._process.returncode is not None:
if self._process.returncode != 0:
- raise Exception("%s invocation had non zero exit: %i" %
- (self.args, self._process.returncode))
+ raise Exception(f"{self.args} invocation had non zero exit: {self._process.returncode}")
return
self.handle.close()
@@ -165,7 +163,7 @@ class _process_handle:
def compress_handle(binary_path, handle, compresslevel=9, extra_args=()):
- args = [binary_path, '-%ic' % compresslevel]
+ args = [binary_path, f'-{compresslevel}c']
args.extend(extra_args)
return _process_handle(handle, args, False)