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 20  
21 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 24 # All rights reserved.
25 25 #
26 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 50 # 2014-01-14 v0.02 PL: - added riglob, ziglob
51 51 # 2014-12-26 v0.03 PL: - moved code from balbuzard into a separate package
52 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 59 #=== IMPORTS =================================================================
... ... @@ -83,6 +85,10 @@ def riglob (pathname):
83 85 filenames, using wildcards, e.g. *.txt
84 86 """
85 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 92 for dirpath, dirnames, files in os.walk(path):
87 93 for f in fnmatch.filter(files, filespec):
88 94 yield os.path.join(dirpath, f)
... ... @@ -118,6 +124,7 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'):
118 124 #TODO: catch exceptions and yield them for the caller (no file found, file is not zip, wrong password, etc)
119 125 #TODO: use logging instead of printing
120 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 128 # choose recursive or non-recursive iglob:
122 129 if recursive:
123 130 iglob = riglob
... ... @@ -132,8 +139,11 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'):
132 139 #print 'Looking for file(s) matching "%s"' % zip_fname
133 140 for subfilename in ziglob(z, zip_fname):
134 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 147 z.close()
138 148 else:
139 149 # normal file
... ...