From a9a0cb27a126bddcf325648d082ffeb407c02337 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Fri, 15 Jul 2016 13:09:10 -0400 Subject: [PATCH] Make audit_modified_casks an internal cmd (#22826) --- ci/travis/script.sh | 2 +- developer/bin/audit_modified_casks | 154 ------------------- lib/hbc/cli.rb | 1 + lib/hbc/cli/internal_audit_modified_casks.rb | 124 +++++++++++++++ 4 files changed, 126 insertions(+), 155 deletions(-) delete mode 100755 developer/bin/audit_modified_casks create mode 100644 lib/hbc/cli/internal_audit_modified_casks.rb diff --git a/ci/travis/script.sh b/ci/travis/script.sh index 519e3f4d57..1640a6ce6c 100644 --- a/ci/travis/script.sh +++ b/ci/travis/script.sh @@ -12,7 +12,7 @@ header 'Running script.sh...' if any_casks_modified; then modified_casks=($(modified_cask_files)) - run developer/bin/audit_modified_casks "${TRAVIS_COMMIT_RANGE}" + run brew cask _audit_modified_casks "${TRAVIS_COMMIT_RANGE}" run brew cask style "${modified_casks[@]}" fi diff --git a/developer/bin/audit_modified_casks b/developer/bin/audit_modified_casks deleted file mode 100755 index 4b37a2d549..0000000000 --- a/developer/bin/audit_modified_casks +++ /dev/null @@ -1,154 +0,0 @@ -#!/usr/bin/env ruby -# -# audit_modified_casks -# - -### -### dependencies -### - -require 'pathname' -require 'open3' - -$LOAD_PATH.unshift(Pathname(`brew --repository`.chomp).realpath.join('Library/Homebrew')) -$LOAD_PATH.unshift(Pathname(__FILE__).realpath.join('../../../lib')) - -require 'vendor/homebrew-fork/global' -require 'hbc' - -### -### constants -### - -CASK_DIR = Pathname(__FILE__).realpath.join('../../../Casks') -RELEVANT_STANZAS = %i{version sha256 url} - -### -### methods -### - -def cd_to_project_root - Dir.chdir Pathname(__FILE__).realpath.dirname - @git_root ||= git(*%w[rev-parse --show-toplevel]) - Dir.chdir @git_root -end - -def die(msg, status=1) - onoe msg - exit status -end - -def main - cd_to_project_root - modified_cask_files.zip(modified_casks).each do |cask_file, cask| - audit(cask, cask_file) - end - report_failures -end - -def commit_range - $commit_range -end - -def modified_cask_files - return @modified_cask_files if defined? @modified_cask_files - out = git(*%w[diff --name-only --diff-filter=AM], commit_range, - '--', "#{CASK_DIR}/*.rb") - @modified_cask_files = out.split("\n") -end - -def modified_casks - return @modified_casks if defined? @modified_casks - @modified_casks = modified_cask_files.map { |f| Hbc.load(f) } - if @modified_casks.any? - num_modified = @modified_casks.size - ohai "#{num_modified} modified #{pluralize('cask', num_modified)}: " \ - "#{@modified_casks.join(' ')}" - end - @modified_casks -end - -def audit(cask, cask_file) - audit_download = audit_download?(cask, cask_file) - success = Hbc::Auditor.audit(cask, audit_download: audit_download) - failed_casks << cask unless success -end - -def failed_casks - @failed_casks ||= [] -end - -def audit_download?(cask, cask_file) - cask.sha256 != :no_check && relevant_stanza_modified?(cask_file) -end - -def relevant_stanza_modified?(cask_file) - out = git('diff', commit_range, '--', cask_file) - out =~ /^\+\s*(#{RELEVANT_STANZAS.join('|')})/ -end - -def git(*args) - ohai ['git', *args].join(' ') if $debug - out, err, status = Open3.capture3('git', *args) - return out.chomp if status.success? - die err.chomp, status.exitstatus -end - -def report_failures - return if failed_casks.empty? - num_failed = failed_casks.size - cask_pluralized = pluralize('cask', num_failed) - die "audit failed for #{num_failed} #{cask_pluralized}: " \ - "#{failed_casks.join(' ')}" -end - -def pluralize(str, num) - num == 1 ? str : "#{str}s" -end - -def cleanup - Hbc::CLI::Cleanup.run if $clean_cache -end - -### -### main -### - -usage = <<-EOS -Usage: audit_modified_casks [options...] - -Given a range of Git commits, find any Casks that were modified and run `brew -cask audit' on them. If the `url', `version', or `sha256' stanzas were modified, -run with the `--download' flag to verify the hash. - -Options: - -c, --clean-cache - Remove all cached downloads. Use with care. - -d, --debug - Enable debugging output. - -h, --help - Display usage and exit. -EOS - -while ARGV.any? do - case ARGV.first - when /^-+h(elp)?$/i - puts usage - exit 0 - when /^-+d(ebug)?$/i - $debug = 1 - ARGV.shift - when /^-+c(lean-cache)?$/i - $clean_cache = 1 - ARGV.shift - else - die usage if $commit_range - $commit_range = ARGV.shift - end -end - -die usage unless $commit_range - -at_exit { cleanup } - -main diff --git a/lib/hbc/cli.rb b/lib/hbc/cli.rb index 925e92a209..0d09b14b25 100644 --- a/lib/hbc/cli.rb +++ b/lib/hbc/cli.rb @@ -22,6 +22,7 @@ require "hbc/cli/update" require "hbc/cli/zap" require "hbc/cli/internal_use_base" +require "hbc/cli/internal_audit_modified_casks" require "hbc/cli/internal_checkurl" require "hbc/cli/internal_dump" require "hbc/cli/internal_help" diff --git a/lib/hbc/cli/internal_audit_modified_casks.rb b/lib/hbc/cli/internal_audit_modified_casks.rb new file mode 100644 index 0000000000..66b0c06c88 --- /dev/null +++ b/lib/hbc/cli/internal_audit_modified_casks.rb @@ -0,0 +1,124 @@ +class Hbc::CLI::InternalAuditModifiedCasks < Hbc::CLI::InternalUseBase + RELEVANT_STANZAS = %i{version sha256 url}.freeze + + class << self + def run(*args) + commit_range = commit_range(args) + cleanup = args.any? { |a| a =~ %r{^-+c(leanup)?$}i } + new(commit_range, cleanup: cleanup).run + end + + def commit_range(args) + posargs = args.reject { |a| a.empty? || a.chars.first == "-" } + odie usage unless posargs.size == 1 + posargs.first + end + + def posargs(args) + args.reject { |a| a.empty? || a.chars.first == "-" } + end + + def usage + <<-EOS.undent + Usage: brew cask _audit_modified_casks [options...] + + Given a range of Git commits, find any Casks that were modified and run `brew + cask audit' on them. If the `url', `version', or `sha256' stanzas were modified, + run with the `--download' flag to verify the hash. + + Options: + -c, --cleanup + Remove all cached downloads. Use with care. + EOS + end + end + + def initialize(commit_range, cleanup: false) + @commit_range = commit_range + @cleanup = cleanup + end + + attr_reader :commit_range + + def cleanup? + @cleanup + end + + def run + at_exit { cleanup } + Dir.chdir git_root do + modified_cask_files.zip(modified_casks).each do |cask_file, cask| + audit(cask, cask_file) + end + end + report_failures + end + + def git_root + @git_root ||= git(*%w[rev-parse --show-toplevel]) + end + + def cask_dir + @cask_dir ||= Pathname(git_root).join("Casks") + end + + def modified_cask_files + return @modified_cask_files if defined? @modified_cask_files + out = git(*%w[diff --name-only --diff-filter=AM], commit_range, + "--", "#{cask_dir}/*.rb") + @modified_cask_files = out.split("\n") + end + + def modified_casks + return @modified_casks if defined? @modified_casks + @modified_casks = modified_cask_files.map { |f| Hbc.load(f) } + if @modified_casks.any? + num_modified = @modified_casks.size + ohai "#{num_modified} modified #{pluralize('cask', num_modified)}: " \ + "#{@modified_casks.join(' ')}" + end + @modified_casks + end + + def audit(cask, cask_file) + audit_download = audit_download?(cask, cask_file) + success = Hbc::Auditor.audit(cask, audit_download: audit_download) + failed_casks << cask unless success + end + + def failed_casks + @failed_casks ||= [] + end + + def audit_download?(cask, cask_file) + cask.sha256 != :no_check && relevant_stanza_modified?(cask_file) + end + + def relevant_stanza_modified?(cask_file) + out = git("diff", commit_range, "--", cask_file) + out =~ %r{^\+\s*(#{RELEVANT_STANZAS.join('|')})} + end + + def git(*args) + odebug ["git", *args].join(" ") + out, err, status = Open3.capture3("git", *args) + return out.chomp if status.success? + odie err.chomp + end + + def report_failures + return if failed_casks.empty? + num_failed = failed_casks.size + cask_pluralized = pluralize("cask", num_failed) + odie "audit failed for #{num_failed} #{cask_pluralized}: " \ + "#{failed_casks.join(' ')}" + end + + def pluralize(str, num) + num == 1 ? str : "#{str}s" + end + + def cleanup + Hbc::CLI::Cleanup.run if cleanup? + end +end