forked from UDST/ansible-conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconda.py
executable file
·302 lines (239 loc) · 8.19 KB
/
conda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = """
---
module: conda
short_description: Manage Python libraries via conda
description:
>
Manage Python libraries via conda.
Can install, update, and remove packages.
author: Synthicity
notes:
>
Requires conda to already be installed.
Will look under the home directory for a conda executable.
options:
name:
description: The name of a Python library to install
required: true
default: null
version:
description: A specific version of a library to install
required: false
default: null
state:
description: State in which to leave the Python package
required: false
default: present
choices: [ "present", "absent", "latest" ]
channels:
description: Extra channels to use when installing packages
required: false
default: null
executable:
description: Full path to the conda executable
required: false
default: null
extra_args:
description: Extra arguments passed to conda
required: false
default: null
"""
EXAMPLES = """
- name: install numpy via conda
conda: name=numpy state=latest
- name: install scipy 0.14 via conda
conda: name=scipy version="0.14"
- name: remove matplotlib from conda
conda: name=matplotlib state=absent
"""
from distutils.spawn import find_executable
import os.path
import json
def _find_conda(module, executable):
"""
If `executable` is not None, checks whether it points to a valid file
and returns it if this is the case. Otherwise tries to find the `conda`
executable in the path. Calls `fail_json` if either of these fail.
"""
if not executable:
conda = find_executable('conda')
if conda:
return conda
else:
if os.path.isfile(os.path.expanduser(executable)):
return executable
module.fail_json(msg="could not find conda executable")
def _add_channels_to_command(command, channels):
"""
Add extra channels to a conda command by splitting the channels
and putting "--channel" before each one.
"""
if channels:
channels = channels.strip().split()
dashc = []
for channel in channels:
dashc.append('--channel')
dashc.append(channel)
return command[:2] + dashc + command[2:]
else:
return command
def _add_extras_to_command(command, extras):
"""
Add extra arguments to a conda command by splitting the arguments
on white space and inserting them after the second item in the command.
"""
if extras:
extras = extras.strip().split()
return command[:2] + extras + command[2:]
else:
return command
def _check_installed(module, conda, name):
"""
Check whether a package is installed. Returns (bool, version_str).
"""
command = [
conda,
'list',
'^' + name + '$',
'--json'
]
command = _add_extras_to_command(command, module.params['extra_args'])
rc, stdout, stderr = module.run_command(command)
if rc != 0:
return False, None
installed = False
version = None
data = json.loads(stdout)
if data:
# At this point data will be a list of len 1, with the element of
# the format: "channel::package-version-py35_1"
line = data[0]
if "::" in line:
channel, other = line.split('::')
else:
other = line
if isinstance(other, dict):
pname = other.get('name', '')
pversion = other.get('version', '')
else:
# split carefully as some package names have "-" in them (scikit-learn)
pname, pversion, pdist = other.rsplit('-', 2)
if pname == name: # verify match for safety
installed = True
version = pversion
return installed, version
def _remove_package(module, conda, installed, name):
"""
Use conda to remove a given package if it is installed.
"""
if module.check_mode and installed:
module.exit_json(changed=True)
if not installed:
module.exit_json(changed=False)
command = [
conda,
'remove',
'--yes',
name
]
command = _add_extras_to_command(command, module.params['extra_args'])
rc, stdout, stderr = module.run_command(command)
if rc != 0:
module.fail_json(msg='failed to remove package ' + name, stderr=stderr)
module.exit_json(changed=True, name=name, stdout=stdout, stderr=stderr)
def _install_package(
module, conda, installed, name, version, installed_version):
"""
Install a package at a specific version, or install a missing package at
the latest version if no version is specified.
"""
if installed and (version is None or installed_version == version):
module.exit_json(changed=False, name=name, version=version)
if module.check_mode:
if not installed or (installed and installed_version != version):
module.exit_json(changed=True)
if version:
install_str = name + '=' + version
else:
install_str = name
command = [
conda,
'install',
'--yes',
install_str
]
command = _add_channels_to_command(command, module.params['channels'])
command = _add_extras_to_command(command, module.params['extra_args'])
rc, stdout, stderr = module.run_command(command)
if rc != 0:
module.fail_json(msg='failed to install package ' + name, stderr=stderr)
module.exit_json(
changed=True, name=name, version=version, stdout=stdout, stderr=stderr)
def _update_package(module, conda, installed, name):
"""
Make sure an installed package is at its latest version.
"""
if not installed:
module.fail_json(msg='can\'t update a package that is not installed')
# see if it's already installed at the latest version
command = [
conda,
'update',
'--dry-run',
name
]
command = _add_channels_to_command(command, module.params['channels'])
command = _add_extras_to_command(command, module.params['extra_args'])
rc, stdout, stderr = module.run_command(command)
if rc != 0:
module.fail_json(msg='can\'t update a package that is not installed', stderr=stderr)
if 'requested packages already installed' in stdout:
module.exit_json(changed=False, name=name)
# now we're definitely gonna update the package
if module.check_mode:
module.exit_json(changed=True, name=name)
command = [
conda,
'update',
'--yes',
name
]
command = _add_channels_to_command(command, module.params['channels'])
command = _add_extras_to_command(command, module.params['extra_args'])
rc, stdout, stderr = module.run_command(command)
if rc != 0:
module.fail_json(msg='failed to update package ' + name, stderr=stderr)
module.exit_json(changed=True, name=name, stdout=stdout, stderr=stderr)
def main():
module = AnsibleModule(
argument_spec={
'name': {'required': True, 'type': 'str'},
'version': {'default': None, 'required': False, 'type': 'str'},
'state': {
'default': 'present',
'required': False,
'choices': ['present', 'absent', 'latest']
},
'channels': {'default': None, 'required': False},
'executable': {'default': None, 'required': False},
'extra_args': {'default': None, 'required': False, 'type': 'str'}
},
supports_check_mode=True)
conda = _find_conda(module, module.params['executable'])
name = module.params['name']
state = module.params['state']
version = module.params['version']
installed, installed_version = _check_installed(module, conda, name)
if state == 'absent':
_remove_package(module, conda, installed, name)
elif state == 'present' or (state == 'latest' and not installed):
_install_package(
module, conda, installed, name, version, installed_version)
elif state == 'latest':
_update_package(module, conda, installed, name)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()