2014-09-02 18:07:02 +05:30
|
|
|
require 'fileutils'
|
|
|
|
require 'open3'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Popen
|
|
|
|
extend self
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def popen(cmd, path = nil)
|
2014-09-02 18:07:02 +05:30
|
|
|
unless cmd.is_a?(Array)
|
|
|
|
raise "System commands must be given as an array of strings"
|
|
|
|
end
|
|
|
|
|
|
|
|
path ||= Dir.pwd
|
|
|
|
vars = { "PWD" => path }
|
|
|
|
options = { chdir: path }
|
|
|
|
|
|
|
|
unless File.directory?(path)
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
cmd_output = ""
|
|
|
|
cmd_status = 0
|
2014-09-02 18:07:02 +05:30
|
|
|
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
|
2016-09-29 09:46:39 +05:30
|
|
|
yield(stdin) if block_given?
|
2015-04-26 12:48:37 +05:30
|
|
|
stdin.close
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
cmd_output << stdout.read
|
|
|
|
cmd_output << stderr.read
|
|
|
|
cmd_status = wait_thr.value.exitstatus
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
[cmd_output, cmd_status]
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|