Class: ChkBuild::LogFile
- Inherits:
-
Object
- Object
- ChkBuild::LogFile
- Defined in:
- lib/chkbuild/logfile.rb
Constant Summary collapse
- InitialMark =
'=='
Class Method Summary collapse
Instance Method Summary collapse
- #change_default_output ⇒ Object
- #dependencies ⇒ Object
- #depsuffixed_name ⇒ Object
- #each_secname(&block) ⇒ Object
- #get_all_log ⇒ Object
- #get_section(secname) ⇒ Object
-
#initialize(filename, writemode) ⇒ LogFile
constructor
A new instance of LogFile.
- #modify_section(secname, data) ⇒ Object
- #secnames ⇒ Object
- #section_size(secname) ⇒ Object
-
#start_section(secname) ⇒ Object
start_section returns the (unique) section name.
- #suffixed_name ⇒ Object
- #suffixes ⇒ Object
- #target_name ⇒ Object
-
#with_default_output ⇒ Object
logfile.with_default_output { … }.
Constructor Details
#initialize(filename, writemode) ⇒ LogFile
Returns a new instance of LogFile.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/chkbuild/logfile.rb', line 83 def initialize(filename, writemode) @writemode = writemode mode = writemode ? File::RDWR|File::CREAT|File::APPEND : File::RDONLY @filename = filename @io = File.open(filename, mode) @io.set_encoding("ascii-8bit") if @io.respond_to? :set_encoding @io.sync = true @mark = read_separator @sections = detect_sections end |
Class Method Details
.read_open(filename) ⇒ Object
79 80 81 |
# File 'lib/chkbuild/logfile.rb', line 79 def self.read_open(filename) self.new(filename, false) end |
.write_open(filename, build) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/chkbuild/logfile.rb', line 33 def self.write_open(filename, build) logfile = self.new(filename, true) logfile.start_section build.depsuffixed_name logfile.with_default_output { system("uname -a") section_started = false build.traverse_depbuild {|depbuild| if !section_started logfile.start_section 'dependencies' section_started = true end if depbuild.suffixed_name == depbuild.version puts "#{depbuild.suffixed_name} #{depbuild.start_time}" else puts "#{depbuild.suffixed_name} #{depbuild.start_time} (#{depbuild.version})" end } } logfile end |
Instance Method Details
#change_default_output ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/chkbuild/logfile.rb', line 134 def change_default_output raise "not opened for writing" if !@writemode STDOUT.reopen(@save_io = File.for_fd(@io.fileno, File::WRONLY|File::APPEND)) STDERR.reopen(STDOUT) STDOUT.sync = true STDERR.sync = true end |
#dependencies ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/chkbuild/logfile.rb', line 54 def dependencies return [] unless log = self.get_section('dependencies') r = [] log.each_line {|line| if /^(\S+) (\d+T\d+) \((.*)\)$/ =~ line r << [$1, $2, $3] elsif /^(\S+) (\d+T\d+)$/ =~ line r << [$1, $2, $1] end } r end |
#depsuffixed_name ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/chkbuild/logfile.rb', line 67 def depsuffixed_name return @depsuffixed_name if defined? @depsuffixed_name if /\A\S+\s+(\S+)/ =~ self.get_all_log return @depsuffixed_name = $1 end raise "unexpected log format" end |
#each_secname(&block) ⇒ Object
169 170 171 |
# File 'lib/chkbuild/logfile.rb', line 169 def each_secname(&block) @sections.keys.sort_by {|secname| @sections[secname] }.each(&block) end |
#get_all_log ⇒ Object
188 189 190 191 |
# File 'lib/chkbuild/logfile.rb', line 188 def get_all_log @io.rewind @io.read end |
#get_section(secname) ⇒ Object
181 182 183 184 185 186 |
# File 'lib/chkbuild/logfile.rb', line 181 def get_section(secname) spos = @sections[secname] return nil if !spos @io.seek spos @io.gets("\n#{@mark} ").chomp("#{@mark} ").sub(/\A.*\n/, '') end |
#modify_section(secname, data) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/chkbuild/logfile.rb', line 193 def modify_section(secname, data) raise "not opened for writing" if !@writemode spos = @sections[secname] raise ArgumentError, "no section: #{secname.inspect}" if !spos data += "\n" if /\n\z/ !~ data old = nil File.open(@filename, File::RDWR) {|f| f.seek spos rest = f.read if /\n#{Regexp.quote @mark} / =~ rest epos = $~.begin(0) + 1 curr = rest[0...epos] rest = rest[epos..-1] else curr = rest rest = '' end if /\n/ =~ curr secline = $` + $& old = $' else secline = curr + "\n" old = '' end f.seek spos f.print secline, data, rest f.flush f.truncate(f.pos) } off = data.length - old.length @sections.each_pair {|n, pos| if spos < pos @sections[n] = pos + off end } nil end |
#secnames ⇒ Object
165 166 167 |
# File 'lib/chkbuild/logfile.rb', line 165 def secnames @sections.keys.sort_by {|secname| @sections[secname] } end |
#section_size(secname) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/chkbuild/logfile.rb', line 173 def section_size(secname) spos = @sections[secname] raise ArgumentError, "no section : #{secname.inspect}" if !spos epos = @sections.values.reject {|pos| pos <= spos }.min epos = @io.stat.size if !epos epos - spos end |
#start_section(secname) ⇒ Object
start_section returns the (unique) section name.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/chkbuild/logfile.rb', line 143 def start_section(secname) @io.flush if 0 < @io.stat.size @io.seek(-1, IO::SEEK_END) if @io.read != "\n" @io.write "\n" end end spos = @io.pos secname = secname.strip if @sections[secname] i = 2 while @sections["#{secname}(#{i})"] i += 1 end secname = "#{secname}(#{i})" end @sections[secname] = spos @io.write "#{@mark} #{secname} \# #{Time.now.iso8601}\n" secname end |
#suffixed_name ⇒ Object
75 |
# File 'lib/chkbuild/logfile.rb', line 75 def suffixed_name() depsuffixed_name.sub(/_.*/, '') end |
#suffixes ⇒ Object
77 |
# File 'lib/chkbuild/logfile.rb', line 77 def suffixes() suffixed_name.split(/-/)[1..-1] end |
#target_name ⇒ Object
76 |
# File 'lib/chkbuild/logfile.rb', line 76 def target_name() suffixed_name.sub(/-.*/, '') end |
#with_default_output ⇒ Object
logfile.with_default_output { … }
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/chkbuild/logfile.rb', line 121 def with_default_output raise "not opened for writing" if !@writemode File.open(@filename, File::WRONLY|File::APPEND) {|f| STDERR.tmp_reopen(f) { STDERR.sync = true STDOUT.tmp_reopen(f) { STDOUT.sync = true yield } } } end |