Commit c5c1b504b367cba77807f840b3b0444838220a1b

Authored by decalage2
1 parent 6b091f95

olevba3: fixed absolute imports

Showing 1 changed file with 18 additions and 7 deletions
oletools/olevba3.py
@@ -26,7 +26,7 @@ https://github.com/unixfreak0037/officeparser @@ -26,7 +26,7 @@ https://github.com/unixfreak0037/officeparser
26 26
27 # === LICENSE ================================================================== 27 # === LICENSE ==================================================================
28 28
29 -# olevba is copyright (c) 2014-2016 Philippe Lagadec (http://www.decalage.info) 29 +# olevba is copyright (c) 2014-2017 Philippe Lagadec (http://www.decalage.info)
30 # All rights reserved. 30 # All rights reserved.
31 # 31 #
32 # Redistribution and use in source and binary forms, with or without modification, 32 # Redistribution and use in source and binary forms, with or without modification,
@@ -189,8 +189,9 @@ from __future__ import print_function @@ -189,8 +189,9 @@ from __future__ import print_function
189 # 2016-10-25 PL: - fixed raise and print statements for Python 3 189 # 2016-10-25 PL: - fixed raise and print statements for Python 3
190 # 2016-10-25 PL: - fixed regex bytes strings (PR/issue #100) 190 # 2016-10-25 PL: - fixed regex bytes strings (PR/issue #100)
191 # 2016-11-03 v0.51 PL: - added EnumDateFormats and EnumSystemLanguageGroupsW 191 # 2016-11-03 v0.51 PL: - added EnumDateFormats and EnumSystemLanguageGroupsW
  192 +# 2017-04-26 PL: - fixed absolute imports
192 193
193 -__version__ = '0.51a' 194 +__version__ = '0.51dev6'
194 195
195 #------------------------------------------------------------------------------ 196 #------------------------------------------------------------------------------
196 # TODO: 197 # TODO:
@@ -224,7 +225,7 @@ __version__ = '0.51a' @@ -224,7 +225,7 @@ __version__ = '0.51a'
224 225
225 #--- IMPORTS ------------------------------------------------------------------ 226 #--- IMPORTS ------------------------------------------------------------------
226 227
227 -import sys, logging 228 +import sys, logging, os
228 import struct 229 import struct
229 from _io import StringIO,BytesIO 230 from _io import StringIO,BytesIO
230 import math 231 import math
@@ -238,8 +239,6 @@ import email # for MHTML parsing @@ -238,8 +239,6 @@ import email # for MHTML parsing
238 import string # for printable 239 import string # for printable
239 import json # for json output mode (argument --json) 240 import json # for json output mode (argument --json)
240 241
241 -from pyparsing import ParserElement  
242 -  
243 # import lxml or ElementTree for XML parsing: 242 # import lxml or ElementTree for XML parsing:
244 try: 243 try:
245 # lxml: best performance for XML processing 244 # lxml: best performance for XML processing
@@ -257,14 +256,26 @@ except ImportError: @@ -257,14 +256,26 @@ except ImportError:
257 + "see http://codespeak.net/lxml " \ 256 + "see http://codespeak.net/lxml " \
258 + "or http://effbot.org/zone/element-index.htm") 257 + "or http://effbot.org/zone/element-index.htm")
259 258
260 -import oletools.thirdparty.olefile as olefile 259 +# IMPORTANT: it should be possible to run oletools directly as scripts
  260 +# in any directory without installing them with pip or setup.py.
  261 +# In that case, relative imports are NOT usable.
  262 +# And to enable Python 2+3 compatibility, we need to use absolute imports,
  263 +# so we add the oletools parent folder to sys.path (absolute+normalized path):
  264 +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  265 +# print('_thismodule_dir = %r' % _thismodule_dir)
  266 +_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..'))
  267 +# print('_parent_dir = %r' % _thirdparty_dir)
  268 +if not _parent_dir in sys.path:
  269 + sys.path.insert(0, _parent_dir)
  270 +
  271 +from oletools.thirdparty import olefile
261 from oletools.thirdparty.prettytable import prettytable 272 from oletools.thirdparty.prettytable import prettytable
262 from oletools.thirdparty.xglob import xglob, PathNotFoundException 273 from oletools.thirdparty.xglob import xglob, PathNotFoundException
263 from oletools.thirdparty.pyparsing.pyparsing import \ 274 from oletools.thirdparty.pyparsing.pyparsing import \
264 CaselessKeyword, CaselessLiteral, Combine, Forward, Literal, \ 275 CaselessKeyword, CaselessLiteral, Combine, Forward, Literal, \
265 Optional, QuotedString,Regex, Suppress, Word, WordStart, \ 276 Optional, QuotedString,Regex, Suppress, Word, WordStart, \
266 alphanums, alphas, hexnums,nums, opAssoc, srange, \ 277 alphanums, alphas, hexnums,nums, opAssoc, srange, \
267 - infixNotation 278 + infixNotation, ParserElement
268 import oletools.ppt_parser as ppt_parser 279 import oletools.ppt_parser as ppt_parser
269 280
270 # monkeypatch email to fix issue #32: 281 # monkeypatch email to fix issue #32: