Merge pull request #8215 from SeekingMeaning/spdx/util

utils: add SPDX module
This commit is contained in:
Mike McQuaid 2020-08-07 10:38:45 +01:00 committed by GitHub
commit 4c23fd0156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 11 deletions

View File

@ -4,6 +4,7 @@ require "formula"
require "formula_versions" require "formula_versions"
require "utils/curl" require "utils/curl"
require "utils/notability" require "utils/notability"
require "utils/spdx"
require "extend/ENV" require "extend/ENV"
require "formula_cellar_checks" require "formula_cellar_checks"
require "cmd/search" require "cmd/search"
@ -118,8 +119,7 @@ module Homebrew
# Check style in a single batch run up front for performance # Check style in a single batch run up front for performance
style_results = Style.check_style_json(style_files, options) if style_files style_results = Style.check_style_json(style_files, options) if style_files
# load licenses # load licenses
spdx = HOMEBREW_LIBRARY_PATH/"data/spdx.json" spdx_data = SPDX.spdx_data
spdx_data = JSON.parse(spdx.read)
new_formula_problem_lines = [] new_formula_problem_lines = []
audit_formulae.sort.each do |f| audit_formulae.sort.each do |f|
only = only_cops ? ["style"] : args.only only = only_cops ? ["style"] : args.only

View File

@ -1,14 +1,11 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cli/parser" require "cli/parser"
require "utils/github" require "utils/spdx"
module Homebrew module Homebrew
module_function module_function
SPDX_PATH = (HOMEBREW_LIBRARY_PATH/"data/spdx.json").freeze
SPDX_API_URL = "https://api.github.com/repos/spdx/license-list-data/releases/latest"
def update_license_data_args def update_license_data_args
Homebrew::CLI::Parser.new do Homebrew::CLI::Parser.new do
usage_banner <<~EOS usage_banner <<~EOS
@ -29,16 +26,14 @@ module Homebrew
args = update_license_data_args.parse args = update_license_data_args.parse
ohai "Updating SPDX license data..." ohai "Updating SPDX license data..."
latest_tag = GitHub.open_api(SPDX_API_URL)["tag_name"] SPDX.download_latest_license_data!
data_url = "https://raw.githubusercontent.com/spdx/license-list-data/#{latest_tag}/json/licenses.json"
curl_download(data_url, to: SPDX_PATH, partial: false)
Homebrew.failed = system("git", "diff", "--stat", "--exit-code", SPDX_PATH) if args.fail_if_not_changed? Homebrew.failed = system("git", "diff", "--stat", "--exit-code", SPDX::JSON_PATH) if args.fail_if_not_changed?
return unless args.commit? return unless args.commit?
ohai "git add" ohai "git add"
safe_system "git", "add", SPDX_PATH safe_system "git", "add", SPDX::JSON_PATH
ohai "git commit" ohai "git commit"
system "git", "commit", "--message", "data/spdx.json: update to #{latest_tag}" system "git", "commit", "--message", "data/spdx.json: update to #{latest_tag}"
end end

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
require "utils/spdx"
describe SPDX do
describe ".spdx_data" do
it "has the license list version" do
expect(described_class.spdx_data["licenseListVersion"]).not_to eq(nil)
end
it "has the release date" do
expect(described_class.spdx_data["releaseDate"]).not_to eq(nil)
end
it "has licenses" do
expect(described_class.spdx_data["licenses"].length).not_to eq(0)
end
end
describe ".download_latest_license_data!", :needs_network do
let(:tmp_json_path) { Pathname.new("#{TEST_TMPDIR}/spdx.json") }
after do
FileUtils.rm tmp_json_path
end
it "downloads latest license data" do
described_class.download_latest_license_data! to: tmp_json_path
expect(tmp_json_path).to exist
end
end
end

View File

@ -0,0 +1,20 @@
# frozen_string_literal: true
require "utils/github"
module SPDX
module_function
JSON_PATH = (HOMEBREW_LIBRARY_PATH/"data/spdx.json").freeze
API_URL = "https://api.github.com/repos/spdx/license-list-data/releases/latest"
def spdx_data
@spdx_data ||= JSON.parse(JSON_PATH.read)
end
def download_latest_license_data!(to: JSON_PATH)
latest_tag = GitHub.open_api(API_URL)["tag_name"]
data_url = "https://raw.githubusercontent.com/spdx/license-list-data/#{latest_tag}/json/licenses.json"
curl_download(data_url, to: to, partial: false)
end
end