Skip to content

Commit

Permalink
Add python_path to pyvenv class (#686)
Browse files Browse the repository at this point in the history
* Add python_path to pyvenv class
Issue #685

- Add: `python_path` variable to `python::pyvenv`

* Fixed whitespace issue

* Update manifests/pyvenv.pp

Co-authored-by: Tim Meusel <[email protected]>

* Updated reference.md

---------

Co-authored-by: Wouter Mellema <[email protected]>
Co-authored-by: Tim Meusel <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2024
1 parent 8f09514 commit 05cbf65
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ The following parameters are available in the `python::pyvenv` defined type:
* [`path`](#-python--pyvenv--path)
* [`environment`](#-python--pyvenv--environment)
* [`prompt`](#-python--pyvenv--prompt)
* [`python_path`](#-python--pyvenv--python_path)
* [`pip_version`](#-python--pyvenv--pip_version)

##### <a name="-python--pyvenv--ensure"></a>`ensure`
Expand Down Expand Up @@ -995,6 +996,14 @@ Optionally specify the virtualenv prompt (python >= 3.6)

Default value: `undef`

##### <a name="-python--pyvenv--python_path"></a>`python_path`

Data type: `Optional[Stdlib::Absolutepath]`

Optionally specify python path for creation of virtualenv

Default value: `undef`

##### <a name="-python--pyvenv--pip_version"></a>`pip_version`

Data type: `Python::Venv::PipVersion`
Expand Down
14 changes: 11 additions & 3 deletions manifests/pyvenv.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# @param path Specifies the PATH variable.
# @param environment Optionally specify environment variables for pyvenv
# @param prompt Optionally specify the virtualenv prompt (python >= 3.6)
# @param python_path Optionally specify python path for creation of virtualenv
#
# @example
# python::pyvenv { '/var/www/project1' :
Expand All @@ -34,6 +35,7 @@
Array $environment = [],
Optional[String[1]] $prompt = undef,
Python::Venv::PipVersion $pip_version = 'latest',
Optional[Stdlib::Absolutepath] $python_path = undef,
) {
include python

Expand All @@ -46,11 +48,17 @@
$python_version_parts = split($python_version, '[.]')
$normalized_python_version = sprintf('%s.%s', $python_version_parts[0], $python_version_parts[1])

$local_exec_prefix = $python_path ? {
undef => $python::exec_prefix,
default => $python_path,
}
# pyvenv is deprecated since 3.6 and will be removed in 3.8
if versioncmp($normalized_python_version, '3.6') >=0 {
$virtualenv_cmd = "${python::exec_prefix}python${normalized_python_version} -m venv"
if $python_path != undef {
$virtualenv_cmd = "${python_path} -m venv"
} elsif versioncmp($normalized_python_version, '3.6') >=0 {
$virtualenv_cmd = "${local_exec_prefix}python${normalized_python_version} -m venv"
} else {
$virtualenv_cmd = "${python::exec_prefix}pyvenv-${normalized_python_version}"
$virtualenv_cmd = "${local_exec_prefix}pyvenv-${normalized_python_version}"
}

$_path = $python::provider ? {
Expand Down
49 changes: 49 additions & 0 deletions spec/acceptance/pyvenv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,53 @@ class { 'python':
its(:stdout) { is_expected.to match %r{agent.* 0\.1\.2} }
end
end

context 'with versioned minimal python::pip and without systempkgs using custom python path' do
it 'works with no errors' do
pp = <<-PUPPET
class { 'python':
dev => 'present',
venv => 'present',
}
file { '/usr/bin/mycustompython':
ensure => link,
target => '/usr/bin/python',
}
user { 'agent':
ensure => 'present',
managehome => true,
home => '/opt/agent',
}
group { 'agent':
ensure => 'present',
system => true,
}
python::pyvenv { '/opt/agent/venv':
ensure => 'present',
systempkgs => false,
owner => 'agent',
group => 'agent',
mode => '0755',
pip_version => '<= 20.3.4',
python_path => '/usr/bin/mycustompython',
}
python::pip { 'agent' :
ensure => '0.1.2',
virtualenv => '/opt/agent/venv',
owner => 'agent',
group => 'agent',
}
PUPPET

# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

describe command('/opt/agent/venv/bin/pip list') do
its(:exit_status) { is_expected.to eq 0 }
its(:stdout) { is_expected.to match %r{agent.* 0\.1\.2} }
end
end
end

0 comments on commit 05cbf65

Please sign in to comment.