Commit 535e3f13 by Michael DeHaan

Merge pull request #845 from zecrazytux/bugfix/pip2

Bugfix/pip2
parents 9e934acf 487d07a8
...@@ -647,7 +647,7 @@ class Runner(object): ...@@ -647,7 +647,7 @@ class Runner(object):
def _remote_md5(self, conn, tmp, path): def _remote_md5(self, conn, tmp, path):
''' takes a remote md5sum without requiring python, and returns 0 if no file ''' ''' takes a remote md5sum without requiring python, and returns 0 if no file '''
test = "rc=0; [[ -r \"%s\" ]] || rc=2; [[ -f \"%s\" ]] || rc=1" % (path,path) test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1" % (path,path)
md5s = [ md5s = [
"(/usr/bin/md5sum %s 2>/dev/null)" % path, "(/usr/bin/md5sum %s 2>/dev/null)" % path,
"(/sbin/md5sum -q %s 2>/dev/null)" % path, "(/sbin/md5sum -q %s 2>/dev/null)" % path,
......
...@@ -19,10 +19,6 @@ ...@@ -19,10 +19,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
PIP = None
VIRTUALENV = None
ENV = None
def _get_full_name(name, version=None): def _get_full_name(name, version=None):
if version is None: if version is None:
...@@ -32,11 +28,11 @@ def _get_full_name(name, version=None): ...@@ -32,11 +28,11 @@ def _get_full_name(name, version=None):
return resp return resp
def _find_pip(): def _find_pip(module, env):
paths = ['/usr/local/bin', '/usr/bin'] paths = ['/usr/local/bin', '/usr/bin']
if ENV: if env:
paths = [os.path.join(ENV, 'bin')] + paths paths = [os.path.join(env, 'bin')] + paths
for p in paths: for p in paths:
pe = p + '/pip' pe = p + '/pip'
...@@ -46,7 +42,7 @@ def _find_pip(): ...@@ -46,7 +42,7 @@ def _find_pip():
module.fail_json(msg='pip is not installed') module.fail_json(msg='pip is not installed')
def _find_virtualenv(): def _find_virtualenv(module):
paths = ['/usr/local/bin', '/usr/bin'] paths = ['/usr/local/bin', '/usr/bin']
for p in paths: for p in paths:
...@@ -57,15 +53,15 @@ def _find_virtualenv(): ...@@ -57,15 +53,15 @@ def _find_virtualenv():
module.fail_json(msg='virtualenv is not installed') module.fail_json(msg='virtualenv is not installed')
def _ensure_virtualenv(): def _ensure_virtualenv(module, env, virtualenv):
if os.path.exists(os.path.join(ENV, 'bin', 'activate')): if os.path.exists(os.path.join(env, 'bin', 'activate')):
return 0, '', '' return 0, '', ''
else: else:
return _run('%s %s' % (VIRTUALENV, ENV)) return _run('%s %s' % (virtualenv, env))
def _is_package_installed(name, version=None): def _is_package_installed(name, pip, version=None):
rc, status_stdout, status_stderr = _run('%s freeze' % PIP) rc, status_stdout, status_stderr = _run('%s freeze' % pip)
return _get_full_name(name, version).lower() in status_stdout.lower() return _get_full_name(name, version).lower() in status_stdout.lower()
...@@ -82,6 +78,10 @@ def _run(cmd): ...@@ -82,6 +78,10 @@ def _run(cmd):
def main(): def main():
pip = None
virtualenv = None
env = None
arg_spec = dict( arg_spec = dict(
state=dict(default='present', choices=['absent', 'present', 'latest']), state=dict(default='present', choices=['absent', 'present', 'latest']),
name=dict(default=None, required=False), name=dict(default=None, required=False),
...@@ -96,13 +96,13 @@ def main(): ...@@ -96,13 +96,13 @@ def main():
err = '' err = ''
out = '' out = ''
ENV = module.params['virtualenv'] env = module.params['virtualenv']
PIP = _find_pip() pip = _find_pip(module, env)
if ENV: if env:
VIRTUALENV = _find_virtualenv() virtualenv = _find_virtualenv(module)
rc_venv, out_venv, err_venv = _ensure_virtualenv() rc_venv, out_venv, err_venv = _ensure_virtualenv(module, env, virtualenv)
rc += rc_venv rc += rc_venv
out += out_venv out += out_venv
...@@ -147,7 +147,7 @@ def main(): ...@@ -147,7 +147,7 @@ def main():
installed = None installed = None
if requirements: if requirements:
cmd = '%s %s -r %s --use-mirrors' % (PIP, command_map[state], requirements) cmd = '%s %s -r %s --use-mirrors' % (pip, command_map[state], requirements)
rc_pip, out_pip, err_pip = _run(cmd) rc_pip, out_pip, err_pip = _run(cmd)
rc += rc_pip rc += rc_pip
...@@ -158,7 +158,7 @@ def main(): ...@@ -158,7 +158,7 @@ def main():
(not _did_install(out) and state == 'absent')) (not _did_install(out) and state == 'absent'))
if name and state == 'latest': if name and state == 'latest':
cmd = '%s %s %s --upgrade' % (PIP, command_map[state], name) cmd = '%s %s %s --upgrade' % (pip, command_map[state], name)
rc_pip, out_pip, err_pip = _run(cmd) rc_pip, out_pip, err_pip = _run(cmd)
...@@ -169,7 +169,7 @@ def main(): ...@@ -169,7 +169,7 @@ def main():
changed = 'Successfully installed' in out_pip changed = 'Successfully installed' in out_pip
elif name: elif name:
installed = _is_package_installed(name, version) installed = _is_package_installed(name, pip, version)
changed = ((installed and state == 'absent') or changed = ((installed and state == 'absent') or
(not installed and state == 'present')) (not installed and state == 'present'))
...@@ -180,7 +180,7 @@ def main(): ...@@ -180,7 +180,7 @@ def main():
else: else:
full_name = name full_name = name
cmd = '%s %s %s' % (PIP, command_map[state], full_name) cmd = '%s %s %s' % (pip, command_map[state], full_name)
if state == 'absent': if state == 'absent':
cmd = cmd + ' -y' cmd = cmd + ' -y'
...@@ -197,7 +197,7 @@ def main(): ...@@ -197,7 +197,7 @@ def main():
module.fail_json(msg=err, cmd=cmd) module.fail_json(msg=err, cmd=cmd)
module.exit_json(changed=changed, cmd=cmd, name=name, version=version, module.exit_json(changed=changed, cmd=cmd, name=name, version=version,
state=state, requirements=requirements, virtualenv=ENV) state=state, requirements=requirements, virtualenv=env)
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment