Commit 1feffdc3433c30d7a2cc567ae9fbb4a1abdf40c2

Authored by decalage2
1 parent 35ac1dbc

oleobj: fixed absolute imports (issue #141)

Showing 2 changed files with 18 additions and 5 deletions
oletools/oleobj.py
@@ -15,7 +15,7 @@ http://www.decalage.info/python/oletools @@ -15,7 +15,7 @@ http://www.decalage.info/python/oletools
15 15
16 # === LICENSE ================================================================== 16 # === LICENSE ==================================================================
17 17
18 -# oleobj is copyright (c) 2015-2016 Philippe Lagadec (http://www.decalage.info) 18 +# oleobj is copyright (c) 2015-2017 Philippe Lagadec (http://www.decalage.info)
19 # All rights reserved. 19 # All rights reserved.
20 # 20 #
21 # Redistribution and use in source and binary forms, with or without modification, 21 # Redistribution and use in source and binary forms, with or without modification,
@@ -47,8 +47,9 @@ http://www.decalage.info/python/oletools @@ -47,8 +47,9 @@ http://www.decalage.info/python/oletools
47 # 2016-07-19 PL: - fixed Python 2.6-7 support 47 # 2016-07-19 PL: - fixed Python 2.6-7 support
48 # 2016-11-17 v0.51 PL: - fixed OLE native object extraction 48 # 2016-11-17 v0.51 PL: - fixed OLE native object extraction
49 # 2016-11-18 PL: - added main for setup.py entry point 49 # 2016-11-18 PL: - added main for setup.py entry point
  50 +# 2017-05-03 PL: - fixed absolute imports (issue #141)
50 51
51 -__version__ = '0.51' 52 +__version__ = '0.51dev7'
52 53
53 #------------------------------------------------------------------------------ 54 #------------------------------------------------------------------------------
54 # TODO: 55 # TODO:
@@ -70,8 +71,20 @@ __version__ = '0.51' @@ -70,8 +71,20 @@ __version__ = '0.51'
70 71
71 import logging, struct, optparse, os, re, sys 72 import logging, struct, optparse, os, re, sys
72 73
73 -from .thirdparty.olefile import olefile  
74 -from .thirdparty.xglob import xglob 74 +# IMPORTANT: it should be possible to run oletools directly as scripts
  75 +# in any directory without installing them with pip or setup.py.
  76 +# In that case, relative imports are NOT usable.
  77 +# And to enable Python 2+3 compatibility, we need to use absolute imports,
  78 +# so we add the oletools parent folder to sys.path (absolute+normalized path):
  79 +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  80 +# print('_thismodule_dir = %r' % _thismodule_dir)
  81 +_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..'))
  82 +# print('_parent_dir = %r' % _thirdparty_dir)
  83 +if not _parent_dir in sys.path:
  84 + sys.path.insert(0, _parent_dir)
  85 +
  86 +from oletools.thirdparty.olefile import olefile
  87 +from oletools.thirdparty.xglob import xglob
75 88
76 # === LOGGING ================================================================= 89 # === LOGGING =================================================================
77 90
setup.py
@@ -41,7 +41,7 @@ import os, fnmatch @@ -41,7 +41,7 @@ import os, fnmatch
41 #--- METADATA ----------------------------------------------------------------- 41 #--- METADATA -----------------------------------------------------------------
42 42
43 name = "oletools" 43 name = "oletools"
44 -version = '0.51dev6' 44 +version = '0.51dev7'
45 desc = "Python tools to analyze security characteristics of MS Office and OLE files (also called Structured Storage, Compound File Binary Format or Compound Document File Format), for Malware Analysis and Incident Response #DFIR" 45 desc = "Python tools to analyze security characteristics of MS Office and OLE files (also called Structured Storage, Compound File Binary Format or Compound Document File Format), for Malware Analysis and Incident Response #DFIR"
46 long_desc = open('oletools/README.rst').read() 46 long_desc = open('oletools/README.rst').read()
47 author = "Philippe Lagadec" 47 author = "Philippe Lagadec"