diff --git a/pisi/archive.py b/pisi/archive.py index dcd3aa6b..3a5c8f7c 100644 --- a/pisi/archive.py +++ b/pisi/archive.py @@ -28,17 +28,17 @@ class ArchiveError(pisi.Error): class ArchiveBase(object): """Base class for Archive classes.""" - def __init__(self, filepath, atype): - self.filePath = filepath + def __init__(self, file_path, atype): + self.file_path = file_path self.type = atype - def unpack(self, targetDir, cleanDir = False): - self.targetDir = targetDir + def unpack(self, target_dir, clean_dir = False): + self.target_dir = target_dir # first we check if we need to clean-up our working env. - if os.path.exists(self.targetDir) and cleanDir: - util.clean_dir(self.targetDir) + if os.path.exists(self.target_dir) and clean_dir: + util.clean_dir(self.target_dir) - os.makedirs(self.targetDir) + os.makedirs(self.target_dir) class ArchiveTar(ArchiveBase): @@ -46,12 +46,12 @@ class ArchiveTar(ArchiveBase): type. Provides access to tar, tar.gz and tar.bz2 files. This class provides the unpack magic for tar archives.""" - def __init__(self, filepath, ArchType = "tar"): - super(ArchiveTar, self).__init__(filepath, ArchType) + def __init__(self, file_path, arch_type = "tar"): + super(ArchiveTar, self).__init__(file_path, arch_type) - def unpack(self, targetDir, cleanDir = False): - """Unpack tar archive to a given target directory(targetDir).""" - super(ArchiveTar, self).unpack(targetDir, cleanDir) + def unpack(self, target_dir, clean_dir = False): + """Unpack tar archive to a given target directory(target_dir).""" + super(ArchiveTar, self).unpack(target_dir, clean_dir) rmode = "" if self.type == 'tar': @@ -63,9 +63,9 @@ class ArchiveTar(ArchiveBase): else: raise ArchiveError("Archive type not recognized") - tar = tarfile.open(self.filePath, rmode) + tar = tarfile.open(self.file_path, rmode) oldwd = os.getcwd() - os.chdir(self.targetDir) + os.chdir(self.target_dir) for tarinfo in tar: tar.extract(tarinfo) os.chdir(oldwd) @@ -81,79 +81,79 @@ class ArchiveZip(ArchiveBase): symmagic = 2716663808 #long of hex val '0xA1ED0000L' - def __init__(self, filepath, ArchType = "zip", mode = 'r'): - super(ArchiveZip, self).__init__(filepath, ArchType) + def __init__(self, file_path, arch_type = "zip", mode = 'r'): + 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): """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""" # It's a pity that zipfile can't handle unicode strings. Grrr! - fileName = str(fileName) - if os.path.isdir(fileName) and not os.path.islink(fileName): - self.zipObj.writestr(fileName + '/', '') - for f in os.listdir(fileName): - self.add_to_archive(os.path.join(fileName, f)) + file_name = str(file_name) + if os.path.isdir(file_name) and not os.path.islink(file_name): + self.zip_obj.writestr(file_name + '/', '') + for f in os.listdir(file_name): + self.add_to_archive(os.path.join(file_name, f)) else: - if os.path.islink(fileName): - dest = os.readlink(fileName) + if os.path.islink(file_name): + dest = os.readlink(file_name) attr = zipfile.ZipInfo() - attr.filename = fileName + attr.filename = file_name attr.create_system = 3 attr.external_attr = self.symmagic - self.zipObj.writestr(attr, dest) + self.zip_obj.writestr(attr, dest) 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 - 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 archive.""" cwd = os.getcwd() - pathName = os.path.dirname(fileName) - fileName = os.path.basename(fileName) - if pathName: - os.chdir(pathName) - self.add_to_archive(fileName) + path_name = os.path.dirname(file_name) + file_name = os.path.basename(file_name) + if path_name: + os.chdir(path_name) + self.add_to_archive(file_name) 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 -> bool""" - zipObj = self.zipObj - for info in zipObj.infolist(): + zip_obj = self.zip_obj + for info in zip_obj.infolist(): if pred(info.filename): # check if condition holds # below code removes that, so we find it here - isDir = info.filename.endswith('/') + is_dir = info.filename.endswith('/') # calculate output file name - if archiveRoot == '': + if archive_root == '': outpath = info.filename else: - # change archiveRoot - if util.subpath(archiveRoot, info.filename): - outpath = util.removepathprefix(archiveRoot, + # change archive_root + if util.subpath(archive_root, info.filename): + outpath = util.removepathprefix(archive_root, info.filename) else: 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. - os.mkdir(os.path.join(targetDir, info.filename)) + if is_dir: # a directory is present. + os.mkdir(os.path.join(target_dir, info.filename)) continue # check that output dir is present util.check_dir(os.path.dirname(ofile)) if info.external_attr == self.symmagic: - target = zipObj.read(info.filename) + target = zip_obj.read(info.filename) if os.path.exists(ofile): os.remove(ofile) os.symlink(target, ofile) @@ -163,24 +163,24 @@ class ArchiveZip(ArchiveBase): perm >>= 16 perm |= 0x00000100 buff = open (ofile, 'wb') - fileContent = zipObj.read(info.filename) - buff.write(fileContent) + file_content = zip_obj.read(info.filename) + buff.write(file_content) buff.close() os.chmod(ofile, perm) - def unpack_files(self, paths, targetDir): - self.unpack_file_cond(lambda f:f in paths, targetDir) + def unpack_files(self, paths, target_dir): + self.unpack_file_cond(lambda f:f in paths, target_dir) - def unpack_dir(self, path, targetDir): - self.unpack_file_cond(lambda f:util.subpath(path, f), targetDir) + def unpack_dir(self, path, target_dir): + self.unpack_file_cond(lambda f:util.subpath(path, f), target_dir) - def unpack_dir_flat(self, path, targetDir): - self.unpack_file_cond(lambda f:util.subpath(path, f), targetDir, path) + def unpack_dir_flat(self, path, target_dir): + self.unpack_file_cond(lambda f:util.subpath(path, f), target_dir, path) - def unpack(self, targetDir, cleanDir=False): - super(ArchiveZip, self).unpack(targetDir, cleanDir) + def unpack(self, target_dir, clean_dir=False): + 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() return @@ -189,7 +189,7 @@ class Archive: """Archive is the main factory for ArchiveClasses, regarding the Abstract Factory Pattern :).""" - def __init__(self, filepath, ArchType): + def __init__(self, file_path, arch_type): """accepted archive types: targz, tarbz2, zip, tar""" @@ -200,10 +200,10 @@ class Archive: '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): - self.archive.unpack(targetDir, cleanDir) + def unpack(self, target_dir, clean_dir = False): + self.archive.unpack(target_dir, clean_dir) - def unpack_files(self, files, targetDir): - self.archive.unpack_files(files, targetDir) + def unpack_files(self, files, target_dir): + self.archive.unpack_files(files, target_dir)