From 82d7d0840f0ecefa8cb8b0fb55e01224e4518709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Tue, 21 Jun 2005 09:42:36 +0000 Subject: [PATCH] * yeni path fonksiyonlari implement et * archiveRoot'u implement et unpack_file_cond() icinde --- pisi/archive.py | 13 ++++++++----- pisi/util.py | 13 +++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pisi/archive.py b/pisi/archive.py index 75e7720f..29f6476d 100644 --- a/pisi/archive.py +++ b/pisi/archive.py @@ -57,17 +57,20 @@ class ArchiveZip(ArchiveBase): def __init__(self, filepath, type="zip"): super(ArchiveZip, self).__init__(filepath, type) - def unpack_file_cond(self, pred, targetDir, extractRoot=''): + def unpack_file_cond(self, pred, targetDir, archiveRoot=''): """ unpack file according to predicate function filename -> bool""" super(ArchiveTarFile, self).unpack(targetDir) zip = zipfile.ZipFile(self.filePath, 'r') for file in zip.namelist(): if pred(file): # check if condition holds - if extractRoot=='': - ofile = os.path.join(targetDir, file) - else: - raise ArchiveError("changing extraction root not implemented yet") + if archiveRoot!='': + # change archiveRoot + if util.subpath(archiveRoot, file): + file = util.removepathprefix(archiveRoot, file) + else: + continue # don't extract if not under + ofile = os.path.join(targetDir, file) if os.path.isdir(ofile): continue util.check_dir(os.path.dirname(ofile)) diff --git a/pisi/util.py b/pisi/util.py index c49b70a1..b5832671 100644 --- a/pisi/util.py +++ b/pisi/util.py @@ -41,6 +41,11 @@ def prefix(a, b): if a[i]!=b[i]: return False return True + +def remove_prefix(a,b): + "remove prefix a from sequence b" + assert prefix(a,b) + return b[len(a):] # path functions @@ -57,10 +62,14 @@ def commonprefix(l): return common # but this one is necessary -def under_path(a, b): - """find if path a is a descendant of b in the directory tree""" +def subpath(a, b): + """find if path a is before b in the directory tree""" return prefix(os.path.split(a), os.path.split(b)) +def removepathprefix(a, b): + comps = remove_prefix(os.path.split(a), os.path.split(b)) + return reduce(os.path.join, comps) + # file/dir functions def check_file(file, mode = os.F_OK):