-
Notifications
You must be signed in to change notification settings - Fork 3
/
topaz.rb
122 lines (99 loc) · 3.03 KB
/
topaz.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
119
120
121
122
#!/usr/bin/ruby
class TopazError < RuntimeError
attr_accessor :exit_status, :output
def to_s
# Get the child's exit code
puts 'Topaz Error encountered: '
puts @exit_status >> 8
puts @output
puts 'EOE'
end
def initialize(exit_status, output)
@exit_status = exit_status
@output = output
end
end
class String
def execute_on_topaz_stream(topaz_stream)
topaz_stream.puts(self)
end
end
class Array
def execute_on_topaz_stream(topaz_stream)
join("\n").execute_on_topaz_stream(topaz_stream)
end
def line(line_number)
self[line_number]
end
end
class Topaz
attr_accessor :output
def initialize(stone, topaz_command='topaz -l -T 300000')
@stone = stone
@topaz_command = "$GEMSTONE/bin/#{topaz_command} 2>&1"
@output = []
end
def commands(topaz_commands_array, full_logfile)
fail "We expect the stone #{@stone.name} to be running if doing topaz commands. (Is this overly restrictive?)" if [email protected]?
@stone.initialize_gemstone_environment
send_all_commands_to_topaz_and_exit(topaz_commands_array, full_logfile)
if topaz_failed?
raise_error_with_last_log
else
build_up_output_tuple(topaz_commands_array)
return @output
end
end
def topaz_failed?
$?.exitstatus > 0
end
def send_all_commands_to_topaz_and_exit(topaz_commands_array, full_logfile)
IO.popen(@topaz_command, "w+") do |io|
log_everything_to(full_logfile, io)
topaz_commands_array.each_with_index do | command, index |
log_command_separately(index, io)
command.execute_on_topaz_stream(io)
pop_log_output(io)
end
"exit".execute_on_topaz_stream(io)
end
end
def build_up_output_tuple(topaz_commands_array)
0.upto(topaz_commands_array.size) do | index |
@output << [topaz_commands_array[index], read_output_file(log_file_name(index))]
end
end
def raise_error_with_last_log
raise TopazError.new($?, File.read(@most_recent_log_file_name))
end
def log_everything_to(full_logfile, io)
"output append #{full_logfile}".execute_on_topaz_stream(io)
end
def log_file_name(index)
"#{@stone.log_directory}/topaz.#{index}.log"
end
def log_command_separately(index, io)
@most_recent_log_file_name = log_file_name(index)
File.delete @most_recent_log_file_name if File.exist? @most_recent_log_file_name
"output push #{@most_recent_log_file_name}".execute_on_topaz_stream(io)
end
def pop_log_output(io)
'output pop'.execute_on_topaz_stream(io)
end
def read_output_file(name)
return [] unless File.exist? name
lines = File.readlines(name)
fail "expected #{lines.last} to be 'output pop' or 'Logging out' or 'exit' in file named #{name}" unless
match_expected(lines)
lines[0..-2]
end
def match_expected(lines)
!lines.empty? and lines.last.match(/topaz( \d)?> (output pop|exit)|^Logging out session/)
end
def dump_as_script(*topaz_commands)
topaz_commands.each do | command |
command.execute_on_topaz_stream(STDOUT)
end
self
end
end