Class: ResourceAuditor

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/dev-cmd/audit.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, spec_name, options = {}) ⇒ ResourceAuditor

Returns a new instance of ResourceAuditor



828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'Library/Homebrew/dev-cmd/audit.rb', line 828

def initialize(resource, spec_name, options = {})
  @name     = resource.name
  @version  = resource.version
  @checksum = resource.checksum
  @url      = resource.url
  @mirrors  = resource.mirrors
  @using    = resource.using
  @specs    = resource.specs
  @owner    = resource.owner
  @spec_name = spec_name
  @online    = options[:online]
  @strict    = options[:strict]
  @problems  = []
end

Instance Attribute Details

#checksumObject (readonly)

Returns the value of attribute checksum



825
826
827
# File 'Library/Homebrew/dev-cmd/audit.rb', line 825

def checksum
  @checksum
end

#mirrorsObject (readonly)

Returns the value of attribute mirrors



825
826
827
# File 'Library/Homebrew/dev-cmd/audit.rb', line 825

def mirrors
  @mirrors
end

#nameObject (readonly)

Returns the value of attribute name



825
826
827
# File 'Library/Homebrew/dev-cmd/audit.rb', line 825

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner



825
826
827
# File 'Library/Homebrew/dev-cmd/audit.rb', line 825

def owner
  @owner
end

#problemsObject (readonly)

Returns the value of attribute problems



826
827
828
# File 'Library/Homebrew/dev-cmd/audit.rb', line 826

def problems
  @problems
end

#spec_nameObject (readonly)

Returns the value of attribute spec_name



826
827
828
# File 'Library/Homebrew/dev-cmd/audit.rb', line 826

def spec_name
  @spec_name
end

#specsObject (readonly)

Returns the value of attribute specs



825
826
827
# File 'Library/Homebrew/dev-cmd/audit.rb', line 825

def specs
  @specs
end

#urlObject (readonly)

Returns the value of attribute url



825
826
827
# File 'Library/Homebrew/dev-cmd/audit.rb', line 825

def url
  @url
end

#usingObject (readonly)

Returns the value of attribute using



825
826
827
# File 'Library/Homebrew/dev-cmd/audit.rb', line 825

def using
  @using
end

#versionObject (readonly)

Returns the value of attribute version



825
826
827
# File 'Library/Homebrew/dev-cmd/audit.rb', line 825

def version
  @version
end

Class Method Details

.curl_openssl_and_depsObject



914
915
916
917
918
919
920
921
922
923
924
# File 'Library/Homebrew/dev-cmd/audit.rb', line 914

def self.curl_openssl_and_deps
  @curl_openssl_and_deps ||= begin
    formulae_names = ["curl", "openssl"]
    formulae_names += formulae_names.flat_map do |f|
      Formula[f].recursive_dependencies.map(&:name)
    end
    formulae_names.uniq
  rescue FormulaUnavailableError
    []
  end
end

Instance Method Details

#auditObject



843
844
845
846
847
848
# File 'Library/Homebrew/dev-cmd/audit.rb', line 843

def audit
  audit_version
  audit_download_strategy
  audit_urls
  self
end

#audit_download_strategyObject



871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
# File 'Library/Homebrew/dev-cmd/audit.rb', line 871

def audit_download_strategy
  if url =~ %r{^(cvs|bzr|hg|fossil)://} || url =~ %r{^(svn)\+http://}
    problem "Use of the #{$&} scheme is deprecated, pass `:using => :#{Regexp.last_match(1)}` instead"
  end

  url_strategy = DownloadStrategyDetector.detect(url)

  if using == :git || url_strategy == GitDownloadStrategy
    if specs[:tag] && !specs[:revision]
      problem "Git should specify :revision when a :tag is specified."
    end
  end

  return unless using

  if using == :ssl3 || \
     (Object.const_defined?("CurlSSL3DownloadStrategy") && using == CurlSSL3DownloadStrategy)
    problem "The SSL3 download strategy is deprecated, please choose a different URL"
  elsif (Object.const_defined?("CurlUnsafeDownloadStrategy") && using == CurlUnsafeDownloadStrategy) || \
        (Object.const_defined?("UnsafeSubversionDownloadStrategy") && using == UnsafeSubversionDownloadStrategy)
    problem "#{using.name} is deprecated, please choose a different URL"
  end

  if using == :cvs
    mod = specs[:module]

    problem "Redundant :module value in URL" if mod == name

    if url =~ %r{:[^/]+$}
      mod = url.split(":").last

      if mod == name
        problem "Redundant CVS module appended to URL"
      else
        problem "Specify CVS module as `:module => \"#{mod}\"` instead of appending it to the URL"
      end
    end
  end

  return unless url_strategy == DownloadStrategyDetector.detect("", using)
  problem "Redundant :using value in URL"
end

#audit_urlsObject



926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# File 'Library/Homebrew/dev-cmd/audit.rb', line 926

def audit_urls
  urls = [url] + mirrors

  curl_openssl_or_deps = ResourceAuditor.curl_openssl_and_deps.include?(owner.name)

  if spec_name == :stable && curl_openssl_or_deps
    problem "should not use xz tarballs" if url.end_with?(".xz")

    unless urls.find { |u| u.start_with?("http://") }
      problem "should always include at least one HTTP mirror"
    end
  end

  return unless @online
  urls.each do |url|
    next if !@strict && mirrors.include?(url)

    strategy = DownloadStrategyDetector.detect(url, using)
    if strategy <= CurlDownloadStrategy && !url.start_with?("file")
      # A `brew mirror`'ed URL is usually not yet reachable at the time of
      # pull request.
      next if url =~ %r{^https://dl.bintray.com/homebrew/mirror/}
      if http_content_problem = curl_check_http_content(url, require_http: curl_openssl_or_deps)
        problem http_content_problem
      end
    elsif strategy <= GitDownloadStrategy
      unless Utils.git_remote_exists url
        problem "The URL #{url} is not a valid git URL"
      end
    elsif strategy <= SubversionDownloadStrategy
      next unless DevelopmentTools.subversion_handles_most_https_certificates?
      next unless Utils.svn_available?
      unless Utils.svn_remote_exists url
        problem "The URL #{url} is not a valid svn URL"
      end
    end
  end
end

#audit_versionObject



850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# File 'Library/Homebrew/dev-cmd/audit.rb', line 850

def audit_version
  if version.nil?
    problem "missing version"
  elsif version.to_s.empty?
    problem "version is set to an empty string"
  elsif !version.detected_from_url?
    version_text = version
    version_url = Version.detect(url, specs)
    if version_url.to_s == version_text.to_s && version.instance_of?(Version)
      problem "version #{version_text} is redundant with version scanned from URL"
    end
  end

  if version.to_s.start_with?("v")
    problem "version #{version} should not have a leading 'v'"
  end

  return unless version.to_s =~ /_\d+$/
  problem "version #{version} should not end with an underline and a number"
end

#problem(text) ⇒ Object



965
966
967
# File 'Library/Homebrew/dev-cmd/audit.rb', line 965

def problem(text)
  @problems << text
end