From c73d8012b4f0b6281523caca434cd3578c612133 Mon Sep 17 00:00:00 2001
From: Pedro Romano <pedro.romano@salesseek.co.uk>
Date: Mon, 26 Aug 2013 10:02:54 +0100
Subject: [PATCH] Implement a '_get_easy_install' helper function to find the
 desired 'easy_install' executable, modeled on '_get_pip' from the 'pip'
 module to correctly handle all combinations of explicit executable and
 virtualenv.

---
 packaging/easy_install | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/packaging/easy_install b/packaging/easy_install
index 086ab6d5b4f..fbf222033ec 100644
--- a/packaging/easy_install
+++ b/packaging/easy_install
@@ -19,6 +19,8 @@
 # along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+import os.path
+
 DOCUMENTATION = '''
 ---
 module: easy_install
@@ -92,13 +94,39 @@ def _is_package_installed(module, name, easy_install):
     return not ('Reading' in status_stdout or 'Downloading' in status_stdout)
 
 
+def _get_easy_install(module, env=None, executable=None):
+    candidate_easy_inst_basenames = ['easy_install']
+    easy_install = None
+    if executable is not None:
+        if os.path.isabs(executable):
+            easy_install = executable
+        else:
+            candidate_easy_inst_basenames.insert(0, executable)
+    if easy_install is None:
+        if env is None:
+            opt_dirs = []
+        else:
+            # Try easy_install with the virtualenv directory first.
+            opt_dirs = ['%s/bin' % env]
+        for basename in candidate_easy_inst_basenames:
+            easy_install = module.get_bin_path(basename, False, opt_dirs)
+            if easy_install is not None:
+                break
+    # easy_install should have been found by now.  The final call to
+    # get_bin_path will trigger fail_json.
+    if easy_install is None:
+        basename = candidate_easy_inst_basenames[0]
+        easy_install = module.get_bin_path(basename, True, opt_dirs)
+    return easy_install
+
+
 def main():
     arg_spec = dict(
         name=dict(required=True),
         virtualenv=dict(default=None, required=False),
         virtualenv_site_packages=dict(default='no', type='bool'),
         virtualenv_command=dict(default='virtualenv', required=False),
-        executable=dict(default='easy_install', required=False),
+        executable=dict(default=None, required=False),
     )
 
     module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True)
@@ -106,6 +134,8 @@ def main():
     name = module.params['name']
     env = module.params['virtualenv']
     easy_install = module.get_bin_path(module.params['executable'], True, ['%s/bin' % env])
+    executable = module.params['executable']
+    easy_install = _get_easy_install(module, env, executable)
     site_packages = module.params['virtualenv_site_packages']
     virtualenv_command = module.params['virtualenv_command']