Created
September 13, 2011 22:21
-
-
Save webcoyote/1215342 to your computer and use it in GitHub Desktop.
Log file iterator pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pathname' | |
require 'date' | |
class WalkDir | |
def initialize (opts = {}) | |
# Set default handlers | |
@file_pattern = opts[:file_pattern] || /^[^.]/ # no hidden files | |
@dir_pattern = opts[:dir_pattern] || /^[^.]/ # no hidden directories | |
@result = opts[:result] || lambda { |pathname, match| return pathname, match } | |
# Validate params | |
@file_pattern.respond_to?('match') || raise('invalid file_pattern') | |
@dir_pattern.respond_to?('match') || raise('invalid dir_pattern') | |
@result.respond_to?('call') || raise('invalid result') | |
end | |
def each (filenames) | |
filenames.each do |filename| | |
Pathname.new(filename).find() do |pathname| | |
if pathname.file? then | |
match = @file_pattern.match(pathname.basename.to_s) | |
next if not match | |
result = @result.call(pathname, match) | |
next if not result | |
yield result | |
else | |
Find.prune if not @dir_pattern.match(pathname.basename.to_s) | |
end | |
end | |
end | |
end | |
end | |
def date_expired? (date, daysOld) | |
time = Date.strptime(date,"%Y%m%d").to_time | |
delta = (Time.now - time) / (24 * 60 * 60) | |
delta > daysOld | |
end | |
def recent_filename_selector (daysOld) | |
filter = lambda do |pathname, match| | |
date = match[1] | |
return nil if date_expired?(date, daysOld) | |
return pathname, date | |
end | |
WalkDir.new( | |
:file_pattern => /^error.log-([0-9]+)(\.gz){0,1}$/, | |
:result => filter | |
) | |
end | |
def recent_filename_opener (daysOld) | |
filter = lambda do |pathname, match| | |
date, zipped = match[1..2] | |
return nil if date_expired?(date, daysOld) | |
file = zipped ? IO.popen("gunzip -c -d #{pathname.to_s}") : File.open(pathname.to_s) | |
return pathname, date, file | |
end | |
WalkDir.new( | |
:file_pattern => /^error.log-([0-9]+)(\.gz){0,1}$/, | |
:result => filter | |
) | |
end | |
# Main | |
if $0 == __FILE__ | |
puts "* Test 0: defaults" | |
WalkDir.new().each(ARGV) do |pathname, match| | |
puts " #{pathname.to_s}" | |
end | |
puts "* Test 1: regular expression" | |
file_pattern = /error.log-([0-9]+)(\.gz){0,1}$/ | |
WalkDir.new(:file_pattern => file_pattern).each(ARGV) do |pathname, match| | |
puts " #{pathname.to_s} -> #{match[1]}" | |
end | |
puts "* Test 2: selector class for file names" | |
recent_filename_selector(14).each(ARGV) do |pathname, date| | |
puts " #{pathname.to_s} -> #{date}" | |
end | |
puts "* Test 3: selector class for file data" | |
recent_filename_opener(14).each(ARGV) do |pathname, date, file| | |
puts " #{pathname.to_s} -> #{date}" | |
file.each_line { |line| puts " #{line}" } | |
file.close | |
end | |
end | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment