Skip to content

Commit

Permalink
Optimize fcontext parsing methods
Browse files Browse the repository at this point in the history
This uses filter_map (introduced in Ruby 2.7) to avoid constructing an
array by pushing values. It also uses start_with to avoid a regex
lookup.
  • Loading branch information
ekohl committed Jan 27, 2024
1 parent a3653da commit f3e4366
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
23 changes: 11 additions & 12 deletions lib/puppet/provider/selinux_fcontext/semanage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ def self.file_type_map(val)
end

def self.parse_fcontext_lines(lines)
ret = []
lines.each do |line|
lines.filter_map do |line|
next if line.strip.empty?
next if line =~ %r{^#}
next if line.start_with?('#')

split = line.split(%r{\s+})
if split.length == 2
Expand All @@ -45,16 +44,16 @@ def self.parse_fcontext_lines(lines)
user = range = role = nil
end
ft = file_type_map(file_type)
ret.push(new(ensure: :present,
name: "#{path_spec}_#{ft}",
pathspec: path_spec,
seltype: type,
seluser: user,
selrole: role,
selrange: range,
file_type: ft))

new(ensure: :present,
name: "#{path_spec}_#{ft}",
pathspec: path_spec,
seltype: type,
seluser: user,
selrole: role,
selrange: range,
file_type: ft)
end
ret
end

def self.parse_fcontext_file(path)
Expand Down
13 changes: 6 additions & 7 deletions lib/puppet/provider/selinux_fcontext_equivalence/semanage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
mk_resource_methods

def self.parse_fcontext_subs_lines(lines)
ret = []
lines.each do |line|
lines.filter_map do |line|
next if line.strip.empty?
next if line =~ %r{^#}
next if line.start_with?('#')

source, target = line.split(%r{\s+})
ret.push(new(ensure: :present,
name: source,
target: target))

new(ensure: :present,
name: source,
target: target)
end
ret
end

def self.instances
Expand Down

0 comments on commit f3e4366

Please sign in to comment.