/usr/lib/python2.3/tabnanny.py archive.py

This commit is contained in:
S.Çağlar Onur
2005-06-21 13:59:00 +00:00
parent 861dad0aea
commit d762ee1e52
+32 -32
View File
@@ -17,49 +17,49 @@ class ArchiveError:
class ArchiveBase(object): class ArchiveBase(object):
def __init__(self, filepath, atype): def __init__(self, filepath, atype):
self.filePath = filepath self.filePath = filepath
self.type = atype self.type = atype
def unpack(self, targetDir): def unpack(self, targetDir):
self.targetDir = targetDir self.targetDir = targetDir
# first we check if we need to clean-up our working env. # first we check if we need to clean-up our working env.
if os.path.exists(self.targetDir): if os.path.exists(self.targetDir):
util.clean_dir(self.targetDir) util.clean_dir(self.targetDir)
else: else:
os.makedirs(self.targetDir) os.makedirs(self.targetDir)
class ArchiveTarFile(ArchiveBase): class ArchiveTarFile(ArchiveBase):
def __init__(self, filepath, type="tar"): def __init__(self, filepath, type="tar"):
super(ArchiveTarFile, self).__init__(filepath, type) super(ArchiveTarFile, self).__init__(filepath, type)
def unpack(self, targetDir): def unpack(self, targetDir):
super(ArchiveTarFile, self).unpack(targetDir) super(ArchiveTarFile, self).unpack(targetDir)
rmode = "" rmode = ""
if self.type == 'tar': if self.type == 'tar':
rmode = 'r:' rmode = 'r:'
elif self.type == 'targz': elif self.type == 'targz':
rmode = 'r:gz' rmode = 'r:gz'
elif self.type == 'tarbz2': elif self.type == 'tarbz2':
rmode = 'r:bz2' rmode = 'r:bz2'
else: else:
raise ArchiveError("Archive type not recognized") raise ArchiveError("Archive type not recognized")
tar = tarfile.open(self.filePath, rmode) tar = tarfile.open(self.filePath, rmode)
oldwd = os.getcwd() oldwd = os.getcwd()
os.chdir(self.targetDir) os.chdir(self.targetDir)
for tarinfo in tar: for tarinfo in tar:
tar.extract(tarinfo) tar.extract(tarinfo)
os.chdir(oldwd) os.chdir(oldwd)
tar.close() tar.close()
class ArchiveZip(ArchiveBase): class ArchiveZip(ArchiveBase):
def __init__(self, filepath, type="zip"): def __init__(self, filepath, type="zip"):
super(ArchiveZip, self).__init__(filepath, type) super(ArchiveZip, self).__init__(filepath, type)
def unpack_file_cond(self, pred, targetDir, archiveRoot=''): def unpack_file_cond(self, pred, targetDir, archiveRoot=''):
""" unpack file according to predicate function filename -> bool""" """ unpack file according to predicate function filename -> bool"""
super(ArchiveZip, self).unpack(targetDir) super(ArchiveZip, self).unpack(targetDir)
zip = zipfile.ZipFile(self.filePath, 'r') zip = zipfile.ZipFile(self.filePath, 'r')
for file in zip.namelist(): for file in zip.namelist():
if pred(file): # check if condition holds if pred(file): # check if condition holds
@@ -110,20 +110,20 @@ class Archive:
"""Unpack magic for Archive files...""" """Unpack magic for Archive files..."""
def __init__(self, filepath, type): def __init__(self, filepath, type):
"""accepted archive types: """accepted archive types:
targz, tarbz2, zip, tar""" targz, tarbz2, zip, tar"""
handlers = { handlers = {
'targz': ArchiveTarFile, 'targz': ArchiveTarFile,
'tarbz2': ArchiveTarFile, 'tarbz2': ArchiveTarFile,
'tar': ArchiveTarFile, 'tar': ArchiveTarFile,
'zip': ArchiveZip 'zip': ArchiveZip
} }
self.archive = handlers.get(type)(filepath, type) self.archive = handlers.get(type)(filepath, type)
def unpack(self, targetDir): def unpack(self, targetDir):
self.archive.unpack(targetDir) self.archive.unpack(targetDir)
def unpack_files(self, files, targetDir): def unpack_files(self, files, targetDir):
self.archive.unpack_files(files, targetDir) self.archive.unpack_files(files, targetDir)