Class: Optimus::Runners::GenericRunner

Inherits:
Object
  • Object
show all
Includes:
Transformers
Defined in:
lib/runners/generic_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extractor_class, *args) ⇒ GenericRunner

Returns a new instance of GenericRunner.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/runners/generic_runner.rb', line 25

def initialize(extractor_class, *args)
  @extractor_class = extractor_class
  # caller() returns an array of 'filename:line' -- the last element
  # should contain the name of the script that started this process
  @script_name = File.basename(caller.last.split(':').first)
  @out = STDOUT
  @err = STDERR
  @args = args
  @data = nil
  @timing_extractor = nil
end

Instance Attribute Details

#errObject

Returns the value of attribute err.



24
25
26
# File 'lib/runners/generic_runner.rb', line 24

def err
  @err
end

#outObject

Returns the value of attribute out.



24
25
26
# File 'lib/runners/generic_runner.rb', line 24

def out
  @out
end

Instance Method Details

#extract_timingsObject



55
56
57
58
59
60
61
62
# File 'lib/runners/generic_runner.rb', line 55

def extract_timings
  @timing_extractor = @extractor_class.new(@data)
  template_code = ''
  File.open(@options.template_file) { |f| 
    template_code = f.read 
  }
  @timing_extractor.instance_eval(template_code)
end

#process!Object



37
38
39
40
41
42
43
# File 'lib/runners/generic_runner.rb', line 37

def process!
  process_arguments(*@args)
  validate
  read_data
  extract_timings
  write_timings
end

#read_dataObject



45
46
47
48
49
50
51
52
53
# File 'lib/runners/generic_runner.rb', line 45

def read_data
  data = Optimus::Data.new()
  @options.input_files.each do |infile|
    File.open(infile) do |f|
      data.merge!(Optimus::Reader.new(f).optimus_data)
    end
  end
  @data = data
end

#show_help!Object



98
99
100
# File 'lib/runners/generic_runner.rb', line 98

def show_help!
  @err.puts @op.to_s
end

#usageObject



102
103
104
# File 'lib/runners/generic_runner.rb', line 102

def usage
  "#{@op.banner.to_s} \n#{@script_name} --help for help"
end

#validateObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/runners/generic_runner.rb', line 82

def validate
  if @options.help || @args.flatten.size == 0
    show_help! and raise Exception.new()
  end
  if @options.input_files.empty?
    raise ArgumentError.new("no input files given\n#{usage}")
  end
  if !@options.template_file
    raise ArgumentError.new("no template file given\n#{usage}")
  end
  if !File.readable?(@options.template_file)
    raise ArgumentError.new("can't read #{@options.template_file}\n#{usage}")
  end
  return true
end

#write_timingsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/runners/generic_runner.rb', line 64

def write_timings
  if @options.outfile
    @out = File.open(@options.outfile, 'w')
  end
  writer = TabfileWriter.new(
    @timing_extractor.extracted_data, @out, 
    {:column_labels => @options.column_labels})
  begin
    writer.write
  rescue Errno::EPIPE => e
    # This is OK
  ensure
    if @options.outfile
      @out.close
    end
  end
end