Commit 31479934db979a49b440b122dde7c5735622f902

Authored by decalage2
1 parent 65449b94

mraptor: fixed absolute imports

oletools/mraptor.py
... ... @@ -22,7 +22,7 @@ http://www.decalage.info/python/oletools
22 22  
23 23 # === LICENSE ==================================================================
24 24  
25   -# MacroRaptor is copyright (c) 2016 Philippe Lagadec (http://www.decalage.info)
  25 +# MacroRaptor is copyright (c) 2016-2017 Philippe Lagadec (http://www.decalage.info)
26 26 # All rights reserved.
27 27 #
28 28 # Redistribution and use in source and binary forms, with or without modification,
... ... @@ -55,8 +55,9 @@ http://www.decalage.info/python/oletools
55 55 # 2016-09-05 PL: - added Document_BeforeClose keyword for MS Publisher (.pub)
56 56 # 2016-10-25 PL: - fixed print for Python 3
57 57 # 2016-12-21 v0.51 PL: - added more ActiveX macro triggers
  58 +# 2017-03-08 PL: - fixed absolute imports
58 59  
59   -__version__ = '0.51'
  60 +__version__ = '0.51dev2'
60 61  
61 62 #------------------------------------------------------------------------------
62 63 # TODO:
... ... @@ -64,12 +65,24 @@ __version__ = '0.51'
64 65  
65 66 #--- IMPORTS ------------------------------------------------------------------
66 67  
67   -import sys, logging, optparse, re
  68 +import sys, logging, optparse, re, os
68 69  
69   -from .thirdparty.xglob import xglob
70   -from .thirdparty.tablestream import tablestream
  70 +# IMPORTANT: it should be possible to run oletools directly as scripts
  71 +# in any directory without installing them with pip or setup.py.
  72 +# In that case, relative imports are NOT usable.
  73 +# And to enable Python 2+3 compatibility, we need to use absolute imports,
  74 +# so we add the oletools parent folder to sys.path (absolute+normalized path):
  75 +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  76 +# print('_thismodule_dir = %r' % _thismodule_dir)
  77 +_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..'))
  78 +# print('_parent_dir = %r' % _thirdparty_dir)
  79 +if not _parent_dir in sys.path:
  80 + sys.path.insert(0, _parent_dir)
71 81  
72   -import olevba
  82 +from oletools.thirdparty.xglob import xglob
  83 +from oletools.thirdparty.tablestream import tablestream
  84 +
  85 +from oletools import olevba
73 86  
74 87 # === LOGGING =================================================================
75 88  
... ... @@ -228,7 +241,7 @@ def main():
228 241 'critical': logging.CRITICAL
229 242 }
230 243  
231   - usage = 'usage: %prog [options] <filename> [filename2 ...]'
  244 + usage = 'usage: mraptor [options] <filename> [filename2 ...]'
232 245 parser = optparse.OptionParser(usage=usage)
233 246 parser.add_option("-r", action="store_true", dest="recursive",
234 247 help='find files recursively in subdirectories.')
... ... @@ -247,6 +260,8 @@ def main():
247 260  
248 261 # Print help if no arguments are passed
249 262 if len(args) == 0:
  263 + print('MacroRaptor %s - http://decalage.info/python/oletools' % __version__)
  264 + print('This is work in progress, please report issues at %s' % URL_ISSUES)
250 265 print(__doc__)
251 266 parser.print_help()
252 267 print('\nAn exit code is returned based on the analysis result:')
... ...
oletools/olevba.py
... ... @@ -270,6 +270,7 @@ _parent_dir = os.path.normpath(os.path.join(_thismodule_dir, &#39;..&#39;))
270 270 # print('_parent_dir = %r' % _thirdparty_dir)
271 271 if not _parent_dir in sys.path:
272 272 sys.path.insert(0, _parent_dir)
  273 +
273 274 from oletools.thirdparty import olefile
274 275 from oletools.thirdparty.prettytable import prettytable
275 276 from oletools.thirdparty.xglob import xglob, PathNotFoundException
... ... @@ -3243,7 +3244,7 @@ def main():
3243 3244 'critical': logging.CRITICAL
3244 3245 }
3245 3246  
3246   - usage = 'usage: %prog [options] <filename> [filename2 ...]'
  3247 + usage = 'usage: olevba [options] <filename> [filename2 ...]'
3247 3248 parser = optparse.OptionParser(usage=usage)
3248 3249 # parser.add_option('-o', '--outfile', dest='outfile',
3249 3250 # help='output file')
... ...