Skip to content

Commit

Permalink
Accept to use python3 if python is not on the path
Browse files Browse the repository at this point in the history
RHEL8 in particular has no python command by design
attempt to use python3 if there is no python command.

https://developers.redhat.com/blog/2018/11/14/python-in-rhel-8/
  • Loading branch information
traylenator committed Jul 11, 2019
1 parent 8fd78a0 commit 6359cfb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -1453,8 +1453,8 @@ class { 'collectd::plugin::protocols':

#### Class: `collectd::plugin::python`
The plugin uses a fact `python_dir` to find the python load path for modules.
python must be installed as a pre-requisite for the this fact to give a
non-default value.
python or python3 must be installed as a pre-requisite for the this
fact to give a non-default value.

* `modulepaths` is an array of paths where will be Collectd looking for Python
modules, Puppet will ensure that each of specified directories exists and it
Expand Down
6 changes: 6 additions & 0 deletions lib/facter/python_dir.rb
Expand Up @@ -10,6 +10,12 @@
else
Facter::Util::Resolution.exec('python -c "import site; print(site.getsitepackages()[0])"')
end
elsif Facter::Util::Resolution.which('python3')
if Facter.value(:osfamily) == 'RedHat'
Facter::Util::Resolution.exec('python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"')
else
Facter::Util::Resolution.exec('python3 -c "import site; print(site.getsitepackages()[0])"')
end
else
''
end
Expand Down
13 changes: 13 additions & 0 deletions spec/unit/python_dir_spec.rb
Expand Up @@ -26,10 +26,23 @@
expect(Facter.value(:python_dir)).to eq('/usr/lib/python2.7/site-packages')
end
end

context 'RedHat versioned python' do
before do
Facter.fact(:osfamily).stubs(:value).returns('RedHat')
Facter::Util::Resolution.stubs(:which).with('python').returns(false)
Facter::Util::Resolution.stubs(:which).with('python3').returns(true)
Facter::Util::Resolution.stubs(:exec).with('python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"').returns('/usr/lib/python3.6/site-packages')
end
it do
expect(Facter.value(:python_dir)).to eq('/usr/lib/python3.6/site-packages')
end
end
end

it 'is empty string if python not installed' do
Facter::Util::Resolution.stubs(:which).with('python').returns(nil)
Facter::Util::Resolution.stubs(:which).with('python3').returns(nil)
expect(Facter.fact(:python_dir).value).to eq('')
end
end

0 comments on commit 6359cfb

Please sign in to comment.