aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Contributions/cmd/brew-mirror-check.rb
blob: 4fa0defa01b5bc92618569c2674aac0a1729ec33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'formula'

class Formula
  def test_mirror mirror
    # Checksum verification is done against @active_spec, so we need only
    # populate the stub spec object with the mirror URL.
    spec = SoftwareSpec.new(mirror)
    downloader = download_strategy.new(name, spec)

    # Force the downloader to attempt the download by removing the tarball if
    # it is allready cached.
    cached_download.unlink if cached_download.exist?

    downloader.fetch
  rescue StandardError
    opoo "Failed to fetch from URL: #{url}"
  else
    verify_download_integrity(cached_download) if cached_download.file?
  end
end

module Homebrew extend self
  def check_mirrors
    mirror_check_usage = <<-EOS
Usage: brew mirror-check <formulae ...>

Cycle through mirror lists for each formula, attempt a download and validate file hashes.
    EOS

    if ARGV.empty?
      puts mirror_check_usage
      exit 0
    end

    formulae = ARGV.formulae
    raise FormulaUnspecifiedError if formulae.empty?

    formulae.each do |f|
      if f.mirrors.empty?
        opoo "#{f.name} has no mirrors"
        next
      else
        oh1 "Testing mirrors for #{f.name}"
        f.mirrors.each{ |m| f.test_mirror m }
      end
    end
  end
end

# Here is the actual code that gets run when `brew` loads this external
# command.
Homebrew.check_mirrors