Commit c4362bb8 authored by Julien Muchembled's avatar Julien Muchembled

default: add support for xz/lz archives to 'self.extract'

parent 1046b48f
Pipeline #19076 passed with stage
in 0 seconds
...@@ -36,7 +36,7 @@ import tempfile ...@@ -36,7 +36,7 @@ import tempfile
from setuptools import archive_util from setuptools import archive_util
import zc.buildout import zc.buildout
from .. import is_true, rmtree, EnvironMixin, Shared from .. import is_true, rmtree, EnvironMixin, Shared
from ..downloadunpacked import extraction_drivers, patched_extraction_drivers from ..downloadunpacked import unpack_archive
def readElfAsDict(f): def readElfAsDict(f):
"""Reads ELF information from file""" """Reads ELF information from file"""
...@@ -155,11 +155,7 @@ class Script(EnvironMixin): ...@@ -155,11 +155,7 @@ class Script(EnvironMixin):
extract_dir = tempfile.mkdtemp(self.name) extract_dir = tempfile.mkdtemp(self.name)
self.cleanup_list.append(extract_dir) self.cleanup_list.append(extract_dir)
self.logger.debug('Created working directory: %s', extract_dir) self.logger.debug('Created working directory: %s', extract_dir)
try: unpack_archive(self, path, extract_dir)
archive_util.extraction_drivers = patched_extraction_drivers
archive_util.unpack_archive(path, extract_dir)
finally:
archive_util.extraction_drivers = extraction_drivers
return extract_dir return extract_dir
def pipeCommand(self, command_list_list, *args, **kwargs): def pipeCommand(self, command_list_list, *args, **kwargs):
......
...@@ -66,25 +66,10 @@ class Recipe(EnvironMixin): ...@@ -66,25 +66,10 @@ class Recipe(EnvironMixin):
hash_name=True)(self._url, self.options.get('md5sum') or None, hash_name=True)(self._url, self.options.get('md5sum') or None,
**({'alternate_url': alternate} if alternate else {})) **({'alternate_url': alternate} if alternate else {}))
try: try:
archive_util.extraction_drivers = patched_extraction_drivers unpack_archive(self, path, location)
# ad-hoc support for .xz and .lz archive
with open(path, 'rb') as f:
hdr = f.read(6)
for magic, cmd in ((b'\xfd7zXZ\x00', ('xzcat',)),
(b'LZIP', ('lunzip', '-c'))):
if hdr.startswith(magic):
with tempfile.NamedTemporaryFile() as uncompressed_archive:
subprocess.check_call(cmd + (path,),
stdout=uncompressed_archive, env=self.environ)
archive_util.unpack_archive(
uncompressed_archive.name, location)
break
else:
archive_util.unpack_archive(path, location)
finally: finally:
if is_temp: if is_temp:
os.unlink(path) os.unlink(path)
archive_util.extraction_drivers = extraction_drivers
strip = self._strip strip = self._strip
if strip is None: if strip is None:
...@@ -108,9 +93,11 @@ class Recipe(EnvironMixin): ...@@ -108,9 +93,11 @@ class Recipe(EnvironMixin):
def update(self): def update(self):
pass pass
# Monkey patch to keep symlinks in tarfile
def unpack_tarfile_patched(filename, extract_dir, def unpack_archive(recipe, *args):
progress_filter=archive_util.default_filter): # Monkey patch to keep symlinks in tarfile
def unpack_tarfile_patched(filename, extract_dir,
progress_filter=archive_util.default_filter):
"""Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir` """Unpack tar/tar.gz/tar.bz2 `filename` to `extract_dir`
Raises ``UnrecognizedFormat`` if `filename` is not a tarfile (as determined Raises ``UnrecognizedFormat`` if `filename` is not a tarfile (as determined
...@@ -120,6 +107,18 @@ def unpack_tarfile_patched(filename, extract_dir, ...@@ -120,6 +107,18 @@ def unpack_tarfile_patched(filename, extract_dir,
try: try:
tarobj = tarfile.open(filename) tarobj = tarfile.open(filename)
except tarfile.TarError: except tarfile.TarError:
# ad-hoc support for .xz and .lz archive
with open(filename, 'rb') as f:
hdr = f.read(6)
for magic, cmd in ((b'\xfd7zXZ\x00', ('xzcat',)),
(b'LZIP', ('lunzip', '-c'))):
if hdr.startswith(magic):
with tempfile.NamedTemporaryFile() as uncompressed_archive:
subprocess.check_call(cmd + (filename,),
stdout=uncompressed_archive, env=recipe.environ)
archive_util.unpack_archive(
uncompressed_archive.name, extract_dir, progress_filter)
return True
raise archive_util.UnrecognizedFormat( raise archive_util.UnrecognizedFormat(
"%s is not a compressed or uncompressed tar file" % (filename,) "%s is not a compressed or uncompressed tar file" % (filename,)
) )
...@@ -148,7 +147,5 @@ def unpack_tarfile_patched(filename, extract_dir, ...@@ -148,7 +147,5 @@ def unpack_tarfile_patched(filename, extract_dir,
pass pass
return True return True
extraction_drivers = archive_util.extraction_drivers return archive_util.unpack_archive(*args,
patched_extraction_drivers = extraction_drivers[:2] + ( drivers = archive_util.extraction_drivers[:2] + (unpack_tarfile_patched,))
unpack_tarfile_patched,
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment