From 6c03b83748e27a027145f925801a49c3747e6739 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sat, 28 Jul 2018 12:35:03 -0700 Subject: [PATCH] issue #291: don't attempt mitogen import until sys.path modified. Given an extracted download of mitogen-2.2.tar.gz, with strategy_plugins pointing into it, if an old version of the package was pip-installed, then the old pip-installed package would be imported and override whatever came from the tarball. Instead, modify sys.path before attempting any import. This still isn't perfect, but it's better. --- ansible_mitogen/plugins/strategy/mitogen.py | 12 ++++++------ ansible_mitogen/plugins/strategy/mitogen_free.py | 12 ++++++------ ansible_mitogen/plugins/strategy/mitogen_linear.py | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/ansible_mitogen/plugins/strategy/mitogen.py b/ansible_mitogen/plugins/strategy/mitogen.py index 3ef522b4..4f595161 100644 --- a/ansible_mitogen/plugins/strategy/mitogen.py +++ b/ansible_mitogen/plugins/strategy/mitogen.py @@ -44,12 +44,12 @@ import sys # debuggers and isinstance() work predictably. # -try: - import ansible_mitogen -except ImportError: - base_dir = os.path.dirname(__file__) - sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..'))) - del base_dir +BASE_DIR = os.path.abspath( + os.path.join(os.path.dirname(__file__), '../../..') +) + +if BASE_DIR not in sys.path: + sys.path.insert(0, BASE_DIR) import ansible_mitogen.strategy import ansible.plugins.strategy.linear diff --git a/ansible_mitogen/plugins/strategy/mitogen_free.py b/ansible_mitogen/plugins/strategy/mitogen_free.py index 34f959ca..8dfaa16e 100644 --- a/ansible_mitogen/plugins/strategy/mitogen_free.py +++ b/ansible_mitogen/plugins/strategy/mitogen_free.py @@ -44,12 +44,12 @@ import sys # debuggers and isinstance() work predictably. # -try: - import ansible_mitogen -except ImportError: - base_dir = os.path.dirname(__file__) - sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..'))) - del base_dir +BASE_DIR = os.path.abspath( + os.path.join(os.path.dirname(__file__), '../../..') +) + +if BASE_DIR not in sys.path: + sys.path.insert(0, BASE_DIR) import ansible_mitogen.loaders import ansible_mitogen.strategy diff --git a/ansible_mitogen/plugins/strategy/mitogen_linear.py b/ansible_mitogen/plugins/strategy/mitogen_linear.py index a5ea2a3d..d995b67b 100644 --- a/ansible_mitogen/plugins/strategy/mitogen_linear.py +++ b/ansible_mitogen/plugins/strategy/mitogen_linear.py @@ -44,12 +44,12 @@ import sys # debuggers and isinstance() work predictably. # -try: - import ansible_mitogen -except ImportError: - base_dir = os.path.dirname(__file__) - sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..'))) - del base_dir +BASE_DIR = os.path.abspath( + os.path.join(os.path.dirname(__file__), '../../..') +) + +if BASE_DIR not in sys.path: + sys.path.insert(0, BASE_DIR) import ansible_mitogen.loaders import ansible_mitogen.strategy