Skip to content

Commit 380a434

Browse files
authored
Merge pull request #641 from ookisan/python-pyvenv-add-prompt
2 parents c47ce1e + bd3f212 commit 380a434

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

REFERENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ The following parameters are available in the `python::pyvenv` defined type:
903903
* [`mode`](#mode)
904904
* [`path`](#path)
905905
* [`environment`](#environment)
906+
* [`prompt`](#prompt)
906907
* [`pip_version`](#pip_version)
907908

908909
##### <a name="ensure"></a>`ensure`
@@ -977,6 +978,14 @@ Optionally specify environment variables for pyvenv
977978

978979
Default value: `[]`
979980

981+
##### <a name="prompt"></a>`prompt`
982+
983+
Data type: `Variant[Boolean,String[1]]`
984+
985+
Optionally specify the virtualenv prompt (python >= 3.6)
986+
987+
Default value: ``false``
988+
980989
##### <a name="pip_version"></a>`pip_version`
981990

982991
Data type: `Python::Venv::PipVersion`

manifests/pyvenv.pp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# @param mode Optionally specify directory mode
1111
# @param path Specifies the PATH variable.
1212
# @param environment Optionally specify environment variables for pyvenv
13+
# @param prompt Optionally specify the virtualenv prompt (python >= 3.6)
1314
#
1415
# @example
1516
# python::pyvenv { '/var/www/project1' :
@@ -31,6 +32,7 @@
3132
Stdlib::Filemode $mode = '0755',
3233
Array[Stdlib::Absolutepath] $path = ['/bin', '/usr/bin', '/usr/sbin', '/usr/local/bin',],
3334
Array $environment = [],
35+
Optional[String[1]] $prompt = undef,
3436
Python::Venv::PipVersion $pip_version = 'latest',
3537
) {
3638
include python
@@ -62,6 +64,12 @@
6264
$system_pkgs_flag = ''
6365
}
6466

67+
if versioncmp($normalized_python_version, '3.6') >=0 and $prompt {
68+
$prompt_arg = "--prompt ${shell_escape($prompt)}"
69+
} else {
70+
$prompt_arg = ''
71+
}
72+
6573
file { $venv_dir:
6674
ensure => directory,
6775
owner => $owner,
@@ -78,7 +86,7 @@
7886
}
7987

8088
exec { "python_virtualenv_${venv_dir}":
81-
command => "${virtualenv_cmd} --clear ${system_pkgs_flag} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${pip_upgrade} && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade setuptools",
89+
command => "${virtualenv_cmd} --clear ${system_pkgs_flag} ${prompt_arg} ${venv_dir} && ${pip_cmd} --log ${venv_dir}/pip.log install ${pip_upgrade} && ${pip_cmd} --log ${venv_dir}/pip.log install --upgrade setuptools",
8290
user => $owner,
8391
creates => "${venv_dir}/bin/activate",
8492
path => $_path,

spec/classes/python_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
it {
147147
expect(subject).to contain_exec('python_virtualenv_/opt/env1').
148148
with(
149-
command: 'python3.8 -m venv --clear /opt/env1 && /opt/env1/bin/pip --log /opt/env1/pip.log install --upgrade pip && /opt/env1/bin/pip --log /opt/env1/pip.log install --upgrade setuptools',
149+
command: 'python3.8 -m venv --clear /opt/env1 && /opt/env1/bin/pip --log /opt/env1/pip.log install --upgrade pip && /opt/env1/bin/pip --log /opt/env1/pip.log install --upgrade setuptools',
150150
user: 'root',
151151
creates: '/opt/env1/bin/activate',
152152
path: [
@@ -166,7 +166,7 @@
166166
it {
167167
expect(subject).to contain_exec('python_virtualenv_/opt/env2').
168168
with(
169-
command: 'python3.8 -m venv --clear /opt/env2 && /opt/env2/bin/pip --log /opt/env2/pip.log install --upgrade \'pip <= 20.3.4\' && /opt/env2/bin/pip --log /opt/env2/pip.log install --upgrade setuptools',
169+
command: 'python3.8 -m venv --clear /opt/env2 && /opt/env2/bin/pip --log /opt/env2/pip.log install --upgrade \'pip <= 20.3.4\' && /opt/env2/bin/pip --log /opt/env2/pip.log install --upgrade setuptools',
170170
user: 'root',
171171
creates: '/opt/env2/bin/activate',
172172
path: [

spec/defines/pyvenv_spec.rb

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
context 'with default parameters' do
2121
it { is_expected.to contain_file('/opt/env').that_requires('Class[python::install]') }
22-
it { is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools') }
22+
it { is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools') }
2323
end
2424

2525
describe 'when ensure' do
@@ -36,5 +36,54 @@
3636
end
3737
end
3838
end
39+
40+
context "prompt on #{os} with python 3.6" do
41+
let :facts do
42+
# python 3.6 is required for venv and prompt
43+
facts.merge(
44+
python3_version: '3.6.1'
45+
)
46+
end
47+
let :title do
48+
'/opt/env'
49+
end
50+
51+
context 'with prompt' do
52+
let :params do
53+
{
54+
prompt: 'custom prompt',
55+
}
56+
end
57+
58+
it {
59+
is_expected.to contain_file('/opt/env').that_requires('Class[python::install]')
60+
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('python3.6 -m venv --clear --prompt custom\\ prompt /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
61+
}
62+
end
63+
end
64+
65+
context "prompt on #{os} with python 3.5" do
66+
let :facts do
67+
facts.merge(
68+
python3_version: '3.5.1'
69+
)
70+
end
71+
let :title do
72+
'/opt/env'
73+
end
74+
75+
context 'with prompt' do
76+
let :params do
77+
{
78+
prompt: 'custom prompt',
79+
}
80+
end
81+
82+
it {
83+
is_expected.to contain_file('/opt/env').that_requires('Class[python::install]')
84+
is_expected.to contain_exec('python_virtualenv_/opt/env').with_command('pyvenv-3.5 --clear /opt/env && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade pip && /opt/env/bin/pip --log /opt/env/pip.log install --upgrade setuptools')
85+
}
86+
end
87+
end
3988
end
4089
end

0 commit comments

Comments
 (0)