From 42953c40ce5a313211e488b51443a11d6cc93a65 Mon Sep 17 00:00:00 2001 From: Robin Roth Date: Mon, 14 May 2018 14:49:41 +0200 Subject: [PATCH] Pip: Make main method smaller (#38788) --- lib/ansible/modules/packaging/language/pip.py | 91 ++++++++++--------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/lib/ansible/modules/packaging/language/pip.py b/lib/ansible/modules/packaging/language/pip.py index fbe26fb92d1..83ee128c293 100644 --- a/lib/ansible/modules/packaging/language/pip.py +++ b/lib/ansible/modules/packaging/language/pip.py @@ -342,6 +342,53 @@ def _get_package_info(module, package, env=None): return formatted_dep +def setup_virtualenv(module, env, chdir, out, err): + if module.check_mode: + module.exit_json(changed=True) + + cmd = module.params['virtualenv_command'] + if os.path.basename(cmd) == cmd: + cmd = module.get_bin_path(cmd, True) + + if module.params['virtualenv_site_packages']: + cmd += ' --system-site-packages' + else: + cmd_opts = _get_cmd_options(module, cmd) + if '--no-site-packages' in cmd_opts: + cmd += ' --no-site-packages' + + virtualenv_python = module.params['virtualenv_python'] + # -p is a virtualenv option, not compatible with pyenv or venv + # this if validates if the command being used is not any of them + if not any(ex in module.params['virtualenv_command'] for ex in ('pyvenv', '-m venv')): + if virtualenv_python: + cmd += ' -p%s' % virtualenv_python + elif PY3: + # Ubuntu currently has a patch making virtualenv always + # try to use python2. Since Ubuntu16 works without + # python2 installed, this is a problem. This code mimics + # the upstream behaviour of using the python which invoked + # virtualenv to determine which python is used inside of + # the virtualenv (when none are specified). + cmd += ' -p%s' % sys.executable + + # if venv or pyvenv are used and virtualenv_python is defined, then + # virtualenv_python is ignored, this has to be acknowledged + elif module.params['virtualenv_python']: + module.fail_json( + msg='virtualenv_python should not be used when' + ' using the venv module or pyvenv as virtualenv_command' + ) + + cmd = "%s %s" % (cmd, env) + rc, out_venv, err_venv = module.run_command(cmd, cwd=chdir) + out += out_venv + err += err_venv + if rc != 0: + _fail(module, cmd, out, err) + return out, err + + def main(): state_map = dict( present='install', @@ -377,7 +424,6 @@ def main(): version = module.params['version'] requirements = module.params['requirements'] extra_args = module.params['extra_args'] - virtualenv_python = module.params['virtualenv_python'] chdir = module.params['chdir'] umask = module.params['umask'] env = module.params['virtualenv'] @@ -410,48 +456,7 @@ def main(): if env: if not os.path.exists(os.path.join(env, 'bin', 'activate')): venv_created = True - if module.check_mode: - module.exit_json(changed=True) - - cmd = module.params['virtualenv_command'] - if os.path.basename(cmd) == cmd: - cmd = module.get_bin_path(cmd, True) - - if module.params['virtualenv_site_packages']: - cmd += ' --system-site-packages' - else: - cmd_opts = _get_cmd_options(module, cmd) - if '--no-site-packages' in cmd_opts: - cmd += ' --no-site-packages' - - # -p is a virtualenv option, not compatible with pyenv or venv - # this if validates if the command being used is not any of them - if not any(ex in module.params['virtualenv_command'] for ex in ('pyvenv', '-m venv')): - if virtualenv_python: - cmd += ' -p%s' % virtualenv_python - elif PY3: - # Ubuntu currently has a patch making virtualenv always - # try to use python2. Since Ubuntu16 works without - # python2 installed, this is a problem. This code mimics - # the upstream behaviour of using the python which invoked - # virtualenv to determine which python is used inside of - # the virtualenv (when none are specified). - cmd += ' -p%s' % sys.executable - - # if venv or pyvenv are used and virtualenv_python is defined, then - # virtualenv_python is ignored, this has to be acknowledged - elif module.params['virtualenv_python']: - module.fail_json( - msg='virtualenv_python should not be used when' - ' using the venv module or pyvenv as virtualenv_command' - ) - - cmd = "%s %s" % (cmd, env) - rc, out_venv, err_venv = module.run_command(cmd, cwd=chdir) - out += out_venv - err += err_venv - if rc != 0: - _fail(module, cmd, out, err) + out, err = setup_virtualenv(module, env, chdir, out, err) pip = _get_pip(module, env, module.params['executable'])