2013-10-08 14:36:16 -04:00
|
|
|
require 'fileutils'
|
2014-02-25 05:57:42 -05:00
|
|
|
require 'open3'
|
2013-10-08 14:36:16 -04:00
|
|
|
|
2013-01-28 10:22:45 -05:00
|
|
|
module Gitlab
|
|
|
|
module Popen
|
2014-02-25 05:58:58 -05:00
|
|
|
extend self
|
|
|
|
|
2014-02-25 05:58:22 -05:00
|
|
|
def popen(cmd, path=nil)
|
2014-02-25 05:57:42 -05:00
|
|
|
unless cmd.is_a?(Array)
|
|
|
|
raise "System commands must be given as an array of strings"
|
|
|
|
end
|
|
|
|
|
2014-02-25 05:58:22 -05:00
|
|
|
path ||= Dir.pwd
|
2013-01-28 10:22:45 -05:00
|
|
|
vars = { "PWD" => path }
|
2013-05-05 10:01:10 -04:00
|
|
|
options = { chdir: path }
|
2013-01-28 10:22:45 -05:00
|
|
|
|
2013-10-08 14:36:16 -04:00
|
|
|
unless File.directory?(path)
|
2013-10-08 14:53:22 -04:00
|
|
|
FileUtils.mkdir_p(path)
|
2013-10-08 14:36:16 -04:00
|
|
|
end
|
|
|
|
|
2013-01-28 10:22:45 -05:00
|
|
|
@cmd_output = ""
|
|
|
|
@cmd_status = 0
|
2014-02-25 05:57:42 -05:00
|
|
|
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
|
2014-12-17 06:18:11 -05:00
|
|
|
# We are not using stdin so we should close it, in case the command we
|
|
|
|
# are running waits for input.
|
|
|
|
stdin.close
|
2013-01-28 10:22:45 -05:00
|
|
|
@cmd_output << stdout.read
|
|
|
|
@cmd_output << stderr.read
|
2014-02-25 05:57:42 -05:00
|
|
|
@cmd_status = wait_thr.value.exitstatus
|
2013-01-28 10:22:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
return @cmd_output, @cmd_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|