Commit 51567c7861de4186cf6076b46776a4ca8e62fb03

Authored by decalage2
1 parent f4bb562c

oledir: fixed issue #77 (imports from thirdparty dir)

Showing 1 changed file with 33 additions and 12 deletions
oletools/oledir.py
@@ -43,8 +43,9 @@ http://www.decalage.info/python/oletools @@ -43,8 +43,9 @@ http://www.decalage.info/python/oletools
43 # 2015-04-17 v0.01 PL: - first version 43 # 2015-04-17 v0.01 PL: - first version
44 # 2015-04-21 v0.02 PL: - improved display with prettytable 44 # 2015-04-21 v0.02 PL: - improved display with prettytable
45 # 2016-01-13 v0.03 PL: - replaced prettytable by tablestream, added colors 45 # 2016-01-13 v0.03 PL: - replaced prettytable by tablestream, added colors
  46 +# 2016-08-09 v0.50 PL: - fixed issue #77 (imports from thirdparty dir)
46 47
47 -__version__ = '0.03' 48 +__version__ = '0.50'
48 49
49 #------------------------------------------------------------------------------ 50 #------------------------------------------------------------------------------
50 # TODO: 51 # TODO:
@@ -54,17 +55,27 @@ __version__ = '0.03' @@ -54,17 +55,27 @@ __version__ = '0.03'
54 # === IMPORTS ================================================================ 55 # === IMPORTS ================================================================
55 56
56 import sys, os 57 import sys, os
57 -from thirdparty.olefile import olefile  
58 -# from thirdparty.prettytable import prettytable  
59 -from thirdparty.tablestream import tablestream  
60 -from thirdparty.colorclass import colorclass  
61 58
  59 +# add the thirdparty subfolder to sys.path (absolute+normalized path):
  60 +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  61 +# print('_thismodule_dir = %r' % _thismodule_dir)
  62 +# assumption: the thirdparty dir is a subfolder:
  63 +_thirdparty_dir = os.path.normpath(os.path.join(_thismodule_dir, 'thirdparty'))
  64 +# print('_thirdparty_dir = %r' % _thirdparty_dir)
  65 +if not _thirdparty_dir in sys.path:
  66 + sys.path.insert(0, _thirdparty_dir)
62 67
63 -def sid_display(sid):  
64 - if sid == olefile.NOSTREAM:  
65 - return '-' #None  
66 - else:  
67 - return sid 68 +import colorclass
  69 +
  70 +# On Windows, colorclass needs to be enabled:
  71 +if os.name == 'nt':
  72 + colorclass.Windows.enable(auto_colors=True)
  73 +
  74 +import olefile
  75 +from tablestream import tablestream
  76 +
  77 +
  78 +# === CONSTANTS ==============================================================
68 79
69 STORAGE_NAMES = { 80 STORAGE_NAMES = {
70 olefile.STGTY_EMPTY: 'Empty', 81 olefile.STGTY_EMPTY: 'Empty',
@@ -90,11 +101,21 @@ STATUS_COLORS = { @@ -90,11 +101,21 @@ STATUS_COLORS = {
90 'ORPHAN': 'red', 101 'ORPHAN': 'red',
91 } 102 }
92 103
  104 +
  105 +# === FUNCTIONS ==============================================================
  106 +
  107 +def sid_display(sid):
  108 + if sid == olefile.NOSTREAM:
  109 + return '-' # None
  110 + else:
  111 + return sid
  112 +
  113 +
93 # === MAIN =================================================================== 114 # === MAIN ===================================================================
94 115
95 if __name__ == '__main__': 116 if __name__ == '__main__':
96 # print banner with version 117 # print banner with version
97 - print 'oledir %s - http://decalage.info/python/oletools' % __version__ 118 + print('oledir %s - http://decalage.info/python/oletools' % __version__)
98 119
99 if os.name == 'nt': 120 if os.name == 'nt':
100 colorclass.Windows.enable(auto_colors=True, reset_atexit=True) 121 colorclass.Windows.enable(auto_colors=True, reset_atexit=True)
@@ -126,7 +147,7 @@ if __name__ == '__main__': @@ -126,7 +147,7 @@ if __name__ == '__main__':
126 # TODO: oledir option to hexdump the raw direntries 147 # TODO: oledir option to hexdump the raw direntries
127 # TODO: olefile should be less picky about incorrect directory structures 148 # TODO: olefile should be less picky about incorrect directory structures
128 149
129 - for id in xrange(len(ole.direntries)): 150 + for id in range(len(ole.direntries)):
130 d = ole.direntries[id] 151 d = ole.direntries[id]
131 if d is None: 152 if d is None:
132 # this direntry is not part of the tree: either unused or an orphan 153 # this direntry is not part of the tree: either unused or an orphan