Commit 66a9fd77b6d1c8e8799d6fc6dbf2d744d937bf08
1 parent
c16ca78e
olemeta: added optparse and xglob to process multiple files and zips (issue #141)
Showing
2 changed files
with
54 additions
and
12 deletions
oletools/olemeta.py
| @@ -48,19 +48,20 @@ http://www.decalage.info/python/oletools | @@ -48,19 +48,20 @@ http://www.decalage.info/python/oletools | ||
| 48 | # 2016-10-25 PL: - fixed print for Python 3 | 48 | # 2016-10-25 PL: - fixed print for Python 3 |
| 49 | # 2016-10-28 PL: - removed the UTF8 codec for console display | 49 | # 2016-10-28 PL: - removed the UTF8 codec for console display |
| 50 | # 2017-04-26 v0.51 PL: - fixed absolute imports (issue #141) | 50 | # 2017-04-26 v0.51 PL: - fixed absolute imports (issue #141) |
| 51 | +# 2017-05-04 PL: - added optparse and xglob (issue #141) | ||
| 51 | 52 | ||
| 52 | -__version__ = '0.51' | 53 | +__version__ = '0.51dev7' |
| 53 | 54 | ||
| 54 | #------------------------------------------------------------------------------ | 55 | #------------------------------------------------------------------------------ |
| 55 | # TODO: | 56 | # TODO: |
| 56 | -# + optparse | ||
| 57 | # + nicer output: table with fixed columns, datetime, etc | 57 | # + nicer output: table with fixed columns, datetime, etc |
| 58 | # + CSV output | 58 | # + CSV output |
| 59 | # + option to only show available properties (by default) | 59 | # + option to only show available properties (by default) |
| 60 | +# + display codepage names | ||
| 60 | 61 | ||
| 61 | #=== IMPORTS ================================================================= | 62 | #=== IMPORTS ================================================================= |
| 62 | 63 | ||
| 63 | -import sys, os, codecs | 64 | +import sys, os, optparse |
| 64 | 65 | ||
| 65 | # IMPORTANT: it should be possible to run oletools directly as scripts | 66 | # IMPORTANT: it should be possible to run oletools directly as scripts |
| 66 | # in any directory without installing them with pip or setup.py. | 67 | # in any directory without installing them with pip or setup.py. |
| @@ -75,17 +76,13 @@ if not _parent_dir in sys.path: | @@ -75,17 +76,13 @@ if not _parent_dir in sys.path: | ||
| 75 | sys.path.insert(0, _parent_dir) | 76 | sys.path.insert(0, _parent_dir) |
| 76 | 77 | ||
| 77 | from oletools.thirdparty import olefile | 78 | from oletools.thirdparty import olefile |
| 79 | +from oletools.thirdparty import xglob | ||
| 78 | from oletools.thirdparty.tablestream import tablestream | 80 | from oletools.thirdparty.tablestream import tablestream |
| 79 | 81 | ||
| 80 | 82 | ||
| 81 | #=== MAIN ================================================================= | 83 | #=== MAIN ================================================================= |
| 82 | 84 | ||
| 83 | -def main(): | ||
| 84 | - try: | ||
| 85 | - ole = olefile.OleFileIO(sys.argv[1]) | ||
| 86 | - except IndexError: | ||
| 87 | - sys.exit(__doc__) | ||
| 88 | - | 85 | +def process_ole(ole): |
| 89 | # parse and display metadata: | 86 | # parse and display metadata: |
| 90 | meta = ole.get_metadata() | 87 | meta = ole.get_metadata() |
| 91 | 88 | ||
| @@ -128,7 +125,53 @@ def main(): | @@ -128,7 +125,53 @@ def main(): | ||
| 128 | t.write_row([prop, value], colors=[None, 'yellow']) | 125 | t.write_row([prop, value], colors=[None, 'yellow']) |
| 129 | t.close() | 126 | t.close() |
| 130 | 127 | ||
| 131 | - ole.close() | 128 | + |
| 129 | +# === MAIN =================================================================== | ||
| 130 | + | ||
| 131 | +def main(): | ||
| 132 | + # print banner with version | ||
| 133 | + print('olemeta %s - http://decalage.info/python/oletools' % __version__) | ||
| 134 | + print ('THIS IS WORK IN PROGRESS - Check updates regularly!') | ||
| 135 | + print ('Please report any issue at https://github.com/decalage2/oletools/issues') | ||
| 136 | + | ||
| 137 | + usage = 'usage: olemeta [options] <filename> [filename2 ...]' | ||
| 138 | + parser = optparse.OptionParser(usage=usage) | ||
| 139 | + parser.add_option("-r", action="store_true", dest="recursive", | ||
| 140 | + help='find files recursively in subdirectories.') | ||
| 141 | + parser.add_option("-z", "--zip", dest='zip_password', type='str', default=None, | ||
| 142 | + help='if the file is a zip archive, open all files from it, using the provided password (requires Python 2.6+)') | ||
| 143 | + parser.add_option("-f", "--zipfname", dest='zip_fname', type='str', default='*', | ||
| 144 | + help='if the file is a zip archive, file(s) to be opened within the zip. Wildcards * and ? are supported. (default:*)') | ||
| 145 | + | ||
| 146 | + # TODO: add logfile option | ||
| 147 | + # parser.add_option('-l', '--loglevel', dest="loglevel", action="store", default=DEFAULT_LOG_LEVEL, | ||
| 148 | + # help="logging level debug/info/warning/error/critical (default=%default)") | ||
| 149 | + | ||
| 150 | + (options, args) = parser.parse_args() | ||
| 151 | + | ||
| 152 | + # Print help if no arguments are passed | ||
| 153 | + if len(args) == 0: | ||
| 154 | + print(__doc__) | ||
| 155 | + parser.print_help() | ||
| 156 | + sys.exit() | ||
| 157 | + | ||
| 158 | + for container, filename, data in xglob.iter_files(args, recursive=options.recursive, | ||
| 159 | + zip_password=options.zip_password, zip_fname=options.zip_fname): | ||
| 160 | + # TODO: handle xglob errors | ||
| 161 | + # ignore directory names stored in zip files: | ||
| 162 | + if container and filename.endswith('/'): | ||
| 163 | + continue | ||
| 164 | + full_name = '%s in %s' % (filename, container) if container else filename | ||
| 165 | + print("=" * 79) | ||
| 166 | + print('FILE: %s\n' % full_name) | ||
| 167 | + if data is not None: | ||
| 168 | + # data extracted from zip file | ||
| 169 | + ole = olefile.OleFileIO(data) | ||
| 170 | + else: | ||
| 171 | + # normal filename | ||
| 172 | + ole = olefile.OleFileIO(filename) | ||
| 173 | + process_ole(ole) | ||
| 174 | + ole.close() | ||
| 132 | 175 | ||
| 133 | if __name__ == '__main__': | 176 | if __name__ == '__main__': |
| 134 | main() | 177 | main() |
oletools/oletimes.py
| @@ -49,7 +49,7 @@ http://www.decalage.info/python/oletools | @@ -49,7 +49,7 @@ http://www.decalage.info/python/oletools | ||
| 49 | # 2016-07-20 v0.50 SL: - added Python 3 support | 49 | # 2016-07-20 v0.50 SL: - added Python 3 support |
| 50 | # 2016-09-05 PL: - added main entry point for setup.py | 50 | # 2016-09-05 PL: - added main entry point for setup.py |
| 51 | # 2017-05-03 v0.51 PL: - fixed absolute imports (issue #141) | 51 | # 2017-05-03 v0.51 PL: - fixed absolute imports (issue #141) |
| 52 | -# 2017-05-04 PL: - added optparse and xglob | 52 | +# 2017-05-04 PL: - added optparse and xglob (issue #141) |
| 53 | 53 | ||
| 54 | __version__ = '0.51dev7' | 54 | __version__ = '0.51dev7' |
| 55 | 55 | ||
| @@ -112,7 +112,6 @@ def main(): | @@ -112,7 +112,6 @@ def main(): | ||
| 112 | print('oletimes %s - http://decalage.info/python/oletools' % __version__) | 112 | print('oletimes %s - http://decalage.info/python/oletools' % __version__) |
| 113 | print ('THIS IS WORK IN PROGRESS - Check updates regularly!') | 113 | print ('THIS IS WORK IN PROGRESS - Check updates regularly!') |
| 114 | print ('Please report any issue at https://github.com/decalage2/oletools/issues') | 114 | print ('Please report any issue at https://github.com/decalage2/oletools/issues') |
| 115 | - print ('') | ||
| 116 | 115 | ||
| 117 | usage = 'usage: oletimes [options] <filename> [filename2 ...]' | 116 | usage = 'usage: oletimes [options] <filename> [filename2 ...]' |
| 118 | parser = optparse.OptionParser(usage=usage) | 117 | parser = optparse.OptionParser(usage=usage) |