diff options
| author | Jack Nagel | 2012-06-18 19:58:35 -0500 |
|---|---|---|
| committer | Jack Nagel | 2012-07-04 22:47:33 -0500 |
| commit | 76b2eee7771ecf2b339d1023699ef9f30fa3fe65 (patch) | |
| tree | df5ab7ba72806e408a6e9617bb3eaa195889be0a /Library/Homebrew/formula_support.rb | |
| parent | 2c6e93cf8aa77c5cfa6f16cafa7bf372d3afc7ab (diff) | |
| download | brew-76b2eee7771ecf2b339d1023699ef9f30fa3fe65.tar.bz2 | |
Refactor checksumming
Signed-off-by: Jack Nagel <jacknagel@gmail.com>
Diffstat (limited to 'Library/Homebrew/formula_support.rb')
| -rw-r--r-- | Library/Homebrew/formula_support.rb | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/Library/Homebrew/formula_support.rb b/Library/Homebrew/formula_support.rb index 9bd7b6d6b..0449094cb 100644 --- a/Library/Homebrew/formula_support.rb +++ b/Library/Homebrew/formula_support.rb @@ -1,10 +1,9 @@ require 'download_strategy' +require 'checksums' class SoftwareSpec attr_reader :checksum, :mirrors, :specs, :strategy - CHECKSUM_TYPES = [:md5, :sha1, :sha256].freeze - VCS_SYMBOLS = { :bzr => BazaarDownloadStrategy, :curl => CurlDownloadStrategy, @@ -16,17 +15,6 @@ class SoftwareSpec :svn => SubversionDownloadStrategy, } - # Detect which type of checksum is being used, or nil if none - def checksum_type - @checksum_type ||= CHECKSUM_TYPES.detect do |type| - instance_variable_defined?("@#{type}") - end - end - - def has_checksum? - (checksum_type and self.send(checksum_type)) || false - end - # Was the version defined in the DSL, or detected from the URL? def explicit_version? @explicit_version || false @@ -45,11 +33,29 @@ class SoftwareSpec return detected end + def verify_download_integrity fn + fn.verify_checksum @checksum + rescue ChecksumMissingError + opoo "Cannot verify package integrity" + puts "The formula did not provide a download checksum" + puts "For your reference the SHA1 is: #{fn.sha1}" + rescue ChecksumMismatchError => e + e.advice = <<-EOS.undent + Archive: #{fn} + (To retry an incomplete download, remove the file above.) + EOS + raise e + end + # The methods that follow are used in the block-form DSL spec methods - CHECKSUM_TYPES.each do |cksum| + Checksum::TYPES.each do |cksum| class_eval %Q{ def #{cksum}(val=nil) - val.nil? ? @#{cksum} : @#{cksum} = val + if val.nil? + @checksum if @checksum.nil? or @checksum.hash_type == :#{cksum} + else + @checksum = Checksum.new(:#{cksum}, val) + end end } end @@ -84,7 +90,6 @@ class HeadSoftwareSpec < SoftwareSpec def initialize super @version = 'HEAD' - @checksum = nil end def verify_download_integrity fn @@ -97,13 +102,14 @@ class Bottle < SoftwareSpec attr_reader :revision def initialize + super @revision = 0 @strategy = CurlBottleDownloadStrategy end # Checksum methods in the DSL's bottle block optionally take # a Hash, which indicates the platform the checksum applies on. - CHECKSUM_TYPES.each do |cksum| + Checksum::TYPES.each do |cksum| class_eval %Q{ def #{cksum}(val=nil) @#{cksum} ||= Hash.new @@ -111,11 +117,13 @@ class Bottle < SoftwareSpec when nil @#{cksum}[MacOS.cat] when String - @#{cksum}[:lion] = val + @#{cksum}[:lion] = Checksum.new(:#{cksum}, val) when Hash key, value = val.shift - @#{cksum}[value] = key + @#{cksum}[value] = Checksum.new(:#{cksum}, key) end + + @checksum = @#{cksum}[MacOS.cat] if @#{cksum}.has_key? MacOS.cat end } end |
