From 24523f82250787e243fd2fc2ec9854ed43bd0d73 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 22 Oct 2020 10:01:40 -0400 Subject: [PATCH 1/4] utils/spdx: allow other license symbols --- Library/Homebrew/formula_installer.rb | 11 ++++++----- Library/Homebrew/test/utils/spdx_spec.rb | 22 +++++++++++++++++++++- Library/Homebrew/utils/spdx.rb | 14 +++++++++----- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index 1ff0ab1712..f8b2d12887 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -1139,11 +1139,12 @@ class FormulaInstaller end def forbidden_license_check - forbidden_licenses = Homebrew::EnvConfig.forbidden_licenses - .to_s - .sub("Public Domain", "public_domain") - .split(" ") - .to_h do |license| + forbidden_licenses = Homebrew::EnvConfig.forbidden_licenses.to_s + SPDX::ALLOWED_LICENSE_SYMBOLS.each do |s| + pattern = /#{s.to_s.tr("_", " ")}/i + forbidden_licenses = forbidden_licenses.sub(pattern, s.to_s) + end + forbidden_licenses = forbidden_licenses.split(" ").to_h do |license| [license, SPDX.license_version_info(license)] end diff --git a/Library/Homebrew/test/utils/spdx_spec.rb b/Library/Homebrew/test/utils/spdx_spec.rb index 5530a27b4e..40045647f9 100644 --- a/Library/Homebrew/test/utils/spdx_spec.rb +++ b/Library/Homebrew/test/utils/spdx_spec.rb @@ -91,6 +91,10 @@ describe SPDX do it "returns :public_domain" do expect(described_class.parse_license_expression(:public_domain).first).to eq [:public_domain] end + + it "returns :cannot_represent" do + expect(described_class.parse_license_expression(:cannot_represent).first).to eq [:cannot_represent] + end end describe ".valid_license?" do @@ -113,6 +117,14 @@ describe SPDX do it "returns true for :public_domain" do expect(described_class.valid_license?(:public_domain)).to eq true end + + it "returns true for :cannot_represent" do + expect(described_class.valid_license?(:cannot_represent)).to eq true + end + + it "returns false for invalid symbol" do + expect(described_class.valid_license?(:invalid_symbol)).to eq false + end end describe ".deprecated_license?" do @@ -131,6 +143,10 @@ describe SPDX do it "returns false for :public_domain" do expect(described_class.deprecated_license?(:public_domain)).to eq false end + + it "returns false for :cannot_represent" do + expect(described_class.deprecated_license?(:cannot_represent)).to eq false + end end describe ".valid_license_exception?" do @@ -187,9 +203,13 @@ describe SPDX do it "returns :public_domain" do expect(described_class.license_expression_to_string(:public_domain)).to eq "Public Domain" end + + it "returns :cannot_represent" do + expect(described_class.license_expression_to_string(:cannot_represent)).to eq "Cannot Represent" + end end - describe ".license_version_info_info" do + describe ".license_version_info" do it "returns license without version" do expect(described_class.license_version_info("MIT")).to eq ["MIT"] end diff --git a/Library/Homebrew/utils/spdx.rb b/Library/Homebrew/utils/spdx.rb index 3ae60ea72c..65a307770f 100644 --- a/Library/Homebrew/utils/spdx.rb +++ b/Library/Homebrew/utils/spdx.rb @@ -15,6 +15,10 @@ module SPDX DATA_PATH = (HOMEBREW_DATA_PATH/"spdx").freeze API_URL = "https://api.github.com/repos/spdx/license-list-data/releases/latest" + ALLOWED_LICENSE_SYMBOLS = [ + :public_domain, + :cannot_represent, + ].freeze def license_data @license_data ||= JSON.parse (DATA_PATH/"spdx_licenses.json").read @@ -64,14 +68,14 @@ module SPDX end def valid_license?(license) - return true if license == :public_domain + return ALLOWED_LICENSE_SYMBOLS.include? license if license.is_a? Symbol license = license.delete_suffix "+" license_data["licenses"].any? { |spdx_license| spdx_license["licenseId"] == license } end def deprecated_license?(license) - return false if license == :public_domain + return false if ALLOWED_LICENSE_SYMBOLS.include? license return false unless valid_license?(license) license_data["licenses"].none? do |spdx_license| @@ -89,8 +93,8 @@ module SPDX case license_expression when String license_expression - when :public_domain - "Public Domain" + when Symbol + license_expression.to_s.tr("_", " ").titleize when Hash expressions = [] @@ -125,7 +129,7 @@ module SPDX end def license_version_info(license) - return [license] if license == :public_domain + return [license] if ALLOWED_LICENSE_SYMBOLS.include? license match = license.match(/-(?[0-9.]+)(?:-.*?)??(?\+|-only|-or-later)?$/) return [license] if match.blank? From e459056d2718b661b249fd20fb9a88176020889c Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 22 Oct 2020 10:24:12 -0400 Subject: [PATCH 2/4] docs: add reference to :cannot_represent license --- docs/License-Guidelines.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/License-Guidelines.md b/docs/License-Guidelines.md index 13d7ea8773..2b626b31cf 100644 --- a/docs/License-Guidelines.md +++ b/docs/License-Guidelines.md @@ -18,6 +18,12 @@ The public domain can be indicated using a symbol: license :public_domain ``` +If the license for a formula cannot be represented using an SPDX expression: + +```ruby +license :cannot_represent +``` + ## Complex SPDX License Expressions Some formulae have multiple licenses that need to be combined in different ways. In these cases, a more complex license expression can be used. These expressions are based on the [SPDX License Expression Guidelines](https://spdx.github.io/spdx-spec/appendix-IV-SPDX-license-expressions/). From b06bcf3db154cfb27fc7ce240136719284e95b09 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 22 Oct 2020 11:22:06 -0400 Subject: [PATCH 3/4] formula_installer: unfreeze forbidden licenses string --- Library/Homebrew/formula_installer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index f8b2d12887..d80004daea 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -1139,10 +1139,10 @@ class FormulaInstaller end def forbidden_license_check - forbidden_licenses = Homebrew::EnvConfig.forbidden_licenses.to_s + forbidden_licenses = Homebrew::EnvConfig.forbidden_licenses.dup SPDX::ALLOWED_LICENSE_SYMBOLS.each do |s| pattern = /#{s.to_s.tr("_", " ")}/i - forbidden_licenses = forbidden_licenses.sub(pattern, s.to_s) + forbidden_licenses.sub!(pattern, s.to_s) end forbidden_licenses = forbidden_licenses.split(" ").to_h do |license| [license, SPDX.license_version_info(license)] From 42d75c2787e3e68d81b23865a67111c48a670d9e Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 22 Oct 2020 15:15:56 -0400 Subject: [PATCH 4/4] formula_installer: fix forbidden license check Convert forbidden licenses to a string before duplicating to have empty strings instead of `nil`. --- Library/Homebrew/formula_installer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index d80004daea..9911f18d27 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -1139,7 +1139,7 @@ class FormulaInstaller end def forbidden_license_check - forbidden_licenses = Homebrew::EnvConfig.forbidden_licenses.dup + forbidden_licenses = Homebrew::EnvConfig.forbidden_licenses.to_s.dup SPDX::ALLOWED_LICENSE_SYMBOLS.each do |s| pattern = /#{s.to_s.tr("_", " ")}/i forbidden_licenses.sub!(pattern, s.to_s)