From 216444849edfea44153a97cca1282714c6ebf324 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Wed, 22 Jan 2014 18:29:00 -0500 Subject: [PATCH] Add copious debugging with --debug - add new file "cask/utils.rb" analogous to "utils.rb" in Homebrew - define odebug and odumpcask, analogs of ohai and friends, but which only give output when --debug is in effect - move the debug setting from an instance variable in Cask::CLI to a method Cask.debug, defined in "lib/cask/options.rb", which was added in #2276. (Perhaps options.rb should be merged back into Cask::CLI). - sprinkle odebug statements liberally throughout the codebase - update tests --- lib/cask.rb | 7 ++++++- lib/cask/artifact.rb | 6 +++++- lib/cask/audit.rb | 5 +++++ lib/cask/cli.rb | 6 +++--- lib/cask/cli/alfred.rb | 4 ++++ lib/cask/cli/audit.rb | 1 + lib/cask/cli/checklinks.rb | 1 + lib/cask/cli/create.rb | 1 + lib/cask/cli/edit.rb | 1 + lib/cask/cli/home.rb | 2 ++ lib/cask/cli/info.rb | 1 + lib/cask/cli/install.rb | 1 + lib/cask/cli/list.rb | 1 + lib/cask/cli/uninstall.rb | 1 + lib/cask/container.rb | 6 +++++- lib/cask/download.rb | 6 +++++- lib/cask/download_strategy.rb | 1 + lib/cask/installer.rb | 15 +++++++++++++++ lib/cask/options.rb | 8 ++++++++ lib/cask/pkg.rb | 3 +++ lib/cask/qualified_cask_name.rb | 1 + lib/cask/source.rb | 7 ++++++- lib/cask/source/uri.rb | 1 + lib/cask/system_command.rb | 1 + lib/cask/utils.rb | 32 ++++++++++++++++++++++++++++++++ test/cask/cli/options_test.rb | 5 +++-- 26 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 lib/cask/utils.rb diff --git a/lib/cask.rb b/lib/cask.rb index e755f8a954..e5e04cd5ae 100644 --- a/lib/cask.rb +++ b/lib/cask.rb @@ -29,6 +29,7 @@ require 'cask/source' require 'cask/system_command' require 'cask/underscore_supporting_uri' require 'cask/url' +require 'cask/utils' require 'plist/parser' @@ -39,6 +40,7 @@ class Cask include Cask::Options def self.init + odebug 'Creating directories' HOMEBREW_CACHE.mkpath unless HOMEBREW_CACHE.exist? unless caskroom.exist? ohai "We need to make Caskroom for the first time at #{caskroom}" @@ -57,7 +59,10 @@ class Cask end def self.load(query) - Cask::Source.for_query(query).load + odebug 'Loading Cask definitions' + cask = Cask::Source.for_query(query).load + odumpcask cask + cask end def self.title diff --git a/lib/cask/artifact.rb b/lib/cask/artifact.rb index 89e27fdb35..5547cf3d25 100644 --- a/lib/cask/artifact.rb +++ b/lib/cask/artifact.rb @@ -43,6 +43,10 @@ module Cask::Artifact end def self.for_cask(cask) - artifacts.select { |artifact| artifact.me?(cask) } + odebug "Determining which artifacts are present in Cask #{cask}" + artifacts.select do |artifact| + odebug "Checking for artifact class #{artifact}" + artifact.me?(cask) + end end end diff --git a/lib/cask/audit.rb b/lib/cask/audit.rb index 97fe0cf440..985e25abae 100644 --- a/lib/cask/audit.rb +++ b/lib/cask/audit.rb @@ -24,27 +24,32 @@ class Cask::Audit def _check_required_fields + odebug "Auditing required fields" add_error "url is required" unless cask.url add_error "version is required" unless cask.version add_error "homepage is required" unless cask.homepage end def _check_checksums + odebug "Auditing checksums" return if cask.sums == 0 add_error "could not find checksum or no_checksum" unless cask.sums.is_a?(Array) && cask.sums.length > 0 end def _check_no_checksums_if_latest + odebug "Verifying no_checkum with version 'latest'" add_error "you should use no_checksum when version is latest" if cask.version == "latest" && cask.sums.is_a?(Array) end def _check_download(download) + odebug "Auditing download" download.perform rescue => e add_error "download not possible: #{e.message}" end def _check_sourceforge_download_url_format + odebug "Auditing URL format" if _bad_sourceforge_url? add_warning "SourceForge URL format incorrect. See https://github.com/phinze/homebrew-cask/blob/master/CONTRIBUTING.md#sourceforge-urls" end diff --git a/lib/cask/cli.rb b/lib/cask/cli.rb index 65885ec7da..4b1bb94c1f 100644 --- a/lib/cask/cli.rb +++ b/lib/cask/cli.rb @@ -49,11 +49,11 @@ class Cask::CLI run_command(command, *rest) rescue CaskAlreadyInstalledError => e opoo e - $stderr.puts e.backtrace if @debug + $stderr.puts e.backtrace if Cask.debug exit 0 rescue CaskError => e onoe e - $stderr.puts e.backtrace if @debug + $stderr.puts e.backtrace if Cask.debug exit 1 end @@ -112,7 +112,7 @@ class Cask::CLI Cask.no_binaries = true end opts.on("--debug") do |v| - @debug = true + Cask.debug = true end end end diff --git a/lib/cask/cli/alfred.rb b/lib/cask/cli/alfred.rb index fe54d20890..47de7eed88 100644 --- a/lib/cask/cli/alfred.rb +++ b/lib/cask/cli/alfred.rb @@ -47,6 +47,7 @@ class Cask::CLI::Alfred if linked? opoo "Alfred is already linked to homebrew-cask." else + odebug 'Linking Alfred scopes' save_alfred_scopes(alfred_scopes << Cask.caskroom) ohai "Successfully linked Alfred to homebrew-cask." end @@ -58,6 +59,7 @@ class Cask::CLI::Alfred if !linked? opoo "Alfred is already unlinked from homebrew-cask." else + odebug 'Unlinking Alfred scopes' save_alfred_scopes(alfred_scopes.reject { |x| x == Cask.caskroom.to_s }) ohai "Successfully unlinked Alfred from homebrew-cask." end @@ -105,8 +107,10 @@ class Cask::CLI::Alfred def self.alfred_preference(key, value=nil) if value + odebug 'Writing Alfred preferences' @system_command.run('/usr/bin/defaults', :args => ['write', DOMAIN, key, %Q("#{value}")]) else + odebug 'Reading Alfred preferences' @system_command.run('/usr/bin/defaults', :args => ['read', DOMAIN, key]) end end diff --git a/lib/cask/cli/audit.rb b/lib/cask/cli/audit.rb index b3a2e87b1f..1c683e2cb4 100644 --- a/lib/cask/cli/audit.rb +++ b/lib/cask/cli/audit.rb @@ -17,6 +17,7 @@ class Cask::CLI::Audit end def audit(cask) + odebug "Auditing Cask #{cask}" @auditor.audit(cask, :audit_download => audit_download?) end diff --git a/lib/cask/cli/checklinks.rb b/lib/cask/cli/checklinks.rb index ef72936af3..bf4bcc295a 100644 --- a/lib/cask/cli/checklinks.rb +++ b/lib/cask/cli/checklinks.rb @@ -2,6 +2,7 @@ class Cask::CLI::Checklinks def self.run(*args) casks_to_check = args.empty? ? Cask.all : args.map { |arg| Cask.load(arg) } casks_to_check.each do |cask| + odebug "Checking links for Cask #{cask}" checker = Cask::LinkChecker.new(cask) checker.run puts checker.summary diff --git a/lib/cask/cli/create.rb b/lib/cask/cli/create.rb index 38f11d1aac..9cdd04a1eb 100644 --- a/lib/cask/cli/create.rb +++ b/lib/cask/cli/create.rb @@ -3,6 +3,7 @@ module Cask::CLI::Create raise CaskUnspecifiedError if arguments.empty? cask_name, *_ = *arguments cask_path = Cask.path(cask_name) + odebug "Creating Cask #{cask_name}" if cask_path.exist? raise CaskAlreadyCreatedError.new cask_name diff --git a/lib/cask/cli/edit.rb b/lib/cask/cli/edit.rb index 9ea6a429c4..cdfd6e9fc5 100644 --- a/lib/cask/cli/edit.rb +++ b/lib/cask/cli/edit.rb @@ -3,6 +3,7 @@ module Cask::CLI::Edit raise CaskUnspecifiedError if arguments.empty? cask_name, *_ = *arguments cask_path = Cask.path(cask_name) + odebug "Opening editor for Cask #{cask_name}" unless cask_path.exist? raise CaskUnavailableError, "#{cask_name}, use `brew cask create #{cask_name}` to make a new cask with this name" end diff --git a/lib/cask/cli/home.rb b/lib/cask/cli/home.rb index 51e44e351e..f7f710e5be 100644 --- a/lib/cask/cli/home.rb +++ b/lib/cask/cli/home.rb @@ -1,9 +1,11 @@ module Cask::CLI::Home def self.run(*cask_names) if cask_names.empty? + odebug "Opening project homepage" system "/usr/bin/open", 'http://caskroom.io/' else cask_names.each do |cask_name| + odebug "Opening homepage for Cask #{cask_name}" cask = Cask.load(cask_name) system "/usr/bin/open", cask.homepage end diff --git a/lib/cask/cli/info.rb b/lib/cask/cli/info.rb index aad573f6d5..8bea2f7827 100644 --- a/lib/cask/cli/info.rb +++ b/lib/cask/cli/info.rb @@ -2,6 +2,7 @@ class Cask::CLI::Info def self.run(*cask_names) raise CaskUnspecifiedError if cask_names.empty? cask_names.each do |cask_name| + odebug "Getting info for Cask #{cask_name}" cask = Cask.load(cask_name) puts info(cask) Cask::Installer.print_caveats(cask) diff --git a/lib/cask/cli/install.rb b/lib/cask/cli/install.rb index 55ff427cc2..59a1465473 100644 --- a/lib/cask/cli/install.rb +++ b/lib/cask/cli/install.rb @@ -4,6 +4,7 @@ class Cask::CLI::Install cask_names = args.reject { |a| a.chars.first == '-' } force = args.include? '--force' cask_names.each do |cask_name| + odebug "Installing Cask #{cask_name}" cask = Cask.load(cask_name) Cask::Installer.new(cask).install(force) end diff --git a/lib/cask/cli/list.rb b/lib/cask/cli/list.rb index e8db3a83d1..f0a0b284fc 100644 --- a/lib/cask/cli/list.rb +++ b/lib/cask/cli/list.rb @@ -9,6 +9,7 @@ class Cask::CLI::List def self.list_files(*cask_names) cask_names.each do |cask_name| + odebug "Listing files for Cask #{cask_name}" cask = Cask.load(cask_name) if cask.installed? Cask::PrettyListing.new(cask).print diff --git a/lib/cask/cli/uninstall.rb b/lib/cask/cli/uninstall.rb index 11ddb444ab..e4c3f7a6ca 100644 --- a/lib/cask/cli/uninstall.rb +++ b/lib/cask/cli/uninstall.rb @@ -3,6 +3,7 @@ class Cask::CLI::Uninstall raise CaskUnspecifiedError if args.empty? cask_names = args.reject { |a| a.chars.first == '-' } cask_names.each do |cask_name| + odebug "Uninstalling Cask #{cask_name}" cask = Cask.load(cask_name) raise CaskNotInstalledError.new(cask) unless cask.installed? Cask::Installer.new(cask).uninstall diff --git a/lib/cask/container.rb b/lib/cask/container.rb index 1d252ddbd8..c7d50b96d6 100644 --- a/lib/cask/container.rb +++ b/lib/cask/container.rb @@ -18,7 +18,11 @@ class Cask::Container end def self.for_path(path, command) + odebug "Determining which containers to use" criteria = Cask::Container::Criteria.new(path, command) - containers.find { |c| c.me?(criteria) } + containers.find do |c| + odebug "Checking container class #{c}" + c.me?(criteria) + end end end diff --git a/lib/cask/download.rb b/lib/cask/download.rb index 3d3d54e6c2..d372df2be6 100644 --- a/lib/cask/download.rb +++ b/lib/cask/download.rb @@ -21,7 +21,11 @@ class Cask::Download sums.each do |sum| unless sum.empty? computed = Checksum.new(sum.hash_type, Digest.const_get(sum.hash_type.to_s.upcase).file(path).hexdigest) - raise ChecksumMismatchError.new(sum, computed) unless sum == computed + if sum == computed + odebug "Checksums match" + else + raise ChecksumMismatchError.new(sum, computed) + end has_sum = true end end diff --git a/lib/cask/download_strategy.rb b/lib/cask/download_strategy.rb index d1eb4a545d..21f7368872 100644 --- a/lib/cask/download_strategy.rb +++ b/lib/cask/download_strategy.rb @@ -16,6 +16,7 @@ class Cask::DownloadStrategy < CurlDownloadStrategy end def _fetch + odebug "Calling curl with args #{curl_args}" curl(*curl_args) end diff --git a/lib/cask/installer.rb b/lib/cask/installer.rb index 470499b705..f83116fd39 100644 --- a/lib/cask/installer.rb +++ b/lib/cask/installer.rb @@ -9,6 +9,7 @@ class Cask::Installer end def self.print_caveats(cask) + odebug "Printing caveats" unless cask.caveats.empty? ohai "Caveats" cask.caveats.each do |caveat| @@ -22,6 +23,7 @@ class Cask::Installer end def install(force=false) + odebug "Cask::Installer.install" if @cask.installed? && !force raise CaskAlreadyInstalledError.new(@cask) end @@ -43,22 +45,30 @@ class Cask::Installer def download + odebug "Downloading" download = Cask::Download.new(@cask) @downloaded_path = download.perform + odebug "Downloaded to -> #{@downloaded_path}" + @downloaded_path end def extract_primary_container + odebug "Extracting primary container" FileUtils.mkdir_p @cask.destination_path container = Cask::Container.for_path(@downloaded_path, @command) unless container raise "uh oh, could not identify primary container for #{@downloaded_path}" end + odebug "Using container class #{container} for #{@downloaded_path}" container.new(@cask, @downloaded_path, @command).extract end def install_artifacts + odebug "Installing artifacts" artifacts = Cask::Artifact.for_cask(@cask) + odebug "#{artifacts.length} artifact/s defined", artifacts artifacts.each do |artifact| + odebug "Installing artifact of class #{artifact}" artifact.new(@cask, @command).install end end @@ -91,18 +101,23 @@ class Cask::Installer end def uninstall + odebug "Cask::Installer.uninstall" uninstall_artifacts purge_files end def uninstall_artifacts + odebug "Un-installing artifacts" artifacts = Cask::Artifact.for_cask(@cask) + odebug "#{artifacts.length} artifact/s defined", artifacts artifacts.each do |artifact| + odebug "Un-installing artifact of class #{artifact}" artifact.new(@cask, @command).uninstall end end def purge_files + odebug "Purging files" if @cask.destination_path.exist? @cask.destination_path.rmtree end diff --git a/lib/cask/options.rb b/lib/cask/options.rb index d1d0b52fd2..c01f527c63 100644 --- a/lib/cask/options.rb +++ b/lib/cask/options.rb @@ -11,5 +11,13 @@ module Cask::Options def no_binaries=(_no_binaries) @no_binaries = _no_binaries end + + def debug + @debug ||= false + end + + def debug=(_debug) + @debug = _debug + end end end diff --git a/lib/cask/pkg.rb b/lib/cask/pkg.rb index e5752eb8f3..8ee0981dc3 100644 --- a/lib/cask/pkg.rb +++ b/lib/cask/pkg.rb @@ -13,9 +13,11 @@ class Cask::Pkg end def uninstall + odebug "Deleting pkg files" list('files').each_slice(500) do |file_slice| @command.run('/bin/rm', :args => file_slice.unshift('-f'), :sudo => true) end + odebug "Deleting pkg directories" _deepest_path_first(list('dirs')).each do |dir| if dir.exist? _with_full_permissions(dir) do @@ -29,6 +31,7 @@ class Cask::Pkg end def forget + odebug "Unregistering pkg receipt (aka forgetting)" @command.run!('/usr/sbin/pkgutil', :args => ['--forget', package_id], :sudo => true) end diff --git a/lib/cask/qualified_cask_name.rb b/lib/cask/qualified_cask_name.rb index cb554a5d76..90fc8cb223 100644 --- a/lib/cask/qualified_cask_name.rb +++ b/lib/cask/qualified_cask_name.rb @@ -44,6 +44,7 @@ module Cask::QualifiedCaskName user, repo, cask = path_elements end repo.sub!(%r{^#{repo_prefix}}, '') + odebug "[user, repo, cask] might be [#{user}, #{repo}, #{cask}]" [user, repo, cask] end end diff --git a/lib/cask/source.rb b/lib/cask/source.rb index 3bbc816110..760b6ac48e 100644 --- a/lib/cask/source.rb +++ b/lib/cask/source.rb @@ -20,8 +20,13 @@ module Cask::Source end def self.for_query(query) - source = sources.find { |s| s.me?(query) } + odebug "Translating '#{query}' into a valid Cask source" + source = sources.find do |s| + odebug "Testing source class #{s}" + s.me?(query) + end raise CaskUnavailableError.new(query) unless source + odebug "Using source class #{source}" source.new(query) end end diff --git a/lib/cask/source/uri.rb b/lib/cask/source/uri.rb index 9e941d4961..958659e466 100644 --- a/lib/cask/source/uri.rb +++ b/lib/cask/source/uri.rb @@ -13,6 +13,7 @@ class Cask::Source::URI HOMEBREW_CACHE_CASKS.mkpath path = HOMEBREW_CACHE_CASKS.join(File.basename(uri)) ohai "Downloading #{uri}" + odebug "Download target -> #{path.to_s}" curl(uri, '-o', path.to_s) Cask::Source::Path.new(path).load rescue ErrorDuringExecution diff --git a/lib/cask/system_command.rb b/lib/cask/system_command.rb index f247b78bb4..41a3709d77 100644 --- a/lib/cask/system_command.rb +++ b/lib/cask/system_command.rb @@ -1,6 +1,7 @@ class Cask::SystemCommand def self.run(command, options={}) command = _process_options(command, options) + odebug "Executing: #{command}" output = '' IO.popen(command, 'r+') do |pipe| if options[:input] diff --git a/lib/cask/utils.rb b/lib/cask/utils.rb new file mode 100644 index 0000000000..683d184a2d --- /dev/null +++ b/lib/cask/utils.rb @@ -0,0 +1,32 @@ + +# see Homebrew Library/Homebrew/utils.rb + +require 'yaml' + +# monkeypatch Tty +class Tty + class << self + def magenta; color 35; end + end +end + +def odebug title, *sput + if Cask.respond_to?(:debug) and Cask.debug + width = Tty.width * 4 - 6 + if $stdout.tty? and title.to_s.length > width + title = title.to_s[0, width - 3] + '...' + end + puts "#{Tty.magenta}==>#{Tty.white} #{title}#{Tty.reset}" + puts sput unless sput.empty? + end +end + +def odumpcask cask + if Cask.respond_to?(:debug) and Cask.debug + odebug "Cask instance dumps in YAML:" + odebug "Cask instance toplevel:", cask.to_yaml + [:homepage, :url, :version, :sums, :artifacts, :caveats].each do |method| + odebug "Cask instance method '#{method}':", cask.send(method).to_yaml + end + end +end diff --git a/test/cask/cli/options_test.rb b/test/cask/cli/options_test.rb index a116a31b50..13eb30d585 100644 --- a/test/cask/cli/options_test.rb +++ b/test/cask/cli/options_test.rb @@ -107,9 +107,10 @@ describe Cask::CLI do end describe "--debug" do - it "sets the CLI's debug variable to true" do + it "sets the Cask debug method to true" do Cask::CLI.process_options %w{help --debug} - Cask::CLI.instance_variable_get(:@debug).must_equal true + Cask.debug.must_equal true + Cask.debug = false end end