Merge pull request #557 from ilvalle/fileutils.py

updated _extractall (python 2.4 support was dropped)
This commit is contained in:
mdipierro
2014-12-12 05:12:25 -06:00

View File

@@ -193,62 +193,7 @@ def cleanpath(path):
def _extractall(filename, path='.', members=None):
# FIXME: this should be dropped because python 2.4 support was dropped
if not hasattr(tarfile.TarFile, 'extractall'):
from tarfile import ExtractError
class TarFile(tarfile.TarFile):
def extractall(self, path='.', members=None):
"""Extract all members from the archive to the current working
directory and set owner, modification time and permissions on
directories afterwards. `path' specifies a different directory
to extract to. `members' is optional and must be a subset of the
list returned by getmembers().
"""
directories = []
if members is None:
members = self
for tarinfo in members:
if tarinfo.isdir():
# Extract directory with a safe mode, so that
# all files below can be extracted as well.
try:
os.makedirs(os.path.join(path,
tarinfo.name), 0777)
except EnvironmentError:
pass
directories.append(tarinfo)
else:
self.extract(tarinfo, path)
# Reverse sort directories.
directories.sort(lambda a, b: cmp(a.name, b.name))
directories.reverse()
# Set correct owner, mtime and filemode on directories.
for tarinfo in directories:
path = os.path.join(path, tarinfo.name)
try:
self.chown(tarinfo, path)
self.utime(tarinfo, path)
self.chmod(tarinfo, path)
except ExtractError, e:
if self.errorlevel > 1:
raise
else:
self._dbg(1, 'tarfile: %s' % e)
_cls = TarFile
else:
_cls = tarfile.TarFile
tar = _cls(filename, 'r')
tar = tarfile.TarFile(filename, 'r')
ret = tar.extractall(path, members)
tar.close()
return ret