|
@@ -1,32 +1,8 @@ |
|
|
# Copyright (c) 2011, SmartFile <btimby@smartfile.com> |
|
|
|
|
|
# All rights reserved. |
|
|
|
|
|
# |
|
|
|
|
|
# Redistribution and use in source and binary forms, with or without |
|
|
|
|
|
# modification, are permitted provided that the following conditions are met: |
|
|
|
|
|
# * Redistributions of source code must retain the above copyright |
|
|
|
|
|
# notice, this list of conditions and the following disclaimer. |
|
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright |
|
|
|
|
|
# notice, this list of conditions and the following disclaimer in the |
|
|
|
|
|
# documentation and/or other materials provided with the distribution. |
|
|
|
|
|
# * Neither the name of the organization nor the |
|
|
|
|
|
# names of its contributors may be used to endorse or promote products |
|
|
|
|
|
# derived from this software without specific prior written permission. |
|
|
|
|
|
# |
|
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
|
|
|
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
|
|
|
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
|
|
|
|
|
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
|
|
|
|
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
|
|
|
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
|
|
|
|
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
|
|
|
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
|
|
|
|
|
|
|
|
|
import time |
|
|
|
|
|
|
|
|
import os |
|
|
from libarchive import is_archive, Entry, SeekableArchive |
|
|
from libarchive import is_archive, Entry, SeekableArchive |
|
|
from tarfile import DEFAULT_FORMAT, USTAR_FORMAT, GNU_FORMAT, PAX_FORMAT, ENCODING |
|
|
|
|
|
from tarfile import REGTYPE, AREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE, GNUTYPE_SPARSE |
|
|
|
|
|
|
|
|
from tarfile import DEFAULT_FORMAT, USTAR_FORMAT, GNU_FORMAT, PAX_FORMAT, \ |
|
|
|
|
|
ENCODING |
|
|
|
|
|
from tarfile import REGTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CHRTYPE, BLKTYPE |
|
|
|
|
|
|
|
|
FORMAT_CONVERSION = { |
|
|
FORMAT_CONVERSION = { |
|
|
USTAR_FORMAT: 'tar', |
|
|
USTAR_FORMAT: 'tar', |
|
@@ -76,7 +52,13 @@ class TarInfo(Entry): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TarFile(SeekableArchive): |
|
|
class TarFile(SeekableArchive): |
|
|
def __init__(self, name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, encoding=ENCODING): |
|
|
|
|
|
|
|
|
getmember = SeekableArchive.getentry |
|
|
|
|
|
list = SeekableArchive.printlist |
|
|
|
|
|
extract = SeekableArchive.readpath |
|
|
|
|
|
extractfile = SeekableArchive.readstream |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, name=None, mode='r', fileobj=None, |
|
|
|
|
|
format=DEFAULT_FORMAT, tarinfo=TarInfo, encoding=ENCODING): |
|
|
if name: |
|
|
if name: |
|
|
f = name |
|
|
f = name |
|
|
elif fileobj: |
|
|
elif fileobj: |
|
@@ -85,12 +67,8 @@ class TarFile(SeekableArchive): |
|
|
format = FORMAT_CONVERSION.get(format) |
|
|
format = FORMAT_CONVERSION.get(format) |
|
|
except KeyError: |
|
|
except KeyError: |
|
|
raise Exception('Invalid tar format: %s' % format) |
|
|
raise Exception('Invalid tar format: %s' % format) |
|
|
super(TarFile, self).__init__(f, mode=mode, format=format, entry_class=tarinfo, encoding=encoding) |
|
|
|
|
|
|
|
|
|
|
|
getmember = SeekableArchive.getentry |
|
|
|
|
|
list = SeekableArchive.printlist |
|
|
|
|
|
extract = SeekableArchive.readpath |
|
|
|
|
|
extractfile = SeekableArchive.readstream |
|
|
|
|
|
|
|
|
super(TarFile, self).__init__(f, mode=mode, format=format, |
|
|
|
|
|
entry_class=tarinfo, encoding=encoding) |
|
|
|
|
|
|
|
|
def getmembers(self): |
|
|
def getmembers(self): |
|
|
return list(self) |
|
|
return list(self) |
|
@@ -99,6 +77,7 @@ class TarFile(SeekableArchive): |
|
|
return list(self.iterpaths) |
|
|
return list(self.iterpaths) |
|
|
|
|
|
|
|
|
def next(self): |
|
|
def next(self): |
|
|
|
|
|
raise NotImplementedError |
|
|
pass # TODO: how to do this? |
|
|
pass # TODO: how to do this? |
|
|
|
|
|
|
|
|
def extract(self, member, path=None): |
|
|
def extract(self, member, path=None): |
|
@@ -113,10 +92,10 @@ class TarFile(SeekableArchive): |
|
|
def add(self, name, arcname, recursive=True, exclude=None, filter=None): |
|
|
def add(self, name, arcname, recursive=True, exclude=None, filter=None): |
|
|
pass # TODO: implement this. |
|
|
pass # TODO: implement this. |
|
|
|
|
|
|
|
|
def addfile(tarinfo, fileobj): |
|
|
|
|
|
|
|
|
def addfile(self, tarinfo, fileobj): |
|
|
return self.writepath(fileobj, tarinfo) |
|
|
return self.writepath(fileobj, tarinfo) |
|
|
|
|
|
|
|
|
def gettarinfo(name=None, arcname=None, fileobj=None): |
|
|
|
|
|
|
|
|
def gettarinfo(self, name=None, arcname=None, fileobj=None): |
|
|
if name: |
|
|
if name: |
|
|
f = name |
|
|
f = name |
|
|
elif fileobj: |
|
|
elif fileobj: |
|
|