2012-08-07 22:38:04 +02:00
|
|
|
#!/usr/bin/python
|
2012-08-08 16:35:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Matt Wright <matt@nobien.net>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
2012-08-07 22:38:04 +02:00
|
|
|
|
2013-03-15 23:05:21 +01:00
|
|
|
import tempfile
|
2015-08-06 15:24:41 +02:00
|
|
|
import re
|
2013-05-19 02:59:21 +02:00
|
|
|
import os
|
2013-03-15 23:05:21 +01:00
|
|
|
|
2012-09-29 20:53:28 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: pip
|
|
|
|
short_description: Manages Python library dependencies.
|
|
|
|
description:
|
2013-05-22 18:09:01 +02:00
|
|
|
- "Manage Python library dependencies. To use this module, one of the following keys is required: C(name)
|
|
|
|
or C(requirements)."
|
2012-09-29 20:53:28 +02:00
|
|
|
version_added: "0.7"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2013-05-19 02:59:21 +02:00
|
|
|
- The name of a Python library to install or the url of the remote package.
|
2013-03-14 03:25:58 +01:00
|
|
|
required: false
|
2012-09-29 20:53:28 +02:00
|
|
|
default: null
|
|
|
|
version:
|
|
|
|
description:
|
2012-11-21 18:49:30 +01:00
|
|
|
- The version number to install of the Python library specified in the I(name) parameter
|
2012-09-29 20:53:28 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
requirements:
|
|
|
|
description:
|
2015-12-22 16:20:37 +01:00
|
|
|
- The path to a pip requirements file, which should be local to the remote system.
|
|
|
|
File can be specified as a relative path if using the chdir option.
|
2012-09-29 20:53:28 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
virtualenv:
|
|
|
|
description:
|
2012-11-21 18:49:30 +01:00
|
|
|
- An optional path to a I(virtualenv) directory to install into
|
2012-09-29 20:53:28 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
2013-01-29 18:30:09 +01:00
|
|
|
virtualenv_site_packages:
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.0"
|
2013-01-29 18:30:09 +01:00
|
|
|
description:
|
|
|
|
- Whether the virtual environment will inherit packages from the
|
|
|
|
global site-packages directory. Note that if this setting is
|
|
|
|
changed on an already existing virtual environment it will not
|
|
|
|
have any effect, the environment must be deleted and newly
|
|
|
|
created.
|
|
|
|
required: false
|
2013-03-12 13:18:12 +01:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2013-02-20 14:52:02 +01:00
|
|
|
virtualenv_command:
|
2015-01-12 13:38:42 +01:00
|
|
|
version_added: "1.1"
|
2013-02-20 14:52:02 +01:00
|
|
|
description:
|
2013-05-22 18:15:21 +02:00
|
|
|
- The command or a pathname to the command to create the virtual
|
|
|
|
environment with. For example C(pyvenv), C(virtualenv),
|
|
|
|
C(virtualenv2), C(~/bin/virtualenv), C(/usr/local/bin/virtualenv).
|
2013-02-20 14:52:02 +01:00
|
|
|
required: false
|
|
|
|
default: virtualenv
|
2015-01-28 12:45:25 +01:00
|
|
|
virtualenv_python:
|
2015-07-21 02:52:43 +02:00
|
|
|
version_added: "2.0"
|
2015-01-28 12:45:25 +01:00
|
|
|
description:
|
|
|
|
- The Python executable used for creating the virtual environment.
|
|
|
|
For example C(python3.4), C(python2.7). When not specified, the
|
|
|
|
system Python version is used.
|
|
|
|
required: false
|
|
|
|
default: null
|
2012-09-29 20:53:28 +02:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The state of module
|
2016-01-12 00:22:41 +01:00
|
|
|
- The 'forcereinstall' option is only available in Ansible 2.1 and above.
|
2012-09-29 20:53:28 +02:00
|
|
|
required: false
|
|
|
|
default: present
|
2015-10-30 02:58:53 +01:00
|
|
|
choices: [ "present", "absent", "latest", "forcereinstall" ]
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
extra_args:
|
|
|
|
description:
|
|
|
|
- Extra arguments passed to pip.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
version_added: "1.0"
|
2015-08-30 16:24:13 +02:00
|
|
|
editable:
|
|
|
|
description:
|
|
|
|
- Pass the editable flag for versioning URLs.
|
|
|
|
required: false
|
|
|
|
default: yes
|
|
|
|
version_added: "2.0"
|
2013-06-03 17:14:21 +02:00
|
|
|
chdir:
|
|
|
|
description:
|
|
|
|
- cd into this directory before running the command
|
|
|
|
version_added: "1.3"
|
|
|
|
required: false
|
|
|
|
default: null
|
2013-08-16 13:02:23 +02:00
|
|
|
executable:
|
|
|
|
description:
|
|
|
|
- The explicit executable or a pathname to the executable to be used to
|
|
|
|
run pip for a specific version of Python installed in the system. For
|
|
|
|
example C(pip-3.3), if there are both Python 2.7 and 3.3 installations
|
|
|
|
in the system and you want to run pip for the Python 3.3 installation.
|
|
|
|
version_added: "1.3"
|
|
|
|
required: false
|
|
|
|
default: null
|
2012-09-29 20:53:28 +02:00
|
|
|
notes:
|
2015-01-23 03:05:54 +01:00
|
|
|
- Please note that virtualenv (U(http://www.virtualenv.org/)) must be installed on the remote host if the virtualenv parameter is specified and the virtualenv needs to be initialized.
|
2012-10-01 09:18:54 +02:00
|
|
|
requirements: [ "virtualenv", "pip" ]
|
2015-06-15 21:53:30 +02:00
|
|
|
author: "Matt Wright (@mattupstate)"
|
2012-09-29 20:53:28 +02:00
|
|
|
'''
|
2012-08-07 22:38:04 +02:00
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
2013-08-16 11:51:14 +02:00
|
|
|
# Install (Bottle) python package.
|
|
|
|
- pip: name=bottle
|
2013-06-14 11:53:43 +02:00
|
|
|
|
2013-08-16 11:51:14 +02:00
|
|
|
# Install (Bottle) python package on version 0.11.
|
|
|
|
- pip: name=bottle version=0.11
|
2013-06-14 11:53:43 +02:00
|
|
|
|
2014-02-17 22:52:49 +01:00
|
|
|
# Install (MyApp) using one of the remote protocols (bzr+,hg+,git+,svn+). You do not have to supply '-e' option in extra_args.
|
2013-06-14 11:53:43 +02:00
|
|
|
- pip: name='svn+http://myrepo/svn/MyApp#egg=MyApp'
|
|
|
|
|
2015-08-30 16:24:13 +02:00
|
|
|
# Install MyApp using one of the remote protocols (bzr+,hg+,git+) in a non editable way.
|
|
|
|
- pip: name='git+http://myrepo/app/MyApp' editable=false
|
|
|
|
|
2015-02-10 20:56:16 +01:00
|
|
|
# Install (MyApp) from local tarball
|
|
|
|
- pip: name='file:///path/to/MyApp.tar.gz'
|
|
|
|
|
2013-08-16 11:51:14 +02:00
|
|
|
# Install (Bottle) into the specified (virtualenv), inheriting none of the globally installed modules
|
|
|
|
- pip: name=bottle virtualenv=/my_app/venv
|
2013-06-14 11:53:43 +02:00
|
|
|
|
2013-08-16 11:51:14 +02:00
|
|
|
# Install (Bottle) into the specified (virtualenv), inheriting globally installed modules
|
|
|
|
- pip: name=bottle virtualenv=/my_app/venv virtualenv_site_packages=yes
|
2013-06-14 11:53:43 +02:00
|
|
|
|
2013-08-16 11:51:14 +02:00
|
|
|
# Install (Bottle) into the specified (virtualenv), using Python 2.7
|
|
|
|
- pip: name=bottle virtualenv=/my_app/venv virtualenv_command=virtualenv-2.7
|
2013-06-14 11:53:43 +02:00
|
|
|
|
|
|
|
# Install specified python requirements.
|
|
|
|
- pip: requirements=/my_app/requirements.txt
|
|
|
|
|
|
|
|
# Install specified python requirements in indicated (virtualenv).
|
|
|
|
- pip: requirements=/my_app/requirements.txt virtualenv=/my_app/venv
|
|
|
|
|
|
|
|
# Install specified python requirements and custom Index URL.
|
|
|
|
- pip: requirements=/my_app/requirements.txt extra_args='-i https://example.com/pypi/simple'
|
2013-08-16 13:02:23 +02:00
|
|
|
|
|
|
|
# Install (Bottle) for Python 3.3 specifically,using the 'pip-3.3' executable.
|
|
|
|
- pip: name=bottle executable=pip-3.3
|
2015-10-30 02:58:53 +01:00
|
|
|
|
|
|
|
# Install (Bottle), forcing reinstallation if it's already installed
|
|
|
|
- pip: name=bottle state=forcereinstall
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
2013-11-06 00:51:41 +01:00
|
|
|
def _get_cmd_options(module, cmd):
|
|
|
|
thiscmd = cmd + " --help"
|
|
|
|
rc, stdout, stderr = module.run_command(thiscmd)
|
|
|
|
if rc != 0:
|
2014-01-14 20:22:37 +01:00
|
|
|
module.fail_json(msg="Could not get output from %s: %s" % (thiscmd, stdout + stderr))
|
2013-11-06 00:51:41 +01:00
|
|
|
|
|
|
|
words = stdout.strip().split()
|
|
|
|
cmd_options = [ x for x in words if x.startswith('--') ]
|
|
|
|
return cmd_options
|
2015-07-22 01:07:25 +02:00
|
|
|
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
|
2012-08-07 22:38:04 +02:00
|
|
|
def _get_full_name(name, version=None):
|
2012-08-09 23:54:29 +02:00
|
|
|
if version is None:
|
|
|
|
resp = name
|
|
|
|
else:
|
|
|
|
resp = name + '==' + version
|
|
|
|
return resp
|
2012-08-07 22:38:04 +02:00
|
|
|
|
2014-03-26 15:08:23 +01:00
|
|
|
def _is_present(name, version, installed_pkgs):
|
|
|
|
for pkg in installed_pkgs:
|
|
|
|
if '==' not in pkg:
|
|
|
|
continue
|
|
|
|
|
|
|
|
[pkg_name, pkg_version] = pkg.split('==')
|
|
|
|
|
|
|
|
if pkg_name == name and (version is None or version == pkg_version):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-08-07 22:38:04 +02:00
|
|
|
|
2013-08-26 10:59:09 +02:00
|
|
|
def _get_pip(module, env=None, executable=None):
|
|
|
|
# On Debian and Ubuntu, pip is pip.
|
|
|
|
# On Fedora18 and up, pip is python-pip.
|
|
|
|
# On Fedora17 and below, CentOS and RedHat 6 and 5, pip is pip-python.
|
|
|
|
# On Fedora, CentOS, and RedHat, the exception is in the virtualenv.
|
|
|
|
# There, pip is just pip.
|
|
|
|
candidate_pip_basenames = ['pip', 'python-pip', 'pip-python']
|
|
|
|
pip = None
|
|
|
|
if executable is not None:
|
2015-03-29 21:06:36 +02:00
|
|
|
executable = os.path.expanduser(executable)
|
2013-08-26 10:59:09 +02:00
|
|
|
if os.path.isabs(executable):
|
|
|
|
pip = executable
|
|
|
|
else:
|
2014-01-29 20:09:58 +01:00
|
|
|
# If you define your own executable that executable should be the only candidate.
|
|
|
|
candidate_pip_basenames = [executable]
|
2013-08-26 10:59:09 +02:00
|
|
|
if pip is None:
|
|
|
|
if env is None:
|
|
|
|
opt_dirs = []
|
|
|
|
else:
|
|
|
|
# Try pip with the virtualenv directory first.
|
|
|
|
opt_dirs = ['%s/bin' % env]
|
|
|
|
for basename in candidate_pip_basenames:
|
|
|
|
pip = module.get_bin_path(basename, False, opt_dirs)
|
|
|
|
if pip is not None:
|
|
|
|
break
|
|
|
|
# pip should have been found by now. The final call to get_bin_path will
|
|
|
|
# trigger fail_json.
|
|
|
|
if pip is None:
|
|
|
|
basename = candidate_pip_basenames[0]
|
|
|
|
pip = module.get_bin_path(basename, True, opt_dirs)
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
return pip
|
2012-08-08 16:35:07 +02:00
|
|
|
|
|
|
|
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
def _fail(module, cmd, out, err):
|
|
|
|
msg = ''
|
|
|
|
if out:
|
|
|
|
msg += "stdout: %s" % (out, )
|
|
|
|
if err:
|
|
|
|
msg += "\n:stderr: %s" % (err, )
|
|
|
|
module.fail_json(cmd=cmd, msg=msg)
|
|
|
|
|
|
|
|
|
2012-08-07 22:38:04 +02:00
|
|
|
def main():
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
state_map = dict(
|
|
|
|
present='install',
|
|
|
|
absent='uninstall -y',
|
|
|
|
latest='install -U',
|
2015-10-30 02:58:53 +01:00
|
|
|
forcereinstall='install -U --force-reinstall',
|
2012-08-07 22:38:04 +02:00
|
|
|
)
|
|
|
|
|
2012-08-12 00:13:29 +02:00
|
|
|
module = AnsibleModule(
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
argument_spec=dict(
|
|
|
|
state=dict(default='present', choices=state_map.keys()),
|
|
|
|
name=dict(default=None, required=False),
|
2014-07-13 06:05:32 +02:00
|
|
|
version=dict(default=None, required=False, type='str'),
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
requirements=dict(default=None, required=False),
|
|
|
|
virtualenv=dict(default=None, required=False),
|
2013-02-23 22:56:45 +01:00
|
|
|
virtualenv_site_packages=dict(default='no', type='bool'),
|
2013-02-20 14:52:02 +01:00
|
|
|
virtualenv_command=dict(default='virtualenv', required=False),
|
2015-01-28 12:45:25 +01:00
|
|
|
virtualenv_python=dict(default=None, required=False, type='str'),
|
2013-02-23 22:56:45 +01:00
|
|
|
use_mirrors=dict(default='yes', type='bool'),
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
extra_args=dict(default=None, required=False),
|
2015-08-30 16:24:13 +02:00
|
|
|
editable=dict(default='yes', type='bool', required=False),
|
2015-10-07 16:38:47 +02:00
|
|
|
chdir=dict(default=None, required=False, type='path'),
|
2013-08-16 13:02:23 +02:00
|
|
|
executable=dict(default=None, required=False),
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
),
|
|
|
|
required_one_of=[['name', 'requirements']],
|
|
|
|
mutually_exclusive=[['name', 'requirements']],
|
2013-02-27 21:23:35 +01:00
|
|
|
supports_check_mode=True
|
2012-08-12 00:13:29 +02:00
|
|
|
)
|
2012-08-07 22:38:04 +02:00
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
name = module.params['name']
|
|
|
|
version = module.params['version']
|
|
|
|
requirements = module.params['requirements']
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
extra_args = module.params['extra_args']
|
2015-01-28 12:45:25 +01:00
|
|
|
virtualenv_python = module.params['virtualenv_python']
|
2013-06-03 17:14:21 +02:00
|
|
|
chdir = module.params['chdir']
|
2012-08-07 22:38:04 +02:00
|
|
|
|
|
|
|
if state == 'latest' and version is not None:
|
2012-08-12 00:13:29 +02:00
|
|
|
module.fail_json(msg='version is incompatible with state=latest')
|
2012-08-07 22:38:04 +02:00
|
|
|
|
2015-10-07 18:18:36 +02:00
|
|
|
if chdir is None:
|
2015-10-07 18:19:50 +02:00
|
|
|
# this is done to avoid permissions issues with privilege escalation and virtualenvs
|
2015-10-07 18:18:36 +02:00
|
|
|
chdir = tempfile.gettempdir()
|
|
|
|
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
err = ''
|
|
|
|
out = ''
|
2012-08-07 22:38:04 +02:00
|
|
|
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
env = module.params['virtualenv']
|
2013-02-20 14:52:02 +01:00
|
|
|
virtualenv_command = module.params['virtualenv_command']
|
2012-08-07 22:38:04 +02:00
|
|
|
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
if env:
|
2013-03-14 04:58:04 +01:00
|
|
|
env = os.path.expanduser(env)
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
if not os.path.exists(os.path.join(env, 'bin', 'activate')):
|
2013-02-27 21:23:35 +01:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2015-01-23 03:05:54 +01:00
|
|
|
|
2015-01-28 12:45:25 +01:00
|
|
|
cmd = os.path.expanduser(virtualenv_command)
|
|
|
|
if os.path.basename(cmd) == cmd:
|
|
|
|
cmd = module.get_bin_path(virtualenv_command, True)
|
2015-01-23 03:05:54 +01:00
|
|
|
|
2013-02-23 19:59:52 +01:00
|
|
|
if module.params['virtualenv_site_packages']:
|
2015-01-28 12:45:25 +01:00
|
|
|
cmd += ' --system-site-packages'
|
2013-01-29 18:30:09 +01:00
|
|
|
else:
|
2015-01-28 12:45:25 +01:00
|
|
|
cmd_opts = _get_cmd_options(module, cmd)
|
2013-11-06 00:51:41 +01:00
|
|
|
if '--no-site-packages' in cmd_opts:
|
2015-01-28 12:45:25 +01:00
|
|
|
cmd += ' --no-site-packages'
|
|
|
|
|
|
|
|
if virtualenv_python:
|
|
|
|
cmd += ' -p%s' % virtualenv_python
|
|
|
|
|
|
|
|
cmd = "%s %s" % (cmd, env)
|
2015-10-07 16:38:47 +02:00
|
|
|
rc, out_venv, err_venv = module.run_command(cmd, cwd=chdir)
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
out += out_venv
|
|
|
|
err += err_venv
|
|
|
|
if rc != 0:
|
|
|
|
_fail(module, cmd, out, err)
|
|
|
|
|
2013-08-16 13:02:23 +02:00
|
|
|
pip = _get_pip(module, env, module.params['executable'])
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
|
|
|
|
cmd = '%s %s' % (pip, state_map[state])
|
2015-01-23 03:05:54 +01:00
|
|
|
|
2013-10-09 18:50:18 +02:00
|
|
|
# If there's a virtualenv we want things we install to be able to use other
|
2015-01-28 12:45:33 +01:00
|
|
|
# installations that exist as binaries within this virtualenv. Example: we
|
|
|
|
# install cython and then gevent -- gevent needs to use the cython binary,
|
|
|
|
# not just a python package that will be found by calling the right python.
|
2013-10-09 18:50:18 +02:00
|
|
|
# So if there's a virtualenv, we add that bin/ to the beginning of the PATH
|
|
|
|
# in run_command by setting path_prefix here.
|
|
|
|
path_prefix = None
|
|
|
|
if env:
|
2015-01-28 12:45:33 +01:00
|
|
|
path_prefix = "/".join(pip.split('/')[:-1])
|
2013-05-09 10:59:42 +02:00
|
|
|
|
2014-02-17 22:52:49 +01:00
|
|
|
# Automatically apply -e option to extra_args when source is a VCS url. VCS
|
|
|
|
# includes those beginning with svn+, git+, hg+ or bzr+
|
2015-08-06 15:24:41 +02:00
|
|
|
has_vcs = bool(name and re.match(r'(svn|git|hg|bzr)\+', name))
|
|
|
|
if has_vcs and module.params['editable']:
|
|
|
|
args_list = [] # used if extra_args is not used at all
|
|
|
|
if extra_args:
|
|
|
|
args_list = extra_args.split(' ')
|
|
|
|
if '-e' not in args_list:
|
|
|
|
args_list.append('-e')
|
|
|
|
# Ok, we will reconstruct the option string
|
|
|
|
extra_args = ' '.join(args_list)
|
2013-06-04 02:01:42 +02:00
|
|
|
|
2014-02-17 23:28:40 +01:00
|
|
|
if extra_args:
|
|
|
|
cmd += ' %s' % extra_args
|
|
|
|
if name:
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
cmd += ' %s' % _get_full_name(name, version)
|
|
|
|
elif requirements:
|
|
|
|
cmd += ' -r %s' % requirements
|
2015-01-23 03:05:54 +01:00
|
|
|
|
2014-03-10 22:11:24 +01:00
|
|
|
|
2014-03-26 15:08:23 +01:00
|
|
|
if module.check_mode:
|
2014-12-01 01:31:09 +01:00
|
|
|
if extra_args or requirements or state == 'latest' or not name:
|
2014-03-26 15:08:23 +01:00
|
|
|
module.exit_json(changed=True)
|
2015-08-06 15:24:41 +02:00
|
|
|
elif has_vcs:
|
2014-03-26 15:08:23 +01:00
|
|
|
module.exit_json(changed=True)
|
2015-01-23 03:05:54 +01:00
|
|
|
|
2014-03-26 15:08:23 +01:00
|
|
|
freeze_cmd = '%s freeze' % pip
|
2015-10-07 16:38:47 +02:00
|
|
|
|
|
|
|
rc, out_pip, err_pip = module.run_command(freeze_cmd, cwd=chdir)
|
2014-03-26 15:08:23 +01:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
|
|
|
out += out_pip
|
|
|
|
err += err_pip
|
|
|
|
|
|
|
|
is_present = _is_present(name, version, out.split())
|
|
|
|
|
2015-10-30 02:58:53 +01:00
|
|
|
changed = (state == 'present' and not is_present) or (state == 'absent' and is_present) or (state == 'forcereinstall' and is_present)
|
2014-03-26 15:08:23 +01:00
|
|
|
module.exit_json(changed=changed, cmd=freeze_cmd, stdout=out, stderr=err)
|
|
|
|
|
2015-08-06 15:24:41 +02:00
|
|
|
if requirements or has_vcs:
|
2015-08-06 14:34:25 +02:00
|
|
|
freeze_cmd = '%s freeze' % pip
|
|
|
|
out_freeze_before = module.run_command(freeze_cmd, cwd=chdir)[1]
|
|
|
|
else:
|
|
|
|
out_freeze_before = None
|
|
|
|
|
2015-10-07 16:38:47 +02:00
|
|
|
rc, out_pip, err_pip = module.run_command(cmd, path_prefix=path_prefix, cwd=chdir)
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
out += out_pip
|
|
|
|
err += err_pip
|
2015-07-22 01:07:25 +02:00
|
|
|
if rc == 1 and state == 'absent' and \
|
|
|
|
('not installed' in out_pip or 'not installed' in err_pip):
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
pass # rc is 1 when attempting to uninstall non-installed package
|
|
|
|
elif rc != 0:
|
|
|
|
_fail(module, cmd, out, err)
|
2012-08-07 22:38:04 +02:00
|
|
|
|
pip module improvements
- Do not silently ignore malformed pip requirements files.
- Properly reports changed when removing packages.
- "latest" i.e. --upgrade is *not* incompatible with requirements files.
- Less branchy, simpler logic.
- Removed pointless variable "initializations", Python doesn't need that.
Other code simplifications.
- Fun fact; pip install is (kind of) case insensitive, pip freeze is not.
So, 'sqlalchemy' will be reported as installed by install, but missing
by freeze.
The perhaps controversial change and the one that led to finding /
fixing above issues...
Instead of adding command parameters 'index', and 'find', and 'mirrors',
and etc. Added 'extra_args' which are passed onto pip.
The use case for --index-url is having a private pypi repo, like
http://pypi.python.org/pypi/localshop, to which you publish private
packages. I'm sure most every pip option has a use case for someone.
extra_args handles all those. Can reserve ansible command parameters for
the most common.
Tested with pip 1.1.
2012-12-12 22:40:25 +01:00
|
|
|
if state == 'absent':
|
|
|
|
changed = 'Successfully uninstalled' in out_pip
|
|
|
|
else:
|
2015-08-06 14:34:25 +02:00
|
|
|
if out_freeze_before is None:
|
|
|
|
changed = 'Successfully installed' in out_pip
|
|
|
|
else:
|
|
|
|
out_freeze_after = module.run_command(freeze_cmd, cwd=chdir)[1]
|
|
|
|
changed = out_freeze_before != out_freeze_after
|
2012-08-07 22:38:04 +02:00
|
|
|
|
|
|
|
module.exit_json(changed=changed, cmd=cmd, name=name, version=version,
|
2015-01-28 12:45:33 +01:00
|
|
|
state=state, requirements=requirements, virtualenv=env,
|
|
|
|
stdout=out, stderr=err)
|
2012-08-07 22:38:04 +02:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2012-08-07 22:38:04 +02:00
|
|
|
|
|
|
|
main()
|