Skip to content

Commit 80c1e70

Browse files
committed
revert previous patch for a much safer version
1 parent 8a6a1c6 commit 80c1e70

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,29 @@ cpan {'DBD::DB2':
132132

133133
Some modules like `Sys::RunAlone` don't return a code 0 when invoked with `perl -M$MODULE_NAME -e1`.
134134
This means there's no way for puppet to check if they're installed or not (in an agnostic way).
135-
Therefore you'll have to override the method. Use the `%` sign if you need the module name in the command line.
135+
Therefore you'll have to change the `exists_strategy`:
136136

137137
```puppet
138138
cpan {'Sys::RunAlone':
139139
ensure => 'present',
140-
exists_command => 'perl -M% -e"__END__"',
140+
exists_strategy => 'find',
141141
}
142142
```
143143

144+
Currently two strategies are implemented:
145+
146+
#### include (default)
147+
148+
Tries to include module:
149+
150+
```
151+
perl -M$MODULE_NAME
152+
```
153+
154+
#### find
155+
156+
Tries to find module in `@INC` list much like `pmpath` is doing.
157+
144158
## Reference
145159

146160
## Classes

lib/puppet/provider/cpan/default.rb

+16-6
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ def create
4646
system("#{umask} yes | perl #{ll} -MCPAN -e 'CPAN::install #{resource[:name]}'")
4747
end
4848

49-
exists_command = "perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1"
50-
if resource[:exists_command]
51-
exists_command = resource[:exists_command].gsub(/\%/,resource[:name])
49+
case resource[:exists_strategy]
50+
when :find
51+
# stolen from pmtools/pmpath
52+
# will not work on windows
53+
# use Module::Find maybe instead
54+
exists_command = "perl #{ll} -e '$module=#{resource[:name]}; eval \"local \\$^W = 0; require $module\";for ($shortpath = $module) {s{::}{/}g;s/$/.pm/;}if (defined($INC{$shortpath})) {exit 0;}; exit 2'"
55+
else
56+
exists_command = "perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1"
5257
end
5358
system(exists_command)
5459
estatus = $?.exitstatus
@@ -91,9 +96,14 @@ def exists?
9196
if resource[:local_lib]
9297
ll = "-Mlocal::lib=#{resource[:local_lib]}"
9398
end
94-
exists_command = "perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1"
95-
if resource[:exists_command]
96-
exists_command = resource[:exists_command].gsub(/\%/,resource[:name])
99+
case resource[:exists_strategy]
100+
when :find
101+
# stolen from pmtools/pmpath
102+
# will not work on windows
103+
# use Module::Find maybe instead
104+
exists_command = "perl #{ll} -e '$module=#{resource[:name]}; eval \"local \\$^W = 0; require $module\";for ($shortpath = $module) {s{::}{/}g;s/$/.pm/;}if (defined($INC{$shortpath})) {exit 0;}; exit 2'"
105+
else
106+
exists_command = "perl #{ll} -M#{resource[:name]} -e1 > /dev/null 2>&1"
97107
end
98108
Puppet.debug(exists_command)
99109
output = `#{exists_command}`

lib/puppet/type/cpan.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@
3838
end
3939
end
4040

41-
newparam(:exists_command) do
42-
desc 'Custom command to test for module existence'
43-
validate do |value|
44-
raise ArgumentError, 'String expected for exists_command.' unless value.is_a?(String)
45-
end
41+
newparam(:exists_strategy) do
42+
defaultto "include"
43+
newvalues("include", "find")
44+
desc 'Strategy to test for module existence'
4645
end
4746

4847
newparam(:force, :boolean => true, :parent => Puppet::Parameter::Boolean) do

test/exists_command.pp

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@
55
}
66

77
cpan { 'Sys::RunAlone':
8-
ensure => present,
9-
exists_command => 'perl -Mlocal::lib=/tmp/cpan -M% -e"__END__"',
10-
local_lib => '/tmp/cpan',
8+
ensure => present,
9+
exists_strategy => 'find',
10+
local_lib => '/tmp/cpan',
11+
}
12+
13+
cpan { 'Class::AccessorMaker':
14+
ensure => present,
15+
exists_strategy => 'find',
16+
local_lib => '/tmp/cpan',
17+
}
18+
19+
cpan { 'Riemann::Client':
20+
ensure => present,
21+
exists_strategy => 'include',
22+
local_lib => '/tmp/cpan',
1123
}

0 commit comments

Comments
 (0)