* yeni path fonksiyonlari implement et
* archiveRoot'u implement et unpack_file_cond() icinde
This commit is contained in:
+7
-4
@@ -57,17 +57,20 @@ 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, extractRoot=''):
|
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(ArchiveTarFile, self).unpack(targetDir)
|
super(ArchiveTarFile, 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
|
||||||
if extractRoot=='':
|
if archiveRoot!='':
|
||||||
ofile = os.path.join(targetDir, file)
|
# change archiveRoot
|
||||||
|
if util.subpath(archiveRoot, file):
|
||||||
|
file = util.removepathprefix(archiveRoot, file)
|
||||||
else:
|
else:
|
||||||
raise ArchiveError("changing extraction root not implemented yet")
|
continue # don't extract if not under
|
||||||
|
ofile = os.path.join(targetDir, file)
|
||||||
if os.path.isdir(ofile):
|
if os.path.isdir(ofile):
|
||||||
continue
|
continue
|
||||||
util.check_dir(os.path.dirname(ofile))
|
util.check_dir(os.path.dirname(ofile))
|
||||||
|
|||||||
+11
-2
@@ -42,6 +42,11 @@ def prefix(a, b):
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def remove_prefix(a,b):
|
||||||
|
"remove prefix a from sequence b"
|
||||||
|
assert prefix(a,b)
|
||||||
|
return b[len(a):]
|
||||||
|
|
||||||
# path functions
|
# path functions
|
||||||
|
|
||||||
# I'm not sure how necessary this is. Ahem.
|
# I'm not sure how necessary this is. Ahem.
|
||||||
@@ -57,10 +62,14 @@ def commonprefix(l):
|
|||||||
return common
|
return common
|
||||||
|
|
||||||
# but this one is necessary
|
# but this one is necessary
|
||||||
def under_path(a, b):
|
def subpath(a, b):
|
||||||
"""find if path a is a descendant of b in the directory tree"""
|
"""find if path a is before b in the directory tree"""
|
||||||
return prefix(os.path.split(a), os.path.split(b))
|
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
|
# file/dir functions
|
||||||
|
|
||||||
def check_file(file, mode = os.F_OK):
|
def check_file(file, mode = os.F_OK):
|
||||||
|
|||||||
Reference in New Issue
Block a user