From 85af288e209a67baf88e169c54a87b4481dc8f4e Mon Sep 17 00:00:00 2001 From: Emmanuel Guerin Date: Thu, 26 Jan 2023 23:28:23 +0100 Subject: [PATCH 1/2] Fix pip installation for python 3.6 The previous installation was causing trouble with python -m venv. The new way: - uses ensurepip module to bootstrap the pip install - uses the pip module (and not get-pip) to upgrade pip --- resources/runtime.rb | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/resources/runtime.rb b/resources/runtime.rb index 5260bdf..44e6657 100644 --- a/resources/runtime.rb +++ b/resources/runtime.rb @@ -25,8 +25,6 @@ end action :install do - get_pip_location = ::File.join(::Chef::Config['cache_path'], 'get-pip.py') - python_install new_resource.python_name do version new_resource.python_version source new_resource.python_provider @@ -38,20 +36,11 @@ end converge_if_changed :pip_version do - directory ::Chef::Config['cache_path'] do - action :create - recursive true - end - - remote_file 'get-pip.py' do - source new_resource.get_pip_url - checksum new_resource.get_pip_checksum - path get_pip_location - action :create + execute 'ensure_pip' do + command "#{::Python3::Path.python_binary(new_resource)} -m ensurepip" end - execute 'install_pip' do - command "#{::Python3::Path.python_binary(new_resource)} #{get_pip_location} --upgrade --force-reinstall pip==#{new_resource.pip_version} --no-setuptools --no-wheel" + command "#{::Python3::Path.python_binary(new_resource)} -m pip install --upgrade --force-reinstall pip==#{new_resource.pip_version}" end end From 3258fe93ad990cdcd0a79ca2c42ed770d5d0637e Mon Sep 17 00:00:00 2001 From: Emmanuel Guerin Date: Thu, 26 Jan 2023 23:31:44 +0100 Subject: [PATCH 2/2] Use venv module to create virtualenv With this, we no longer rely on virtualenv to create the environments. --- libraries/helpers.rb | 4 --- resources/virtualenv.rb | 25 ++++++------------- .../cookbooks/python3-test/recipes/default.rb | 1 - 3 files changed, 7 insertions(+), 23 deletions(-) diff --git a/libraries/helpers.rb b/libraries/helpers.rb index d2cd62a..f6fd813 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -50,10 +50,6 @@ def self.pip_binary(resource = new_resource) ::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' diff --git a/resources/virtualenv.rb b/resources/virtualenv.rb index 55726ae..a55f7a5 100644 --- a/resources/virtualenv.rb +++ b/resources/virtualenv.rb @@ -1,36 +1,25 @@ provides :python_virtualenv -property :virtualenv, String, name_property: true -property :system_site_packages, equal_to: [true, false], default: false +property :path, String, name_property: true property :group, [String, Integer, NilClass] property :user, [String, Integer, NilClass] -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'] } +property :system_site_packages, [TrueClass, FalseClass], default: false load_current_value do |new_resource| - current_value_does_not_exist! unless ::File.exist?(::File.join(new_resource.virtualenv, 'bin/activate')) + current_value_does_not_exist! unless ::File.exist?(::File.join(new_resource.path, 'bin/activate')) end action :create do - python_install node['python3']['name'] do - version new_resource.python_version - source new_resource.python_provider - checksum new_resource.python_checksum - end - - 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}" + cmd = "#{::Python3::Path.python_binary(new_resource)} -m venv" + cmd += ' --system-site-packages' if new_resource.system_site_packages + cmd += " #{new_resource.path}" execute cmd do user new_resource.user group new_resource.group - creates new_resource.virtualenv + creates new_resource.path end end diff --git a/test/cookbooks/python3-test/recipes/default.rb b/test/cookbooks/python3-test/recipes/default.rb index 523cd77..ab15851 100644 --- a/test/cookbooks/python3-test/recipes/default.rb +++ b/test/cookbooks/python3-test/recipes/default.rb @@ -18,7 +18,6 @@ python_virtualenv "/opt/blah-#{idx}" do action :create - pip_binary_name python['pip_binary_name'] binary_name python['binary_name'] end