From 1feffdc3433c30d7a2cc567ae9fbb4a1abdf40c2 Mon Sep 17 00:00:00 2001 From: decalage2 Date: Wed, 3 May 2017 09:53:46 +0200 Subject: [PATCH] oleobj: fixed absolute imports (issue #141) --- oletools/oleobj.py | 21 +++++++++++++++++---- setup.py | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/oletools/oleobj.py b/oletools/oleobj.py index 51e5d22..196ceb3 100644 --- a/oletools/oleobj.py +++ b/oletools/oleobj.py @@ -15,7 +15,7 @@ http://www.decalage.info/python/oletools # === LICENSE ================================================================== -# oleobj is copyright (c) 2015-2016 Philippe Lagadec (http://www.decalage.info) +# oleobj is copyright (c) 2015-2017 Philippe Lagadec (http://www.decalage.info) # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -47,8 +47,9 @@ http://www.decalage.info/python/oletools # 2016-07-19 PL: - fixed Python 2.6-7 support # 2016-11-17 v0.51 PL: - fixed OLE native object extraction # 2016-11-18 PL: - added main for setup.py entry point +# 2017-05-03 PL: - fixed absolute imports (issue #141) -__version__ = '0.51' +__version__ = '0.51dev7' #------------------------------------------------------------------------------ # TODO: @@ -70,8 +71,20 @@ __version__ = '0.51' import logging, struct, optparse, os, re, sys -from .thirdparty.olefile import olefile -from .thirdparty.xglob import xglob +# IMPORTANT: it should be possible to run oletools directly as scripts +# in any directory without installing them with pip or setup.py. +# In that case, relative imports are NOT usable. +# And to enable Python 2+3 compatibility, we need to use absolute imports, +# so we add the oletools parent folder to sys.path (absolute+normalized path): +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__))) +# print('_thismodule_dir = %r' % _thismodule_dir) +_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..')) +# print('_parent_dir = %r' % _thirdparty_dir) +if not _parent_dir in sys.path: + sys.path.insert(0, _parent_dir) + +from oletools.thirdparty.olefile import olefile +from oletools.thirdparty.xglob import xglob # === LOGGING ================================================================= diff --git a/setup.py b/setup.py index afeae9b..9268d6a 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,7 @@ import os, fnmatch #--- METADATA ----------------------------------------------------------------- name = "oletools" -version = '0.51dev6' +version = '0.51dev7' 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" long_desc = open('oletools/README.rst').read() author = "Philippe Lagadec" -- libgit2 0.21.4