Class: Session::AbstractSession
- Inherits:
-
Object
- Object
- Session::AbstractSession
- Defined in:
- lib/session.rb
Overview
class Command
Class Attribute Summary collapse
-
.debug ⇒ Object
Returns the value of attribute debug.
-
.track_history ⇒ Object
Returns the value of attribute track_history.
-
.use_open3 ⇒ Object
Returns the value of attribute use_open3.
-
.use_spawn ⇒ Object
Returns the value of attribute use_spawn.
Instance Attribute Summary collapse
-
#debug ⇒ Object
(also: #debug?)
Returns the value of attribute debug.
-
#errproc ⇒ Object
Returns the value of attribute errproc.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#opts ⇒ Object
readonly
attributes.
-
#outproc ⇒ Object
Returns the value of attribute outproc.
-
#prog ⇒ Object
readonly
Returns the value of attribute prog.
-
#stderr ⇒ Object
(also: #e)
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
(also: #i)
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
(also: #o)
readonly
Returns the value of attribute stdout.
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
-
#track_history ⇒ Object
Returns the value of attribute track_history.
-
#use_open3 ⇒ Object
readonly
Returns the value of attribute use_open3.
-
#use_spawn ⇒ Object
readonly
Returns the value of attribute use_spawn.
Class Method Summary collapse
Instance Method Summary collapse
- #__fork(*a, &b) ⇒ Object
- #__popen3(*cmd) ⇒ Object
-
#clear ⇒ Object
(also: #flush)
abstract methods.
- #close! ⇒ Object (also: #close)
- #execute(command, redirects = {}) ⇒ Object
- #getopt(opt, hash, default = nil) ⇒ Object
- #hasopt(opt, hash) ⇒ Object
-
#initialize(*args) ⇒ AbstractSession
constructor
instance methods.
- #path ⇒ Object
- #path= ⇒ Object
- #ready? ⇒ Boolean
- #send_command(cmd) ⇒ Object
Constructor Details
#initialize(*args) ⇒ AbstractSession
instance methods
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/session.rb', line 139 def initialize(*args) @opts = hashify(*args) @prog = getopt('prog', opts, getopt('program', opts, self.class::default_prog)) raise(ArgumentError, "no program specified") unless @prog @track_history = nil @track_history = Session::track_history unless Session::track_history.nil? @track_history = self.class::track_history unless self.class::track_history.nil? @track_history = getopt('history', opts) if hasopt('history', opts) @track_history = getopt('track_history', opts) if hasopt('track_history', opts) @use_spawn = nil @use_spawn = Session::use_spawn unless Session::use_spawn.nil? @use_spawn = self.class::use_spawn unless self.class::use_spawn.nil? @use_spawn = getopt('use_spawn', opts) if hasopt('use_spawn', opts) if defined? JRUBY_VERSION @use_open3 = true else @use_open3 = nil @use_open3 = Session::use_open3 unless Session::use_open3.nil? @use_open3 = self.class::use_open3 unless self.class::use_open3.nil? @use_open3 = getopt('use_open3', opts) if hasopt('use_open3', opts) end @debug = nil @debug = Session::debug unless Session::debug.nil? @debug = self.class::debug unless self.class::debug.nil? @debug = getopt('debug', opts) if hasopt('debug', opts) @history = nil @history = History::new if @track_history @outproc = nil @errproc = nil @stdin, @stdout, @stderr = if @use_spawn Spawn::spawn @prog elsif @use_open3 Open3::popen3 @prog else __popen3 @prog end @threads = [] clear if block_given? ret = nil begin ret = yield self ensure self.close! end return ret end return self end |
Class Attribute Details
.debug ⇒ Object
Returns the value of attribute debug.
106 107 108 |
# File 'lib/session.rb', line 106 def debug @debug end |
.track_history ⇒ Object
Returns the value of attribute track_history.
103 104 105 |
# File 'lib/session.rb', line 103 def track_history @track_history end |
.use_open3 ⇒ Object
Returns the value of attribute use_open3.
105 106 107 |
# File 'lib/session.rb', line 105 def use_open3 @use_open3 end |
.use_spawn ⇒ Object
Returns the value of attribute use_spawn.
104 105 106 |
# File 'lib/session.rb', line 104 def use_spawn @use_spawn end |
Instance Attribute Details
#debug ⇒ Object Also known as: debug?
Returns the value of attribute debug.
134 135 136 |
# File 'lib/session.rb', line 134 def debug @debug end |
#errproc ⇒ Object
Returns the value of attribute errproc.
131 132 133 |
# File 'lib/session.rb', line 131 def errproc @errproc end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
128 129 130 |
# File 'lib/session.rb', line 128 def history @history end |
#opts ⇒ Object (readonly)
attributes
120 121 122 |
# File 'lib/session.rb', line 120 def opts @opts end |
#outproc ⇒ Object
Returns the value of attribute outproc.
130 131 132 |
# File 'lib/session.rb', line 130 def outproc @outproc end |
#prog ⇒ Object (readonly)
Returns the value of attribute prog.
121 122 123 |
# File 'lib/session.rb', line 121 def prog @prog end |
#stderr ⇒ Object (readonly) Also known as: e
Returns the value of attribute stderr.
126 127 128 |
# File 'lib/session.rb', line 126 def stderr @stderr end |
#stdin ⇒ Object (readonly) Also known as: i
Returns the value of attribute stdin.
122 123 124 |
# File 'lib/session.rb', line 122 def stdin @stdin end |
#stdout ⇒ Object (readonly) Also known as: o
Returns the value of attribute stdout.
124 125 126 |
# File 'lib/session.rb', line 124 def stdout @stdout end |
#threads ⇒ Object (readonly)
Returns the value of attribute threads.
136 137 138 |
# File 'lib/session.rb', line 136 def threads @threads end |
#track_history ⇒ Object
Returns the value of attribute track_history.
129 130 131 |
# File 'lib/session.rb', line 129 def track_history @track_history end |
#use_open3 ⇒ Object (readonly)
Returns the value of attribute use_open3.
133 134 135 |
# File 'lib/session.rb', line 133 def use_open3 @use_open3 end |
#use_spawn ⇒ Object (readonly)
Returns the value of attribute use_spawn.
132 133 134 |
# File 'lib/session.rb', line 132 def use_spawn @use_spawn end |
Class Method Details
.default_prog ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/session.rb', line 91 def default_prog return @default_prog if defined? @default_prog and @default_prog if defined? self::DEFAULT_PROG return @default_prog = self::DEFAULT_PROG else @default_prog = ENV["SESSION_#{ self }_PROG"] end nil end |
.default_prog=(prog) ⇒ Object
100 101 102 |
# File 'lib/session.rb', line 100 def default_prog= prog @default_prog = prog end |
.init ⇒ Object
107 108 109 110 111 112 |
# File 'lib/session.rb', line 107 def init @track_history = nil @use_spawn = nil @use_open3 = nil @debug = nil end |
Instance Method Details
#__fork(*a, &b) ⇒ Object
259 260 261 262 263 264 265 266 267 |
# File 'lib/session.rb', line 259 def __fork(*a, &b) verbose = $VERBOSE begin $VERBOSE = nil Kernel::fork(*a, &b) ensure $VERBOSE = verbose end end |
#__popen3(*cmd) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/session.rb', line 220 def __popen3(*cmd) pw = IO::pipe # pipe[0] for read, pipe[1] for write pr = IO::pipe pe = IO::pipe pid = __fork{ # child pw[1].close STDIN.reopen(pw[0]) pw[0].close pr[0].close STDOUT.reopen(pr[1]) pr[1].close pe[0].close STDERR.reopen(pe[1]) pe[1].close exec(*cmd) } Process::detach pid # avoid zombies pw[0].close pr[1].close pe[1].close pi = [pw[1], pr[0], pe[0]] pw[1].sync = true if defined? yield begin return yield(*pi) ensure pi.each{|p| p.close unless p.closed?} end end pi end |
#clear ⇒ Object Also known as: flush
abstract methods
270 271 272 |
# File 'lib/session.rb', line 270 def clear raise NotImplementedError end |
#close! ⇒ Object Also known as: close
294 295 296 297 298 |
# File 'lib/session.rb', line 294 def close! [stdin, stdout, stderr].each{|pipe| pipe.close} stdin, stdout, stderr = nil, nil, nil true end |
#execute(command, redirects = {}) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/session.rb', line 304 def execute(command, redirects = {}) $session_command = command if @debug raise(PipeError, command) unless ready? # clear buffers clear # setup redirects rerr = redirects[:e] || redirects[:err] || redirects[:stderr] || redirects['stderr'] || redirects['e'] || redirects['err'] || redirects[2] || redirects['2'] rout = redirects[:o] || redirects[:out] || redirects[:stdout] || redirects['stdout'] || redirects['o'] || redirects['out'] || redirects[1] || redirects['1'] # create cmd object and add to history cmd = Command::new command.to_s # store cmd if tracking history history << cmd if track_history # mutex for accessing shared data mutex = Mutex::new # io data for stderr and stdout err = { :io => stderr, :cmd => cmd.err, :name => 'stderr', :begin => false, :end => false, :begin_pat => cmd.begin_err_pat, :end_pat => cmd.end_err_pat, :redirect => rerr, :proc => errproc, :yield => lambda{|buf| yield(nil, buf)}, :mutex => mutex, } out = { :io => stdout, :cmd => cmd.out, :name => 'stdout', :begin => false, :end => false, :begin_pat => cmd.begin_out_pat, :end_pat => cmd.end_out_pat, :redirect => rout, :proc => outproc, :yield => lambda{|buf| yield(buf, nil)}, :mutex => mutex, } begin # send command in the background so we can begin processing output # immediately - thanks to tanaka akira for this suggestion threads << Thread::new { send_command cmd } # init main = Thread::current exceptions = [] # fire off reader threads [err, out].each do |iodat| threads << Thread::new(iodat, main) do |iodat, main| loop do main.raise(PipeError, command) unless ready? main.raise ExecutionError, iodat[:name] if iodat[:end] and not iodat[:begin] break if iodat[:end] or iodat[:io].eof? line = iodat[:io].gets # In case their are weird chars, this will avoid a "invalid byte sequence in US-ASCII" error line.force_encoding("binary") if line.respond_to? :force_encoding buf = nil case line when iodat[:end_pat] iodat[:end] = true # handle the special case of non-newline terminated output if((m = %r/(.+)__CMD/o.match(line)) and (pre = m[1])) buf = pre end when iodat[:begin_pat] iodat[:begin] = true else next unless iodat[:begin] and not iodat[:end] # ignore chaff buf = line end if buf iodat[:mutex].synchronize do iodat[:cmd] << buf iodat[:redirect] << buf if iodat[:redirect] iodat[:proc].call buf if iodat[:proc] iodat[:yield].call buf if block_given? end end end true end end ensure # reap all threads - accumulating and rethrowing any exceptions begin while((t = threads.shift)) t.join raise ExecutionError, 'iodat thread failure' unless t.value end rescue => e exceptions << e retry unless threads.empty? ensure unless exceptions.empty? = '<' << exceptions.map{|e| "#{ e. } - (#{ e.class })"}.join('|') << '>' = exceptions.map{|e| e.backtrace}.flatten raise ExecutionError, , end end end # this should only happen if eof was reached before end pat [err, out].each do |iodat| raise ExecutionError, iodat[:name] unless iodat[:begin] and iodat[:end] end # get the exit status get_status if respond_to? :get_status out = err = iodat = nil return [cmd.out, cmd.err] end |
#getopt(opt, hash, default = nil) ⇒ Object
202 203 204 205 206 207 208 209 210 |
# File 'lib/session.rb', line 202 def getopt opt, hash, default = nil key = opt return hash[key] if hash.has_key? key key = "#{ key }" return hash[key] if hash.has_key? key key = key.intern return hash[key] if hash.has_key? key return default end |
#hasopt(opt, hash) ⇒ Object
211 212 213 214 215 216 217 218 219 |
# File 'lib/session.rb', line 211 def hasopt opt, hash key = opt return key if hash.has_key? key key = "#{ key }" return key if hash.has_key? key key = key.intern return key if hash.has_key? key return false end |
#path ⇒ Object
274 275 276 |
# File 'lib/session.rb', line 274 def path raise NotImplementedError end |
#path= ⇒ Object
277 278 279 |
# File 'lib/session.rb', line 277 def path= raise NotImplementedError end |
#ready? ⇒ Boolean
289 290 291 292 293 |
# File 'lib/session.rb', line 289 def ready? (stdin and stdout and stderr) and (IO === stdin and IO === stdout and IO === stderr) and (not (stdin.closed? or stdout.closed? or stderr.closed?)) end |
#send_command(cmd) ⇒ Object
280 281 282 |
# File 'lib/session.rb', line 280 def send_command cmd raise NotImplementedError end |