Module: MJ::Tools::SubProcess::ClassMethods
- Defined in:
- lib/mj/tools/subprocess.rb
Instance Method Summary collapse
-
#adjust_environment(wd = nil, env = nil, lang = "C") ⇒ Object
Helper method to adjust LANG to āCā.
-
#execute(command, wd = ENV["HOME"], env = nil) ⇒ Object
Executes command.
Instance Method Details
#adjust_environment(wd = nil, env = nil, lang = "C") ⇒ Object
Helper method to adjust LANG to āCā
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 |
# File 'lib/mj/tools/subprocess.rb', line 49 def adjust_environment( wd=nil, env=nil, lang="C" ) if wd and !$noop cwd = Dir.getwd Dir.chdir(wd) end # Set the environment the user wants oldenv = Hash.new if env env.each do |var, value| oldenv[var] = ENV[var] if value.nil? or value == "" next if ENV.has_key?( value ) ENV[var] = nil logger.verbose "Removing #{var} from environment" else logger.verbose "#{var} = #{value}" ENV[var] = value end end end # Save old LANG setting and switch to 'C' oldlang = ENV['LANG'] ENV['LANG'] = lang yield # Reset the old LANG setting ENV['LANG'] = oldlang # Reset our changes to ENV oldenv.each do |var, value| ENV[var] = value end # Reset the current working directory if wd and !$noop Dir.chdir(cwd) end end |
#execute(command, wd = ENV["HOME"], env = nil) ⇒ Object
Executes command
Executes the given command and yields each line of output. Returns $? .
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 |
# File 'lib/mj/tools/subprocess.rb', line 13 def execute( command, wd = ENV["HOME"], env = nil ) cwd ||= Dir.getwd logger.trace "(#{wd}) > #{command} 2>&1" if !$noop adjust_environment( wd, env ) { IO.popen( "#{command} 2>&1" ) { |f| begin while line = f.readline if block_given? yield line.chomp else logger.verbose line.chomp end end rescue EOFError # Expected. Do nothing end } if $?.coredump? or $?.signaled? raise CoreDumpError, "Command '#{command}' core dumped because of signal #{$?.termsig}!" end logger.trace "= #{$?.exitstatus}" return $?.exitstatus } else adjust_environment( wd, env ) { logger.trace "= 0 # noop" } return 0 end end |