Class: R::Command

Inherits:
Object show all
Defined in:
lib/rub/r/command.rb

Overview

A high level interface for executing commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Command

Create a new command to run.

Parameters:

  • cmd (Array<String,#to_s>)

    The command that will be run.



74
75
76
77
78
79
80
81
# File 'lib/rub/r/command.rb', line 74

def initialize(cmd)
	@env = {}
	
	@clearenv = false
	@mergeouts = false
	
	@cmd = cmd.map{|a| a.to_s}
end

Instance Attribute Details

#clearenvtrue, false

If set, the executed command will not inherit environment variables from Rub. Only the values in #env will be present in the environment.

Defaults to false.

Returns:

  • (true, false)

    If true don’t inherit the environment.



66
67
68
# File 'lib/rub/r/command.rb', line 66

def clearenv
  @clearenv
end

#cmdObject (readonly)

Returns the value of attribute cmd.



29
30
31
# File 'lib/rub/r/command.rb', line 29

def cmd
  @cmd
end

#envObject (readonly)

Returns the value of attribute env.



40
41
42
# File 'lib/rub/r/command.rb', line 40

def env
  @env
end

#mergeoutstrue, false

Returns If true merge #stdout into #stderr.

Returns:



69
70
71
# File 'lib/rub/r/command.rb', line 69

def mergeouts
  @mergeouts
end

#statusObject (readonly)

Returns the value of attribute status.



56
57
58
# File 'lib/rub/r/command.rb', line 56

def status
  @status
end

#stderrObject (readonly)

Returns the value of attribute stderr.



49
# File 'lib/rub/r/command.rb', line 49

attr_reader   :stdout, :stderr

#stdinString

Returns The string to use as input to the command.

Returns:

  • (String)

    The string to use as input to the command.



44
45
46
# File 'lib/rub/r/command.rb', line 44

def stdin
  @stdin
end

#stdoutString (readonly)

Returns The output produced by the command.

Returns:

  • (String)

    The output produced by the command.



49
50
51
# File 'lib/rub/r/command.rb', line 49

def stdout
  @stdout
end

Instance Method Details

#blocktrue, false

Wait for a command to finish.

Block until a currently running process finishes. Behaviour is undefined if the command is not currently running (#start has been called since the last #block).

After this call returns #status will be available.

Returns:

  • (true, false)

    Whether the command completed successfully.



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rub/r/command.rb', line 144

def block
	@stdout = @stdoutr.read
	@stderr = @stderrr.read
	
	#puts "Blocking on #{@pid} #{@cmd.join' '}"
	pid, @status = Process.wait2 @pid
	#puts "Done #{@cmd.join' '}"
	
	@stdoutr.close
	@stderrr.close
	
	success?
end

#runtrue, false

Run the command and block until completion.

Equivalent to calling #start then #block.

Returns:

  • (true, false)

    Whether the command completed successfully.



130
131
132
133
# File 'lib/rub/r/command.rb', line 130

def run
	start
	block
end

#startObject

Note:

Calling this command a second time before #block returns produces undefined behaviour. A R::Command can be run multiple times but it must finish before being run again.

Start the command.

Executes the command. The command will run in the background. If you want to wait for the command to complete call #block.



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
123
# File 'lib/rub/r/command.rb', line 91

def start
	@status = nil

	@stdinr,  @stdinw  = IO.pipe
	@stdoutr, @stdoutw = IO.pipe
	@stderrr, @stderrw = IO.pipe
	
	@stdout = ""
	@stderr = ""
	@status = nil
	
	args = [
		@env,
		*@cmd.map{|a| a.to_s},
		:unsetenv_others=>@clearenv,
		:in =>@stdinr,
		:out=>@stdoutw,
		:err=>(@mergeouts?@stdoutw:@stderrw),
	]
	
	#p "args: #{args}"
	@pid = spawn(*args)
	
	@stdinr.close
	
	@stdinw.write @stdin
	
	@stdinw.close
	@stdoutw.close
	@stderrw.close
	
	@pid
end

#success?true, false

Note:

If the command has not been executed or has not completed this returns false.

Check if the command was successful

Returns:

  • (true, false)

    true if the command executed successfully otherwise false.



164
165
166
# File 'lib/rub/r/command.rb', line 164

def success?
	!!( @status and @status.exitstatus == 0 )
end