Class: Bintray Private

Inherits:
Object
  • Object
show all
Includes:
Context
Defined in:
Library/Homebrew/bintray.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.

Bintray API client.

Defined Under Namespace

Classes: Error

Constant Summary collapse

API_URL =

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

"https://api.bintray.com"

Instance Method Summary collapse

Methods included from Context

current, current=, #debug?, #quiet?, #verbose?, #with_context

Constructor Details

#initialize(org: "homebrew") ⇒ Bintray

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 a new instance of Bintray.

Raises:


21
22
23
24
25
26
27
# File 'Library/Homebrew/bintray.rb', line 21

def initialize(org: "homebrew")
  @bintray_org = org

  raise UsageError, "Must set a Bintray organisation!" unless @bintray_org

  ENV["HOMEBREW_FORCE_HOMEBREW_ON_LINUX"] = "1" if @bintray_org == "homebrew" && !OS.mac?
end

Instance Method Details

#create_package(repo:, package:, **extra_data_args) ⇒ Object

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.


109
110
111
112
113
114
115
# File 'Library/Homebrew/bintray.rb', line 109

def create_package(repo:, package:, **extra_data_args)
  url = "#{API_URL}/packages/#{@bintray_org}/#{repo}"
  data = { name: package, public_download_numbers: true }
  data[:public_stats] = official_org?
  data.merge! extra_data_args
  open_api url, "--header", "Content-Type: application/json", "--request", "POST", "--data", data.to_json
end

#file_published?(repo:, remote_file:) ⇒ 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)

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'Library/Homebrew/bintray.rb', line 134

def file_published?(repo:, remote_file:)
  url = "https://dl.bintray.com/#{@bintray_org}/#{repo}/#{remote_file}"
  begin
    curl "--fail", "--silent", "--head", "--output", "/dev/null", url
  rescue ErrorDuringExecution => e
    stderr = e.output
              .select { |type,| type == :stderr }
              .map { |_, line| line }
              .join
    raise if e.status.exitstatus != 22 && !stderr.include?("404 Not Found")

    false
  else
    true
  end
end

#inspectObject

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.


17
18
19
# File 'Library/Homebrew/bintray.rb', line 17

def inspect
  "#<Bintray: org=#{@bintray_org}>"
end

#mirror_formula(formula, repo: "mirror", publish_package: false) ⇒ Object

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.


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'Library/Homebrew/bintray.rb', line 80

def mirror_formula(formula, repo: "mirror", publish_package: false)
  package = Utils::Bottles::Bintray.package formula.name

  create_package(repo: repo, package: package) unless package_exists?(repo: repo, package: package)

  formula.downloader.fetch

  version = ERB::Util.url_encode(formula.pkg_version)
  filename = ERB::Util.url_encode(formula.downloader.basename)
  destination_url = "https://dl.bintray.com/#{@bintray_org}/#{repo}/#{filename}"

  odebug "Uploading to #{destination_url}"

  upload(
    formula.downloader.cached_location,
    repo:        repo,
    package:     package,
    version:     version,
    sha256:      formula.stable.checksum,
    remote_file: filename,
  )
  return destination_url unless publish_package

  odebug "Publishing #{@bintray_org}/#{repo}/#{package}/#{version}"
  publish(repo: repo, package: package, version: version, file_count: 1)

  destination_url
end

#official_org?(org: @bintray_org) ⇒ 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)

70
71
72
# File 'Library/Homebrew/bintray.rb', line 70

def official_org?(org: @bintray_org)
  %w[homebrew linuxbrew].include? org
end

#open_api(url, *extra_curl_args, auth: true) ⇒ Object

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.


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'Library/Homebrew/bintray.rb', line 29

def open_api(url, *extra_curl_args, auth: true)
  args = extra_curl_args

  if auth
    raise UsageError, "HOMEBREW_BINTRAY_USER is unset." unless (user = Homebrew::EnvConfig.bintray_user)
    raise UsageError, "HOMEBREW_BINTRAY_KEY is unset." unless (key = Homebrew::EnvConfig.bintray_key)

    args += ["--user", "#{user}:#{key}"]
  end

  curl(*args, url,
       show_output: verbose?,
       secrets:     key)
end

#package_exists?(repo:, 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)

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'Library/Homebrew/bintray.rb', line 117

def package_exists?(repo:, package:)
  url = "#{API_URL}/packages/#{@bintray_org}/#{repo}/#{package}"
  begin
    open_api url, "--fail", "--silent", "--output", "/dev/null", auth: false
  rescue ErrorDuringExecution => e
    stderr = e.output
              .select { |type,| type == :stderr }
              .map { |_, line| line }
              .join
    raise if e.status.exitstatus != 22 && !stderr.include?("404 Not Found")

    false
  else
    true
  end
end

#publish(repo:, package:, version:, file_count:, warn_on_error: false) ⇒ Object

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.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'Library/Homebrew/bintray.rb', line 55

def publish(repo:, package:, version:, file_count:, warn_on_error: false)
  url = "#{API_URL}/content/#{@bintray_org}/#{repo}/#{package}/#{version}/publish"
  result = open_api url, "--request", "POST", "--fail"
  json = JSON.parse(result.stdout)
  if file_count.present? && json["files"] != file_count
    message = "Bottle publish failed: expected #{file_count} bottles, but published #{json["files"]} instead."
    raise message unless warn_on_error

    opoo message
  end

  odebug "Published #{json["files"]} bottles"
  result
end

#stable_mirrored?(url) ⇒ 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)

74
75
76
77
78
# File 'Library/Homebrew/bintray.rb', line 74

def stable_mirrored?(url)
  headers, = curl_output("--connect-timeout", "15", "--location", "--head", url)
  status_code = headers.scan(%r{^HTTP/.* (\d+)}).last.first
  status_code.start_with?("2")
end

#upload(local_file, repo:, package:, version:, remote_file:, sha256: nil) ⇒ Object

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.


44
45
46
47
48
49
50
51
52
53
# File 'Library/Homebrew/bintray.rb', line 44

def upload(local_file, repo:, package:, version:, remote_file:, sha256: nil)
  url = "#{API_URL}/content/#{@bintray_org}/#{repo}/#{package}/#{version}/#{remote_file}"
  args = ["--fail", "--upload-file", local_file]
  args += ["--header", "X-Checksum-Sha2: #{sha256}"] unless sha256.blank?
  result = open_api url, *args
  json = JSON.parse(result.stdout)
  raise "Bottle upload failed: #{json["message"]}" if json["message"] != "success"

  result
end

#upload_bottle_json(json_files, publish_package: false, warn_on_error: false) ⇒ Object

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.


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'Library/Homebrew/bintray.rb', line 151

def upload_bottle_json(json_files, publish_package: false, warn_on_error: false)
  bottles_hash = json_files.reduce({}) do |hash, json_file|
    hash.deep_merge(JSON.parse(IO.read(json_file)))
  end

  formula_packaged = {}

  bottles_hash.each do |formula_name, bottle_hash|
    version = ERB::Util.url_encode(bottle_hash["formula"]["pkg_version"])
    bintray_package = bottle_hash["bintray"]["package"]
    bintray_repo = bottle_hash["bintray"]["repository"]

    bottle_hash["bottle"]["tags"].each do |_tag, tag_hash|
      filename = tag_hash["filename"] # URL encoded in Bottle::Filename#bintray
      sha256 = tag_hash["sha256"]

      odebug "Checking remote file #{@bintray_org}/#{bintray_repo}/#{filename}"
      if file_published? repo: bintray_repo, remote_file: filename
        already_published = "#{filename} is already published."
        failed_message = <<~EOS
          #{already_published}
          Please remove it manually from:
            https://bintray.com/#{@bintray_org}/#{bintray_repo}/#{bintray_package}/view#files
          Or run:
            curl -X DELETE -u $HOMEBREW_BINTRAY_USER:$HOMEBREW_BINTRAY_KEY \\
            https://api.bintray.com/content/#{@bintray_org}/#{bintray_repo}/#{filename}
        EOS
        raise Error, failed_message unless warn_on_error

        opoo already_published
        next
      end

      if !formula_packaged[formula_name] && !package_exists?(repo: bintray_repo, package: bintray_package)
        odebug "Creating package #{@bintray_org}/#{bintray_repo}/#{bintray_package}"
        create_package repo: bintray_repo, package: bintray_package
        formula_packaged[formula_name] = true
      end

      odebug "Uploading #{@bintray_org}/#{bintray_repo}/#{bintray_package}/#{version}/#{filename}"
      upload(tag_hash["local_filename"],
             repo:        bintray_repo,
             package:     bintray_package,
             version:     version,
             remote_file: filename,
             sha256:      sha256)
    end
    next unless publish_package

    bottle_count = bottle_hash["bottle"]["tags"].length
    odebug "Publishing #{@bintray_org}/#{bintray_repo}/#{bintray_package}/#{version}"
    publish(repo:          bintray_repo,
            package:       bintray_package,
            version:       version,
            file_count:    bottle_count,
            warn_on_error: warn_on_error)
  end
end