Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5cce1a

Browse files
captnswingjtimberman
authored and
jtimberman
committed
COOK-1048: pip and virtualenv honor python installed from source, with custom prefix_dir
Signed-off-by: jtimberman <[email protected]>
1 parent 3bd772d commit a5cce1a

File tree

5 files changed

+34
-25
lines changed

5 files changed

+34
-25
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Attributes
2323
See `attributes/default.rb` for default values.
2424

2525
* `node["python"]["install_method"]` - method to install python with, default `package`.
26-
* `node["python"]["distribute_install_py_version"]` - version of python to use when installing distribute. default '', specify version like '2.6' (uses python2.6 binary).
2726

2827
The file also contains the following attributes:
2928

attributes/default.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@
1818
# limitations under the License.
1919
#
2020

21-
default['python']['distribute_install_py_version'] = ''
22-
2321
default['python']['install_method'] = 'package'
24-
22+
default['python']['prefix_dir'] = '/usr/local'
2523
default['python']['url'] = 'http://www.python.org/ftp/python'
2624
default['python']['version'] = '2.7.1'
2725
default['python']['checksum'] = '80e387bcf57eae8ce26726753584fd63e060ec11682d1145af921e85fd612292'
28-
default['python']['prefix_dir'] = '/usr/local'
29-
3026
default['python']['configure_options'] = %W{--prefix=#{python['prefix_dir']}}

providers/pip.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
require 'chef/mixin/language'
2323
include Chef::Mixin::ShellOut
2424

25-
# the logic in all action methods mirror that of
25+
# the logic in all action methods mirror that of
2626
# the Chef::Provider::Package which will make
2727
# refactoring into core chef easy
2828

@@ -40,7 +40,7 @@
4040
if @new_resource.timeout
4141
timeout = @new_resource.timeout
4242
end
43-
43+
4444
if install_version
4545
Chef::Log.info("Installing #{@new_resource} version #{install_version}")
4646
status = install_package(@new_resource.package_name, install_version, timeout)
@@ -98,26 +98,26 @@ def expand_options(options)
9898
options ? " #{options}" : ""
9999
end
100100

101-
# these methods are the required overrides of
102-
# a provider that extends from Chef::Provider::Package
101+
# these methods are the required overrides of
102+
# a provider that extends from Chef::Provider::Package
103103
# so refactoring into core Chef should be easy
104104

105105
def load_current_resource
106106
@current_resource = Chef::Resource::PythonPip.new(@new_resource.name)
107107
@current_resource.package_name(@new_resource.package_name)
108108
@current_resource.version(nil)
109-
109+
110110
unless current_installed_version.nil?
111111
@current_resource.version(current_installed_version)
112112
end
113-
113+
114114
@current_resource
115115
end
116116

117117
def current_installed_version
118118
@current_installed_version ||= begin
119119
delimeter = /==/
120-
120+
121121
version_check_cmd = "#{pip_cmd(@new_resource)} freeze | grep -i '^#{@new_resource.package_name}=='"
122122
# incase you upgrade pip with pip!
123123
if @new_resource.package_name.eql?('pip')
@@ -133,7 +133,7 @@ def current_installed_version
133133
def candidate_version
134134
@candidate_version ||= begin
135135
# `pip search` doesn't return versions yet
136-
# `pip list` may be coming soon:
136+
# `pip list` may be coming soon:
137137
# https://bitbucket.org/ianb/pip/issue/197/option-to-show-what-version-would-be
138138
@new_resource.version||'latest'
139139
end
@@ -156,5 +156,11 @@ def remove_package(name, version, timeout)
156156
# TODO remove when provider is moved into Chef core
157157
# this allows PythonPip to work with Chef::Resource::Package
158158
def pip_cmd(nr)
159-
(nr.respond_to?("virtualenv") && nr.virtualenv) ? ::File.join(nr.virtualenv,'/bin/pip') : 'pip'
159+
if (nr.respond_to?("virtualenv") && nr.virtualenv)
160+
::File.join(nr.virtualenv,'/bin/pip')
161+
elsif "#{node['python']['install_method']}".eql?("source")
162+
::File.join("#{node['python']['prefix_dir']}","/bin/pip")
163+
else
164+
'pip'
165+
end
160166
end

providers/virtualenv.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
action :create do
2626
unless exists?
2727
Chef::Log.info("Creating virtualenv #{@new_resource} at #{@new_resource.path}")
28-
execute "virtualenv --python=#{@new_resource.interpreter} #{@new_resource.path}" do
28+
execute "#{virtualenv_cmd} --python=#{@new_resource.interpreter} #{@new_resource.path}" do
2929
user new_resource.owner if new_resource.owner
3030
group new_resource.group if new_resource.group
3131
end
3232
new_resource.updated_by_last_action(true)
3333
end
3434
end
3535

36-
action :delete do
36+
action :delete do
3737
if exists?
3838
Chef::Log.info("Deleting virtualenv #{@new_resource} at #{@new_resource.path}")
3939
FileUtils.rm_rf(@new_resource.path)
@@ -44,7 +44,7 @@
4444
def load_current_resource
4545
@current_resource = Chef::Resource::PythonVirtualenv.new(@new_resource.name)
4646
@current_resource.path(@new_resource.path)
47-
47+
4848
if exists?
4949
cstats = ::File.stat(@current_resource.path)
5050
@current_resource.owner(cstats.uid)
@@ -53,6 +53,14 @@ def load_current_resource
5353
@current_resource
5454
end
5555

56+
def virtualenv_cmd()
57+
if "#{node['python']['install_method']}".eql?("source")
58+
::File.join("#{node['python']['prefix_dir']}","/bin/virtualenv")
59+
else
60+
"virtualenv"
61+
end
62+
end
63+
5664
private
5765
def exists?
5866
::File.exist?(@current_resource.path) && ::File.directory?(@current_resource.path) \

recipes/pip.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@
1818
# limitations under the License.
1919
#
2020

21-
# Ubuntu's python-setuptools, python-pip and python-virtualenv packages
21+
python_bindir = "#{node['python']['prefix_dir']}/bin/"
22+
23+
# Ubuntu's python-setuptools, python-pip and python-virtualenv packages
2224
# are broken...this feels like Rubygems!
2325
# http://stackoverflow.com/questions/4324558/whats-the-proper-way-to-install-pip-virtualenv-and-distribute-for-python
2426
# https://bitbucket.org/ianb/pip/issue/104/pip-uninstall-on-ubuntu-linux
2527
remote_file "#{Chef::Config[:file_cache_path]}/distribute_setup.py" do
2628
source "http://python-distribute.org/distribute_setup.py"
2729
mode "0644"
28-
not_if "which pip"
30+
not_if { ::File.exists?(python_bindir+'pip') }
2931
end
3032

31-
use_version = node['python']['distribute_install_py_version']
32-
3333
bash "install-pip" do
3434
cwd Chef::Config[:file_cache_path]
3535
code <<-EOF
36-
python#{use_version} distribute_setup.py
37-
easy_install pip
36+
#{python_bindir}python distribute_setup.py
37+
#{python_bindir}easy_install pip
3838
EOF
39-
not_if "which pip"
39+
not_if { ::File.exists?(python_bindir+'pip') }
4040
end

0 commit comments

Comments
 (0)