forked from poise/poise-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_package.rb
More file actions
384 lines (349 loc) · 14.2 KB
/
Copy pathpython_package.rb
File metadata and controls
384 lines (349 loc) · 14.2 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'shellwords'
require 'chef/mixin/which'
require 'chef/provider/package'
require 'chef/resource/package'
require 'poise'
require 'poise_python/python_command_mixin'
module PoisePython
module Resources
# (see PythonPackage::Resource)
# @since 1.0.0
module PythonPackage
# A Python snippet to check which versions of things pip would try to
# install. Probably not 100% bulletproof.
PIP_HACK_SCRIPT = <<-EOH
import json
import re
import sys
import pip
# Don't use pkg_resources because I don't want to require it before this anyway.
if re.match(r'0|1|6\\.0', pip.__version__):
sys.stderr.write('The python_package resource requires pip >= 6.1.0, currently '+pip.__version__+'\\n')
sys.exit(1)
from pip.commands import InstallCommand
from pip.index import PackageFinder
from pip.req import InstallRequirement
packages = {}
cmd = InstallCommand()
options, args = cmd.parse_args(sys.argv[1:])
with cmd._build_session(options) as session:
if options.no_index:
index_urls = []
else:
index_urls = [options.index_url] + options.extra_index_urls
finder_options = dict(
find_links=options.find_links,
index_urls=index_urls,
allow_all_prereleases=options.pre,
process_dependency_links=options.process_dependency_links,
trusted_hosts=options.trusted_hosts,
session=session,
)
if getattr(options, 'format_control', None):
finder_options['format_control'] = options.format_control
finder = PackageFinder(**finder_options)
find_all = getattr(finder, 'find_all_candidates', getattr(finder, '_find_all_versions', None))
for arg in args:
req = InstallRequirement.from_line(arg)
found = finder.find_requirement(req, True)
all_candidates = find_all(req.name)
candidate = [c for c in all_candidates if c.location == found]
if candidate:
packages[candidate[0].project.lower()] = str(candidate[0].version)
json.dump(packages, sys.stdout)
EOH
# A `python_package` resource to manage Python installations using pip.
#
# @provides python_package
# @action install
# @action upgrade
# @action uninstall
# @example
# python_package 'django' do
# python '2'
# version '1.8.3'
# end
class Resource < Chef::Resource::Package
include PoisePython::PythonCommandMixin
provides(:python_package)
# Manually create matchers because #actions is unreliable.
%i{install upgrade remove}.each do |action|
Poise::Helpers::ChefspecMatchers.create_matcher(:python_package, action)
end
# @!attribute group
# System group to install the package.
# @return [String, Integer, nil]
attribute(:group, kind_of: [String, Integer, NilClass], default: lazy { default_group })
# @!attribute install_options
# Options string to be used with `pip install`.
# @return [String, Array<String>, nil, false]
attribute(:install_options, kind_of: [String, Array, NilClass, FalseClass], default: nil)
# @!attribute list_options
# Options string to be used with `pip list`.
# @return [String, Array<String>, nil, false]
attribute(:list_options, kind_of: [String, Array, NilClass, FalseClass], default: nil)
# @!attribute user
# System user to install the package.
# @return [String, Integer, nil]
attribute(:user, kind_of: [String, Integer, NilClass], default: lazy { default_user })
def initialize(*args)
super
# For older Chef.
@resource_name = :python_package
# We don't have these actions.
@allowed_actions.delete(:purge)
@allowed_actions.delete(:reconfig)
end
# Upstream attribute we don't support. Sets are an error and gets always
# return nil.
#
# @api private
# @param arg [Object] Ignored
# @return [nil]
def response_file(arg=nil)
raise NoMethodError if arg
end
# (see #response_file)
def response_file_variables(arg=nil)
raise NoMethodError if arg && arg != {}
end
# (see #response_file)
def source(arg=nil)
raise NoMethodError if arg
end
private
# Find a default group, if any, from the parent Python.
#
# @api private
# @return [String, Integer, nil]
def default_group
# Use an explicit is_a? hack because python_runtime is a container so
# it gets the whole DSL and this will always respond_to?(:group).
if parent_python && parent_python.is_a?(PoisePython::Resources::PythonVirtualenv::Resource)
parent_python.group
else
nil
end
end
# Find a default user, if any, from the parent Python.
#
# @api private
# @return [String, Integer, nil]
def default_user
# See default_group for explanation of is_a? hack grossness.
if parent_python && parent_python.is_a?(PoisePython::Resources::PythonVirtualenv::Resource)
parent_python.user
else
nil
end
end
end
# The default provider for the `python_package` resource.
#
# @see Resource
class Provider < Chef::Provider::Package
include PoisePython::PythonCommandMixin
provides(:python_package)
# Load current and candidate versions for all needed packages.
#
# @api private
# @return [Chef::Resource]
def load_current_resource
@current_resource = new_resource.class.new(new_resource.name, run_context)
current_resource.package_name(new_resource.package_name)
check_package_versions(current_resource)
current_resource
end
# Populate current and candidate versions for all needed packages.
#
# @api private
# @param resource [PoisePython::Resources::PythonPackage::Resource]
# Resource to load for.
# @param version [String, Array<String>] Current version(s) of package(s).
# @return [void]
def check_package_versions(resource, version=new_resource.version)
version_data = Hash.new {|hash, key| hash[key] = {current: nil, candidate: nil} }
# Get the version for everything currently installed.
list = pip_command('list', :list).stdout
parse_pip_list(list).each do |name, current|
# Merge current versions in to the data.
version_data[name][:current] = current
end
# Check for newer candidates.
outdated = pip_outdated(pip_requirements(resource.package_name, version, parse: true)).stdout
Chef::JSONCompat.parse(outdated).each do |name, candidate|
# Merge candidates in to the existing versions.
version_data[name][:candidate] = candidate
end
# Populate the current resource and candidate versions. Youch this is
# a gross mix of data flow.
if(resource.package_name.is_a?(Array))
@candidate_version = []
versions = []
[resource.package_name].flatten.each do |name|
ver = version_data[parse_package_name(name)]
versions << ver[:current]
@candidate_version << ver[:candidate]
end
resource.version(versions)
else
ver = version_data[parse_package_name(resource.package_name)]
resource.version(ver[:current])
@candidate_version = ver[:candidate]
end
end
# Install package(s) using pip.
#
# @param name [String, Array<String>] Name(s) of package(s).
# @param version [String, Array<String>] Version(s) of package(s).
# @return [void]
def install_package(name, version)
pip_install(name, version, upgrade: false)
end
# Upgrade package(s) using pip.
#
# @param name [String, Array<String>] Name(s) of package(s).
# @param version [String, Array<String>] Version(s) of package(s).
# @return [void]
def upgrade_package(name, version)
pip_install(name, version, upgrade: true)
end
# Uninstall package(s) using pip.
#
# @param name [String, Array<String>] Name(s) of package(s).
# @param version [String, Array<String>] Version(s) of package(s).
# @return [void]
def remove_package(name, version)
pip_command('uninstall', :install, %w{--yes} + [name].flatten)
end
private
# Convert name(s) and version(s) to an array of pkg_resources.Requirement
# compatible strings. These are strings like "django" or "django==1.0".
#
# @param name [String, Array<String>] Name or names for the packages.
# @param version [String, Array<String>] Version or versions for the
# packages.
# @param parse [Boolean] Use parsed package names.
# @return [Array<String>]
def pip_requirements(name, version, parse: false)
[name].flatten.zip([version].flatten).map do |n, v|
n = parse_package_name(n) if parse
v = v.to_s.strip
if n =~ /:\/\//
# Probably a URI.
n
elsif v.empty?
# No version requirement, send through unmodified.
n
elsif v =~ /^\d/
"#{n}==#{v}"
else
# If the first character isn't a digit, assume something fancy.
n + v
end
end
end
# Run a pip command.
#
# @param pip_command [String, nil] The pip subcommand to run (eg. install).
# @param options_type [Symbol] Either `:install` to `:list` to select
# which extra options to use.
# @param pip_options [Array<String>] Options for the pip command.
# @param opts [Hash] Mixlib::ShellOut options.
# @return [Mixlib::ShellOut]
def pip_command(pip_command, options_type, pip_options=[], opts={})
runner = opts.delete(:pip_runner) || %w{-m pip.__main__}
type_specific_options = new_resource.send(:"#{options_type}_options")
full_cmd = if new_resource.options || type_specific_options
if (new_resource.options && new_resource.options.is_a?(String)) || (type_specific_options && type_specific_options.is_a?(String))
# We have to use a string for this case to be safe because the
# options are a string and I don't want to try and parse that.
global_options = new_resource.options.is_a?(Array) ? Shellwords.join(new_resource.options) : new_resource.options.to_s
type_specific_options = type_specific_options.is_a?(Array) ? Shellwords.join(type_specific_options) : type_specific_options.to_s
"#{runner.join(' ')} #{pip_command} #{global_options} #{type_specific_options} #{Shellwords.join(pip_options)}"
else
runner + (pip_command ? [pip_command] : []) + (new_resource.options || []) + (type_specific_options || []) + pip_options
end
else
# No special options, use an array to skip the extra /bin/sh.
runner + (pip_command ? [pip_command] : []) + pip_options
end
# Set user and group.
opts[:user] = new_resource.user if new_resource.user
opts[:group] = new_resource.group if new_resource.group
python_shell_out!(full_cmd, opts)
end
# Run `pip install` to install a package(s).
#
# @param name [String, Array<String>] Name(s) of package(s) to install.
# @param version [String, Array<String>] Version(s) of package(s) to
# install.
# @param upgrade [Boolean] Use upgrade mode?
# @return [Mixlib::ShellOut]
def pip_install(name, version, upgrade: false)
cmd = pip_requirements(name, version)
# Prepend --upgrade if needed.
cmd = %w{--upgrade} + cmd if upgrade
pip_command('install', :install, cmd)
end
# Run my hacked version of `pip list --outdated` with a specific set of
# package requirements.
#
# @see #pip_requirements
# @param requirements [Array<String>] Pip-formatted package requirements.
# @return [Mixlib::ShellOut]
def pip_outdated(requirements)
pip_command(nil, :list, requirements, input: PIP_HACK_SCRIPT, pip_runner: %w{-})
end
# Parse the output from `pip list`. Returns a hash of package key to
# current version.
#
# @param text [String] Output to parse.
# @return [Hash<String, String>]
def parse_pip_list(text)
text.split(/\r?\n/).inject({}) do |memo, line|
# Example of a line:
# boto (2.25.0)
if md = line.match(/^(\S+)\s+\(([^\s,]+).*\)$/i)
memo[parse_package_name(md[1])] = md[2]
else
Chef::Log.debug("[#{new_resource}] Unparsable line in pip list: #{line}")
end
memo
end
end
# Regexp for package URLs.
PACKAGE_NAME_URL = /:\/\/.*?#egg=(.*)$/
# Regexp for extras.
PACKAGE_NAME_EXTRA = /^(.*?)\[.*?\]$/
# Find the underlying name from a pip input sequence.
#
# @param raw_name [String] Raw package name.
# @return [String]
def parse_package_name(raw_name)
case raw_name
when PACKAGE_NAME_URL, PACKAGE_NAME_EXTRA
$1
else
raw_name
end.downcase.gsub(/_/, '-')
end
end
end
end
end