Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# pyenv Changelog

## Unreleased

- Add `:upgrade` action to the pyenv_pip resource

## 3.3.2 (2020-08-05)

- Do not attempt to rehash in a system-wide install
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ pyenv_pip 'requests' do
end
```

The pyenv_pip resource has the following actions:

* `:install` - Default. Install a python package. If a version is specified, install the specified version of the python package.
* `:upgrade` - Install/upgrade a python package. Call `install` command with `--upgrade` flag. If version is not specified, latest version will be installed.
* `:uninstall` - Uninstall a python package.

## Global

```ruby
Expand Down
52 changes: 46 additions & 6 deletions resources/pip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@
end
end

action :upgrade do
upgrade_target = if new_resource.version
"#{new_resource.package_name}==#{new_resource.version}"
else
new_resource.package_name.to_s
end

pip_args = "install --upgrade #{new_resource.options} #{upgrade_target}"

# without virtualenv, upgrade package with system's pip
command = if new_resource.virtualenv
"#{new_resource.virtualenv}/bin/pip #{pip_args}"
else
"pip #{pip_args}"
end

pyenv_script new_resource.package_name do
code command
user new_resource.user if new_resource.user
only_if { require_upgrade? }
end
end

action :uninstall do
uninstall_mode = if new_resource.requirement
'--requirement'
Expand Down Expand Up @@ -86,6 +109,25 @@
include Chef::Pyenv::ScriptHelpers

def require_install?
current_version = get_current_version
return true unless current_version

unless new_resource.version
Chef::Log.debug("already installed: #{new_resource.package_name} #{current_version}")
return false
end

is_different_version?(current_version)
end

def require_upgrade?
current_version = get_current_version
return true unless current_version

is_different_version?(current_version)
end

def get_current_version
current_version = nil
show = pip_command("show #{new_resource.package_name}").stdout
show.split(/\n+/).each do |line|
Expand All @@ -94,14 +136,11 @@ def require_install?
Chef::Log.debug("current_version: #{new_resource.package_name} #{current_version}")
unless current_version
Chef::Log.debug("not installed: #{new_resource.package_name}")
return true
end

unless new_resource.version
Chef::Log.debug("already installed: #{new_resource.package_name} #{current_version}")
return false
end
current_version
end

def is_different_version?(current_version)
if current_version != new_resource.version
Chef::Log.debug("different version installed: #{new_resource.package_name} current=#{current_version} candidate=#{new_resource.version}")
true
Expand All @@ -111,3 +150,4 @@ def require_install?
end
end
end

3 changes: 2 additions & 1 deletion test/fixtures/cookbooks/test/files/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
fire==0.1.2
requests==2.18.1
requests==2.18.1
urllib3==1.24.3
6 changes: 6 additions & 0 deletions test/fixtures/cookbooks/test/recipes/system_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
requirement true
end

pyenv_pip 'urllib3' do
virtualenv venv_root
action :upgrade
version '1.25.11'
end

pyenv_pip 'requests' do
virtualenv venv_root
action :uninstall
Expand Down
8 changes: 7 additions & 1 deletion test/integration/system_install/controls/system_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
its('stdout') { should match('Version: 16.2.0') }
end

desc 'Pip should install package requests inside virtualenv according to requirements.txt'
desc 'Pip should upgrade package urllib3 inside virtualenv'
describe bash("sudo -H bash -c 'source /etc/profile.d/pyenv.sh && #{venv_root}/bin/pip show urllib3'") do
its('exit_status') { should eq(0) }
its('stdout') { should match('Version: 1.25.11') }
end

desc 'Pip should install package fire inside virtualenv according to requirements.txt'
describe bash("sudo -H bash -c 'source /etc/profile.d/pyenv.sh && #{venv_root}/bin/pip show fire'") do
its('exit_status') { should eq(0) }
its('stdout') { should match('Version: 0.1.2') }
Expand Down
2 changes: 1 addition & 1 deletion test/integration/user_install/controls/user_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
its('stdout') { should match('Version: 16.2.0') }
end

desc 'Pip should install package requests inside virtualenv according to requirements.txt'
desc 'Pip should install package fire inside virtualenv according to requirements.txt'
describe bash("sudo -H -u #{user} bash -c 'source /etc/profile.d/pyenv.sh && #{venv_root}/bin/pip show fire'") do
its('exit_status') { should eq(0) }
its('stdout') { should match('Version: 0.1.2') }
Expand Down