Skip to content

Commit 69dea59

Browse files
author
Greg Dubicki
committed
Stop using 'pip search' for ensure => latest
because it does not work properly with devpi as own repo/PyPi mirror and is being considered to be deprecated in pip
1 parent f274e51 commit 69dea59

File tree

3 files changed

+87
-23
lines changed

3 files changed

+87
-23
lines changed

manifests/pip.pp

+17-6
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@
111111
default => "--index-url=${index}",
112112
}
113113

114-
$pypi_search_index = $index ? {
115-
false => '',
116-
default => "--index=${index}",
117-
}
118-
119114
$proxy_flag = $proxy ? {
120115
false => '',
121116
default => "--proxy=${proxy}",
@@ -243,10 +238,26 @@
243238
}
244239

245240
'latest': {
241+
# Unfortunately this is the smartest way of getting the latest available package version with pip as of now
242+
# Note: we DO need to repeat ourselves with "from version" in both grep and sed as on some systems pip returns
243+
# more than one line with paretheses.
244+
$latest_version = join(["${pip_env} install ${proxy_flag} ${pkgname}==notreallyaversion 2>&1",
245+
' | grep -oP "\(from versions: .*\)" | sed -E "s/\(from versions: (.*?, )*(.*)\)/\2/g"',
246+
' | tr -d "[:space:]"'])
247+
248+
# Packages with underscores in their names are listed with dashes in their place in `pip freeze` output
249+
$pkgname_with_dashes = regsubst($pkgname, '_', '-', 'G')
250+
$grep_regex_pkgname_with_dashes = "^${pkgname_with_dashes}=="
251+
$installed_version = join(["${pip_env} freeze --all",
252+
" | grep -i -e ${grep_regex_pkgname_with_dashes} | cut -d= -f3",
253+
" | tr -d '[:space:]'"])
254+
255+
$unless_command = "[ \$(${latest_version}) = \$(${installed_version}) ]"
256+
246257
# Latest version.
247258
exec { "pip_install_${name}":
248259
command => "${wheel_check} ; { ${pip_install} --upgrade \$wheel_support_flag ${pip_common_args} || ${pip_install} --upgrade ${pip_common_args} ;}",
249-
unless => "${pip_env} search ${pypi_search_index} ${proxy_flag} ${source} | grep -i INSTALLED.*latest",
260+
unless => $unless_command,
250261
user => $owner,
251262
group => $group,
252263
umask => $umask,

spec/acceptance/virtualenv_spec.rb

+56
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,61 @@ class { 'python' :
5555
apply_manifest(pp, catch_failures: true)
5656
apply_manifest(pp, catch_changes: true)
5757
end
58+
it 'works with ensure=>latest' do
59+
pp = <<-EOS
60+
class { 'python' :
61+
version => 'system',
62+
pip => 'present',
63+
virtualenv => 'present',
64+
}
65+
->
66+
python::virtualenv { 'venv' :
67+
ensure => 'present',
68+
systempkgs => false,
69+
venv_dir => '/opt/venv3',
70+
owner => 'root',
71+
group => 'root',
72+
}
73+
->
74+
python::pip { 'rpyc' :
75+
ensure => 'latest',
76+
virtualenv => '/opt/venv3',
77+
}
78+
EOS
79+
80+
# Run it twice and test for idempotency
81+
apply_manifest(pp, catch_failures: true)
82+
# Of course this test will fail if between the applies a new version of the package will be released,
83+
# but probability of this happening is minimal, so it should be acceptable.
84+
apply_manifest(pp, catch_changes: true)
85+
end
86+
it 'works with ensure=>latest for package with underscore in its name' do
87+
pp = <<-EOS
88+
class { 'python' :
89+
version => 'system',
90+
pip => 'present',
91+
virtualenv => 'present',
92+
}
93+
->
94+
python::virtualenv { 'venv' :
95+
ensure => 'present',
96+
systempkgs => false,
97+
venv_dir => '/opt/venv4',
98+
owner => 'root',
99+
group => 'root',
100+
}
101+
->
102+
python::pip { 'Randomized_Requests' :
103+
ensure => 'latest',
104+
virtualenv => '/opt/venv4',
105+
}
106+
EOS
107+
108+
# Run it twice and test for idempotency
109+
apply_manifest(pp, catch_failures: true)
110+
# Of course this test will fail if between the applies a new version of the package will be released,
111+
# but probability of this happening is minimal, so it should be acceptable.
112+
apply_manifest(pp, catch_changes: true)
113+
end
58114
end
59115
end

spec/defines/pip_spec.rb

+14-17
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,11 @@
6464

6565
it { is_expected.not_to contain_exec('pip_install_rpyc').with_command(%r{--proxy}) }
6666
end
67-
context 'does not add proxy to search command if set to latest and proxy is unset' do
68-
let(:params) { { ensure: 'latest' } }
69-
70-
it { is_expected.not_to contain_exec('pip_install_rpyc').with_command(%r{--proxy}) }
71-
it { is_expected.to contain_exec('pip_install_rpyc').without_unless(%r{--proxy}) }
72-
end
7367
context 'adds proxy to install command if proxy set' do
7468
let(:params) { { proxy: 'http://my.proxy:3128' } }
7569

7670
it { is_expected.to contain_exec('pip_install_rpyc').with_command("pip wheel --help > /dev/null 2>&1 && { pip show wheel > /dev/null 2>&1 || wheel_support_flag='--no-binary :all:'; } ; { pip --log /tmp/pip.log install $wheel_support_flag --proxy=http://my.proxy:3128 rpyc || pip --log /tmp/pip.log install --proxy=http://my.proxy:3128 rpyc ;}") }
7771
end
78-
context 'adds proxy to search command if set to latest' do
79-
let(:params) { { proxy: 'http://my.proxy:3128', ensure: 'latest' } }
80-
81-
it { is_expected.to contain_exec('pip_install_rpyc').with_command("pip wheel --help > /dev/null 2>&1 && { pip show wheel > /dev/null 2>&1 || wheel_support_flag='--no-binary :all:'; } ; { pip --log /tmp/pip.log install --upgrade $wheel_support_flag --proxy=http://my.proxy:3128 rpyc || pip --log /tmp/pip.log install --upgrade --proxy=http://my.proxy:3128 rpyc ;}") }
82-
it { is_expected.to contain_exec('pip_install_rpyc').with_unless('pip search --proxy=http://my.proxy:3128 rpyc | grep -i INSTALLED.*latest') }
83-
end
8472
end
8573

8674
describe 'index as' do
@@ -94,11 +82,6 @@
9482

9583
it { is_expected.to contain_exec('pip_install_rpyc').with_command("pip wheel --help > /dev/null 2>&1 && { pip show wheel > /dev/null 2>&1 || wheel_support_flag='--no-binary :all:'; } ; { pip --log /tmp/pip.log install $wheel_support_flag --index-url=http://www.example.com/simple/ rpyc || pip --log /tmp/pip.log install --index-url=http://www.example.com/simple/ rpyc ;}") }
9684
end
97-
context 'adds index to search command if set to latest' do
98-
let(:params) { { index: 'http://www.example.com/simple/', ensure: 'latest' } }
99-
100-
it { is_expected.to contain_exec('pip_install_rpyc').with_command("pip wheel --help > /dev/null 2>&1 && { pip show wheel > /dev/null 2>&1 || wheel_support_flag='--no-binary :all:'; } ; { pip --log /tmp/pip.log install --upgrade $wheel_support_flag --index-url=http://www.example.com/simple/ rpyc || pip --log /tmp/pip.log install --upgrade --index-url=http://www.example.com/simple/ rpyc ;}") }
101-
end
10285
end
10386

10487
describe 'path as' do
@@ -109,6 +92,20 @@
10992
it { is_expected.to contain_exec('pip_install_rpyc').with_path(['/opt/python3/bin', '/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin']) }
11093
end
11194
end
95+
96+
describe 'install latest' do
97+
context 'does not use pip search in unless' do
98+
let(:params) { { ensure: 'latest' } }
99+
100+
it { is_expected.not_to contain_exec('pip_install_rpyc').with_unless(%r{search}) }
101+
end
102+
context 'checks installed version of a package by converting underscores in its name to dashes' do
103+
let(:params) { { ensure: 'latest', pkgname: 'wordpress_json' } }
104+
105+
# yes, the exec title does not change if we use different pgkname
106+
it { is_expected.to contain_exec('pip_install_rpyc').with_unless(%r{wordpress-json}) }
107+
end
108+
end
112109
end
113110
end
114111

0 commit comments

Comments
 (0)