Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 190 lines (137 sloc) 5.331 kB
4de0e5d @mattupstate add pip module
mattupstate authored
1 #!/usr/bin/python
0116e53 @mattupstate Updates per ansible/ansible#796
mattupstate authored
2 # -*- coding: utf-8 -*-
3
4 # (c) 2012, Matt Wright <matt@nobien.net>
5 #
6 # This file is part of Ansible
7 #
8 # Ansible is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Ansible is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
20 #
4de0e5d @mattupstate add pip module
mattupstate authored
21
22
23 def _get_full_name(name, version=None):
9ad91d7 @cybertoast Fix syntax to work on Python2.4
cybertoast authored
24 if version is None:
25 resp = name
26 else:
27 resp = name + '==' + version
28 return resp
4de0e5d @mattupstate add pip module
mattupstate authored
29
30
487d07a Fix variable scope
Sébastien Bocahu authored
31 def _find_pip(module, env):
4de0e5d @mattupstate add pip module
mattupstate authored
32 paths = ['/usr/local/bin', '/usr/bin']
33
487d07a Fix variable scope
Sébastien Bocahu authored
34 if env:
35 paths = [os.path.join(env, 'bin')] + paths
4de0e5d @mattupstate add pip module
mattupstate authored
36
37 for p in paths:
38 pe = p + '/pip'
39 if os.path.exists(pe):
40 return pe
41
42 module.fail_json(msg='pip is not installed')
43
44
487d07a Fix variable scope
Sébastien Bocahu authored
45 def _find_virtualenv(module):
4de0e5d @mattupstate add pip module
mattupstate authored
46 paths = ['/usr/local/bin', '/usr/bin']
47
48 for p in paths:
49 ve = p + '/virtualenv'
50 if os.path.exists(ve):
51 return ve
52
53 module.fail_json(msg='virtualenv is not installed')
54
55
487d07a Fix variable scope
Sébastien Bocahu authored
56 def _ensure_virtualenv(module, env, virtualenv):
57 if os.path.exists(os.path.join(env, 'bin', 'activate')):
4de0e5d @mattupstate add pip module
mattupstate authored
58 return 0, '', ''
59 else:
487d07a Fix variable scope
Sébastien Bocahu authored
60 return _run('%s %s' % (virtualenv, env))
4de0e5d @mattupstate add pip module
mattupstate authored
61
62
487d07a Fix variable scope
Sébastien Bocahu authored
63 def _is_package_installed(name, pip, version=None):
64 rc, status_stdout, status_stderr = _run('%s freeze' % pip)
4de0e5d @mattupstate add pip module
mattupstate authored
65 return _get_full_name(name, version).lower() in status_stdout.lower()
66
67
0116e53 @mattupstate Updates per ansible/ansible#796
mattupstate authored
68 def _did_install(out):
69 return 'Successfully installed' in out
70
71
4de0e5d @mattupstate add pip module
mattupstate authored
72 def _run(cmd):
73 # returns (rc, stdout, stderr) from shell command
74 process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
75 stderr=subprocess.PIPE, shell=True)
76 stdout, stderr = process.communicate()
77 return (process.returncode, stdout, stderr)
78
79
80 def main():
487d07a Fix variable scope
Sébastien Bocahu authored
81 pip = None
82 virtualenv = None
83 env = None
84
4de0e5d @mattupstate add pip module
mattupstate authored
85 arg_spec = dict(
86 state=dict(default='present', choices=['absent', 'present', 'latest']),
87 name=dict(default=None, required=False),
88 version=dict(default=None, required=False),
89 requirements=dict(default=None, required=False),
90 virtualenv=dict(default=None, required=False)
91 )
92
1e4d45a @mpdehaan Add module common code to allow it to be easier to indicate whether a…
mpdehaan authored
93 module = AnsibleModule(
94 argument_spec=arg_spec,
95 required_one_of=[['name','requirements']],
96 mutually_exclusive=[['name','requirements']],
97 )
4de0e5d @mattupstate add pip module
mattupstate authored
98
99 rc = 0
100 err = ''
101 out = ''
102
487d07a Fix variable scope
Sébastien Bocahu authored
103 env = module.params['virtualenv']
104 pip = _find_pip(module, env)
4de0e5d @mattupstate add pip module
mattupstate authored
105
487d07a Fix variable scope
Sébastien Bocahu authored
106 if env:
107 virtualenv = _find_virtualenv(module)
4de0e5d @mattupstate add pip module
mattupstate authored
108
487d07a Fix variable scope
Sébastien Bocahu authored
109 rc_venv, out_venv, err_venv = _ensure_virtualenv(module, env, virtualenv)
4de0e5d @mattupstate add pip module
mattupstate authored
110
111 rc += rc_venv
112 out += out_venv
113 err += err_venv
114
115 state = module.params['state']
116 name = module.params['name']
117 version = module.params['version']
118 requirements = module.params['requirements']
119 command_map = dict(present='install', absent='uninstall', latest='install')
120
121 if state == 'latest' and version is not None:
1e4d45a @mpdehaan Add module common code to allow it to be easier to indicate whether a…
mpdehaan authored
122 module.fail_json(msg='version is incompatible with state=latest')
4de0e5d @mattupstate add pip module
mattupstate authored
123
124 if state == 'latest' and requirements is not None:
1e4d45a @mpdehaan Add module common code to allow it to be easier to indicate whether a…
mpdehaan authored
125 module.fail_json(msg='requirements is incompatible with state=latest')
126
127 if name is not None and '=' in name:
128 module.fail_json(msg='versions must be specified in the version= parameter')
4de0e5d @mattupstate add pip module
mattupstate authored
129
130 cmd = None
131 installed = None
132
133 if requirements:
1e4d45a @mpdehaan Add module common code to allow it to be easier to indicate whether a…
mpdehaan authored
134
487d07a Fix variable scope
Sébastien Bocahu authored
135 cmd = '%s %s -r %s --use-mirrors' % (pip, command_map[state], requirements)
4de0e5d @mattupstate add pip module
mattupstate authored
136 rc_pip, out_pip, err_pip = _run(cmd)
137
138 rc += rc_pip
139 out += out_pip
140 err += err_pip
141
0116e53 @mattupstate Updates per ansible/ansible#796
mattupstate authored
142 changed = ((_did_install(out) and state == 'present') or
143 (not _did_install(out) and state == 'absent'))
4de0e5d @mattupstate add pip module
mattupstate authored
144
145 if name and state == 'latest':
146
1e4d45a @mpdehaan Add module common code to allow it to be easier to indicate whether a…
mpdehaan authored
147 cmd = '%s %s %s --upgrade' % (pip, command_map[state], name)
4de0e5d @mattupstate add pip module
mattupstate authored
148 rc_pip, out_pip, err_pip = _run(cmd)
149
150 rc += rc_pip
151 out += out_pip
152 err += err_pip
153
154 changed = 'Successfully installed' in out_pip
155
156 elif name:
1e4d45a @mpdehaan Add module common code to allow it to be easier to indicate whether a…
mpdehaan authored
157
487d07a Fix variable scope
Sébastien Bocahu authored
158 installed = _is_package_installed(name, pip, version)
4de0e5d @mattupstate add pip module
mattupstate authored
159 changed = ((installed and state == 'absent') or
160 (not installed and state == 'present'))
161
162 if changed:
66a6231 @cybertoast Further syntax fix work on Python2.4
cybertoast authored
163 if state == 'present':
164 full_name = _get_full_name(name, version)
165 else:
166 full_name = name
4de0e5d @mattupstate add pip module
mattupstate authored
167
487d07a Fix variable scope
Sébastien Bocahu authored
168 cmd = '%s %s %s' % (pip, command_map[state], full_name)
4de0e5d @mattupstate add pip module
mattupstate authored
169
170 if state == 'absent':
171 cmd = cmd + ' -y'
172 else:
173 cmd = cmd + ' --use-mirrors'
174
175 rc_pip, out_pip, err_pip = _run(cmd)
1e4d45a @mpdehaan Add module common code to allow it to be easier to indicate whether a…
mpdehaan authored
176 rc += rc_pip
4de0e5d @mattupstate add pip module
mattupstate authored
177 out += out_pip
178 err += err_pip
179
180 if rc != 0:
181 module.fail_json(msg=err, cmd=cmd)
182
183 module.exit_json(changed=changed, cmd=cmd, name=name, version=version,
487d07a Fix variable scope
Sébastien Bocahu authored
184 state=state, requirements=requirements, virtualenv=env)
4de0e5d @mattupstate add pip module
mattupstate authored
185
186 # this is magic, see lib/ansible/module_common.py
187 #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
188
189 main()
Something went wrong with that request. Please try again.