Class: PyPI::Package Private

Inherits:
Object show all
Defined in:
utils/pypi.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

PyPI Package

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_string, is_url: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • package_string (String)
  • is_url (Boolean) (defaults to: false)


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
# File 'utils/pypi.rb', line 18

def initialize(package_string, is_url: false)
  @pypi_info = nil

  if is_url
    match = if package_string.start_with?(PYTHONHOSTED_URL_PREFIX)
      File.basename(package_string).match(/^(.+)-([a-z\d.]+?)(?:.tar.gz|.zip)$/)
    end
    raise ArgumentError, "Package should be a valid PyPI URL" if match.blank?

    @name = PyPI.normalize_python_package(match[1])
    @version = match[2]
    return
  end

  if package_string.include? "=="
    @name, @version = package_string.split("==")
  else
    @name = package_string
  end

  return unless (match = T.must(@name).match(/^(.*?)\[(.+)\]$/))

  @name = match[1]
  @extras = T.must(match[2]).split ","
end

Instance Attribute Details

#extrasObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



15
16
17
# File 'utils/pypi.rb', line 15

def extras
  @extras
end

#nameObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



15
16
17
# File 'utils/pypi.rb', line 15

def name
  @name
end

#versionObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



15
16
17
# File 'utils/pypi.rb', line 15

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Integer?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Integer, nil)


106
107
108
# File 'utils/pypi.rb', line 106

def <=>(other)
  @name <=> other.name
end

#==(other) ⇒ Boolean Also known as: eql?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Compare only names so we can use .include? and .uniq on a Package array

Parameters:

Returns:

  • (Boolean)


95
96
97
# File 'utils/pypi.rb', line 95

def ==(other)
  same_package?(other)
end

#hashInteger

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Integer)


101
102
103
# File 'utils/pypi.rb', line 101

def hash
  @name.tr("_", "-").downcase.hash
end

#pypi_info(version: nil) ⇒ Array<String>?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Get name, URL, SHA-256 checksum, and latest version for a given PyPI package.

Parameters:

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'utils/pypi.rb', line 46

def pypi_info(version: nil)
  return @pypi_info if @pypi_info.present? && version.blank?

  version ||= @version
   = if version.present?
    "https://pypi.org/pypi/#{@name}/#{version}/json"
  else
    "https://pypi.org/pypi/#{@name}/json"
  end
  out, _, status = curl_output , "--location", "--fail"

  return unless status.success?

  begin
    json = JSON.parse out
  rescue JSON::ParserError
    return
  end

  sdist = json["urls"].find { |url| url["packagetype"] == "sdist" }
  return json["info"]["name"] if sdist.nil?

  @pypi_info = [
    PyPI.normalize_python_package(json["info"]["name"]), sdist["url"],
    sdist["digests"]["sha256"], json["info"]["version"]
  ]
end

#same_package?(other) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


89
90
91
# File 'utils/pypi.rb', line 89

def same_package?(other)
  T.must(@name.tr("_", "-").casecmp(other.name.tr("_", "-"))).zero?
end

#to_sString

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



81
82
83
84
85
86
# File 'utils/pypi.rb', line 81

def to_s
  out = @name
  out += "[#{@extras.join(",")}]" if @extras.present?
  out += "==#{@version}" if @version.present?
  out
end

#valid_pypi_package?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


75
76
77
78
# File 'utils/pypi.rb', line 75

def valid_pypi_package?
  info = pypi_info
  info.present? && info.is_a?(Array)
end