Skip to content

Commit

Permalink
Merge pull request #7 from criteo-cookbooks/updates
Browse files Browse the repository at this point in the history
Handle better pip changes
  • Loading branch information
achamo authored Jan 4, 2023
2 parents f47610b + dee6daf commit 6982394
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Policyfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

run_list ['python3']

named_run_list :test, run_list + ['recipe[python3-test]']
named_run_list :test, ['recipe[python3-test]']

cookbook 'python3', path: '.'
cookbook 'python3-test', path: 'test/cookbooks/python3-test'
37 changes: 18 additions & 19 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ def self.python_binary(resource)
def self.python_path(resource = new_resource)
return ::File.join(resource.virtualenv, 'bin/python3') if resource.respond_to?(:virtualenv) && !resource.virtualenv.nil?

if resource.python_provider == 'portable_pypy3'
"/usr/local/pypy#{resource.python_version}"
if (pypy = pypy_version(resource))
"/usr/local/pypy#{pypy}"
else
'/usr/local/'
end
end

def self.python_system_path(resource = new_resource)
if resource.respond_to?(:python_provider)
provider = resource.python_provider
version = resource.python_version
if (pypy = pypy_version(resource))
::File.join("/usr/local/pypy#{pypy}")
else
provider = resource.source
version = resource.version
'/usr'
end

provider == 'portable_pypy3' ? ::File.join("/usr/local/pypy#{version}") : '/usr'
end

def self.virtualenv(resource = new_resource)
Expand All @@ -41,23 +37,26 @@ def self.virtualenv(resource = new_resource)
end

def self.pip_path(resource = new_resource, system: false)
path = if resource.respond_to?(:virtualenv) && !resource.virtualenv.nil? && !system
resource.virtualenv
elsif resource.python_provider == 'portable_pypy3'
"/usr/local/pypy#{resource.python_version}"
else
'/usr/local/'
end

::File.join(path)
if resource.respond_to?(:virtualenv) && !resource.virtualenv.nil? && !system
resource.virtualenv
elsif (pypy = pypy_version(resource))
"/usr/local/pypy#{pypy}"
else
'/usr/local/'
end
end

def self.pip_binary(resource = new_resource)
::File.join(pip_path(resource), 'bin/pip3')
::File.join(pip_path(resource, system: false), 'bin', resource.pip_binary_name)
end

def self.virtualenv_binary(resource = new_resource)
::File.join(pip_path(resource, system: true), 'bin/virtualenv')
end

def self.pypy_version(resource = new_resource)
return resource.version if resource.respond_to?(:source) && resource.source == 'portable_pypy3'
return resource.python_version if resource.respond_to?(:python_provider) && resource.python_provider == 'portable_pypy3'
end
end
end
17 changes: 13 additions & 4 deletions resources/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,28 @@

load_current_value do |new_resource|
python_binary = ::Python3::Path.python_binary(new_resource)
pip_binary = ::Python3::Path.pip_binary(new_resource)

current_value_does_not_exist! unless ::File.exist?(python_binary)
current_value_does_not_exist! unless ::File.exist?(python_binary) && ::File.exist?(pip_binary)

if new_resource.source != 'portable_pypy3'
pyversion = ::Mixlib::ShellOut.new("#{python_binary} --version").run_command.stdout.match('\s+(^[0-9\.]+)')&.last_match(1)
version pyversion if new_resource.version
python_version = pyversion(python_binary)
pipv = pyversion(pip_binary)

version python_version if new_resource.version
pip_version pipv if new_resource.pip_version
else
version new_resource.version
pip_version new_resource.pip_version
end
end

def pyversion(binary)
return Regexp.last_match(1) if ::Mixlib::ShellOut.new("#{binary} --version").run_command.stdout.match('\s+([0-9\.]+)')
end

action :install do
converge_if_changed :version do
converge_if_changed :version, :pip_version do
valid_name_regex = /^python3\.?\d?$/
if new_resource.source == 'portable_pypy3'
package 'bzip2'
Expand Down
2 changes: 2 additions & 0 deletions test/cookbooks/python3-test/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
version '0.1.0'
supports 'centos'
supports 'windows'

depends 'python3'

0 comments on commit 6982394

Please sign in to comment.