def uncompressTarBz2(filepath, extractPath = None, catchEmpty = False):
entropyLog.log(ETP_LOGPRI_INFO,ETP_LOGLEVEL_VERBOSE,"uncompressTarBz2: called.")
if extractPath is None:
extractPath = os.path.dirname(filepath)
if not os.path.isfile(filepath):
raise OSError
try:
try:
tar = tarfile.open(filepath,"r")
except CompressionError:
tar = tarfile.open(filepath,"r:bz2") # python 2.4 crashes above, so supporting only bz2
except tarfile.ReadError:
if catchEmpty:
return 0
else:
raise
import pdb
pdb.set_trace()
directories = set()
for tarinfo in tar:
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(extractPath, tarinfo.name), 0777)
except EnvironmentError:
pass
directories.add(tarinfo)
else:
try:
tarinfo.name = tarinfo.name.encode(sys.getfilesystemencoding())
except: # default encoding failed
try:
tarinfo.name = tarinfo.name.decode("latin1") # try to convert to latin1 and then back to sys.getfilesystemencoding()
tarinfo.name = tarinfo.name.encode(sys.getfilesystemencoding())
except:
raise
tar.extract(tarinfo, extractPath.encode(sys.getfilesystemencoding()))
# Reverse sort directories.
#'''
directories = list(directories)
directories.sort(lambda a, b: cmp(a.name, b.name))
directories.reverse()
origpath = extractPath
# Set correct owner, mtime and filemode on directories.
for tarinfo in directories:
extractPath = os.path.join(extractPath, tarinfo.name)
try:
tar.chown(tarinfo, extractPath)
tar.utime(tarinfo, extractPath)
tar.chmod(tarinfo, extractPath)
except tarfile.ExtractError:
if tar.errorlevel > 1:
raise
#'''
del directories
tar.close()
del tar
if os.listdir(origpath):
return 0
else:
return -1