Commit 4552f6849762ba99d359ef958e4a1d73768b3ef1
1 parent
42e0faa6
oleid: fixed relative imports for Python 3 (issue #115)
Showing
1 changed file
with
27 additions
and
4 deletions
oletools/oleid.py
| ... | ... | @@ -41,7 +41,9 @@ http://www.decalage.info/python/oletools |
| 41 | 41 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 42 | 42 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 | 43 | |
| 44 | +# To improve Python 2+3 compatibility: | |
| 44 | 45 | from __future__ import print_function |
| 46 | +#from __future__ import absolute_import | |
| 45 | 47 | |
| 46 | 48 | #------------------------------------------------------------------------------ |
| 47 | 49 | # CHANGELOG: |
| ... | ... | @@ -50,8 +52,9 @@ from __future__ import print_function |
| 50 | 52 | # - improved usage display with -h |
| 51 | 53 | # 2014-11-30 v0.03 PL: - improved output with prettytable |
| 52 | 54 | # 2016-10-25 v0.50 PL: - fixed print and bytes strings for Python 3 |
| 55 | +# 2016-12-12 v0.51 PL: - fixed relative imports for Python 3 (issue #115) | |
| 53 | 56 | |
| 54 | -__version__ = '0.50' | |
| 57 | +__version__ = '0.51' | |
| 55 | 58 | |
| 56 | 59 | |
| 57 | 60 | #------------------------------------------------------------------------------ |
| ... | ... | @@ -73,8 +76,21 @@ __version__ = '0.50' |
| 73 | 76 | #=== IMPORTS ================================================================= |
| 74 | 77 | |
| 75 | 78 | import optparse, sys, os, re, zlib, struct |
| 76 | -import thirdparty.olefile as olefile | |
| 77 | -from thirdparty.prettytable import prettytable | |
| 79 | + | |
| 80 | +try: | |
| 81 | + # Relative imports (only works when imported from package): | |
| 82 | + from .thirdparty import olefile | |
| 83 | + from .thirdparty.prettytable import prettytable | |
| 84 | +except: | |
| 85 | + # if it does not work, fall back to absolute imports: | |
| 86 | + # add this module's folder to sys.path (absolute+normalized path): | |
| 87 | + _thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__))) | |
| 88 | + if not _thismodule_dir in sys.path: | |
| 89 | + sys.path.insert(0, _thismodule_dir) | |
| 90 | + # absolute imports: | |
| 91 | + from thirdparty import olefile | |
| 92 | + from thirdparty.prettytable import prettytable | |
| 93 | + | |
| 78 | 94 | |
| 79 | 95 | |
| 80 | 96 | #=== FUNCTIONS =============================================================== |
| ... | ... | @@ -265,6 +281,12 @@ class OleID: |
| 265 | 281 | #=== MAIN ================================================================= |
| 266 | 282 | |
| 267 | 283 | def main(): |
| 284 | + # print banner with version | |
| 285 | + print ('oleid %s - http://decalage.info/oletools' % __version__) | |
| 286 | + print ('THIS IS WORK IN PROGRESS - Check updates regularly!') | |
| 287 | + print ('Please report any issue at https://github.com/decalage2/oletools/issues') | |
| 288 | + print ('') | |
| 289 | + | |
| 268 | 290 | usage = 'usage: %prog [options] <file>' |
| 269 | 291 | parser = optparse.OptionParser(usage=__doc__ + '\n' + usage) |
| 270 | 292 | ## parser.add_option('-o', '--ole', action='store_true', dest='ole', help='Parse an OLE file (e.g. Word, Excel) to look for SWF in each stream') |
| ... | ... | @@ -277,7 +299,7 @@ def main(): |
| 277 | 299 | return |
| 278 | 300 | |
| 279 | 301 | for filename in args: |
| 280 | - print('\nFilename:', filename) | |
| 302 | + print('Filename:', filename) | |
| 281 | 303 | oleid = OleID(filename) |
| 282 | 304 | indicators = oleid.check() |
| 283 | 305 | |
| ... | ... | @@ -293,6 +315,7 @@ def main(): |
| 293 | 315 | t.add_row((indicator.name, indicator.value)) |
| 294 | 316 | |
| 295 | 317 | print(t) |
| 318 | + print ('') | |
| 296 | 319 | |
| 297 | 320 | if __name__ == '__main__': |
| 298 | 321 | main() | ... | ... |