Create Download Strategy sooner in formula install code.

* Instantiate DownloadStrategy instance when creating a formula.
* Rename CurlDownloadStrategy member to clarify what it is for.
* Generate downloaded tarball name in initialize.
This commit is contained in:
Adam Vandenberg 2010-01-16 17:54:14 -08:00
parent 43dc7c9645
commit 6d06b9a179
2 changed files with 29 additions and 20 deletions

View File

@ -53,42 +53,46 @@ class AbstractDownloadStrategy
end end
class CurlDownloadStrategy <AbstractDownloadStrategy class CurlDownloadStrategy <AbstractDownloadStrategy
def initialize url, name, version, specs
super
if @unique_token
@tarball_path=HOMEBREW_CACHE+(@unique_token+ext)
else
@tarball_path=HOMEBREW_CACHE+File.basename(@url)
end
end
def fetch def fetch
ohai "Downloading #{@url}" ohai "Downloading #{@url}"
if @unique_token unless @tarball_path.exist?
@dl=HOMEBREW_CACHE+(@unique_token+ext)
else
@dl=HOMEBREW_CACHE+File.basename(@url)
end
unless @dl.exist?
begin begin
curl @url, '-o', @dl curl @url, '-o', @tarball_path
rescue Exception rescue Exception
ignore_interrupts { @dl.unlink if @dl.exist? } ignore_interrupts { @tarball_path.unlink if @tarball_path.exist? }
raise raise
end end
else else
puts "File already downloaded and cached to #{HOMEBREW_CACHE}" puts "File already downloaded and cached to #{HOMEBREW_CACHE}"
end end
return @dl # thus performs checksum verification return @tarball_path # thus performs checksum verification
end end
def stage def stage
# magic numbers stolen from /usr/share/file/magic/ if @tarball_path.extname == '.jar'
if @dl.extname == '.jar'
magic_bytes = nil magic_bytes = nil
else else
# get the first four bytes # get the first four bytes
File.open(@dl) { |f| magic_bytes = f.read(4) } File.open(@tarball_path) { |f| magic_bytes = f.read(4) }
end end
# magic numbers stolen from /usr/share/file/magic/
case magic_bytes case magic_bytes
when /^PK\003\004/ # .zip archive when /^PK\003\004/ # .zip archive
quiet_safe_system '/usr/bin/unzip', {:quiet_flag => '-qq'}, @dl quiet_safe_system '/usr/bin/unzip', {:quiet_flag => '-qq'}, @tarball_path
chdir chdir
when /^\037\213/, /^BZh/, /^\037\235/ # gzip/bz2/compress compressed when /^\037\213/, /^BZh/, /^\037\235/ # gzip/bz2/compress compressed
# TODO check if it's really a tar archive # TODO check if it's really a tar archive
safe_system '/usr/bin/tar', 'xf', @dl safe_system '/usr/bin/tar', 'xf', @tarball_path
chdir chdir
else else
# we are assuming it is not an archive, use original filename # we are assuming it is not an archive, use original filename
@ -98,7 +102,7 @@ class CurlDownloadStrategy <AbstractDownloadStrategy
# behaviour, just open an issue at github # behaviour, just open an issue at github
# We also do this for jar files, as they are in fact zip files, but # We also do this for jar files, as they are in fact zip files, but
# we don't want to unzip them # we don't want to unzip them
FileUtils.mv @dl, File.basename(@url) FileUtils.mv @tarball_path, File.basename(@url)
end end
end end
@ -130,7 +134,7 @@ end
# Useful for installing jars. # Useful for installing jars.
class NoUnzipCurlDownloadStrategy <CurlDownloadStrategy class NoUnzipCurlDownloadStrategy <CurlDownloadStrategy
def stage def stage
FileUtils.mv @dl, File.basename(@url) FileUtils.mv @tarball_path, File.basename(@url)
end end
end end

View File

@ -112,6 +112,8 @@ class Formula
CHECKSUM_TYPES.each do |type| CHECKSUM_TYPES.each do |type|
set_instance_variable type set_instance_variable type
end end
@downloader=download_strategy.new url, name, version, specs
end end
# if the dir is there, but it's empty we consider it not installed # if the dir is there, but it's empty we consider it not installed
@ -365,12 +367,15 @@ private
end end
def stage def stage
ds=download_strategy.new url, name, version, specs
HOMEBREW_CACHE.mkpath HOMEBREW_CACHE.mkpath
dl=ds.fetch
verify_download_integrity dl if dl.kind_of? Pathname downloaded_tarball = @downloader.fetch
if downloaded_tarball.kind_of? Pathname
verify_download_integrity downloaded_tarball
end
mktemp do mktemp do
ds.stage @downloader.stage
yield yield
end end
end end