Skip to content

Commit

Permalink
Merge pull request #4 from criteo-cookbooks/updates
Browse files Browse the repository at this point in the history
Fixing support of multiple versions
  • Loading branch information
achamo authored Oct 21, 2022
2 parents 8e91cff + 6c23a5f commit e67ebc2
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 29 deletions.
1 change: 1 addition & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
default['python3']['pip']['url'] = 'https://bootstrap.pypa.io/pip/3.6/get-pip.py'
default['python3']['pip']['checksum'] = '0bd6aa5c457b84958cebfe1bd34aec9fa98212a65fe962dbed1195425aea58e1'
default['python3']['pip']['version'] = '21.3.1'
default['python3']['pip']['binary_name'] = 'pip3'
default['python3']['pip']['setuptools_version'] = true
default['python3']['pip']['wheel_version'] = true
default['python3']['pip']['virtualenv_version'] = true
Expand Down
18 changes: 18 additions & 0 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ suites:
attributes:
python3:
name: 'python39'
- name: python39-36
run_list:
- recipe[python3-test::default]
excludes:
- centos-7
attributes:
python3_test:
pythons:
-
name: 'python3'
binary_name: 'python3.6'
pip_binary_name: 'pip3.6'
-
name: 'python39'
binary_name: 'python3.9'
pip_binary_name: 'pip3.9'
- name: python3-pypy3

run_list:
Expand All @@ -42,6 +58,8 @@ suites:
source: 'portable_pypy3'
version: '3.9-v7.3.9'
checksum: '46818cb3d74b96b34787548343d266e2562b531ddbaf330383ba930ff1930ed5'
pip_binary_name: 'pip3.9'
binary_name: 'pypy3.9'
- name: python3-chef-14
excludes:
# dnf provider on Chef < 16 is broken
Expand Down
13 changes: 6 additions & 7 deletions libraries/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ def self.pip_version(resource)
end

module Pip
def self.packages_versions(base_path)
cmd = ::Mixlib::ShellOut.new("PIP_FORMAT=json #{path(base_path)} list").run_command
def self.packages_versions(resource)
cmd = ::Mixlib::ShellOut.new("PIP_FORMAT=json #{path(resource)} list").run_command
::JSON.parse(cmd.stdout).collect { |x| [x['name'].downcase, x['version']] }.to_h if cmd.exitstatus.zero?
end

def self.check_package_version(package, base_path = '')
packages_versions(base_path)&.fetch(package, nil)
def self.check_package_version(package, resource)
packages_versions(resource)&.fetch(package, nil)
end

def self.path(base_path)
base_path = '/usr/local' if base_path.nil? || base_path.empty?
::File.join(base_path, 'bin', 'pip3')
def self.path(resource)
::File.join(::Python3::Path.virtualenv(resource), 'bin', resource.pip_binary_name)
end
end
end
6 changes: 5 additions & 1 deletion libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ def self.python_system_path(resource = new_resource)
end

def self.virtualenv(resource = new_resource)
resource.virtualenv || python_path(resource)
if resource.respond_to?(:virtualenv) && !resource.virtualenv.nil?
resource.virtualenv
else
python_path(resource)
end
end

def self.pip_path(resource = new_resource, system: false)
Expand Down
4 changes: 4 additions & 0 deletions resources/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
property :get_pip_url, String, default: lazy { node['python3']['pip']['url'] }
property :get_pip_checksum, String, default: lazy { node['python3']['pip']['checksum'] }
property :pip_version, [String, TrueClass, FalseClass], default: lazy { node['python3']['pip']['version'] }
property :pip_binary_name, [String, TrueClass, FalseClass], default: lazy { node['python3']['pip']['binary_name'] }
property :setuptools_version, [String, TrueClass, FalseClass], default: true
property :wheel_version, [String, TrueClass, FalseClass], default: true
property :virtualenv_version, [String, TrueClass, FalseClass], default: true
Expand Down Expand Up @@ -63,13 +64,16 @@
get_pip_checksum new_resource.get_pip_checksum
get_pip_url new_resource.get_pip_url
pip_version new_resource.pip_version
pip_binary_name new_resource.pip_binary_name
setuptools_version new_resource.setuptools_version
wheel_version new_resource.wheel_version
virtualenv_version new_resource.virtualenv_version

python_provider new_resource.source
python_version new_resource.version
python_checksum new_resource.checksum
python_name new_resource.name
binary_name new_resource.binary_name
end
end
end
13 changes: 7 additions & 6 deletions resources/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
property :python_version, [String, FalseClass], default: lazy { node['python3']['version'] }
property :python_provider, [String, nil], default: lazy { node['python3']['source'] }

property :pip_binary_name, [String, TrueClass, FalseClass], default: lazy { node['python3']['pip']['binary_name'] }

load_current_value do |new_resource|
packages = ::Kernel.Array(new_resource.package_name)
current_versions = []
packages.each do |pkg|
v = ::Python3::Pip.check_package_version(pkg.downcase, ::Python3::Path.virtualenv(new_resource))
v = ::Python3::Pip.check_package_version(pkg.downcase, new_resource)
current_value_does_not_exist! if v.nil?
current_versions << v
end
Expand All @@ -31,21 +33,20 @@
action :install do
converge_if_changed :version do
converge_by("Install #{new_resource.package_name}") do
venv_path = ::Python3::Path.virtualenv(new_resource)
if new_resource.package_name.is_a?(String)
install_package(venv_path, new_resource.package_name, new_resource.version)
install_package(new_resource, new_resource.package_name, new_resource.version)
else
new_resource.package_name.each_with_index do |pkg, i|
version = new_resource.version.to_a.at(i)
install_package(venv_path, pkg, version)
install_package(new_resource, pkg, version)
end
end
end
end
end

def install_package(virtualenv, pkg, version)
def install_package(resource, pkg, version)
pkg += "==#{version}" if version

Mixlib::ShellOut.new("#{::Python3::Pip.path(virtualenv)} install #{pkg}").run_command
Mixlib::ShellOut.new("#{::Python3::Pip.path(resource)} install #{pkg}").run_command
end
17 changes: 13 additions & 4 deletions resources/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@
property :get_pip_url, String, default: lazy { node['python3']['pip']['url'] }
property :get_pip_checksum, String, default: lazy { node['python3']['pip']['checksum'] }
property :pip_version, [String, TrueClass, FalseClass], desired_state: true, identity: true, default: lazy { node['python3']['pip']['version'] }
property :pip_binary_name, [String, TrueClass, FalseClass], default: lazy { node['python3']['pip']['binary_name'] }
property :setuptools_version, [String, TrueClass, FalseClass], default: true
property :wheel_version, [String, TrueClass, FalseClass], default: true
property :virtualenv_version, [String, TrueClass, FalseClass], default: true

property :python_version, [String, FalseClass], default: lazy { node['python3']['version'] }
property :python_provider, [String, FalseClass], default: lazy { node['python3']['source'] }
property :python_checksum, [String, FalseClass], default: lazy { node['python3']['checksum'] }
property :python_name, [String, FalseClass], default: lazy { node['python3']['name'] }
property :binary_name, String, default: lazy { node['python3']['binary_name'] }

load_current_value do |new_resource|
version = ::Python3.pip_version(new_resource)
current_value_does_not_exist! if version.nil?

pip_version version
setuptools_version(::Python3::Pip.check_package_version('setuptools') || false)
wheel_version(::Python3::Pip.check_package_version('wheel') || false)
virtualenv_version(::Python3::Pip.check_package_version('virtualenv') || false)
setuptools_version(::Python3::Pip.check_package_version('setuptools', new_resource) || false)
wheel_version(::Python3::Pip.check_package_version('wheel', new_resource) || false)
virtualenv_version(::Python3::Pip.check_package_version('virtualenv', new_resource) || false)
end

action :install do
get_pip_location = ::File.join(::Chef::Config['cache_path'], 'get-pip.py')

python_install node['python3']['name'] do
python_install new_resource.python_name do
version new_resource.python_version
source new_resource.python_provider
checksum new_resource.python_checksum
pip_version new_resource.pip_version
pip_binary_name new_resource.pip_binary_name
binary_name new_resource.binary_name
not_if { ::File.exist?(::Python3::Path.python_path(new_resource)) }
end

converge_if_changed :pip_version do
Expand All @@ -54,6 +60,7 @@
version new_resource.setuptools_version if new_resource.setuptools_version.is_a?(String)
python_provider new_resource.python_provider
python_version new_resource.python_version
pip_binary_name new_resource.pip_binary_name
not_if { new_resource.setuptools_version == false }
end
end
Expand All @@ -63,6 +70,7 @@
version new_resource.wheel_version if new_resource.wheel_version.is_a?(String)
python_provider new_resource.python_provider
python_version new_resource.python_version
pip_binary_name new_resource.pip_binary_name
not_if { new_resource.wheel_version == false }
end
end
Expand All @@ -72,6 +80,7 @@
version new_resource.virtualenv_version if new_resource.virtualenv_version.is_a?(String)
python_provider new_resource.python_provider
python_version new_resource.python_version
pip_binary_name new_resource.pip_binary_name
not_if { new_resource.virtualenv_version == false }
end
end
Expand Down
8 changes: 6 additions & 2 deletions resources/virtualenv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
property :system_site_packages, equal_to: [true, false], default: false
property :group, [String, Integer, NilClass]
property :user, [String, Integer, NilClass]
property :pip_version, String
property :pip_version, [String, TrueClass, FalseClass], default: lazy { node['python3']['pip']['version'] }
property :pip_binary_name, [String, TrueClass, FalseClass], default: lazy { node['python3']['pip']['binary_name'] }

property :python_version, [String, FalseClass], default: lazy { node['python3']['version'] }
property :python_provider, [String, FalseClass], default: lazy { node['python3']['source'] }
property :python_checksum, [String, FalseClass], default: lazy { node['python3']['checksum'] }
property :binary_name, [String, FalseClass], default: lazy { node['python3']['binary_name'] }

load_current_value do |new_resource|
current_value_does_not_exist! unless ::File.exist?(::File.join(new_resource.virtualenv, 'bin/activate'))
Expand All @@ -21,8 +23,10 @@
checksum new_resource.python_checksum
end

cmd = "#{::Python3::Path.virtualenv_binary(new_resource)} #{new_resource.virtualenv}"
cmd = ::Python3::Path.virtualenv_binary(new_resource)
cmd += " --pip #{new_resource.pip_version}" if new_resource.pip_version
cmd += " --python #{new_resource.binary_name}" if new_resource.binary_name
cmd += " #{new_resource.virtualenv}"

execute cmd do
user new_resource.user
Expand Down
6 changes: 6 additions & 0 deletions test/cookbooks/python3-test/attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
default['python3_test']['pythons'] = [
{ name: node['python3']['name'],
version: node['python3']['version'],
binary_name: node['python3']['binary_name'],
pip_binary_name: node['python3']['pip']['binary_name'] },
]
28 changes: 19 additions & 9 deletions test/cookbooks/python3-test/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
#
# Copyright:: 2022, The Authors, All Rights Reserved.

python_install node['python3']['name']
node['python3_test']['pythons'].each_with_index do |python, idx|
python_install python['name'] do
pip_binary_name python['pip_binary_name']
binary_name python['binary_name']
version python['version']
end

python_package 'flask' do
version '2.0.1'
end
python_package 'flask' do
version '2.0.1'
pip_binary_name python['pip_binary_name']
end

python_virtualenv '/opt/blah' do
action :create
end
python_virtualenv "/opt/blah-#{idx}" do
action :create
pip_binary_name python['pip_binary_name']
binary_name python['binary_name']
end

python_package 'flask' do
virtualenv '/opt/blah'
python_package 'flask' do
virtualenv "/opt/blah-#{idx}"
pip_binary_name python['pip_binary_name']
end
end

0 comments on commit e67ebc2

Please sign in to comment.