Module: Utils Private
- Defined in:
- utils/uid.rb,
utils.rb,
utils/ast.rb,
utils/git.rb,
utils/svn.rb,
utils/tar.rb,
utils/curl.rb,
utils/fork.rb,
utils/gzip.rb,
utils/link.rb,
utils/path.rb,
utils/popen.rb,
utils/shell.rb,
utils/timer.rb,
utils/output.rb,
utils/socket.rb,
utils/bottles.rb,
utils/linkage.rb,
utils/service.rb,
utils/shebang.rb,
utils/analytics.rb,
utils/backtrace.rb,
utils/inreplace.rb,
utils/autoremove.rb,
utils/attestation.rb,
utils/git_repository.rb,
utils/topological_hash.rb,
utils/ast.rbi
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Defined Under Namespace
Modules: AST, Analytics, Attestation, Backtrace, Bottles, Curl, Git, Gzip, Inreplace, Link, Output, Path, Service, Shebang, Shell, Svn, Tar, Timer, UID, UNIXSocketExt Classes: TopologicalHash, UNIXServerExt
Class Method Summary collapse
- .binary_linked_to_library?(binary, library) ⇒ Boolean private
-
.convert_to_string_or_symbol(string) ⇒ String, Symbol
private
Converts a string starting with
:to a symbol, otherwise returns the string itself. -
.deconstantize(path) ⇒ String
private
Removes the rightmost segment from the constant expression in the string.
- .deep_compact_blank(obj) ⇒ T.type_parameter(:U)? private
- .deep_stringify_symbols(obj) ⇒ T.untyped private
- .deep_unstringify_symbols(obj) ⇒ T.untyped private
-
.demodulize(path) ⇒ String
private
Removes the module part from the expression in the string.
-
.git_branch(repo = Pathname.pwd, safe: true) ⇒ String?
private
Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.
-
.git_commit_message(repo = Pathname.pwd, commit: "HEAD", safe: true) ⇒ String?
private
Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
-
.git_head(repo = Pathname.pwd, length: nil, safe: true) ⇒ String?
private
Gets the full commit hash of the HEAD commit.
-
.git_short_head(repo = Pathname.pwd, length: nil, safe: true) ⇒ String?
private
Gets a short commit hash of the HEAD commit.
- .parse_author!(author) ⇒ Hash private
-
.pluralize(stem, count, plural: "s", singular: "", include_count: false) ⇒ String
private
A lightweight alternative to
ActiveSupport::Inflector.pluralize: Combinesstemwith thesingularorpluralsuffix based oncount. - .popen(args, mode, options = {}, &_block) ⇒ T.type_parameter(:U), String private
- .popen_read(*args, safe: false, **options, &block) ⇒ T.type_parameter(:U), String private
- .popen_write(*args, safe: false, **options, &_block) ⇒ String private
- .rewrite_child_error(child_error) ⇒ Exception private
- .safe_filename(basename) ⇒ String private
- .safe_filename?(basename) ⇒ Boolean private
-
.safe_fork(directory: nil, yield_parent: false, &_blk) ⇒ void
private
When using this function, remember to call
execas soon as reasonably possible. - .safe_popen_read(*args, **options, &block) ⇒ T.type_parameter(:U), String private
- .safe_popen_write(*args, **options, &block) ⇒ T.type_parameter(:U) private
-
.underscore(camel_cased_word) ⇒ String
private
Makes an underscored, lowercase form from the expression in the string.
Class Method Details
.binary_linked_to_library?(binary, library) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
8 9 10 11 12 13 14 15 16 17 |
# File 'utils/linkage.rb', line 8 def self.binary_linked_to_library?(binary, library) library = library.to_s library = File.realpath(library) if library.start_with?(HOMEBREW_PREFIX.to_s) binary_path = BinaryPathname.wrap(binary) binary_path.dynamically_linked_libraries.any? do |dll| dll = File.realpath(dll) if dll.start_with?(HOMEBREW_PREFIX.to_s) dll == library end end |
.convert_to_string_or_symbol(string) ⇒ String, Symbol
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Converts a string starting with : to a symbol, otherwise returns the
string itself.
convert_to_string_or_symbol(":example") # => :example convert_to_string_or_symbol("example") # => "example"
119 120 121 122 123 |
# File 'utils.rb', line 119 def self.convert_to_string_or_symbol(string) return T.must(string[1..]).to_sym if string.start_with?(":") string end |
.deconstantize(path) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Removes the rightmost segment from the constant expression in the string.
deconstantize('Net::HTTP') # => "Net" deconstantize('::Net::HTTP') # => "::Net" deconstantize('String') # => "" deconstantize('::String') # => "" deconstantize('') # => ""
See also #demodulize.
19 20 21 |
# File 'utils.rb', line 19 def self.deconstantize(path) T.must(path[0, path.rindex("::") || 0]) # implementation based on the one in facets' Module#spacename end |
.deep_compact_blank(obj) ⇒ T.type_parameter(:U)?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'utils.rb', line 173 def self.deep_compact_blank(obj) obj = case obj when Hash obj.transform_values { |v| deep_compact_blank(v) } .compact when Array obj.filter_map { |v| deep_compact_blank(v) } else obj end return if obj.blank? || (obj.is_a?(Numeric) && obj.zero?) obj end |
.deep_stringify_symbols(obj) ⇒ T.untyped
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'utils.rb', line 126 def self.deep_stringify_symbols(obj) case obj when String # Escape leading : or \ to avoid confusion with stringified symbols # ":foo" -> "\:foo" # "\foo" -> "\\foo" if obj.start_with?(":", "\\") "\\#{obj}" else obj end when Symbol ":#{obj}" when Hash obj.to_h { |k, v| [deep_stringify_symbols(k), deep_stringify_symbols(v)] } when Array obj.map { |v| deep_stringify_symbols(v) } else obj end end |
.deep_unstringify_symbols(obj) ⇒ T.untyped
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'utils.rb', line 149 def self.deep_unstringify_symbols(obj) case obj when String if obj.start_with?("\\") obj[1..] elsif obj.start_with?(":") T.must(obj[1..]).to_sym else obj end when Hash obj.to_h { |k, v| [deep_unstringify_symbols(k), deep_unstringify_symbols(v)] } when Array obj.map { |v| deep_unstringify_symbols(v) } else obj end end |
.demodulize(path) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Removes the module part from the expression in the string.
demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections" demodulize('Inflections') # => "Inflections" demodulize('::Inflections') # => "Inflections" demodulize('') # => ""
See also #deconstantize.
35 36 37 38 39 40 41 42 43 |
# File 'utils.rb', line 35 def self.demodulize(path) raise ArgumentError, "No constant path provided" if path.nil? if (i = path.rindex("::")) T.must(path[(i + 2)..]) else path end end |
.git_branch(repo = Pathname.pwd, safe: true) ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.
38 39 40 |
# File 'utils/git_repository.rb', line 38 def self.git_branch(repo = Pathname.pwd, safe: true) GitRepository.new(Pathname(repo)).branch_name(safe:) end |
.git_commit_message(repo = Pathname.pwd, commit: "HEAD", safe: true) ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
50 51 52 |
# File 'utils/git_repository.rb', line 50 def self.(repo = Pathname.pwd, commit: "HEAD", safe: true) GitRepository.new(Pathname(repo)).(commit, safe:) end |
.git_head(repo = Pathname.pwd, length: nil, safe: true) ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the full commit hash of the HEAD commit.
13 14 15 16 17 |
# File 'utils/git_repository.rb', line 13 def self.git_head(repo = Pathname.pwd, length: nil, safe: true) return git_short_head(repo, length:) if length GitRepository.new(Pathname(repo)).head_ref(safe:) end |
.git_short_head(repo = Pathname.pwd, length: nil, safe: true) ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets a short commit hash of the HEAD commit.
27 28 29 |
# File 'utils/git_repository.rb', line 27 def self.git_short_head(repo = Pathname.pwd, length: nil, safe: true) GitRepository.new(Pathname(repo)).short_head_ref(length:, safe:) end |
.parse_author!(author) ⇒ Hash
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
67 68 69 70 71 72 73 74 75 76 |
# File 'utils.rb', line 67 def self.() match_data = /^(?<name>[^<]+?)[ \t]*<(?<email>[^>]+?)>$/.match() if match_data name = match_data[:name] email = match_data[:email] end raise UsageError, "Unable to parse name and email." if name.blank? && email.blank? { name: T.must(name), email: T.must(email) } end |
.pluralize(stem, count, plural: "s", singular: "", include_count: false) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
A lightweight alternative to ActiveSupport::Inflector.pluralize:
Combines stem with the singular or plural suffix based on count.
Adds a prefix of the count value if include_count is set to true.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'utils.rb', line 51 def self.pluralize(stem, count, plural: "s", singular: "", include_count: false) case stem when "formula" plural = "e" when "dependency", "try" stem = stem.delete_suffix("y") plural = "ies" singular = "y" end prefix = include_count ? "#{count} " : "" suffix = (count == 1) ? singular : plural "#{prefix}#{stem}#{suffix}" end |
.popen(args, mode, options = {}, &_block) ⇒ T.type_parameter(:U), String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'utils/popen.rb', line 88 def self.popen(args, mode, = {}, &_block) IO.popen("-", mode) do |pipe| if pipe return pipe.read unless block_given? yield pipe else [:err] ||= File::NULL unless ENV["HOMEBREW_STDERR"] cmd = if args[0].is_a? Hash args[1] else args[0] end begin exec(*args, ) rescue Errno::ENOENT $stderr.puts "brew: command not found: #{cmd}" if [:err] != :close exit! 127 rescue SystemCallError $stderr.puts "brew: exec failed: #{cmd}" if [:err] != :close exit! 1 end end end end |
.popen_read(*args, safe: false, **options, &block) ⇒ T.type_parameter(:U), String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
17 18 19 20 21 22 |
# File 'utils/popen.rb', line 17 def self.popen_read(*args, safe: false, **, &block) output = popen(args, "rb", , &block) return output if !safe || $CHILD_STATUS.success? raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) end |
.popen_write(*args, safe: false, **options, &_block) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'utils/popen.rb', line 44 def self.popen_write(*args, safe: false, **, &_block) output = "" popen(args, "w+b", ) do |pipe| # Before we yield to the block, capture as much output as we can loop do output += pipe.read_nonblock(IO_DEFAULT_BUFFER_SIZE) rescue IO::WaitReadable, EOFError break end yield pipe pipe.close_write pipe.wait_readable # Capture the rest of the output output += pipe.read output.freeze end return output if !safe || $CHILD_STATUS.success? raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) end |
.rewrite_child_error(child_error) ⇒ Exception
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'utils/fork.rb', line 9 def self.rewrite_child_error(child_error) inner_class = Object.const_get(child_error["json_class"]) error = if child_error["cmd"] && inner_class == ErrorDuringExecution ErrorDuringExecution.new(child_error["cmd"], status: child_error["status"], output: child_error["output"]) elsif child_error["cmd"] && inner_class == BuildError # We fill `BuildError#formula` and `BuildError#options` in later, # when we rescue this in `FormulaInstaller#build`. BuildError.new(nil, child_error["cmd"], child_error["args"], child_error["env"]) elsif inner_class == Interrupt Interrupt.new else # Everything other error in the child just becomes a RuntimeError. RuntimeError.new <<~EOS An exception occurred within a child process: #{inner_class}: #{child_error["m"]} EOS end error.set_backtrace child_error["b"] error end |
.safe_filename(basename) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
109 110 111 |
# File 'utils.rb', line 109 def self.safe_filename(basename) basename.gsub(SAFE_FILENAME_REGEX, "") end |
.safe_filename?(basename) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
104 105 106 |
# File 'utils.rb', line 104 def self.safe_filename?(basename) !SAFE_FILENAME_REGEX.match?(basename) end |
.safe_fork(directory: nil, yield_parent: false, &_blk) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
When using this function, remember to call exec as soon as reasonably possible.
This function does not protect against the pitfalls of what you can do pre-exec in a fork.
See man fork for more information.
41 42 43 44 45 46 47 48 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'utils/fork.rb', line 41 def self.safe_fork(directory: nil, yield_parent: false, &_blk) require "json/add/exception" block = proc do |tmpdir| UNIXServerExt.open("#{tmpdir}/socket") do |server| read, write = IO.pipe pid = fork do # bootsnap doesn't like these forked processes ENV["HOMEBREW_NO_BOOTSNAP"] = "1" error_pipe = server.path ENV["HOMEBREW_ERROR_PIPE"] = error_pipe server.close read.close write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid yield(error_pipe) # This could be any type of exception, so rescue them all. rescue Exception => e # rubocop:disable Lint/RescueException error_hash = JSON.parse e.to_json # Special case: We need to recreate ErrorDuringExecutions # for proper error messages and because other code expects # to rescue them further down. if e.is_a?(ErrorDuringExecution) error_hash["cmd"] = e.cmd error_hash["status"] = if e.status.is_a?(Process::Status) { exitstatus: e.status.exitstatus, termsig: e.status.termsig, } else e.status end error_hash["output"] = e.output end write.puts error_hash.to_json write.close exit! else exit!(true) end begin yield(nil) if yield_parent begin socket = server.accept_nonblock rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR retry unless Process.waitpid(pid, Process::WNOHANG) else socket.send_io(write) socket.close end write.close data = read.read read.close Process.waitpid(pid) unless socket.nil? rescue Interrupt Process.waitpid(pid) end # 130 is the exit status for a process interrupted via Ctrl-C. raise Interrupt if $CHILD_STATUS.exitstatus == 130 raise Interrupt if $CHILD_STATUS.termsig == Signal.list["INT"] if data.present? error_hash = JSON.parse(data.lines.fetch(0)) raise rewrite_child_error(error_hash) end raise ChildProcessError, $CHILD_STATUS unless $CHILD_STATUS.success? end end if directory block.call(directory) else Dir.mktmpdir("homebrew-fork", HOMEBREW_TEMP, &block) end end |
.safe_popen_read(*args, **options, &block) ⇒ T.type_parameter(:U), String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
32 33 34 |
# File 'utils/popen.rb', line 32 def self.safe_popen_read(*args, **, &block) popen_read(*args, safe: true, **, &block) end |
.safe_popen_write(*args, **options, &block) ⇒ T.type_parameter(:U)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
75 76 77 |
# File 'utils/popen.rb', line 75 def self.safe_popen_write(*args, **, &block) popen_write(*args, safe: true, **, &block) end |
.underscore(camel_cased_word) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Makes an underscored, lowercase form from the expression in the string.
Changes '::' to '/' to convert namespaces to paths.
underscore('ActiveModel') # => "active_model" underscore('ActiveModel::Errors') # => "active_model/errors"
88 89 90 91 92 93 94 95 96 97 98 |
# File 'utils.rb', line 88 def self.underscore(camel_cased_word) return camel_cased_word.to_s unless /[A-Z-]|::/.match?(camel_cased_word) word = camel_cased_word.to_s.gsub("::", "/") word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) do T.must(::Regexp.last_match(1) || ::Regexp.last_match(2)) << "_" end word.tr!("-", "_") word.downcase! word end |