Skip to content

Commit

Permalink
Use venv module to create virtualenv
Browse files Browse the repository at this point in the history
With this, we no longer rely on virtualenv to
create the environments.
  • Loading branch information
emmanuelguerin committed Jan 26, 2023
1 parent 85af288 commit 3258fe9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 deletions.
4 changes: 0 additions & 4 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
25 changes: 7 additions & 18 deletions resources/virtualenv.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion test/cookbooks/python3-test/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3258fe9

Please sign in to comment.