Skip to content

Commit 4fc979c

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 4fc979c

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

manifests/pip.pp

+9-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,18 @@
243238
}
244239

245240
'latest': {
241+
# Unfortunately this is the smartest way of getting the latest available package version with pip as of now
242+
$latest_version = join(["${pip_env} install ${proxy_flag} ${pkgname}==notreallyaversion 2>&1",
243+
' | grep -oP "\(.*\)" | sed -E "s/\(from versions: (.*?, )*(.*)\)/\2/g"'])
244+
245+
# Packages with underscores in their names are listed with dashes in their place by `pip freeze`
246+
$pkgname_with_dashes = regsubst($pkgname, '_', '-', 'G')
247+
$installed_version = "${pip_env} freeze --all | grep -oP '(?i)(?<=${pkgname_with_dashes}==).*'"
248+
246249
# Latest version.
247250
exec { "pip_install_${name}":
248251
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",
252+
unless => "[ `${latest_version}` == `${installed_version}` ]",
250253
user => $owner,
251254
group => $group,
252255
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

+15-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
let(:params) { { proxy: 'http://my.proxy:3128', ensure: 'latest' } }
8080

8181
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') }
82+
it { is_expected.to contain_exec('pip_install_rpyc').with_unless('[ `pip install --proxy=http://my.proxy:3128 rpyc==notreallyaversion 2>&1 | grep -oP "\(.*\)" | sed -E "s/\(from versions: (.*?, )*(.*)\)/\2/g"` == `pip freeze --all | grep -oP \'(?i)(?<=rpyc==).*\'` ]') }
8383
end
8484
end
8585

@@ -109,6 +109,20 @@
109109
it { is_expected.to contain_exec('pip_install_rpyc').with_path(['/opt/python3/bin', '/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin']) }
110110
end
111111
end
112+
113+
describe 'install latest' do
114+
context 'does not use pip search' do
115+
let(:params) { { ensure: 'latest' } }
116+
117+
it { is_expected.not_to contain_exec('pip_install_rpyc').with_unless(%r{search}) }
118+
end
119+
context 'checks installed version of a package by converting underscores in its name with dashes' do
120+
let(:params) { { ensure: 'latest', pkgname: 'wordpress_json' } }
121+
122+
# yes, the exec title does not change if we use different pgkname
123+
it { is_expected.to contain_exec('pip_install_rpyc').with_unless(%r{wordpress-json}) }
124+
end
125+
end
112126
end
113127
end
114128

0 commit comments

Comments
 (0)