Class: PyPI::Package Private

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
brew/Library/Homebrew/utils/pypi.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

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


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'brew/Library/Homebrew/utils/pypi.rb', line 28

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 = match[1]
    @version = match[2]
    return
  end

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

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

  @name = match[1]
  @extras = match[2].split ","
end

Instance Attribute Details

#extrasObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
# File 'brew/Library/Homebrew/utils/pypi.rb', line 24

def extras
  @extras
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'brew/Library/Homebrew/utils/pypi.rb', line 23

def name
  @name
end

#versionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'brew/Library/Homebrew/utils/pypi.rb', line 25

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Integer?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (Integer, nil)


104
105
106
# File 'brew/Library/Homebrew/utils/pypi.rb', line 104

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

#==(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

Returns:

  • (Boolean)


99
100
101
# File 'brew/Library/Homebrew/utils/pypi.rb', line 99

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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'brew/Library/Homebrew/utils/pypi.rb', line 53

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"

  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 = [json["info"]["name"], sdist["url"], sdist["digests"]["sha256"], json["info"]["version"]]
end

#same_package?(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (Boolean)


93
94
95
# File 'brew/Library/Homebrew/utils/pypi.rb', line 93

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

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



85
86
87
88
89
90
# File 'brew/Library/Homebrew/utils/pypi.rb', line 85

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. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


79
80
81
82
# File 'brew/Library/Homebrew/utils/pypi.rb', line 79

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