Skip to content

Commit

Permalink
optimize fetch to use local download
Browse files Browse the repository at this point in the history
if source is already downloaded and can be verified, skip downloading again.
  • Loading branch information
h0tw1r3 committed Apr 17, 2024
1 parent 7e96527 commit 09e250a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/vanagon/component/source/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ def initialize(url, sum:, workdir:, sum_type:, **options)
# Download the source from the url specified. Sets the full path to the
# file as @file and the @extension for the file as a side effect.
def fetch
@file = File.basename(URI.parse(@url).path)
if File.exist?(File.join(workdir, file))
begin
return if verify
rescue RuntimeError, Errno::ENOENT
# ignore invalid "cached" file
end
end
remove_instance_variable(:@file)

@file = download(@url)
end

Expand All @@ -93,9 +103,13 @@ def file
#
# @raise [RuntimeError] an exception is raised if the sum does not match the sum of the file
def verify
return true if @verified

VanagonLogger.info "Verifying file: #{file} against sum: '#{sum}'"
actual = get_sum(File.join(workdir, file), sum_type)
return true if sum == actual

@verified = (sum == actual)
return true if @verified

fail "Unable to verify '#{File.join(workdir, file)}': #{sum_type} mismatch (expected '#{sum}', got '#{actual}')"
end
Expand Down

0 comments on commit 09e250a

Please sign in to comment.