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
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
ohai "Downloading #{@url}"
if @unique_token
@dl=HOMEBREW_CACHE+(@unique_token+ext)
else
@dl=HOMEBREW_CACHE+File.basename(@url)
end
unless @dl.exist?
unless @tarball_path.exist?
begin
curl @url, '-o', @dl
curl @url, '-o', @tarball_path
rescue Exception
ignore_interrupts { @dl.unlink if @dl.exist? }
ignore_interrupts { @tarball_path.unlink if @tarball_path.exist? }
raise
end
else
puts "File already downloaded and cached to #{HOMEBREW_CACHE}"
end
return @dl # thus performs checksum verification
return @tarball_path # thus performs checksum verification
end
def stage
# magic numbers stolen from /usr/share/file/magic/
if @dl.extname == '.jar'
if @tarball_path.extname == '.jar'
magic_bytes = nil
else
# 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
# magic numbers stolen from /usr/share/file/magic/
case magic_bytes
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
when /^\037\213/, /^BZh/, /^\037\235/ # gzip/bz2/compress compressed
# 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
else
# 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
# We also do this for jar files, as they are in fact zip files, but
# we don't want to unzip them
FileUtils.mv @dl, File.basename(@url)
FileUtils.mv @tarball_path, File.basename(@url)
end
end
@ -130,7 +134,7 @@ end
# Useful for installing jars.
class NoUnzipCurlDownloadStrategy <CurlDownloadStrategy
def stage
FileUtils.mv @dl, File.basename(@url)
FileUtils.mv @tarball_path, File.basename(@url)
end
end

View File

@ -112,6 +112,8 @@ class Formula
CHECKSUM_TYPES.each do |type|
set_instance_variable type
end
@downloader=download_strategy.new url, name, version, specs
end
# if the dir is there, but it's empty we consider it not installed
@ -365,12 +367,15 @@ private
end
def stage
ds=download_strategy.new url, name, version, specs
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
ds.stage
@downloader.stage
yield
end
end