brew/Library/Homebrew/utils/repology.rb

83 lines
2.1 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
2020-06-29 10:16:58 -05:00
# frozen_string_literal: true
2020-06-30 11:23:34 -05:00
require "utils/curl"
2020-08-26 09:11:39 +02:00
# Repology API client.
#
# @api private
2020-07-06 03:32:18 +00:00
module Repology
module_function
2020-06-29 09:57:21 -05:00
MAX_PAGINATION = 15
2020-08-26 09:11:39 +02:00
private_constant :MAX_PAGINATION
def query_api(last_package_in_response = "")
last_package_in_response += "/" if last_package_in_response.present?
url = "https://repology.org/api/v1/projects/#{last_package_in_response}?inrepo=homebrew&outdated=1"
2020-06-29 09:51:58 -05:00
output, _errors, _status = curl_output(url.to_s)
JSON.parse(output)
2020-06-29 09:57:21 -05:00
end
2020-06-29 09:51:58 -05:00
def single_package_query(name)
url = %W[
https://repology.org/tools/project-by?repo=homebrew&
name_type=srcname&target_page=api_v1_project&name=#{name}
].join
output, _errors, _status = curl_output("--location", url.to_s)
begin
data = JSON.parse(output)
{ name => data }
rescue
nil
end
end
2020-08-18 22:18:03 +00:00
def parse_api_response(limit = nil)
2020-06-30 10:11:56 -05:00
ohai "Querying outdated packages from Repology"
2020-06-29 09:51:58 -05:00
2020-08-18 22:25:17 +00:00
page_no = 1
outdated_packages = {}
last_package_index = ""
2020-06-29 09:51:58 -05:00
while page_no <= MAX_PAGINATION
2020-07-06 09:08:41 -05:00
odebug "Paginating Repology API page: #{page_no}"
2020-06-29 09:51:58 -05:00
response = query_api(last_package_index.to_s)
2020-06-29 09:57:21 -05:00
response_size = response.size
outdated_packages.merge!(response)
2020-07-02 13:34:47 -05:00
last_package_index = outdated_packages.size - 1
page_no += 1
break if limit && outdated_packages.size >= limit || response_size <= 1
2020-06-29 09:51:58 -05:00
end
2020-08-18 22:25:17 +00:00
puts "#{outdated_packages.size} outdated #{"package".pluralize(outdated_packages.size)} found"
puts
2020-06-29 09:57:21 -05:00
outdated_packages
2020-06-29 09:51:58 -05:00
end
2021-01-11 08:29:34 +05:30
def latest_version(repositories)
# The status is "unique" when the package is present only in Homebrew, so Repology
# has no way of knowing if the package is up-to-date.
2021-01-11 08:29:34 +05:30
is_unique = repositories.find do |repo|
repo["status"] == "unique"
end.present?
2021-01-11 08:29:34 +05:30
return "present only in Homebrew" if is_unique
2021-01-11 08:29:34 +05:30
latest_version = repositories.find do |repo|
repo["status"] == "newest"
end
# Repology cannot identify "newest" versions for packages without a version scheme
2021-01-11 08:29:34 +05:30
return "no latest version" if latest_version.blank?
2021-01-11 08:29:34 +05:30
latest_version["version"]
end
2020-06-29 09:51:58 -05:00
end