Class: ShellCommands

Inherits:
Object show all
Extended by:
ClassMethods
Includes:
Assertions, Examples
Defined in:
app/models/shell_command.rb

Direct Known Subclasses

Arp, DHCP

Defined Under Namespace

Modules: Assertions, ClassMethods, Examples

Constant Summary

Constants included from Examples

Examples::COMMAND_STRING, Examples::Cd_command_array, Examples::Cd_command_hash, Examples::EXAMPLE, Examples::Example_output, Examples::Guaranteed_existing_basename, Examples::Guaranteed_existing_directory, Examples::Hello_world, Examples::Redirect_command, Examples::Redirect_command_string, Examples::Relative_command

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

assemble_array_command, assemble_command_string, assemble_hash_command

Methods included from Assertions

#assert_post_conditions, #assert_pre_conditions

Constructor Details

#initialize(*command) ⇒ ShellCommands

prefer command as array since each element is shell escaped. Most common need for shell excape is spaces in pathnames (a common GUI style)


86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/shell_command.rb', line 86

def initialize(*command)
	parse_argument_array(command)
	execute # do it first time, to repeat call execute
	if $VERBOSE.nil? then
	elsif $VERBOSE then
		$stdout.puts trace # -W2
	else 
		$stdout.puts inspect(:echo_command) # -W1
	end #if

end

Instance Attribute Details

#command_stringObject (readonly)

Returns the value of attribute command_string


60
61
62
# File 'app/models/shell_command.rb', line 60

def command_string
  @command_string
end

#errorsObject (readonly)

Returns the value of attribute errors


60
61
62
# File 'app/models/shell_command.rb', line 60

def errors
  @errors
end

#outputObject (readonly)

Returns the value of attribute output


60
61
62
# File 'app/models/shell_command.rb', line 60

def output
  @output
end

#process_statusObject (readonly)

Returns the value of attribute process_status


60
61
62
# File 'app/models/shell_command.rb', line 60

def process_status
  @process_status
end

Instance Method Details

#closeObject

wait


145
146
147
148
149
150
151
152
153
# File 'app/models/shell_command.rb', line 145

def close
	@stdin.close  # stdin, stdout and stderr should be closed explicitly in this form.
	@output=@stdout.read
	@stdout.close
	@errors=@stderr.read
	@stderr.close
	@process_status = @wait_thr.value  # Process::Status object returned.
	self #allows command chaining
end

#executeObject

execute same command again (also called by new.


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/shell_command.rb', line 62

def execute
#	info "@command="[email protected]
#	info "@command_string="[email protected]_string.inspect
	Open3.popen3(@env, @command_string, @opts) {|stdin, stdout, stderr, wait_thr|
		stdin.close  # stdin, stdout and stderr should be closed explicitly in this form.
		@output=stdout.read
		stdout.close
		@errors=stderr.read
		stderr.close
		@process_status = wait_thr.value # Process::Status object returned.
	}
	self #allows command chaining
rescue StandardError => exception
	$stdout.puts "rescue exception "+exception.inspect
#	info "@command="[email protected]
#	info "@command_string="[email protected]_string.inspect
	if @errors.nil? then
		@errors=exception.inspect
	else
		@errors+=exception.inspect
	end #if
end

#fork(cmd) ⇒ Object

parse_argument_array


128
129
130
131
# File 'app/models/shell_command.rb', line 128

def fork(cmd)
	start(cmd)
	self #allows command chaining
end

#inspect(echo_command = @errors!='' || !success?)) ⇒ Object

success


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/shell_command.rb', line 161

def inspect(echo_command=@errors!='' || !success?)
	ret=''
	if echo_command then
		ret+="$ #{@command_string}\n"
		ret+="@env=#{@env.inspect}\n" if $VERBOSE
		ret+="@command=#{@command.inspect}\n" if $VERBOSE
		ret+="@opts=#{@opts.inspect}\n" if $VERBOSE
	end #if
	if @errors!='' then
		ret+="Shellwords.split(@command_string).inspect=#{Shellwords.split(@command_string).inspect}\n"
		ret+="@errors=#{@errors.inspect}\n"
	end #if
	if !success? then
		ret+="@process_status=#{@process_status.inspect}\n"
	end #if
	if @output.nil? then
		ret
	else
		ret+@output.to_s
	end #if
end

#parse_argument_array(argument_array) ⇒ Object

Allow Process.spawn options and environment to be passed.


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
123
124
125
126
127
# File 'app/models/shell_command.rb', line 98

def parse_argument_array(argument_array)
	@argument_array=argument_array
	case argument_array.size
	when 3 then
		@env=argument_array[0]
		@command=argument_array[1]
		@opts=argument_array[2]
	when 2 then
		if argument_array[0].instance_of?(Hash) then
			if argument_array[1].instance_of?(Hash) then
				@env={}
				@command=argument_array[0]
				@opts=argument_array[1]
			else
				@env=argument_array[0]
				@command=argument_array[1]
				@opts={}
			end #if
		else # command is not a Hash
			@env={}
			@command=argument_array[0]
			@opts=argument_array[1]
		end #if
	when 1 then
		@env={}
		@command=argument_array[0]
		@opts={}
	end #case
	@command_string=ShellCommands.assemble_command_string(@command)
end

#putsObject

inspect


182
183
184
185
# File 'app/models/shell_command.rb', line 182

def puts
	$stdout.puts inspect(:echo_command)
	self # return for command chaining
end

#server(cmd) ⇒ Object

fork


132
133
134
135
# File 'app/models/shell_command.rb', line 132

def server(cmd)
	start
	self #allows command chaining
end

#start(cmd) ⇒ Object

server


136
137
138
139
# File 'app/models/shell_command.rb', line 136

def start(cmd)
	@stdin, @stdout, @stderr, @wait_thr = Open3.popen3(*cmd)
	self #allows command chaining
end

#success?Boolean

close

Returns:

  • (Boolean)

154
155
156
157
158
159
160
# File 'app/models/shell_command.rb', line 154

def success?
	if @process_status.nil? then
		false
	else
		@process_status.success?
	end #if
end

#traceObject

puts


186
187
188
189
190
191
# File 'app/models/shell_command.rb', line 186

def trace
	$stdout.puts inspect(:echo_command)
	shorter_callers=caller.grep(/^[^\/]/)
	$stdout.puts shorter_callers.join("\n")
	self # return for command chaining
end

#waitObject

start


140
141
142
143
144
# File 'app/models/shell_command.rb', line 140

def wait
	@process_status = @wait_thr.value # Process::Status object returned.
	close
	self #allows command chaining
end