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
|
2018-05-30 09:01:35 -04:00
|
|
|
unzipped.force_encoding Encoding::BINARY
|
2013-11-18 19:34:13 -05:00
|
|
|
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')
|
2018-05-30 09:01:35 -04:00
|
|
|
zipped.set_encoding Encoding::BINARY
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
Zlib::GzipWriter.wrap zipped do |io|
|
|
|
|
io.write data
|
|
|
|
end
|
2013-11-18 19:34:13 -05:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
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
|
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# Invokes system, but silences all output.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.silent_system(*command)
|
2018-05-30 09:01:35 -04:00
|
|
|
opt = {:out => IO::NULL, :err => [:child, :out]}
|
2014-07-25 03:55:55 -04:00
|
|
|
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
|
|
|
@silent_mutex ||= Mutex.new
|
|
|
|
|
|
|
|
@silent_mutex.synchronize do
|
|
|
|
begin
|
|
|
|
stdout = STDOUT.dup
|
|
|
|
stderr = STDERR.dup
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
STDOUT.reopen IO::NULL, 'w'
|
|
|
|
STDERR.reopen IO::NULL, 'w'
|
2013-11-25 14:14:49 -05:00
|
|
|
|
|
|
|
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+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.traverse_parents(directory, &block)
|
2013-11-25 14:14:49 -05:00
|
|
|
return enum_for __method__, directory unless block_given?
|
|
|
|
|
|
|
|
here = File.expand_path directory
|
2017-10-07 21:32:18 -04:00
|
|
|
loop do
|
2018-02-05 21:58:35 -05:00
|
|
|
Dir.chdir here, &block rescue Errno::EACCES
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
new_here = File.expand_path('..', here)
|
|
|
|
return if new_here == here # toplevel
|
|
|
|
here = new_here
|
2013-11-25 14:14:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
##
|
|
|
|
# Globs for files matching +pattern+ inside of +directory+,
|
|
|
|
# returning absolute paths to the matching files.
|
|
|
|
|
|
|
|
def self.glob_files_in_dir(glob, base_path)
|
|
|
|
if RUBY_VERSION >= "2.5"
|
2018-12-22 19:20:49 -05:00
|
|
|
Dir.glob(glob, base: base_path).map! {|f| File.expand_path(f, base_path) }
|
2018-08-27 06:05:04 -04:00
|
|
|
else
|
|
|
|
Dir.glob(File.expand_path(glob, base_path))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|