Commit cd61e4ff9d0089e037ef4f1b1c6f23d8562ea722

Authored by decalage2
1 parent 458e9119

olevba: fixed absolute imports

Showing 1 changed file with 22 additions and 7 deletions
oletools/olevba.py
@@ -190,8 +190,9 @@ from __future__ import print_function @@ -190,8 +190,9 @@ from __future__ import print_function
190 # 2016-11-03 v0.51 PL: - added EnumDateFormats and EnumSystemLanguageGroupsW 190 # 2016-11-03 v0.51 PL: - added EnumDateFormats and EnumSystemLanguageGroupsW
191 # 2017-02-07 PL: - temporary fix for issue #132 191 # 2017-02-07 PL: - temporary fix for issue #132
192 # - added keywords for Mac-specific macros (issue #130) 192 # - added keywords for Mac-specific macros (issue #130)
  193 +# 2017-03-08 PL: - fixed absolute imports
193 194
194 -__version__ = '0.51dev1' 195 +__version__ = '0.51dev2'
195 196
196 #------------------------------------------------------------------------------ 197 #------------------------------------------------------------------------------
197 # TODO: 198 # TODO:
@@ -225,7 +226,9 @@ __version__ = '0.51dev1' @@ -225,7 +226,9 @@ __version__ = '0.51dev1'
225 226
226 #--- IMPORTS ------------------------------------------------------------------ 227 #--- IMPORTS ------------------------------------------------------------------
227 228
228 -import sys, logging 229 +import sys
  230 +import os
  231 +import logging
229 import struct 232 import struct
230 import cStringIO 233 import cStringIO
231 import math 234 import math
@@ -256,15 +259,26 @@ except ImportError: @@ -256,15 +259,26 @@ except ImportError:
256 + "see http://codespeak.net/lxml " \ 259 + "see http://codespeak.net/lxml " \
257 + "or http://effbot.org/zone/element-index.htm") 260 + "or http://effbot.org/zone/element-index.htm")
258 261
259 -from .thirdparty import olefile  
260 -from .thirdparty.prettytable import prettytable  
261 -from .thirdparty.xglob import xglob, PathNotFoundException  
262 -from .thirdparty.pyparsing.pyparsing import \ 262 +# IMPORTANT: it should be possible to run oletools directly as scripts
  263 +# in any directory without installing them with pip or setup.py.
  264 +# In that case, relative imports are NOT usable.
  265 +# And to enable Python 2+3 compatibility, we need to use absolute imports,
  266 +# so we add the oletools parent folder to sys.path (absolute+normalized path):
  267 +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  268 +# print('_thismodule_dir = %r' % _thismodule_dir)
  269 +_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..'))
  270 +# print('_parent_dir = %r' % _thirdparty_dir)
  271 +if not _parent_dir in sys.path:
  272 + pass #sys.path.insert(0, _parent_dir)
  273 +from oletools.thirdparty import olefile
  274 +from oletools.thirdparty.prettytable import prettytable
  275 +from oletools.thirdparty.xglob import xglob, PathNotFoundException
  276 +from oletools.thirdparty.pyparsing.pyparsing import \
263 CaselessKeyword, CaselessLiteral, Combine, Forward, Literal, \ 277 CaselessKeyword, CaselessLiteral, Combine, Forward, Literal, \
264 Optional, QuotedString,Regex, Suppress, Word, WordStart, \ 278 Optional, QuotedString,Regex, Suppress, Word, WordStart, \
265 alphanums, alphas, hexnums,nums, opAssoc, srange, \ 279 alphanums, alphas, hexnums,nums, opAssoc, srange, \
266 infixNotation, ParserElement 280 infixNotation, ParserElement
267 -from . import ppt_parser 281 +from oletools import ppt_parser
268 282
269 283
270 # monkeypatch email to fix issue #32: 284 # monkeypatch email to fix issue #32:
@@ -3275,6 +3289,7 @@ def main(): @@ -3275,6 +3289,7 @@ def main():
3275 3289
3276 # Print help if no arguments are passed 3290 # Print help if no arguments are passed
3277 if len(args) == 0: 3291 if len(args) == 0:
  3292 + print('olevba %s - http://decalage.info/python/oletools' % __version__)
3278 print(__doc__) 3293 print(__doc__)
3279 parser.print_help() 3294 parser.print_help()
3280 sys.exit(RETURN_WRONG_ARGS) 3295 sys.exit(RETURN_WRONG_ARGS)