Commit 12653de920a00858928f38af4f332d6b68415573

Authored by decalage2
1 parent 7b3a713e

mraptor_milter: fixed absolute imports (issue #141)

Showing 1 changed file with 15 additions and 2 deletions
oletools/mraptor_milter.py
@@ -24,7 +24,7 @@ http://www.decalage.info/python/oletools @@ -24,7 +24,7 @@ http://www.decalage.info/python/oletools
24 24
25 # === LICENSE ================================================================== 25 # === LICENSE ==================================================================
26 26
27 -# mraptor_milter is copyright (c) 2016 Philippe Lagadec (http://www.decalage.info) 27 +# mraptor_milter is copyright (c) 2016-2017 Philippe Lagadec (http://www.decalage.info)
28 # All rights reserved. 28 # All rights reserved.
29 # 29 #
30 # Redistribution and use in source and binary forms, with or without modification, 30 # Redistribution and use in source and binary forms, with or without modification,
@@ -53,8 +53,9 @@ http://www.decalage.info/python/oletools @@ -53,8 +53,9 @@ http://www.decalage.info/python/oletools
53 # - archive each e-mail to a file before filtering 53 # - archive each e-mail to a file before filtering
54 # 2016-08-30 v0.03 PL: - added daemonize to run as a Unix daemon 54 # 2016-08-30 v0.03 PL: - added daemonize to run as a Unix daemon
55 # 2016-09-06 v0.50 PL: - fixed issue #20, is_zipfile on Python 2.6 55 # 2016-09-06 v0.50 PL: - fixed issue #20, is_zipfile on Python 2.6
  56 +# 2017-04-26 v0.51 PL: - fixed absolute imports (issue #141)
56 57
57 -__version__ = '0.50' 58 +__version__ = '0.51'
58 59
59 # --- TODO ------------------------------------------------------------------- 60 # --- TODO -------------------------------------------------------------------
60 61
@@ -81,6 +82,18 @@ import StringIO @@ -81,6 +82,18 @@ import StringIO
81 82
82 from socket import AF_INET6 83 from socket import AF_INET6
83 84
  85 +# IMPORTANT: it should be possible to run oletools directly as scripts
  86 +# in any directory without installing them with pip or setup.py.
  87 +# In that case, relative imports are NOT usable.
  88 +# And to enable Python 2+3 compatibility, we need to use absolute imports,
  89 +# so we add the oletools parent folder to sys.path (absolute+normalized path):
  90 +_thismodule_dir = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
  91 +# print('_thismodule_dir = %r' % _thismodule_dir)
  92 +_parent_dir = os.path.normpath(os.path.join(_thismodule_dir, '..'))
  93 +# print('_parent_dir = %r' % _thirdparty_dir)
  94 +if not _parent_dir in sys.path:
  95 + sys.path.insert(0, _parent_dir)
  96 +
84 from oletools import olevba, mraptor 97 from oletools import olevba, mraptor
85 98
86 from Milter.utils import parse_addr 99 from Milter.utils import parse_addr