diff options
author | Luke Kanies <luke@madstop.com> | 2007-12-11 00:36:44 -0600 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2007-12-11 00:36:44 -0600 |
commit | 02b64ab3bc9ce29fd5d482a78781c341ba791ba6 (patch) | |
tree | 0f3898968b3cece4da51b1d18645c2dd90a265f1 | |
parent | 584127c71352dfa1b13eb4e948a93d5078eff73f (diff) | |
download | puppet-02b64ab3bc9ce29fd5d482a78781c341ba791ba6.tar.gz puppet-02b64ab3bc9ce29fd5d482a78781c341ba791ba6.tar.xz puppet-02b64ab3bc9ce29fd5d482a78781c341ba791ba6.zip |
Applying patch by josb in #884 to provide pattern
matching in the tidy type.
-rwxr-xr-x | lib/puppet/type/tidy.rb | 20 | ||||
-rwxr-xr-x | test/ral/types/tidy.rb | 52 |
2 files changed, 67 insertions, 5 deletions
diff --git a/lib/puppet/type/tidy.rb b/lib/puppet/type/tidy.rb index 27456f7d6..dacf037ac 100755 --- a/lib/puppet/type/tidy.rb +++ b/lib/puppet/type/tidy.rb @@ -1,7 +1,3 @@ - -require 'etc' -require 'puppet/type/pfile' - module Puppet newtype(:tidy, Puppet.type(:file)) do @doc = "Remove unwanted files based on specific criteria. Multiple @@ -14,11 +10,17 @@ module Puppet isnamevar end + newparam(:matches) do + desc "One or more file glob patterns, which restrict the list of + files to be tidied to those whose basenames match at least one + of the patterns specified. Multiple patterns can be specified + using an array." + end + copyparam(Puppet.type(:file), :backup) newproperty(:ensure) do desc "An internal attribute used to determine which files should be removed." - require 'etc' @nodoc = true @@ -47,6 +49,14 @@ module Puppet end else @out = [] + if @resource[:matches] + basename = File.basename(@resource[:path]) + flags = File::FNM_DOTMATCH | File::FNM_PATHNAME + unless @resource[:matches].any? {|pattern| File.fnmatch(pattern, basename, flags) } + self.debug "No patterns specified match basename, skipping" + return true + end + end TATTRS.each do |param| if property = @resource.property(param) self.debug "No is value for %s", [param] if is[property].nil? diff --git a/test/ral/types/tidy.rb b/test/ral/types/tidy.rb index 8220d9974..59a0e5e2d 100755 --- a/test/ral/types/tidy.rb +++ b/test/ral/types/tidy.rb @@ -235,5 +235,57 @@ class TestTidy < Test::Unit::TestCase assert_apply(tidy) assert(! FileTest.symlink?(link), "link was not tidied") end + + def test_glob_matches_single + dir = mktmpdir + files = { + :remove => File.join(dir, "01-foo"), + :keep => File.join(dir, "default") + } + files.each do |tag, file| + File.open(file, "w") { |f| + f.puts "some stuff" + } + end + + tidy = Puppet.type(:tidy).create( + :name => dir, + :size => "1b", + :rmdirs => true, + :recurse => true, + :matches => "01-*" + ) + assert_apply(tidy) + + assert(FileTest.exists?(files[:keep]), "%s was tidied" % files[:keep]) + assert(!FileTest.exists?(files[:remove]), "Tidied %s still exists" % files[:remove]) + end + + def test_glob_matches_multiple + dir = mktmpdir + files = { + :remove1 => File.join(dir, "01-foo"), + :remove2 => File.join(dir, "02-bar"), + :keep1 => File.join(dir, "default") + } + files.each do |tag, file| + File.open(file, "w") { |f| + f.puts "some stuff" + } + end + + tidy = Puppet.type(:tidy).create( + :name => dir, + :size => "1b", + :rmdirs => true, + :recurse => true, + :matches => ["01-*", "02-*"] + ) + assert_apply(tidy) + + assert(FileTest.exists?(files[:keep1]), "%s was tidied" % files[:keep1]) + assert(!FileTest.exists?(files[:remove1]), "Tidied %s still exists" % files[:remove1]) + assert(!FileTest.exists?(files[:remove2]), "Tidied %s still exists" % files[:remove2]) + end end |