diff --git a/lib/puppet/util/inifile.rb b/lib/puppet/util/inifile.rb index 17e76d1f7..8f5b3c78c 100644 --- a/lib/puppet/util/inifile.rb +++ b/lib/puppet/util/inifile.rb @@ -1,222 +1,340 @@ # Module Puppet::IniConfig # A generic way to parse .ini style files and manipulate them in memory # One 'file' can be made up of several physical files. Changes to sections # on the file are tracked so that only the physical files in which # something has changed are written back to disk # Great care is taken to preserve comments and blank lines from the original # files # # The parsing tries to stay close to python's ConfigParser require 'puppet/util/filetype' +require 'puppet/error' module Puppet::Util::IniConfig # A section in a .ini file class Section attr_reader :name, :file, :entries attr_writer :destroy def initialize(name, file) @name = name @file = file @dirty = false @entries = [] @destroy = false end # Has this section been modified since it's been read in # or written back to disk def dirty? @dirty end def mark_dirty @dirty = true end # Should only be used internally def mark_clean @dirty = false end # Should the file be destroyed? def destroy? @destroy end # Add a line of text (e.g., a comment) Such lines # will be written back out in exactly the same # place they were read in def add_line(line) @entries << line end # Set the entry 'key=value'. If no entry with the # given key exists, one is appended to teh end of the section def []=(key, value) entry = find_entry(key) @dirty = true if entry.nil? @entries << [key, value] else entry[1] = value end end # Return the value associated with KEY. If no such entry # exists, return nil def [](key) entry = find_entry(key) return(entry.nil? ? nil : entry[1]) end # Format the section as text in the way it should be # written to file def format text = "[#{name}]\n" @entries.each do |entry| if entry.is_a?(Array) key, value = entry text << "#{key}=#{value}\n" unless value.nil? else text << entry end end text end private def find_entry(key) @entries.each do |entry| return entry if entry.is_a?(Array) && entry[0] == key end nil end end # A logical .ini-file that can be spread across several physical # files. For each physical file, call #read with the filename class File def initialize @files = {} end # Add the contents of the file with name FILE to the # already existing sections def read(file) text = Puppet::Util::FileType.filetype(:flat).new(file).read raise "Could not find #{file}" if text.nil? section = nil # The name of the current section optname = nil # The name of the last option in section line = 0 @files[file] = [] text.each_line do |l| line += 1 if l.strip.empty? || "#;".include?(l[0,1]) || (l.split(nil, 2)[0].downcase == "rem" && l[0,1].downcase == "r") # Whitespace or comment if section.nil? @files[file] << l else section.add_line(l) end elsif " \t\r\n\f".include?(l[0,1]) && section && optname # continuation line section[optname] += "\n#{l.chomp}" elsif l =~ /^\[([^\]]+)\]/ # section heading section.mark_clean unless section.nil? section = add_section($1, file) optname = nil elsif l =~ /^\s*([^\s=]+)\s*\=(.*)$/ # We allow space around the keys, but not the values # For the values, we don't know if space is significant if section.nil? raise "#{file}:#{line}:Key/value pair outside of a section for key #{$1}" else section[$1] = $2 optname = $1 end else raise "#{file}:#{line}: Can't parse '#{l.chomp}'" end end section.mark_clean unless section.nil? end # Store all modifications made to sections in this file back # to the physical files. If no modifications were made to # a physical file, nothing is written def store @files.each do |file, lines| text = "" dirty = false destroy = false lines.each do |l| if l.is_a?(Section) destroy ||= l.destroy? dirty ||= l.dirty? text << l.format l.mark_clean else text << l end end # We delete the file and then remove it from the list of files. if destroy ::File.unlink(file) @files.delete(file) else if dirty Puppet::Util::FileType.filetype(:flat).new(file).write(text) return file end end end end # Execute BLOCK, passing each section in this file # as an argument def each_section(&block) @files.each do |file, list| list.each do |entry| yield(entry) if entry.is_a?(Section) end end end # Execute BLOCK, passing each file constituting this inifile # as an argument def each_file(&block) @files.keys.each do |file| yield(file) end end # Return the Section with the given name or nil def [](name) name = name.to_s each_section do |section| return section if section.name == name end nil end # Return true if the file contains a section with name NAME def include?(name) ! self[name].nil? end # Add a section to be stored in FILE when store is called def add_section(name, file) raise "A section with name #{name} already exists" if include?(name) result = Section.new(name, file) @files[file] ||= [] @files[file] << result result end end -end + class PhysicalFile + + # @!attribute [r] filetype + # @api private + # @return [Puppet::Util::FileType::FileTypeFlat] + attr_reader :filetype + + # @!attribute [r] contents + # @api private + # @return [Array] + attr_reader :contents + + def initialize(file) + @file = file + @contents = [] + @filetype = Puppet::Util::FileType.filetype(:flat).new(file) + end + + # Read and parse the on-disk file associated with this object + def read + text = @filetype.read + if text.nil? + raise IniParseError, "Cannot read nonexistent file #{@file.inspect}" + end + parse(text) + end + + INI_COMMENT = Regexp.union( + /^\s*$/, + /^[#;]/, + /^\s*rem\s/i + ) + INI_CONTINUATION = /^[ \t\r\n\f]/ + INI_SECTION_NAME = /^\[([^\]]+)\]/ + INI_PROPERTY = /^\s*([^\s=]+)\s*\=(.*)$/ + + # @api private + def parse(text) + section = nil # The name of the current section + optname = nil # The name of the last option in section + line_num = 0 + + text.each_line do |l| + line_num += 1 + if l.match(INI_COMMENT) + # Whitespace or comment + if section.nil? + @contents << l + else + section.add_line(l) + end + elsif l.match(INI_CONTINUATION) && section && optname + # continuation line + section[optname] += "\n#{l.chomp}" + elsif (match = l.match(INI_SECTION_NAME)) + # section heading + section.mark_clean if section + + section_name = match[1] + + if get_section(section_name) + raise IniParseError.new( + "Section #{section_name.inspect} is already defined, cannot redefine", @file, line_num + ) + end + + section = create_section(section_name) + optname = nil + elsif (match = l.match(INI_PROPERTY)) + # We allow space around the keys, but not the values + # For the values, we don't know if space is significant + key = match[1] + val = match[2] + + if section.nil? + raise IniParseError.new("Property with key #{key.inspect} outside of a section") + end + + section[key] = val + optname = key + else + raise IniParseError.new("Can't parse line '#{l.chomp}'", @file, line_num) + end + end + section.mark_clean unless section.nil? + end + + # @return [Array] All sections defined in + # this file. + def sections + @contents.select { |entry| entry.is_a? Section } + end + + # @return [Puppet::Util::IniConfig::Section, nil] The section with the + # given name if it exists, else nil. + def get_section(name) + @contents.find { |entry| entry.is_a? Section and entry.name == name } + end + + private + + # Create a new section and store it in the file contents + # + # @api private + # @param name [String] The name of the section to create + # @return [Puppet::Util::IniConfig::Section] + def create_section(name) + section = Section.new(name, @file) + @contents << section + + section + end + end + + class IniParseError < Puppet::Error + include Puppet::ExternalFileError + end +end diff --git a/spec/unit/util/inifile_spec.rb b/spec/unit/util/inifile_spec.rb index 3be3378e7..66fca61c7 100644 --- a/spec/unit/util/inifile_spec.rb +++ b/spec/unit/util/inifile_spec.rb @@ -1,84 +1,221 @@ require 'spec_helper' require 'puppet/util/inifile' describe Puppet::Util::IniConfig::Section do subject { described_class.new('testsection', '/some/imaginary/file') } describe "determining if the section is dirty" do it "is not dirty on creation" do expect(subject).to_not be_dirty end it "is dirty if a key is changed" do subject['hello'] = 'world' expect(subject).to be_dirty end it "is dirty if the section has been explicitly marked as dirty" do subject.mark_dirty expect(subject).to be_dirty end it "is clean if the section has been explicitly marked as clean" do subject['hello'] = 'world' subject.mark_clean expect(subject).to_not be_dirty end end describe "reading an entry" do it "returns nil if the key is not present" do expect(subject['hello']).to be_nil end it "returns the value if the key is specified" do subject.entries << ['hello', 'world'] expect(subject['hello']).to eq 'world' end it "ignores comments when looking for a match" do subject.entries << '#this = comment' expect(subject['#this']).to be_nil end end describe "formatting the section" do it "prefixes the output with the section header" do expect(subject.format).to eq "[testsection]\n" end it "restores comments and blank lines" do subject.entries << "#comment\n" subject.entries << " " expect(subject.format).to eq( "[testsection]\n" + "#comment\n" + " " ) end it "adds all keys that have values" do subject.entries << ['somekey', 'somevalue'] expect(subject.format).to eq("[testsection]\nsomekey=somevalue\n") end it "excludes keys that have a value of nil" do subject.entries << ['empty', nil] expect(subject.format).to eq("[testsection]\n") end it "preserves the order of the section" do subject.entries << ['firstkey', 'firstval'] subject.entries << "# I am a comment, hear me roar\n" subject.entries << ['secondkey', 'secondval'] expect(subject.format).to eq( "[testsection]\n" + "firstkey=firstval\n" + "# I am a comment, hear me roar\n" + "secondkey=secondval\n" ) end end end + +describe Puppet::Util::IniConfig::PhysicalFile do + subject { described_class.new('/some/nonexistent/file') } + + describe "when reading a file" do + it "raises an error if the file does not exist" do + subject.filetype.stubs(:read) + expect { + subject.read + }.to raise_error(%r[Cannot read nonexistent file .*/some/nonexistent/file]) + end + + it "passes the contents of the file to #parse" do + subject.filetype.stubs(:read).returns "[section]" + subject.expects(:parse).with("[section]") + + subject.read + end + + end + + describe "when parsing a file" do + describe "parsing sections" do + it "creates new sections the first time that the section is found" do + text = "[mysect]\n" + + subject.parse(text) + + expect(subject.contents).to have(1).items + sect = subject.contents[0] + expect(sect.name).to eq "mysect" + end + + it "raises an error if a section is redefined in the file" do + text = "[mysect]\n[mysect]\n" + + expect { + subject.parse(text) + }.to raise_error(Puppet::Util::IniConfig::IniParseError, + /Section "mysect" is already defined, cannot redefine/) + end + + it "raises an error if a section is redefined in the file collection" + end + + describe "parsing properties" do + it "raises an error if the property is not within a section" do + text = "key=val\n" + + expect { + subject.parse(text) + }.to raise_error(Puppet::Util::IniConfig::IniParseError, + /Property with key "key" outside of a section/) + end + + it "adds the property to the current section" do + text = "[main]\nkey=val\n" + + subject.parse(text) + expect(subject.contents).to have(1).items + sect = subject.contents[0] + expect(sect['key']).to eq "val" + end + end + + describe "parsing line continuations" do + + it "adds the continued line to the last parsed property" do + text = "[main]\nkey=val\n moreval" + + subject.parse(text) + expect(subject.contents).to have(1).items + sect = subject.contents[0] + expect(sect['key']).to eq "val\n moreval" + end + end + + describe "parsing comments and whitespace" do + it "treats # as a comment leader" do + text = "# octothorpe comment" + + subject.parse(text) + expect(subject.contents).to eq ["# octothorpe comment"] + end + + it "treats ; as a comment leader" do + text = "; semicolon comment" + + subject.parse(text) + expect(subject.contents).to eq ["; semicolon comment"] + end + + it "treates 'rem' as a comment leader" do + text = "rem rapid eye movement comment" + + subject.parse(text) + expect(subject.contents).to eq ["rem rapid eye movement comment"] + end + + it "stores comments and whitespace in a section in the correct section" do + text = "[main]\n; main section comment" + + subject.parse(text) + + sect = subject.get_section("main") + expect(sect.entries).to eq ["; main section comment"] + end + end + end + + it "can return all sections" do + text = "[first]\n" + + "; comment\n" + + "[second]\n" + + "key=value" + + subject.parse(text) + + sections = subject.sections + expect(sections).to have(2).items + expect(sections[0].name).to eq "first" + expect(sections[1].name).to eq "second" + end + + it "can retrieve a specific section" do + text = "[first]\n" + + "; comment\n" + + "[second]\n" + + "key=value" + + subject.parse(text) + + section = subject.get_section("second") + expect(section.name).to eq "second" + expect(section["key"]).to eq "value" + end +end