-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.rb
118 lines (105 loc) · 3.27 KB
/
report.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# @BEGIN_LICENSE
#
# Halyard - Multimedia authoring and playback system
# Copyright 1993-2009 Trustees of Dartmouth College
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
# @END_LICENSE
require 'buildscript/child_process'
# A list of commands that have been run, and their output.
class Report
# This exception is raised whenever a command fails.
class CommandFailed < RuntimeError
end
# The path to the report file. May be nil.
attr_reader :path
# Create a new Report. Options include:
#
# +silent+:: Do not write report data to standard output.
# +dir+:: A directory in which test reports should be stored.
def initialize options
@silent = options[:silent]
@text = []
@closed = false
if options[:dir]
@path = "#{options[:dir]}/BuildReport.txt"
@report_file = open(@path, "w")
end
end
# Returns true once #close has been called.
def closed?() @closed end
# Close the report.
def close
assert !@closed
@report_file.close if @report_file
@closed = true
end
# Add a new heading to the report.
def heading str
write ">>>>> #{str}\n"
end
# Write a chunk of data to our report.
def write data
assert !@closed
# We store the text as a list so appending will be cheap.
@text << data
unless @silent
$stdout.print data
$stdout.flush
end
if @report_file
@report_file.print data
@report_file.flush
end
end
private :write
# Covert _command_ and _args_ to a formatted string for display to the
# user.
def Report.format_command_line command, *args
"#{command} #{args.join(' ')}"
end
# Run the specified command, and pass its output to our block (if one
# is provided).
def Report.run_capturing_output command, *args
# TODO - Move into ChildProcess?
ChildProcess.exec({:combine_output => true}, command, *args) do |child|
child.in.close
until child.out.eof?
# Consume output one line at a time.
line = child.out.gets
yield line if block_given?
end
unless child.wait.success?
formatted = format_command_line command, *args
raise CommandFailed, "Error running <#{formatted}>", caller
end
end
end
# Run a shell command, and add its output to our report. Raises
# CommandFailed if the child process returns an error.
def run command, *args
formatted = Report.format_command_line command, *args
write ">>> #{formatted}\n"
Report.run_capturing_output(command, *args) do |data|
write data
end
end
# Get the report's data as text.
def text
@text.join('')
end
end