Skip to content

Commit

Permalink
Merge pull request #397 from cyberious/4.6.x
Browse files Browse the repository at this point in the history
4.6.x
  • Loading branch information
cmurphy committed Jan 14, 2015
2 parents 80f0962 + cfacdd5 commit 712a58a
Show file tree
Hide file tree
Showing 25 changed files with 568 additions and 195 deletions.
3 changes: 3 additions & 0 deletions .fixtures.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fixtures:
symlinks:
stdlib: "#{source_dir}"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ spec/fixtures/
.vagrant/
.bundle/
coverage/
.idea/
*.iml
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
sudo: false
language: ruby
bundler_args: --without system_tests
script: "bundle exec rake validate && bundle exec rake lint && bundle exec rake spec SPEC_OPTS='--color --format documentation'"
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
##2015-01-14 - Supported Release 4.6.0
###Summary

Improved functionality and preparing for Puppet Next with new parser

####Features
- MODULES-444: concat can now take more than two arrays
- basename function added to have Ruby File.basename functionality
- delete function can now take an array of items to remove
- MODULES-1473: deprecate type function in favor of type_of

####Bugfixes
- Several test case fixes
- Ensure_resource is more verbose on debug mode


##2014-12-15 - Supported Release 4.5.0
###Summary

Expand Down
142 changes: 84 additions & 58 deletions README.markdown

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/facter/facter_dot_d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
class Facter::Util::DotD
require 'yaml'

def initialize(dir="/etc/facts.d", cache_file="/tmp/facts_cache.yml")
def initialize(dir="/etc/facts.d", cache_file=File.join(Puppet[:libdir], "facts_dot_d.cache"))
@dir = dir
@cache_file = cache_file
@cache = nil
@types = {".txt" => :txt, ".json" => :json, ".yaml" => :yaml}
end

def entries
Dir.entries(@dir).reject{|f| f =~ /^\.|\.ttl$/}.sort.map {|f| File.join(@dir, f) }
Dir.entries(@dir).reject { |f| f =~ /^\.|\.ttl$/ }.sort.map { |f| File.join(@dir, f) }
rescue
[]
end
Expand Down Expand Up @@ -113,7 +113,7 @@ def script_parser(file)

def cache_save!
cache = load_cache
File.open(@cache_file, "w", 0600) {|f| f.write(YAML.dump(cache)) }
File.open(@cache_file, "w", 0600) { |f| f.write(YAML.dump(cache)) }
rescue
end

Expand Down
17 changes: 17 additions & 0 deletions lib/puppet/functions/type_of.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Returns the type when passed a value.
#
# @example how to compare values' types
# # compare the types of two values
# if type_of($first_value) != type_of($second_value) { fail("first_value and second_value are different types") }
# @example how to compare against an abstract type
# unless type_of($first_value) <= Numeric { fail("first_value must be Numeric") }
# unless type_of{$first_value) <= Collection[1] { fail("first_value must be an Array or Hash, and contain at least one element") }
#
# See the documentation for "The Puppet Type System" for more information about types.
# See the `assert_type()` function for flexible ways to assert the type of a value.
#
Puppet::Functions.create_function(:type_of) do
def type_of(value)
Puppet::Pops::Types::TypeCalculator.infer_set(value)
end
end
34 changes: 34 additions & 0 deletions lib/puppet/parser/functions/basename.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Puppet::Parser::Functions
newfunction(:basename, :type => :rvalue, :doc => <<-EOS
Strips directory (and optional suffix) from a filename
EOS
) do |arguments|

if arguments.size < 1 then
raise(Puppet::ParseError, "basename(): No arguments given")
elsif arguments.size > 2 then
raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})")
else

unless arguments[0].is_a?(String)
raise(Puppet::ParseError, 'basename(): Requires string as first argument')
end

if arguments.size == 1 then
rv = File.basename(arguments[0])
elsif arguments.size == 2 then

unless arguments[1].is_a?(String)
raise(Puppet::ParseError, 'basename(): Requires string as second argument')
end

rv = File.basename(arguments[0], arguments[1])
end

end

return rv
end
end

# vim: set ts=2 sw=2 et :
18 changes: 11 additions & 7 deletions lib/puppet/parser/functions/concat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@

module Puppet::Parser::Functions
newfunction(:concat, :type => :rvalue, :doc => <<-EOS
Appends the contents of array 2 onto array 1.
Appends the contents of multiple arrays into array 1.
*Example:*
concat(['1','2','3'],['4','5','6'])
concat(['1','2','3'],['4','5','6'],['7','8','9'])
Would result in:
['1','2','3','4','5','6']
['1','2','3','4','5','6','7','8','9']
EOS
) do |arguments|

# Check that 2 arguments have been given ...
# Check that more than 2 arguments have been given ...
raise(Puppet::ParseError, "concat(): Wrong number of arguments " +
"given (#{arguments.size} for 2)") if arguments.size != 2
"given (#{arguments.size} for < 2)") if arguments.size < 2

a = arguments[0]
b = arguments[1]

# Check that the first parameter is an array
unless a.is_a?(Array)
raise(Puppet::ParseError, 'concat(): Requires array to work with')
end

result = a + Array(b)
result = a
arguments.shift

arguments.each do |x|
result = result + Array(x)
end

return result
end
Expand Down
27 changes: 15 additions & 12 deletions lib/puppet/parser/functions/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ module Puppet::Parser::Functions
delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
Would return: {'a'=>1,'c'=>3}
delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c'])
Would return: {'a'=>1}
delete('abracadabra', 'bra')
Would return: 'acada'
EOS
EOS
) do |arguments|

if (arguments.size != 2) then
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
"given #{arguments.size} for 2.")
"given #{arguments.size} for 2.")
end

collection = arguments[0].dup
item = arguments[1]

case collection
when Array, Hash
collection.delete item
when String
collection.gsub! item, ''
else
raise(TypeError, "delete(): First argument must be an Array, " +
"String, or Hash. Given an argument of class #{collection.class}.")
Array(arguments[1]).each do |item|
case collection
when Array, Hash
collection.delete item
when String
collection.gsub! item, ''
else
raise(TypeError, "delete(): First argument must be an Array, " +
"String, or Hash. Given an argument of class #{collection.class}.")
end
end
collection
end
Expand Down
3 changes: 2 additions & 1 deletion lib/puppet/parser/functions/ensure_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
items.each do |item|
Puppet::Parser::Functions.function(:defined_with_params)
if function_defined_with_params(["#{type}[#{item}]", params])
Puppet.debug("Resource #{type}[#{item}] not created because it already exists")
Puppet.debug("Resource #{type}[#{item}] with params #{params} not created because it already exists")
else
Puppet.debug("Create new resource #{type}[#{item}] with params #{params}")
Puppet::Parser::Functions.function(:create_resources)
function_create_resources([type.capitalize, { item => params }])
end
Expand Down
43 changes: 6 additions & 37 deletions lib/puppet/parser/functions/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,15 @@

module Puppet::Parser::Functions
newfunction(:type, :type => :rvalue, :doc => <<-EOS
Returns the type when passed a variable. Type can be one of:
* string
* array
* hash
* float
* integer
* boolean
DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.
EOS
) do |arguments|

raise(Puppet::ParseError, "type(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1

value = arguments[0]

klass = value.class

if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass)
raise(Puppet::ParseError, 'type(): Unknown type')
end

klass = klass.to_s # Ugly ...
) do |args|

# We note that Integer is the parent to Bignum and Fixnum ...
result = case klass
when /^(?:Big|Fix)num$/ then 'integer'
when /^(?:True|False)Class$/ then 'boolean'
else klass
warning("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.")
if ! Puppet::Parser::Functions.autoloader.loaded?(:type3x)
Puppet::Parser::Functions.autoloader.load(:type3x)
end

if result == "String" then
if value == value.to_i.to_s then
result = "Integer"
elsif value == value.to_f.to_s then
result = "Float"
end
end

return result.downcase
function_type3x(args + [false])
end
end

Expand Down
51 changes: 51 additions & 0 deletions lib/puppet/parser/functions/type3x.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# type3x.rb
#

module Puppet::Parser::Functions
newfunction(:type3x, :type => :rvalue, :doc => <<-EOS
DEPRECATED: This function will be removed when puppet 3 support is dropped; please migrate to the new parser's typing system.
Returns the type when passed a value. Type can be one of:
* string
* array
* hash
* float
* integer
* boolean
EOS
) do |args|
raise(Puppet::ParseError, "type3x(): Wrong number of arguments " +
"given (#{args.size} for 1)") if args.size < 1

value = args[0]

klass = value.class

if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass)
raise(Puppet::ParseError, 'type3x(): Unknown type')
end

klass = klass.to_s # Ugly ...

# We note that Integer is the parent to Bignum and Fixnum ...
result = case klass
when /^(?:Big|Fix)num$/ then 'integer'
when /^(?:True|False)Class$/ then 'boolean'
else klass
end

if result == "String" then
if value == value.to_i.to_s then
result = "Integer"
elsif value == value.to_f.to_s then
result = "Float"
end
end

return result.downcase
end
end

# vim: set ts=2 sw=2 et :
61 changes: 37 additions & 24 deletions lib/puppet/parser/functions/validate_absolute_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ module Puppet::Parser::Functions
The following values will pass:
$my_path = "C:/Program Files (x86)/Puppet Labs/Puppet"
$my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
validate_absolute_path($my_path)
$my_path2 = "/var/lib/puppet"
$my_path2 = '/var/lib/puppet'
validate_absolute_path($my_path2)
$my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet']
validate_absolute_path($my_path3)
$my_path4 = ['/var/lib/puppet','/usr/share/puppet']
validate_absolute_path($my_path4)
The following values will fail, causing compilation to abort:
validate_absolute_path(true)
validate_absolute_path('../var/lib/puppet')
validate_absolute_path('var/lib/puppet')
validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
$undefined = undef
Expand All @@ -28,28 +33,36 @@ module Puppet::Parser::Functions
end

args.each do |arg|
# This logic was borrowed from
# [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)

# Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
if Puppet::Util.respond_to?(:absolute_path?) then
unless Puppet::Util.absolute_path?(arg, :posix) or Puppet::Util.absolute_path?(arg, :windows)
raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
# put arg to candidate var to be able to replace it
candidates = arg
# if arg is just a string with a path to test, convert it to an array
# to avoid test code duplication
unless arg.is_a?(Array) then
candidates = Array.new(1,arg)
end
# iterate over all pathes within the candidates array
candidates.each do |path|
# This logic was borrowed from
# [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
# Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
if Puppet::Util.respond_to?(:absolute_path?) then
unless Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows)
raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
end
else
# This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
# Determine in a platform-specific way whether a path is absolute. This
# defaults to the local platform if none is specified.
# Escape once for the string literal, and once for the regex.
slash = '[\\\\/]'
name = '[^\\\\/]+'
regexes = {
:windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
:posix => %r!^/!,
}
rval = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows]))
rval or raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
end
else
# This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
# Determine in a platform-specific way whether a path is absolute. This
# defaults to the local platform if none is specified.
# Escape once for the string literal, and once for the regex.
slash = '[\\\\/]'
name = '[^\\\\/]+'
regexes = {
:windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
:posix => %r!^/!,
}

rval = (!!(arg =~ regexes[:posix])) || (!!(arg =~ regexes[:windows]))
rval or raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
end
end
end
Expand Down
Loading

0 comments on commit 712a58a

Please sign in to comment.