Skip to content

Commit

Permalink
Fix variable scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Bocahu committed Aug 10, 2012
1 parent a3ff6b1 commit 487d07a
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions library/pip
Expand Up @@ -19,10 +19,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#

PIP = None
VIRTUALENV = None
ENV = None


def _get_full_name(name, version=None):
if version is None:
Expand All @@ -32,11 +28,11 @@ def _get_full_name(name, version=None):
return resp


def _find_pip():
def _find_pip(module, env):
paths = ['/usr/local/bin', '/usr/bin']

if ENV:
paths = [os.path.join(ENV, 'bin')] + paths
if env:
paths = [os.path.join(env, 'bin')] + paths

for p in paths:
pe = p + '/pip'
Expand All @@ -46,7 +42,7 @@ def _find_pip():
module.fail_json(msg='pip is not installed')


def _find_virtualenv():
def _find_virtualenv(module):
paths = ['/usr/local/bin', '/usr/bin']

for p in paths:
Expand All @@ -57,15 +53,15 @@ def _find_virtualenv():
module.fail_json(msg='virtualenv is not installed')


def _ensure_virtualenv():
if os.path.exists(os.path.join(ENV, 'bin', 'activate')):
def _ensure_virtualenv(module, env, virtualenv):
if os.path.exists(os.path.join(env, 'bin', 'activate')):
return 0, '', ''
else:
return _run('%s %s' % (VIRTUALENV, ENV))
return _run('%s %s' % (virtualenv, env))


def _is_package_installed(name, version=None):
rc, status_stdout, status_stderr = _run('%s freeze' % PIP)
def _is_package_installed(name, pip, version=None):
rc, status_stdout, status_stderr = _run('%s freeze' % pip)
return _get_full_name(name, version).lower() in status_stdout.lower()


Expand All @@ -82,6 +78,10 @@ def _run(cmd):


def main():
pip = None
virtualenv = None
env = None

arg_spec = dict(
state=dict(default='present', choices=['absent', 'present', 'latest']),
name=dict(default=None, required=False),
Expand All @@ -96,13 +96,13 @@ def main():
err = ''
out = ''

ENV = module.params['virtualenv']
PIP = _find_pip()
env = module.params['virtualenv']
pip = _find_pip(module, env)

if ENV:
VIRTUALENV = _find_virtualenv()
if env:
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
out += out_venv
Expand Down Expand Up @@ -147,7 +147,7 @@ def main():
installed = None

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 += rc_pip
Expand All @@ -158,7 +158,7 @@ def main():
(not _did_install(out) and state == 'absent'))

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)

Expand All @@ -169,7 +169,7 @@ def main():
changed = 'Successfully installed' in out_pip

elif name:
installed = _is_package_installed(name, version)
installed = _is_package_installed(name, pip, version)

changed = ((installed and state == 'absent') or
(not installed and state == 'present'))
Expand All @@ -180,7 +180,7 @@ def main():
else:
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':
cmd = cmd + ' -y'
Expand All @@ -197,7 +197,7 @@ def main():
module.fail_json(msg=err, cmd=cmd)

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
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
Expand Down

0 comments on commit 487d07a

Please sign in to comment.