suphesiz ki archive'in degisken ve metot tanimlamalari pep-0008'in bize sundugu nimetlerden faydalanilarak yeniden degerlendirildi.
(atsatsak)
This commit is contained in:
+65
-65
@@ -28,17 +28,17 @@ class ArchiveError(pisi.Error):
|
|||||||
|
|
||||||
class ArchiveBase(object):
|
class ArchiveBase(object):
|
||||||
"""Base class for Archive classes."""
|
"""Base class for Archive classes."""
|
||||||
def __init__(self, filepath, atype):
|
def __init__(self, file_path, atype):
|
||||||
self.filePath = filepath
|
self.file_path = file_path
|
||||||
self.type = atype
|
self.type = atype
|
||||||
|
|
||||||
def unpack(self, targetDir, cleanDir = False):
|
def unpack(self, target_dir, clean_dir = False):
|
||||||
self.targetDir = targetDir
|
self.target_dir = target_dir
|
||||||
# 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) and cleanDir:
|
if os.path.exists(self.target_dir) and clean_dir:
|
||||||
util.clean_dir(self.targetDir)
|
util.clean_dir(self.target_dir)
|
||||||
|
|
||||||
os.makedirs(self.targetDir)
|
os.makedirs(self.target_dir)
|
||||||
|
|
||||||
|
|
||||||
class ArchiveTar(ArchiveBase):
|
class ArchiveTar(ArchiveBase):
|
||||||
@@ -46,12 +46,12 @@ class ArchiveTar(ArchiveBase):
|
|||||||
type. Provides access to tar, tar.gz and tar.bz2 files.
|
type. Provides access to tar, tar.gz and tar.bz2 files.
|
||||||
|
|
||||||
This class provides the unpack magic for tar archives."""
|
This class provides the unpack magic for tar archives."""
|
||||||
def __init__(self, filepath, ArchType = "tar"):
|
def __init__(self, file_path, arch_type = "tar"):
|
||||||
super(ArchiveTar, self).__init__(filepath, ArchType)
|
super(ArchiveTar, self).__init__(file_path, arch_type)
|
||||||
|
|
||||||
def unpack(self, targetDir, cleanDir = False):
|
def unpack(self, target_dir, clean_dir = False):
|
||||||
"""Unpack tar archive to a given target directory(targetDir)."""
|
"""Unpack tar archive to a given target directory(target_dir)."""
|
||||||
super(ArchiveTar, self).unpack(targetDir, cleanDir)
|
super(ArchiveTar, self).unpack(target_dir, clean_dir)
|
||||||
|
|
||||||
rmode = ""
|
rmode = ""
|
||||||
if self.type == 'tar':
|
if self.type == 'tar':
|
||||||
@@ -63,9 +63,9 @@ class ArchiveTar(ArchiveBase):
|
|||||||
else:
|
else:
|
||||||
raise ArchiveError("Archive type not recognized")
|
raise ArchiveError("Archive type not recognized")
|
||||||
|
|
||||||
tar = tarfile.open(self.filePath, rmode)
|
tar = tarfile.open(self.file_path, rmode)
|
||||||
oldwd = os.getcwd()
|
oldwd = os.getcwd()
|
||||||
os.chdir(self.targetDir)
|
os.chdir(self.target_dir)
|
||||||
for tarinfo in tar:
|
for tarinfo in tar:
|
||||||
tar.extract(tarinfo)
|
tar.extract(tarinfo)
|
||||||
os.chdir(oldwd)
|
os.chdir(oldwd)
|
||||||
@@ -81,79 +81,79 @@ class ArchiveZip(ArchiveBase):
|
|||||||
|
|
||||||
symmagic = 2716663808 #long of hex val '0xA1ED0000L'
|
symmagic = 2716663808 #long of hex val '0xA1ED0000L'
|
||||||
|
|
||||||
def __init__(self, filepath, ArchType = "zip", mode = 'r'):
|
def __init__(self, file_path, arch_type = "zip", mode = 'r'):
|
||||||
super(ArchiveZip, self).__init__(filepath, ArchType)
|
super(ArchiveZip, self).__init__(file_path, arch_type)
|
||||||
|
|
||||||
self.zipObj = zipfile.ZipFile(self.filePath, mode)
|
self.zip_obj = zipfile.ZipFile(self.file_path, mode)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Close the zip archive."""
|
"""Close the zip archive."""
|
||||||
self.zipObj.close()
|
self.zip_obj.close()
|
||||||
|
|
||||||
def add_to_archive(self, fileName):
|
def add_to_archive(self, file_name):
|
||||||
"""Add file or directory path to the zip file"""
|
"""Add file or directory path to the zip file"""
|
||||||
# It's a pity that zipfile can't handle unicode strings. Grrr!
|
# It's a pity that zipfile can't handle unicode strings. Grrr!
|
||||||
fileName = str(fileName)
|
file_name = str(file_name)
|
||||||
if os.path.isdir(fileName) and not os.path.islink(fileName):
|
if os.path.isdir(file_name) and not os.path.islink(file_name):
|
||||||
self.zipObj.writestr(fileName + '/', '')
|
self.zip_obj.writestr(file_name + '/', '')
|
||||||
for f in os.listdir(fileName):
|
for f in os.listdir(file_name):
|
||||||
self.add_to_archive(os.path.join(fileName, f))
|
self.add_to_archive(os.path.join(file_name, f))
|
||||||
else:
|
else:
|
||||||
if os.path.islink(fileName):
|
if os.path.islink(file_name):
|
||||||
dest = os.readlink(fileName)
|
dest = os.readlink(file_name)
|
||||||
attr = zipfile.ZipInfo()
|
attr = zipfile.ZipInfo()
|
||||||
attr.filename = fileName
|
attr.filename = file_name
|
||||||
attr.create_system = 3
|
attr.create_system = 3
|
||||||
attr.external_attr = self.symmagic
|
attr.external_attr = self.symmagic
|
||||||
self.zipObj.writestr(attr, dest)
|
self.zip_obj.writestr(attr, dest)
|
||||||
else:
|
else:
|
||||||
self.zipObj.write(fileName, fileName, zipfile.ZIP_DEFLATED)
|
self.zip_obj.write(file_name, file_name, zipfile.ZIP_DEFLATED)
|
||||||
|
|
||||||
def add_basename_to_archive(self, fileName):
|
def add_basename_to_archive(self, file_name):
|
||||||
"""Add only the basepath to the zip file. For example; if the given
|
"""Add only the basepath to the zip file. For example; if the given
|
||||||
fileName parameter is /usr/local/bin/somedir, this function
|
file_name parameter is /usr/local/bin/somedir, this function
|
||||||
will create only the base directory/file somedir in the
|
will create only the base directory/file somedir in the
|
||||||
archive."""
|
archive."""
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
pathName = os.path.dirname(fileName)
|
path_name = os.path.dirname(file_name)
|
||||||
fileName = os.path.basename(fileName)
|
file_name = os.path.basename(file_name)
|
||||||
if pathName:
|
if path_name:
|
||||||
os.chdir(pathName)
|
os.chdir(path_name)
|
||||||
self.add_to_archive(fileName)
|
self.add_to_archive(file_name)
|
||||||
os.chdir(cwd)
|
os.chdir(cwd)
|
||||||
|
|
||||||
def unpack_file_cond(self, pred, targetDir, archiveRoot = ''):
|
def unpack_file_cond(self, pred, target_dir, archive_root = ''):
|
||||||
"""Unpack/Extract a file according to predicate function filename ->
|
"""Unpack/Extract a file according to predicate function filename ->
|
||||||
bool"""
|
bool"""
|
||||||
zipObj = self.zipObj
|
zip_obj = self.zip_obj
|
||||||
for info in zipObj.infolist():
|
for info in zip_obj.infolist():
|
||||||
if pred(info.filename): # check if condition holds
|
if pred(info.filename): # check if condition holds
|
||||||
|
|
||||||
# below code removes that, so we find it here
|
# below code removes that, so we find it here
|
||||||
isDir = info.filename.endswith('/')
|
is_dir = info.filename.endswith('/')
|
||||||
|
|
||||||
# calculate output file name
|
# calculate output file name
|
||||||
if archiveRoot == '':
|
if archive_root == '':
|
||||||
outpath = info.filename
|
outpath = info.filename
|
||||||
else:
|
else:
|
||||||
# change archiveRoot
|
# change archive_root
|
||||||
if util.subpath(archiveRoot, info.filename):
|
if util.subpath(archive_root, info.filename):
|
||||||
outpath = util.removepathprefix(archiveRoot,
|
outpath = util.removepathprefix(archive_root,
|
||||||
info.filename)
|
info.filename)
|
||||||
else:
|
else:
|
||||||
continue # don't extract if not under
|
continue # don't extract if not under
|
||||||
|
|
||||||
ofile = os.path.join(targetDir, outpath)
|
ofile = os.path.join(target_dir, outpath)
|
||||||
|
|
||||||
if isDir: # a directory is present.
|
if is_dir: # a directory is present.
|
||||||
os.mkdir(os.path.join(targetDir, info.filename))
|
os.mkdir(os.path.join(target_dir, info.filename))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# check that output dir is present
|
# check that output dir is present
|
||||||
util.check_dir(os.path.dirname(ofile))
|
util.check_dir(os.path.dirname(ofile))
|
||||||
|
|
||||||
if info.external_attr == self.symmagic:
|
if info.external_attr == self.symmagic:
|
||||||
target = zipObj.read(info.filename)
|
target = zip_obj.read(info.filename)
|
||||||
if os.path.exists(ofile):
|
if os.path.exists(ofile):
|
||||||
os.remove(ofile)
|
os.remove(ofile)
|
||||||
os.symlink(target, ofile)
|
os.symlink(target, ofile)
|
||||||
@@ -163,24 +163,24 @@ class ArchiveZip(ArchiveBase):
|
|||||||
perm >>= 16
|
perm >>= 16
|
||||||
perm |= 0x00000100
|
perm |= 0x00000100
|
||||||
buff = open (ofile, 'wb')
|
buff = open (ofile, 'wb')
|
||||||
fileContent = zipObj.read(info.filename)
|
file_content = zip_obj.read(info.filename)
|
||||||
buff.write(fileContent)
|
buff.write(file_content)
|
||||||
buff.close()
|
buff.close()
|
||||||
os.chmod(ofile, perm)
|
os.chmod(ofile, perm)
|
||||||
|
|
||||||
def unpack_files(self, paths, targetDir):
|
def unpack_files(self, paths, target_dir):
|
||||||
self.unpack_file_cond(lambda f:f in paths, targetDir)
|
self.unpack_file_cond(lambda f:f in paths, target_dir)
|
||||||
|
|
||||||
def unpack_dir(self, path, targetDir):
|
def unpack_dir(self, path, target_dir):
|
||||||
self.unpack_file_cond(lambda f:util.subpath(path, f), targetDir)
|
self.unpack_file_cond(lambda f:util.subpath(path, f), target_dir)
|
||||||
|
|
||||||
def unpack_dir_flat(self, path, targetDir):
|
def unpack_dir_flat(self, path, target_dir):
|
||||||
self.unpack_file_cond(lambda f:util.subpath(path, f), targetDir, path)
|
self.unpack_file_cond(lambda f:util.subpath(path, f), target_dir, path)
|
||||||
|
|
||||||
def unpack(self, targetDir, cleanDir=False):
|
def unpack(self, target_dir, clean_dir=False):
|
||||||
super(ArchiveZip, self).unpack(targetDir, cleanDir)
|
super(ArchiveZip, self).unpack(target_dir, clean_dir)
|
||||||
|
|
||||||
self.unpack_file_cond(lambda f: True, targetDir)
|
self.unpack_file_cond(lambda f: True, target_dir)
|
||||||
self.close()
|
self.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -189,7 +189,7 @@ class Archive:
|
|||||||
"""Archive is the main factory for ArchiveClasses, regarding the
|
"""Archive is the main factory for ArchiveClasses, regarding the
|
||||||
Abstract Factory Pattern :)."""
|
Abstract Factory Pattern :)."""
|
||||||
|
|
||||||
def __init__(self, filepath, ArchType):
|
def __init__(self, file_path, arch_type):
|
||||||
"""accepted archive types:
|
"""accepted archive types:
|
||||||
targz, tarbz2, zip, tar"""
|
targz, tarbz2, zip, tar"""
|
||||||
|
|
||||||
@@ -200,10 +200,10 @@ class Archive:
|
|||||||
'zip': ArchiveZip
|
'zip': ArchiveZip
|
||||||
}
|
}
|
||||||
|
|
||||||
self.archive = handlers.get(ArchType)(filepath, ArchType)
|
self.archive = handlers.get(arch_type)(file_path, arch_type)
|
||||||
|
|
||||||
def unpack(self, targetDir, cleanDir = False):
|
def unpack(self, target_dir, clean_dir = False):
|
||||||
self.archive.unpack(targetDir, cleanDir)
|
self.archive.unpack(target_dir, clean_dir)
|
||||||
|
|
||||||
def unpack_files(self, files, targetDir):
|
def unpack_files(self, files, target_dir):
|
||||||
self.archive.unpack_files(files, targetDir)
|
self.archive.unpack_files(files, target_dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user