Initialize cached filenames lazily

Closes Homebrew/homebrew#22751.
This commit is contained in:
Jack Nagel 2013-09-23 21:39:19 -05:00
parent b20335b314
commit 7e981fbedc

View File

@ -4,7 +4,11 @@ require 'utils/json'
class AbstractDownloadStrategy class AbstractDownloadStrategy
attr_accessor :local_bottle_path attr_accessor :local_bottle_path
attr_reader :name, :resource
def initialize name, resource def initialize name, resource
@name = name
@resource = resource
@url = resource.url @url = resource.url
specs = resource.specs specs = resource.specs
@spec, @ref = specs.dup.shift unless specs.empty? @spec, @ref = specs.dup.shift unless specs.empty?
@ -40,34 +44,38 @@ end
class CurlDownloadStrategy < AbstractDownloadStrategy class CurlDownloadStrategy < AbstractDownloadStrategy
def initialize name, resource def initialize name, resource
super super
if name.to_s.empty? || name == '__UNKNOWN__'
@tarball_path = Pathname.new("#{HOMEBREW_CACHE}/#{basename_without_params}")
else
@tarball_path = Pathname.new("#{HOMEBREW_CACHE}/#{name}-#{resource.version}#{ext}")
end
@mirrors = resource.mirrors @mirrors = resource.mirrors
@temporary_path = Pathname.new("#@tarball_path.incomplete") end
def tarball_path
@tarball_path ||= if name.to_s.empty? || name == '__UNKNOWN__'
Pathname.new("#{HOMEBREW_CACHE}/#{basename_without_params}")
else
Pathname.new("#{HOMEBREW_CACHE}/#{name}-#{resource.version}#{ext}")
end
end
def temporary_path
@temporary_path ||= Pathname.new("#{tarball_path}.incomplete")
end end
def cached_location def cached_location
@tarball_path tarball_path
end end
def downloaded_size def downloaded_size
@temporary_path.size? or 0 temporary_path.size? or 0
end end
# Private method, can be overridden if needed. # Private method, can be overridden if needed.
def _fetch def _fetch
curl @url, '-C', downloaded_size, '-o', @temporary_path curl @url, '-C', downloaded_size, '-o', temporary_path
end end
def fetch def fetch
ohai "Downloading #{@url}" ohai "Downloading #{@url}"
unless @tarball_path.exist? unless tarball_path.exist?
had_incomplete_download = @temporary_path.exist? had_incomplete_download = temporary_path.exist?
begin begin
_fetch _fetch
rescue ErrorDuringExecution rescue ErrorDuringExecution
@ -75,16 +83,16 @@ class CurlDownloadStrategy < AbstractDownloadStrategy
# try wiping the incomplete download and retrying once # try wiping the incomplete download and retrying once
if $?.exitstatus == 33 && had_incomplete_download if $?.exitstatus == 33 && had_incomplete_download
ohai "Trying a full download" ohai "Trying a full download"
@temporary_path.unlink temporary_path.unlink
had_incomplete_download = false had_incomplete_download = false
retry retry
else else
raise CurlDownloadStrategyError, "Download failed: #{@url}" raise CurlDownloadStrategyError, "Download failed: #{@url}"
end end
end end
ignore_interrupts { @temporary_path.rename(@tarball_path) } ignore_interrupts { temporary_path.rename(tarball_path) }
else else
puts "Already downloaded: #{@tarball_path}" puts "Already downloaded: #{tarball_path}"
end end
rescue CurlDownloadStrategyError rescue CurlDownloadStrategyError
raise if @mirrors.empty? raise if @mirrors.empty?
@ -92,15 +100,15 @@ class CurlDownloadStrategy < AbstractDownloadStrategy
@url = @mirrors.shift @url = @mirrors.shift
retry retry
else else
@tarball_path tarball_path
end end
def stage def stage
ohai "Pouring #{File.basename(@tarball_path)}" if @tarball_path.to_s.match bottle_regex ohai "Pouring #{File.basename(tarball_path)}" if tarball_path.to_s.match bottle_regex
case @tarball_path.compression_type case tarball_path.compression_type
when :zip when :zip
with_system_path { quiet_safe_system 'unzip', {:quiet_flag => '-qq'}, @tarball_path } with_system_path { quiet_safe_system 'unzip', {:quiet_flag => '-qq'}, tarball_path }
chdir chdir
when :gzip_only when :gzip_only
# gunzip writes the compressed data in the location of the original, # gunzip writes the compressed data in the location of the original,
@ -109,7 +117,7 @@ class CurlDownloadStrategy < AbstractDownloadStrategy
with_system_path do with_system_path do
target = File.basename(basename_without_params, ".gz") target = File.basename(basename_without_params, ".gz")
IO.popen("gunzip -f '#{@tarball_path}' -c") do |pipe| IO.popen("gunzip -f '#{tarball_path}' -c") do |pipe|
File.open(target, "wb") do |f| File.open(target, "wb") do |f|
buf = "" buf = ""
f.write(buf) while pipe.read(1024, buf) f.write(buf) while pipe.read(1024, buf)
@ -119,23 +127,23 @@ class CurlDownloadStrategy < AbstractDownloadStrategy
when :gzip, :bzip2, :compress, :tar when :gzip, :bzip2, :compress, :tar
# Assume these are also tarred # Assume these are also tarred
# TODO check if it's really a tar archive # TODO check if it's really a tar archive
with_system_path { safe_system 'tar', 'xf', @tarball_path } with_system_path { safe_system 'tar', 'xf', tarball_path }
chdir chdir
when :xz when :xz
raise "You must install XZutils: brew install xz" unless File.executable? xzpath raise "You must install XZutils: brew install xz" unless File.executable? xzpath
with_system_path { safe_system "#{xzpath} -dc \"#{@tarball_path}\" | tar xf -" } with_system_path { safe_system "#{xzpath} -dc \"#{tarball_path}\" | tar xf -" }
chdir chdir
when :pkg when :pkg
safe_system '/usr/sbin/pkgutil', '--expand', @tarball_path, basename_without_params safe_system '/usr/sbin/pkgutil', '--expand', tarball_path, basename_without_params
chdir chdir
when :rar when :rar
raise "You must install unrar: brew install unrar" unless which "unrar" raise "You must install unrar: brew install unrar" unless which "unrar"
quiet_safe_system 'unrar', 'x', {:quiet_flag => '-inul'}, @tarball_path quiet_safe_system 'unrar', 'x', {:quiet_flag => '-inul'}, tarball_path
when :p7zip when :p7zip
raise "You must install 7zip: brew install p7zip" unless which "7zr" raise "You must install 7zip: brew install p7zip" unless which "7zr"
safe_system '7zr', 'x', @tarball_path safe_system '7zr', 'x', tarball_path
else else
FileUtils.cp @tarball_path, basename_without_params FileUtils.cp tarball_path, basename_without_params
end end
end end
@ -186,7 +194,7 @@ class CurlApacheMirrorDownloadStrategy < CurlDownloadStrategy
url = mirrors.fetch('preferred') + mirrors.fetch('path_info') url = mirrors.fetch('preferred') + mirrors.fetch('path_info')
ohai "Best Mirror #{url}" ohai "Best Mirror #{url}"
curl url, '-C', downloaded_size, '-o', @temporary_path curl url, '-C', downloaded_size, '-o', temporary_path
rescue IndexError, Utils::JSON::Error rescue IndexError, Utils::JSON::Error
raise "Couldn't determine mirror. Try again later." raise "Couldn't determine mirror. Try again later."
end end
@ -197,14 +205,14 @@ end
class CurlPostDownloadStrategy < CurlDownloadStrategy class CurlPostDownloadStrategy < CurlDownloadStrategy
def _fetch def _fetch
base_url,data = @url.split('?') base_url,data = @url.split('?')
curl base_url, '-d', data, '-C', downloaded_size, '-o', @temporary_path curl base_url, '-d', data, '-C', downloaded_size, '-o', temporary_path
end end
end end
# Download from an SSL3-only host. # Download from an SSL3-only host.
class CurlSSL3DownloadStrategy < CurlDownloadStrategy class CurlSSL3DownloadStrategy < CurlDownloadStrategy
def _fetch def _fetch
curl @url, '-3', '-C', downloaded_size, '-o', @temporary_path curl @url, '-3', '-C', downloaded_size, '-o', temporary_path
end end
end end
@ -212,7 +220,7 @@ end
# Useful for installing jars. # Useful for installing jars.
class NoUnzipCurlDownloadStrategy < CurlDownloadStrategy class NoUnzipCurlDownloadStrategy < CurlDownloadStrategy
def stage def stage
FileUtils.cp @tarball_path, basename_without_params FileUtils.cp tarball_path, basename_without_params
end end
end end
@ -221,7 +229,7 @@ end
# the formula. # the formula.
class CurlUnsafeDownloadStrategy < CurlDownloadStrategy class CurlUnsafeDownloadStrategy < CurlDownloadStrategy
def _fetch def _fetch
curl @url, '--insecure', '-C', downloaded_size, '-o', @temporary_path curl @url, '--insecure', '-C', downloaded_size, '-o', temporary_path
end end
end end
@ -229,10 +237,13 @@ end
class CurlBottleDownloadStrategy < CurlDownloadStrategy class CurlBottleDownloadStrategy < CurlDownloadStrategy
def initialize name, resource def initialize name, resource
super super
@tarball_path = HOMEBREW_CACHE/"#{name}-#{resource.version}#{ext}"
mirror = ENV['HOMEBREW_SOURCEFORGE_MIRROR'] mirror = ENV['HOMEBREW_SOURCEFORGE_MIRROR']
@url = "#{@url}?use_mirror=#{mirror}" if mirror @url = "#{@url}?use_mirror=#{mirror}" if mirror
end end
def tarball_path
@tarball_path ||= HOMEBREW_CACHE/"#{name}-#{resource.version}#{ext}"
end
end end
# This strategy extracts local binary packages. # This strategy extracts local binary packages.