brew/Library/Homebrew/cmd/gist-logs.rb

119 lines
3.5 KiB
Ruby
Raw Normal View History

2020-11-25 17:03:23 +01:00
# typed: true
# frozen_string_literal: true
require "formula"
require "install"
require "system_config"
require "stringio"
require "socket"
2019-04-17 18:25:08 +09:00
require "cli/parser"
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
extend Install
2016-09-26 01:44:51 +02:00
module_function
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
def gist_logs_args
Homebrew::CLI::Parser.new do
description <<~EOS
Upload logs for a failed build of <formula> to a new Gist. Presents an
error message if no logs are found.
EOS
switch "--with-hostname",
2019-04-30 08:44:35 +01:00
description: "Include the hostname in the Gist."
switch "-n", "--new-issue",
description: "Automatically create a new issue in the appropriate GitHub repository "\
"after creating the Gist."
switch "-p", "--private",
2019-04-30 08:44:35 +01:00
description: "The Gist will be marked private and will not appear in listings but will "\
"be accessible with its link."
2020-07-30 18:40:10 +02:00
2021-01-10 14:26:40 -05:00
named_args :formula, number: 1
end
end
def gistify_logs(f, args:)
2015-04-25 22:07:06 -04:00
files = load_logs(f.logs)
build_time = f.logs.ctime
timestamp = build_time.strftime("%Y-%m-%d_%H-%M-%S")
s = StringIO.new
SystemConfig.dump_verbose_config s
# Dummy summary file, asciibetically first, to control display title of gist
files["# #{f.name} - #{timestamp}.txt"] = { content: brief_build_info(f, with_hostname: args.with_hostname?) }
files["00.config.out"] = { content: s.string }
files["00.doctor.out"] = { content: Utils.popen_read("#{HOMEBREW_PREFIX}/bin/brew", "doctor", err: :out) }
unless f.core_formula?
2017-10-15 02:28:32 +02:00
tap = <<~EOS
Formula: #{f.name}
2019-04-01 16:02:13 -04:00
Tap: #{f.tap}
Path: #{f.path}
EOS
files["00.tap.out"] = { content: tap }
end
odie "`brew gist-logs` requires HOMEBREW_GITHUB_API_TOKEN to be set!" if GitHub::API.credentials_type == :none
# Description formatted to work well as page title when viewing gist
2020-03-13 21:15:06 +00:00
descr = if f.core_formula?
"#{f.name} on #{OS_VERSION} - Homebrew build logs"
else
2020-03-13 21:15:06 +00:00
"#{f.name} (#{f.full_name}) on #{OS_VERSION} - Homebrew build logs"
end
2021-02-15 21:48:21 +05:30
url = GitHub.create_gist(files, descr, private: args.private?)
2021-02-15 21:48:21 +05:30
url = GitHub.create_issue(f.tap, "#{f.name} failed to build on #{MacOS.full_version}", url) if args.new_issue?
puts url if url
end
def brief_build_info(f, with_hostname:)
build_time_str = f.logs.ctime.strftime("%Y-%m-%d %H:%M:%S")
2019-04-28 15:22:47 +02:00
s = +<<~EOS
Homebrew build logs for #{f.full_name} on #{OS_VERSION}
EOS
if with_hostname
hostname = Socket.gethostname
s << "Host: #{hostname}\n"
end
s << "Build date: #{build_time_str}\n"
s.freeze
end
# Causes some terminals to display secure password entry indicators.
def noecho_gets
2021-03-17 15:34:20 +00:00
system "stty", "-echo"
result = $stdin.gets
2021-03-17 15:34:20 +00:00
system "stty", "echo"
puts
result
end
2015-04-25 22:07:06 -04:00
def load_logs(dir)
logs = {}
if dir.exist?
dir.children.sort.each do |file|
contents = file.size? ? file.read : "empty log"
# small enough to avoid GitHub "unicorn" page-load-timeout errors
max_file_size = 1_000_000
contents = truncate_text_to_approximate_size(contents, max_file_size, front_weight: 0.2)
logs[file.basename.to_s] = { content: contents }
end
end
odie "No logs." if logs.empty?
2018-09-17 02:45:00 +02:00
logs
end
def gist_logs
2020-07-30 18:40:10 +02:00
args = gist_logs_args.parse
2020-07-27 11:37:07 +02:00
Install.perform_preinstall_checks(all_fatal: true)
Install.perform_build_from_source_checks(all_fatal: true)
gistify_logs(args.named.to_resolved_formulae.first, args: args)
end
end