| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2019-01-23 08:37:28 +00:00
										 |  |  | require "install" | 
					
						
							| 
									
										
										
										
											2016-05-08 20:38:17 +01:00
										 |  |  | require "system_config" | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "stringio" | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  | require "socket" | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  |   def gist_logs_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							|  |  |  |       usage_banner <<~EOS | 
					
						
							| 
									
										
										
										
											2019-01-30 21:32:35 +00:00
										 |  |  |         `gist-logs` [<options>] <formula> | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |         Upload logs for a failed build of <formula> to a new Gist. Presents an | 
					
						
							|  |  |  |         error message if no logs are found. | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  |       EOS | 
					
						
							|  |  |  |       switch "--with-hostname", | 
					
						
							| 
									
										
										
										
											2019-04-30 08:44:35 +01:00
										 |  |  |              description: "Include the hostname in the Gist." | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  |       switch "-n", "--new-issue", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Automatically create a new issue in the appropriate GitHub repository "\ | 
					
						
							|  |  |  |                           "after creating the Gist." | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  |       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 "\ | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |                           "be accessible with its link." | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  |       switch :verbose | 
					
						
							|  |  |  |       switch :debug | 
					
						
							| 
									
										
										
										
											2020-04-14 14:21:53 +01:00
										 |  |  |       named :formula | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def gistify_logs(f) | 
					
						
							| 
									
										
										
										
											2015-04-25 22:07:06 -04:00
										 |  |  |     files = load_logs(f.logs) | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |     build_time = f.logs.ctime | 
					
						
							|  |  |  |     timestamp = build_time.strftime("%Y-%m-%d_%H-%M-%S") | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     s = StringIO.new | 
					
						
							| 
									
										
										
										
											2016-05-08 20:38:17 +01:00
										 |  |  |     SystemConfig.dump_verbose_config s | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |     # Dummy summary file, asciibetically first, to control display title of gist | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |     files["# #{f.name} - #{timestamp}.txt"] = { content: brief_build_info(f) } | 
					
						
							|  |  |  |     files["00.config.out"] = { content: s.string } | 
					
						
							| 
									
										
										
										
											2017-12-14 15:06:10 +00:00
										 |  |  |     files["00.doctor.out"] = { content: Utils.popen_read("#{HOMEBREW_PREFIX}/bin/brew", "doctor", err: :out) } | 
					
						
							| 
									
										
										
										
											2015-02-11 17:29:56 +08:00
										 |  |  |     unless f.core_formula? | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |       tap = <<~EOS | 
					
						
							| 
									
										
										
										
											2015-02-11 17:29:56 +08:00
										 |  |  |         Formula: #{f.name} | 
					
						
							| 
									
										
										
										
											2019-04-01 16:02:13 -04:00
										 |  |  |             Tap: #{f.tap} | 
					
						
							|  |  |  |            Path: #{f.path} | 
					
						
							| 
									
										
										
										
											2015-02-11 17:29:56 +08:00
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |       files["00.tap.out"] = { content: tap } | 
					
						
							| 
									
										
										
										
											2015-02-11 17:29:56 +08:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 16:14:32 +00:00
										 |  |  |     if GitHub.api_credentials_type == :none | 
					
						
							|  |  |  |       puts <<~EOS | 
					
						
							|  |  |  |         You can create a new personal access token: | 
					
						
							| 
									
										
										
										
											2018-10-14 11:59:04 -07:00
										 |  |  |           #{GitHub::ALL_SCOPES_URL} | 
					
						
							| 
									
										
										
										
											2018-10-12 15:26:35 -07:00
										 |  |  |         #{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")} | 
					
						
							| 
									
										
										
										
											2018-02-19 16:14:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |       login! | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |     # 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" | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-03-13 21:15:06 +00:00
										 |  |  |       "#{f.name} (#{f.full_name}) on #{OS_VERSION} - Homebrew build logs" | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |     end | 
					
						
							|  |  |  |     url = create_gist(files, descr) | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-19 13:11:32 +00:00
										 |  |  |     url = create_issue(f.tap, "#{f.name} failed to build on #{MacOS.full_version}", url) if args.new_issue? | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |     puts url if url | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |   def brief_build_info(f) | 
					
						
							|  |  |  |     build_time_str = f.logs.ctime.strftime("%Y-%m-%d %H:%M:%S") | 
					
						
							| 
									
										
										
										
											2019-04-28 15:22:47 +02:00
										 |  |  |     s = +<<~EOS | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |       Homebrew build logs for #{f.full_name} on #{OS_VERSION} | 
					
						
							|  |  |  |     EOS | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  |     if args.with_hostname? | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |       hostname = Socket.gethostname | 
					
						
							|  |  |  |       s << "Host: #{hostname}\n" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     s << "Build date: #{build_time_str}\n" | 
					
						
							| 
									
										
										
										
											2019-04-29 13:02:15 +01:00
										 |  |  |     s.freeze | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:20:45 -04:00
										 |  |  |   # Causes some terminals to display secure password entry indicators | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |   def noecho_gets | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     system "stty -echo" | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |     result = $stdin.gets | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     system "stty echo" | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |     puts | 
					
						
							|  |  |  |     result | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |   def login! | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     print "GitHub User: " | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |     ENV["HOMEBREW_GITHUB_API_USERNAME"] = $stdin.gets.chomp | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     print "Password: " | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |     ENV["HOMEBREW_GITHUB_API_PASSWORD"] = noecho_gets.chomp | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |     puts | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-25 22:07:06 -04:00
										 |  |  |   def load_logs(dir) | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |     logs = {} | 
					
						
							| 
									
										
										
										
											2016-11-13 23:37:40 +01:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     raise "No logs." if logs.empty? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |     logs | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:38:52 -04:00
										 |  |  |   def create_private? | 
					
						
							| 
									
										
										
										
											2019-01-23 09:20:49 +05:30
										 |  |  |     args.private? | 
					
						
							| 
									
										
										
										
											2018-10-20 17:38:52 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |   def create_gist(files, description) | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |     url = "https://api.github.com/gists" | 
					
						
							| 
									
										
										
										
											2018-10-20 17:38:52 -04:00
										 |  |  |     data = { "public" => !create_private?, "files" => files, "description" => description } | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |     scopes = GitHub::CREATE_GIST_SCOPES | 
					
						
							| 
									
										
										
										
											2018-03-07 16:14:55 +00:00
										 |  |  |     GitHub.open_api(url, data: data, scopes: scopes)["html_url"] | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |   def create_issue(repo, title, body) | 
					
						
							|  |  |  |     url = "https://api.github.com/repos/#{repo}/issues" | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |     data = { "title" => title, "body" => body } | 
					
						
							| 
									
										
										
										
											2018-03-04 00:12:59 +05:30
										 |  |  |     scopes = GitHub::CREATE_ISSUE_FORK_OR_PR_SCOPES | 
					
						
							| 
									
										
										
										
											2018-03-07 16:14:55 +00:00
										 |  |  |     GitHub.open_api(url, data: data, scopes: scopes)["html_url"] | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def gist_logs | 
					
						
							| 
									
										
										
										
											2020-01-14 22:04:51 +00:00
										 |  |  |     gist_logs_args.parse | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 12:39:54 +00:00
										 |  |  |     Install.perform_preinstall_checks(all_fatal: true) | 
					
						
							| 
									
										
										
										
											2019-01-21 19:23:31 +00:00
										 |  |  |     Install.perform_build_from_source_checks(all_fatal: true) | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |     gistify_logs(args.resolved_formulae.first) | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |