Skip to content
Snippets Groups Projects
Unverified Commit 3b035970 authored by Dawid Dziurla's avatar Dawid Dziurla
Browse files

bintray: automatically run brew mirror if needed

parent 5a636365
No related branches found
No related tags found
No related merge requests found
......@@ -56,6 +56,38 @@ class Bintray
%w[homebrew linuxbrew].include? org
end
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
def mirror_formula(formula, repo: "mirror")
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,
)
publish(repo: repo, package: package, version: version)
destination_url
end
def create_package(repo:, package:, **extra_data_args)
url = "#{API_URL}/packages/#{@bintray_org}/#{repo}"
data = { name: package, public_download_numbers: true }
......
......@@ -26,37 +26,12 @@ module Homebrew
mirror_args.parse
bintray_org = args.bintray_org || "homebrew"
bintray_repo = "mirror"
bintray = Bintray.new(org: bintray_org)
args.formulae.each do |f|
bintray_package = Utils::Bottles::Bintray.package f.name
unless bintray.package_exists?(repo: bintray_repo, package: bintray_package)
bintray.create_package repo: bintray_repo, package: bintray_package
end
downloader = f.downloader
downloader.fetch
filename = ERB::Util.url_encode(downloader.basename)
destination_url = "https://dl.bintray.com/#{bintray_org}/#{bintray_repo}/#{filename}"
ohai "Uploading to #{destination_url}"
version = ERB::Util.url_encode(f.pkg_version)
bintray.upload(
downloader.cached_location,
repo: bintray_repo,
package: bintray_package,
version: version,
sha256: f.stable.checksum,
remote_file: filename,
)
bintray.publish(repo: bintray_repo, package: bintray_package, version: version)
ohai "Mirrored #{filename}!"
args.formulae.each do |formula|
mirror_url = bintray.mirror_formula(formula)
ohai "Mirrored #{formula.full_name} to #{mirror_url}!"
end
end
end
......@@ -204,18 +204,40 @@ module Homebrew
url = GitHub.get_artifact_url(user, repo, pr, workflow_id: workflow, artifact_name: artifact)
download_artifact(url, dir, pr)
json_files = Dir["*.json"]
if Homebrew.args.dry_run?
puts "brew bottle --merge --write #{Dir["*.json"].join " "}"
puts "brew bottle --merge --write #{json_files.join " "}"
else
quiet_system "#{HOMEBREW_PREFIX}/bin/brew", "bottle", "--merge", "--write", *Dir["*.json"]
quiet_system "#{HOMEBREW_PREFIX}/bin/brew", "bottle", "--merge", "--write", *json_files
end
next if args.no_upload?
if Homebrew.args.dry_run?
puts "Upload bottles described by these JSON files to Bintray:\n #{Dir["*.json"].join("\n ")}"
puts "Upload bottles described by these JSON files to Bintray:\n #{json_files.join("\n ")}"
else
bintray.upload_bottle_json Dir["*.json"], publish_package: !args.no_publish?
bintray.upload_bottle_json json_files, publish_package: !args.no_publish?
end
bottles_hash = json_files.reduce({}) do |hash, json_file|
hash.deep_merge(JSON.parse(IO.read(json_file)))
end
bottles_hash.each do |formula_name, _|
formula = Formula[formula_name]
stable_urls = [formula.stable.url] + formula.stable.mirrors
stable_urls.grep(%r{^https://dl.bintray.com/homebrew/mirror/}) do |mirror_url|
if Homebrew.args.dry_run?
puts "Mirror formulae sources described by these JSON files to Bintray:\n #{json_files.join("\n ")}"
next
end
next if bintray.stable_mirrored?(mirror_url)
mirror_url = bintray.mirror_formula(formula)
ohai "Mirrored #{formula.full_name} to #{mirror_url}!"
end
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment