patches - support detection of compression types

This commit is contained in:
Adam Vandenberg 2012-04-29 11:51:04 -07:00
parent 3ad19e08b7
commit e8c0b01f24
2 changed files with 56 additions and 26 deletions

View File

@ -525,17 +525,16 @@ private
patch_list = Patches.new(patches) patch_list = Patches.new(patches)
return if patch_list.empty? return if patch_list.empty?
unless patch_list.external_curl_args.empty? if patch_list.external_patches?
ohai "Downloading patches" ohai "Downloading patches"
# downloading all at once is much more efficient, especially for FTP patch_list.download!
curl(*patch_list.external_curl_args)
end end
ohai "Patching" ohai "Patching"
patch_list.each do |p| patch_list.each do |p|
case p.compression case p.compression
when :gzip then safe_system "/usr/bin/gunzip", p.download_filename when :gzip then safe_system "/usr/bin/gunzip", p.compressed_filename
when :bzip2 then safe_system "/usr/bin/bunzip2", p.download_filename when :bzip2 then safe_system "/usr/bin/bunzip2", p.compressed_filename
end end
# -f means don't prompt the user if there are errors; just exit with non-zero status # -f means don't prompt the user if there are errors; just exit with non-zero status
safe_system '/usr/bin/patch', '-f', *(p.patch_args) safe_system '/usr/bin/patch', '-f', *(p.patch_args)

View File

@ -15,9 +15,8 @@ class Patches
end end
end end
# Collects the urls and output names of all external patches def external_patches?
def external_curl_args not external_curl_args.empty?
@patches.select{|p| p.external?}.collect{|p| p.curl_args}.flatten
end end
def each(&blk) def each(&blk)
@ -27,8 +26,26 @@ class Patches
@patches.empty? @patches.empty?
end end
def download!
return unless external_patches?
# downloading all at once is much more efficient, especially for FTP
curl(*external_curl_args)
external_patches.each{|p| p.stage!}
end
private private
def external_patches
@patches.select{|p| p.external?}
end
# Collects the urls and output names of all external patches
def external_curl_args
external_patches.collect{|p| p.curl_args}.flatten
end
def normalize_patches patches def normalize_patches patches
if patches.kind_of? Hash if patches.kind_of? Hash
patches patches
@ -40,29 +57,21 @@ private
end end
class Patch class Patch
# Used by formula to unpack after downloading
attr_reader :compression attr_reader :compression
attr_reader :url attr_reader :compressed_filename
attr_reader :download_filename
def initialize patch_p, filename, url def initialize patch_p, filename, url
@patch_p = patch_p @patch_p = patch_p
@patch_filename = filename @patch_filename = filename
@compression = false @compressed_filename = nil
@compression = nil
@url = nil @url = nil
if url.kind_of? File # true when DATA is passed if url.kind_of? File # true when DATA is passed
write_data url write_data url
elsif looks_like_url(url) elsif looks_like_url(url)
@download_filename = @patch_filename @url = url # Save URL to mark this as an external patch
@url = url # Save URL
case @url
when /\.gz$/
@compression = :gzip
@download_filename += '.gz'
when /\.bz2$/
@compression = :bzip2
@download_filename += '.bz2'
end
else else
# it's a file on the local filesystem # it's a file on the local filesystem
# use URL as the filename for patch # use URL as the filename for patch
@ -70,12 +79,22 @@ class Patch
end end
end end
def external? # rename the downloaded file to take compression into account
!!@url def stage!
return unless external?
detect_compression!
case @compression
when :gzip
@compressed_filename = @patch_filename + '.gz'
FileUtils.mv @patch_filename, @compressed_filename
when :bzip2
@compressed_filename = @patch_filename + '.bz2'
FileUtils.mv @patch_filename, @compressed_filename
end
end end
def compressed? def external?
!!@compression not @url.nil?
end end
def patch_args def patch_args
@ -83,11 +102,23 @@ class Patch
end end
def curl_args def curl_args
[@url, '-o', @download_filename] [@url, '-o', @patch_filename]
end end
private private
# Detect compression type from the downloaded patch.
def detect_compression!
# If nil we have not tried to detect yet
if @compression.nil?
path = Pathname.new(@patch_filename)
if path.exist?
@compression = path.compression_type
@compression ||= :none # If nil, convert to :none
end
end
end
# Write the given file object (DATA) out to a local file for patch # Write the given file object (DATA) out to a local file for patch
def write_data f def write_data f
pn = Pathname.new @patch_filename pn = Pathname.new @patch_filename