Class: Puppet::FileServing::Fileset

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/file_serving/fileset.rb

Overview

Operate recursively on a path, returning a set of file paths.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Fileset

Returns a new instance of Fileset.

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/puppet/file_serving/fileset.rb', line 61

def initialize(path, options = {})
  path = path.chomp(File::SEPARATOR) unless path == File::SEPARATOR
  raise ArgumentError.new("Fileset paths must be fully qualified") unless File.expand_path(path) == path

  @path = path

  # Set our defaults.
  @ignore = []
  @links = :manage
  @recurse = false
  @recurselimit = :infinite

  if options.is_a?(Puppet::Indirector::Request)
    initialize_from_request(options)
  else
    initialize_from_hash(options)
  end

  raise ArgumentError.new("Fileset paths must exist") unless stat = stat(path)
  raise ArgumentError.new("Fileset recurse parameter must not be a number anymore, please use recurselimit") if @recurse.is_a?(Integer)
end

Instance Attribute Details

#checksum_typeObject

Returns the value of attribute checksum_type.



12
13
14
# File 'lib/puppet/file_serving/fileset.rb', line 12

def checksum_type
  @checksum_type
end

#ignoreObject

Returns the value of attribute ignore.



11
12
13
# File 'lib/puppet/file_serving/fileset.rb', line 11

def ignore
  @ignore
end

Returns the value of attribute links.



11
12
13
# File 'lib/puppet/file_serving/fileset.rb', line 11

def links
  @links
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/puppet/file_serving/fileset.rb', line 11

def path
  @path
end

#recurseObject

Returns the value of attribute recurse.



12
13
14
# File 'lib/puppet/file_serving/fileset.rb', line 12

def recurse
  @recurse
end

#recurselimitObject

Returns the value of attribute recurselimit.



12
13
14
# File 'lib/puppet/file_serving/fileset.rb', line 12

def recurselimit
  @recurselimit
end

Class Method Details

.merge(*filesets) ⇒ Object

Produce a hash of files, with merged so that earlier files with the same postfix win. E.g., /dir1/subfile beats /dir2/subfile. It’s a hash because we need to know the relative path of each file, and the base directory.

This will probably only ever be used for searching for plugins.


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/puppet/file_serving/fileset.rb', line 19

def self.merge(*filesets)
  result = {}

  filesets.each do |fileset|
    fileset.files.each do |file|
      result[file] ||= fileset.path
    end
  end

  result
end

Instance Method Details

#filesObject

Return a list of all files in our fileset. This is different from the normal definition of find in that we support specific levels of recursion, which means we need to know when we’re going another level deep, which Find doesn’t do.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/puppet/file_serving/fileset.rb', line 35

def files
  files = perform_recursion

  # Now strip off the leading path, so each file becomes relative, and remove
  # any slashes that might end up at the beginning of the path.
  result = files.collect { |file| file.sub(%r{^#{Regexp.escape(@path)}/*}, '') }

  # And add the path itself.
  result.unshift(".")

  result
end

#ignore?(path) ⇒ Boolean

Should we ignore this path?

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/puppet/file_serving/fileset.rb', line 49

def ignore?(path)
  return false if @ignore == [nil]

  # 'detect' normally returns the found result, whereas we just want true/false.
  ! @ignore.detect { |pattern| File.fnmatch?(pattern, path) }.nil?
end

#initialize_from_hash(options) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/puppet/file_serving/fileset.rb', line 97

def initialize_from_hash(options)
  options.each do |option, value|
    method = option.to_s + "="
    begin
      send(method, value)
    rescue NoMethodError
      raise ArgumentError, "Invalid option '#{option}'"
    end
  end
end

#initialize_from_request(request) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/puppet/file_serving/fileset.rb', line 108

def initialize_from_request(request)
  [:links, :ignore, :recurse, :recurselimit, :checksum_type].each do |param|
    if request.options.include?(param) # use 'include?' so the values can be false
      value = request.options[param]
    elsif request.options.include?(param.to_s)
      value = request.options[param.to_s]
    end
    next if value.nil?
    value = true if value == "true"
    value = false if value == "false"
    value = Integer(value) if value.is_a?(String) and value =~ /^\d+$/
    send(param.to_s + "=", value)
  end
end

#recurse?(depth) ⇒ Boolean

Should we recurse further? This is basically a single place for all of the logic around recursion.

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/puppet/file_serving/fileset.rb', line 92

def recurse?(depth)
  # recurse if told to, and infinite recursion or current depth not at the limit
  self.recurse and (self.recurselimit == :infinite or depth <= self.recurselimit)
end

#stat(path) ⇒ Object

Stat a given file, using the links-appropriate method.



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/puppet/file_serving/fileset.rb', line 161

def stat(path)
  @stat_method ||= self.links == :manage ? :lstat : :stat

  begin
    return File.send(@stat_method, path)
  rescue
    # If this happens, it is almost surely because we're
    # trying to manage a link to a file that does not exist.
    return nil
  end
end