4 module Puppet::Parser::Functions
5 newfunction(:is_absolute_path, :type => :rvalue, :arity => 1, :doc => <<-'DOC') do |args|
6 Returns boolean true if the string represents an absolute path in the filesystem. This function works
7 for windows and unix style paths.
9 The following values will return true:
11 $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
12 is_absolute_path($my_path)
13 $my_path2 = '/var/lib/puppet'
14 is_absolute_path($my_path2)
15 $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet']
16 is_absolute_path($my_path3)
17 $my_path4 = ['/var/lib/puppet']
18 is_absolute_path($my_path4)
20 The following values will return false:
22 is_absolute_path(true)
23 is_absolute_path('../var/lib/puppet')
24 is_absolute_path('var/lib/puppet')
26 is_absolute_path($undefined)
29 function_deprecation([:is_absolute_path, 'This method is deprecated, please use the stdlib validate_legacy function,
30 with Stdlib::Compat::Absolute_path. There is further documentation for validate_legacy function in the README.'])
34 # This logic was borrowed from
35 # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
36 # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
37 if Puppet::Util.respond_to?(:absolute_path?)
38 value = (Puppet::Util.absolute_path?(path, :posix) || Puppet::Util.absolute_path?(path, :windows))
40 # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
41 # Determine in a platform-specific way whether a path is absolute. This
42 # defaults to the local platform if none is specified.
43 # Escape once for the string literal, and once for the regex.
47 :windows => %r{^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))}i,
50 value = !!(path =~ regexes[:posix]) || !!(path =~ regexes[:windows]) # rubocop:disable Style/DoubleNegation : No alternative known