Commit 76616bb71789c66fd1ae0bfd1a6d24be9da3fe5f

Authored by Philippe Lagadec
1 parent d2cb1b4a

xglob: do not stop on exceptions, return them as data - fixed issue when using …

…wildcards with empty path
oletools/thirdparty/xglob/xglob.py
@@ -20,7 +20,7 @@ For more info and updates: http://www.decalage.info/xglob @@ -20,7 +20,7 @@ For more info and updates: http://www.decalage.info/xglob
20 20
21 # LICENSE: 21 # LICENSE:
22 # 22 #
23 -# xglob is copyright (c) 2013-2015, Philippe Lagadec (http://www.decalage.info) 23 +# xglob is copyright (c) 2013-2016, Philippe Lagadec (http://www.decalage.info)
24 # All rights reserved. 24 # All rights reserved.
25 # 25 #
26 # Redistribution and use in source and binary forms, with or without modification, 26 # Redistribution and use in source and binary forms, with or without modification,
@@ -50,8 +50,10 @@ For more info and updates: http://www.decalage.info/xglob @@ -50,8 +50,10 @@ For more info and updates: http://www.decalage.info/xglob
50 # 2014-01-14 v0.02 PL: - added riglob, ziglob 50 # 2014-01-14 v0.02 PL: - added riglob, ziglob
51 # 2014-12-26 v0.03 PL: - moved code from balbuzard into a separate package 51 # 2014-12-26 v0.03 PL: - moved code from balbuzard into a separate package
52 # 2015-01-03 v0.04 PL: - fixed issues in iter_files + yield container name 52 # 2015-01-03 v0.04 PL: - fixed issues in iter_files + yield container name
  53 +# 2016-02-24 v0.05 PL: - do not stop on exceptions, return them as data
  54 +# - fixed issue when using wildcards with empty path
53 55
54 -__version__ = '0.04' 56 +__version__ = '0.05'
55 57
56 58
57 #=== IMPORTS ================================================================= 59 #=== IMPORTS =================================================================
@@ -83,6 +85,10 @@ def riglob (pathname): @@ -83,6 +85,10 @@ def riglob (pathname):
83 filenames, using wildcards, e.g. *.txt 85 filenames, using wildcards, e.g. *.txt
84 """ 86 """
85 path, filespec = os.path.split(pathname) 87 path, filespec = os.path.split(pathname)
  88 + # fix path if empty:
  89 + if path == '':
  90 + path = '.'
  91 + # print 'riglob: path=%r, filespec=%r' % (path, filespec)
86 for dirpath, dirnames, files in os.walk(path): 92 for dirpath, dirnames, files in os.walk(path):
87 for f in fnmatch.filter(files, filespec): 93 for f in fnmatch.filter(files, filespec):
88 yield os.path.join(dirpath, f) 94 yield os.path.join(dirpath, f)
@@ -118,6 +124,7 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'): @@ -118,6 +124,7 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'):
118 #TODO: catch exceptions and yield them for the caller (no file found, file is not zip, wrong password, etc) 124 #TODO: catch exceptions and yield them for the caller (no file found, file is not zip, wrong password, etc)
119 #TODO: use logging instead of printing 125 #TODO: use logging instead of printing
120 #TODO: split in two simpler functions, the caller knows if it's a zip or not 126 #TODO: split in two simpler functions, the caller knows if it's a zip or not
  127 + # print 'iter_files: files=%r, recursive=%s' % (files, recursive)
121 # choose recursive or non-recursive iglob: 128 # choose recursive or non-recursive iglob:
122 if recursive: 129 if recursive:
123 iglob = riglob 130 iglob = riglob
@@ -132,8 +139,11 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'): @@ -132,8 +139,11 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'):
132 #print 'Looking for file(s) matching "%s"' % zip_fname 139 #print 'Looking for file(s) matching "%s"' % zip_fname
133 for subfilename in ziglob(z, zip_fname): 140 for subfilename in ziglob(z, zip_fname):
134 #print 'Opening file in zip archive:', filename 141 #print 'Opening file in zip archive:', filename
135 - data = z.read(subfilename, zip_password)  
136 - yield filename, subfilename, data 142 + try:
  143 + data = z.read(subfilename, zip_password)
  144 + yield filename, subfilename, data
  145 + except Exception as e:
  146 + yield filename, subfilename, e
137 z.close() 147 z.close()
138 else: 148 else:
139 # normal file 149 # normal file