Module refinery.units.formats.archive.xttar
Expand source code Browse git
from __future__ import annotations
import datetime
import tarfile
from refinery.lib.structures import MemoryFile
from refinery.units.formats.archive import ArchiveUnit
class xttar(ArchiveUnit, docs='{0}{s}{PathExtractorUnit}'):
"""
Extract files from a Tar archive.
"""
def unpack(self, data: bytearray):
with MemoryFile(data) as stream:
try:
archive = tarfile.open(fileobj=stream)
except Exception:
ustar = data.find(B'ustar')
if ustar < 257:
raise
stream.seek(ustar - 257)
archive = tarfile.open(fileobj=stream)
for info in archive.getmembers():
if not info.isfile():
continue
extractor = archive.extractfile(info)
if extractor is None:
continue
date = datetime.datetime.fromtimestamp(info.mtime)
yield self._pack(info.name, date, lambda e=extractor: e.read())
@classmethod
def handles(cls, data) -> bool:
return data[257:262] == B'ustar'
Classes
class xttar (*paths, list=False, join_path=False, drop_path=False, fuzzy=0, exact=False, regex=False, path=b'path', date=b'date', pwd=b'')-
Extract files from a Tar archive. This unit is a path extractor which extracts data from a hierarchical structure. Each extracted item is emitted as a separate chunk and has attached to it a meta variable that contains its path within the source structure. The positional arguments to the command are patterns that can be used to filter the extracted items by their path. To view only the paths of all chunks, use the listing switch:
emit something | xttar --listOtherwise, extracted items are written to the standard output port and usually require a frame to properly process. In order to dump all extracted data to disk, the following pipeline can be used:
emit something | xttar [| d2p ]If you using xttar to unpack a file on disk, the following pattern can be useful:
ef pack.foo [| xttar -j | d2p ]The unit
efis also a path extractor. By specifying-j(or--join), the paths of extracted items are combined. Thed2punit will deconflict these with the local file system. For example, ifpack.foocontains itemsone.txtandtwo.txt, the following local file tree would be the result:pack.foo pack/one.txt pack/two.txtFinally, the
-d(or--drop) switch can be used to not create (or alter) the path metadata at all, which is useful in cases where path metadata from a previous unit should be preserved.Expand source code Browse git
class xttar(ArchiveUnit, docs='{0}{s}{PathExtractorUnit}'): """ Extract files from a Tar archive. """ def unpack(self, data: bytearray): with MemoryFile(data) as stream: try: archive = tarfile.open(fileobj=stream) except Exception: ustar = data.find(B'ustar') if ustar < 257: raise stream.seek(ustar - 257) archive = tarfile.open(fileobj=stream) for info in archive.getmembers(): if not info.isfile(): continue extractor = archive.extractfile(info) if extractor is None: continue date = datetime.datetime.fromtimestamp(info.mtime) yield self._pack(info.name, date, lambda e=extractor: e.read()) @classmethod def handles(cls, data) -> bool: return data[257:262] == B'ustar'Ancestors
Subclasses
Class variables
var reverse-
The type of the None singleton.
Methods
def unpack(self, data)-
Expand source code Browse git
def unpack(self, data: bytearray): with MemoryFile(data) as stream: try: archive = tarfile.open(fileobj=stream) except Exception: ustar = data.find(B'ustar') if ustar < 257: raise stream.seek(ustar - 257) archive = tarfile.open(fileobj=stream) for info in archive.getmembers(): if not info.isfile(): continue extractor = archive.extractfile(info) if extractor is None: continue date = datetime.datetime.fromtimestamp(info.mtime) yield self._pack(info.name, date, lambda e=extractor: e.read())
Inherited members
ArchiveUnit:CustomJoinBehaviourCustomPathSeparatorFilterEverythingRequiresactassemblecodecconsolefilterfinishhandlesis_quietis_reversibleisattylabelledleniencylog_alwayslog_debuglog_detachlog_faillog_infolog_levellog_warnloggernamenozzleoptional_dependenciesprocessreadread1required_dependenciesresetrunsourcesuperinit