2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# This module contains various utility methods as module methods.
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
module Gem::Util
|
2013-11-25 14:14:49 -05:00
|
|
|
|
|
|
|
@silent_mutex = nil
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# Zlib::GzipReader wrapper that unzips +data+.
|
|
|
|
|
|
|
|
def self.gunzip(data)
|
|
|
|
require 'zlib'
|
2015-07-01 17:50:14 -04:00
|
|
|
require 'stringio'
|
|
|
|
data = StringIO.new(data, 'r')
|
2013-11-18 19:34:13 -05:00
|
|
|
|
|
|
|
unzipped = Zlib::GzipReader.new(data).read
|
|
|
|
unzipped.force_encoding Encoding::BINARY if Object.const_defined? :Encoding
|
|
|
|
unzipped
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Zlib::GzipWriter wrapper that zips +data+.
|
|
|
|
|
|
|
|
def self.gzip(data)
|
|
|
|
require 'zlib'
|
2015-07-01 17:50:14 -04:00
|
|
|
require 'stringio'
|
2016-02-01 07:43:26 -05:00
|
|
|
zipped = StringIO.new(String.new, 'w')
|
2013-11-18 19:34:13 -05:00
|
|
|
zipped.set_encoding Encoding::BINARY if Object.const_defined? :Encoding
|
|
|
|
|
|
|
|
Zlib::GzipWriter.wrap zipped do |io| io.write data end
|
|
|
|
|
|
|
|
zipped.string
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A Zlib::Inflate#inflate wrapper
|
|
|
|
|
|
|
|
def self.inflate(data)
|
|
|
|
require 'zlib'
|
|
|
|
Zlib::Inflate.inflate data
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# This calls IO.popen where it accepts an array for a +command+ (Ruby 1.9+)
|
|
|
|
# and implements an IO.popen-like behavior where it does not accept an array
|
|
|
|
# for a command.
|
|
|
|
|
|
|
|
def self.popen *command
|
2013-11-21 18:27:30 -05:00
|
|
|
IO.popen command, &:read
|
|
|
|
rescue TypeError # ruby 1.8 only supports string command
|
|
|
|
r, w = IO.pipe
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
pid = fork do
|
|
|
|
STDIN.close
|
|
|
|
STDOUT.reopen w
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
exec(*command)
|
|
|
|
end
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
w.close
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
begin
|
|
|
|
return r.read
|
|
|
|
ensure
|
|
|
|
Process.wait pid
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-25 03:55:55 -04:00
|
|
|
NULL_DEVICE = defined?(IO::NULL) ? IO::NULL : Gem.win_platform? ? 'NUL' : '/dev/null'
|
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# Invokes system, but silences all output.
|
|
|
|
|
|
|
|
def self.silent_system *command
|
2014-07-25 03:55:55 -04:00
|
|
|
opt = {:out => NULL_DEVICE, :err => [:child, :out]}
|
|
|
|
if Hash === command.last
|
|
|
|
opt.update(command.last)
|
|
|
|
cmds = command[0...-1]
|
|
|
|
else
|
|
|
|
cmds = command.dup
|
|
|
|
end
|
|
|
|
return system(*(cmds << opt))
|
2014-08-03 09:33:06 -04:00
|
|
|
rescue TypeError
|
2013-11-25 14:14:49 -05:00
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
@silent_mutex ||= Mutex.new
|
|
|
|
|
2014-07-25 03:55:55 -04:00
|
|
|
null_device = NULL_DEVICE
|
2013-11-25 14:14:49 -05:00
|
|
|
|
|
|
|
@silent_mutex.synchronize do
|
|
|
|
begin
|
|
|
|
stdout = STDOUT.dup
|
|
|
|
stderr = STDERR.dup
|
|
|
|
|
|
|
|
STDOUT.reopen null_device, 'w'
|
|
|
|
STDERR.reopen null_device, 'w'
|
|
|
|
|
|
|
|
return system(*command)
|
|
|
|
ensure
|
|
|
|
STDOUT.reopen stdout
|
|
|
|
STDERR.reopen stderr
|
2014-07-25 03:55:48 -04:00
|
|
|
stdout.close
|
|
|
|
stderr.close
|
2013-11-25 14:14:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Enumerates the parents of +directory+.
|
|
|
|
|
|
|
|
def self.traverse_parents directory
|
|
|
|
return enum_for __method__, directory unless block_given?
|
|
|
|
|
|
|
|
here = File.expand_path directory
|
|
|
|
start = here
|
|
|
|
|
|
|
|
Dir.chdir start
|
|
|
|
|
|
|
|
begin
|
|
|
|
loop do
|
|
|
|
yield here
|
|
|
|
|
|
|
|
Dir.chdir '..'
|
|
|
|
|
|
|
|
return if Dir.pwd == here # toplevel
|
|
|
|
|
|
|
|
here = Dir.pwd
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Dir.chdir start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|