Commit aede664b7c2985a4e309a8f20209eb636c383dc2
1 parent
f75482f1
make file existence check in xglob compatible with glob
Showing
1 changed file
with
37 additions
and
1 deletions
oletools/thirdparty/xglob/xglob.py
| ... | ... | @@ -131,7 +131,7 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'): |
| 131 | 131 | else: |
| 132 | 132 | iglob = glob.iglob |
| 133 | 133 | for filespec in files: |
| 134 | - if not os.path.exists(filespec): | |
| 134 | + if not is_glob(filespec) and not os.path.exists(filespec): | |
| 135 | 135 | raise ValueError('given path {} does not exist!'.format(filespec)) |
| 136 | 136 | for filename in iglob(filespec): |
| 137 | 137 | if zip_password is not None: |
| ... | ... | @@ -155,3 +155,39 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'): |
| 155 | 155 | #data = open(filename, 'rb').read() |
| 156 | 156 | #yield None, filename, data |
| 157 | 157 | |
| 158 | + | |
| 159 | +def is_glob(filespec): | |
| 160 | + """ determine if given file specification is a single file name or a glob | |
| 161 | + | |
| 162 | + python's glob and fnmatch can only interpret ?, *, [list], and [ra-nge], | |
| 163 | + (and combinations: hex_*_[A-Fabcdef0-9]). | |
| 164 | + The special chars *?[-] can only be escaped using [] | |
| 165 | + --> file_name is not a glob | |
| 166 | + --> file?name is a glob | |
| 167 | + --> file* is a glob | |
| 168 | + --> file[-._]name is a glob | |
| 169 | + --> file[?]name is not a glob (matches literal "file?name") | |
| 170 | + --> file[*]name is not a glob (matches literal "file*name") | |
| 171 | + --> file[-]name is not a glob (matches literal "file-name") | |
| 172 | + --> file-name is not a glob | |
| 173 | + | |
| 174 | + Also, obviously incorrect globs are treated as non-globs | |
| 175 | + --> file[name is not a glob (matches literal "file[name") | |
| 176 | + --> file]-[name is treated as a glob | |
| 177 | + (it is not a valid glob but detecting errors like this requires | |
| 178 | + sophisticated regular expression matching) | |
| 179 | + | |
| 180 | + Python's glob also works with globs in directory-part of path | |
| 181 | + --> dir-part of path is analyzed just like filename-part | |
| 182 | + --> thirdparty/*/xglob.py is a (valid) glob | |
| 183 | + | |
| 184 | + TODO: create a correct regexp to test for validity of ranges | |
| 185 | + """ | |
| 186 | + | |
| 187 | + # remove escaped special chars | |
| 188 | + cleaned = filespec.replace('[*]', '').replace('[?]', '') \ | |
| 189 | + .replace('[[]', '').replace('[]]', '').replace('[-]', '') | |
| 190 | + | |
| 191 | + # check if special chars remain | |
| 192 | + return '*' in cleaned or '?' in cleaned or \ | |
| 193 | + ('[' in cleaned and ']' in cleaned) | ... | ... |