Class: Session::AbstractSession
- Inherits:
-
Object
- Object
- Session::AbstractSession
- Defined in:
- lib/session.rb,
lib/session-2.4.0.rb
Overview
class Command
Class Attribute Summary collapse
-
.debug ⇒ Object
Returns the value of attribute debug.
-
.track_history ⇒ Object
–}}}.
-
.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
–{{{.
-
#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
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/session.rb', line 164 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) @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) @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.
126 127 128 |
# File 'lib/session.rb', line 126 def debug @debug end |
.track_history ⇒ Object
–}}}
123 124 125 |
# File 'lib/session.rb', line 123 def track_history @track_history end |
.use_open3 ⇒ Object
Returns the value of attribute use_open3.
125 126 127 |
# File 'lib/session.rb', line 125 def use_open3 @use_open3 end |
.use_spawn ⇒ Object
Returns the value of attribute use_spawn.
124 125 126 |
# File 'lib/session.rb', line 124 def use_spawn @use_spawn end |
Instance Attribute Details
#debug ⇒ Object Also known as: debug?
Returns the value of attribute debug.
158 159 160 |
# File 'lib/session.rb', line 158 def debug @debug end |
#errproc ⇒ Object
Returns the value of attribute errproc.
155 156 157 |
# File 'lib/session.rb', line 155 def errproc @errproc end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
152 153 154 |
# File 'lib/session.rb', line 152 def history @history end |
#opts ⇒ Object (readonly)
–{{{
144 145 146 |
# File 'lib/session.rb', line 144 def opts @opts end |
#outproc ⇒ Object
Returns the value of attribute outproc.
154 155 156 |
# File 'lib/session.rb', line 154 def outproc @outproc end |
#prog ⇒ Object (readonly)
Returns the value of attribute prog.
145 146 147 |
# File 'lib/session.rb', line 145 def prog @prog end |
#stderr ⇒ Object (readonly) Also known as: e
Returns the value of attribute stderr.
150 151 152 |
# File 'lib/session.rb', line 150 def stderr @stderr end |
#stdin ⇒ Object (readonly) Also known as: i
Returns the value of attribute stdin.
146 147 148 |
# File 'lib/session.rb', line 146 def stdin @stdin end |
#stdout ⇒ Object (readonly) Also known as: o
Returns the value of attribute stdout.
148 149 150 |
# File 'lib/session.rb', line 148 def stdout @stdout end |
#threads ⇒ Object (readonly)
Returns the value of attribute threads.
160 161 162 |
# File 'lib/session.rb', line 160 def threads @threads end |
#track_history ⇒ Object
Returns the value of attribute track_history.
153 154 155 |
# File 'lib/session.rb', line 153 def track_history @track_history end |
#use_open3 ⇒ Object (readonly)
Returns the value of attribute use_open3.
157 158 159 |
# File 'lib/session.rb', line 157 def use_open3 @use_open3 end |
#use_spawn ⇒ Object (readonly)
Returns the value of attribute use_spawn.
156 157 158 |
# File 'lib/session.rb', line 156 def use_spawn @use_spawn end |
Class Method Details
.default_prog ⇒ Object
–{{{
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/session.rb', line 107 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
–}}}
118 119 120 121 122 |
# File 'lib/session.rb', line 118 def default_prog= prog #--{{{ @default_prog = prog #--}}} end |
.init ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/session.rb', line 127 def init #--{{{ @track_history = nil @use_spawn = nil @use_open3 = nil @debug = nil #--}}} end |
Instance Method Details
#__fork(*a, &b) ⇒ Object
–}}}
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/session.rb', line 288 def __fork(*a, &b) #--{{{ verbose = $VERBOSE begin $VERBOSE = nil Kernel::fork(*a, &b) ensure $VERBOSE = verbose end #--}}} end |
#__popen3(*cmd) ⇒ Object
–}}}
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/session.rb', line 247 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
301 302 303 304 305 |
# File 'lib/session.rb', line 301 def clear #--{{{ raise NotImplementedError #--}}} end |
#close! ⇒ Object Also known as: close
–}}}
337 338 339 340 341 342 343 |
# File 'lib/session.rb', line 337 def close! #--{{{ [stdin, stdout, stderr].each{|pipe| pipe.close} stdin, stdout, stderr = nil, nil, nil true #--}}} end |
#execute(command, redirects = {}) ⇒ Object
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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/session.rb', line 351 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 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.message } - (#{ 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
–}}}
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/session.rb', line 225 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
–}}}
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/session.rb', line 236 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
307 308 309 310 311 |
# File 'lib/session.rb', line 307 def path #--{{{ raise NotImplementedError #--}}} end |
#path= ⇒ Object
–}}}
312 313 314 315 316 |
# File 'lib/session.rb', line 312 def path= #--{{{ raise NotImplementedError #--}}} end |
#ready? ⇒ Boolean
–}}}
330 331 332 333 334 335 336 |
# File 'lib/session.rb', line 330 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
–}}}
317 318 319 320 321 |
# File 'lib/session.rb', line 317 def send_command cmd #--{{{ raise NotImplementedError #--}}} end |