Skip to content

Commit

Permalink
Merge pull request #703 from maxadamo/master
Browse files Browse the repository at this point in the history
simplify packages version detection
  • Loading branch information
maxadamo authored Oct 13, 2024
2 parents 04e4d4c + b51d643 commit 647a8fc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
17 changes: 9 additions & 8 deletions manifests/pip.pp
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,13 @@
$_ensure = $ensure
}

# Check if searching by explicit version.
if $_ensure =~ /^((19|20)[0-9][0-9]-(0[1-9]|1[1-2])-([0-2][1-9]|3[0-1])|[0-9]+\.\w+\+?\w*(\.\w+)*)$/ {
$grep_regex = "^${real_pkgname}[[:space:]]\\+(\\?${_ensure}\\()$\\|$\\|, \\|[[:space:]]\\)"
} else {
$grep_regex = "^${real_pkgname}[[:space:]].*$"
# We do not try to mimic a version scheme validation which is already implemented by the package manager.
# If it starts with a number it is probably a version.
# If it wasn't or if there is any error, the package manager will trigger a failure.
$grep_regex = $_ensure ? {
/^(present|absent|latest)$/ => "^${real_pkgname}[[:space:]].*$",
/^[0-9].*$/ => "^${real_pkgname}[[:space:]]\\+(\\?${_ensure}\\()$\\|$\\|, \\|[[:space:]]\\)",
default => fail('ensure can be a version number or one of: present, absent, latest')
}

$extras_string = empty($extras) ? {
Expand Down Expand Up @@ -193,9 +195,8 @@
}
} else {
case $_ensure {
/^((19|20)[0-9][0-9]-(0[1-9]|1[1-2])-([0-2][1-9]|3[0-1])|[0-9]+\.\w+\+?\w*(\.\w+)*)$/: {
# Version formats as per http://guide.python-distribute.org/specification.html#standard-versioning-schemes
# Explicit version.
/^[0-9].*$/: {
# Specific version
$command = "${pip_install} ${install_args} ${pip_common_args}==${_ensure}"
$unless_command = "${pip_env} list | grep -i -e '${grep_regex}'"
}
Expand Down
31 changes: 31 additions & 0 deletions spec/acceptance/pip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,37 @@ class { 'python':
its(:stdout) { is_expected.not_to match %r{agent.* 0\.1\.2} }
end

context 'fails to install package with wrong version' do
it 'throws an error' do

Check failure on line 78 in spec/acceptance/pip_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Distro Puppet - Archlinux rolling

python::pip defined resource fails to install package with wrong version throws an error Failure/Error: expect(result.stderr).to contain(%r{returned 1 instead of one of}) expected "\e[1;33mWarning: Facter: Container runtime, 'docker', is unsupported, setting to 'container_other'\e...:Pip[agent package]/Exec[pip_install_agent package]: Skipping because of failed dependencies\e[0m\n" to contain /returned 1 instead of one of/
pp = <<-PUPPET
class { 'python':
version => '3',
dev => 'present',
}
python::pyvenv { '/opt/test-venv':
ensure => 'present',
systempkgs => false,
mode => '0755',
pip_version => '<= 20.3.4',
}
python::pip { 'agent package':
virtualenv => '/opt/test-venv',
pkgname => 'agent',
ensure => '0.1.33+2020-this_is_something-fun',
}
PUPPET

result = apply_manifest(pp, expect_failures: true)
expect(result.stderr).to contain(%r{returned 1 instead of one of})
end
end

describe command('/opt/test-venv/bin/pip show agent') do
its(:exit_status) { is_expected.to eq 1 }
end

context 'install package via extra_index' do
it 'works with no errors' do

Check failure on line 109 in spec/acceptance/pip_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Distro Puppet - Archlinux rolling

python::pip defined resource install package via extra_index works with no errors Failure/Error: apply_manifest(pp, catch_failures: true) Beaker::Host::CommandFailure: Host 'archlinuxrolling-64.example.com' exited with 4 running: puppet apply --verbose --detailed-exitcodes /tmp/apply_manifest_124053106.EnAAhK.pp Last 10 lines of output were: �[mNotice: /Stage[main]/Main/Python::Pip[agent package via extra_index]/Exec[pip_install_agent package via extra_index]/returns: File "/opt/test-venv/lib/python3.12/site-packages/pip/_internal/cli/autocompletion.py", line 9, in <module> �[mNotice: /Stage[main]/Main/Python::Pip[agent package via extra_index]/Exec[pip_install_agent package via extra_index]/returns: from pip._internal.cli.main_parser import create_main_parser �[mNotice: /Stage[main]/Main/Python::Pip[agent package via extra_index]/Exec[pip_install_agent package via extra_index]/returns: File "/opt/test-venv/lib/python3.12/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module> �[mNotice: /Stage[main]/Main/Python::Pip[agent package via extra_index]/Exec[pip_install_agent package via extra_index]/returns: from pip._internal.cli import cmdoptions �[mNotice: /Stage[main]/Main/Python::Pip[agent package via extra_index]/Exec[pip_install_agent package via extra_index]/returns: File "/opt/test-venv/lib/python3.12/site-packages/pip/_internal/cli/cmdoptions.py", line 18, in <module> �[mNotice: /Stage[main]/Main/Python::Pip[agent package via extra_index]/Exec[pip_install_agent package via extra_index]/returns: from distutils.util import strtobool �[mNotice: /Stage[main]/Main/Python::Pip[agent package via extra_index]/Exec[pip_install_agent package via extra_index]/returns: ModuleNotFoundError: No module named 'distutils' Error: '/opt/test-venv/bin/pip --log /opt/test-venv/pip.log install --index-url=invalid --extra-index-url=https://pypi.org/simple agent==0.1.2' returned 1 instead of one of [0] Error: /Stage[main]/Main/Python::Pip[agent package via extra_index]/Exec[pip_install_agent package via extra_index]/returns: change from 'notrun' to ['0'] failed: '/opt/test-venv/bin/pip --log /opt/test-venv/pip.log install --index-url=invalid --extra-index-url=https://pypi.org/simple agent==0.1.2' returned 1 instead of one of [0] �[mNotice: Applied catalog in 0.52 seconds
pp = <<-PUPPET
Expand Down

0 comments on commit 647a8fc

Please sign in to comment.