* yeni path fonksiyonlari implement et

* archiveRoot'u implement et unpack_file_cond() icinde
This commit is contained in:
Eray Özkural
2005-06-21 09:42:36 +00:00
parent 544f2f598d
commit 82d7d0840f
2 changed files with 19 additions and 7 deletions
+8 -5
View File
@@ -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))
+11 -2
View File
@@ -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):