Improve/fix HOMEBREW_FORBIDDEN_LICENSES handling

`HOMEBREW_FORBIDDEN_LICENSES` now actually checks for valid SPDX license
identifiers rather than requiring the user to guess.

When an identifier is invalid, it will be ignore and warned about
instead.
This commit is contained in:
Mike McQuaid 2024-10-17 08:34:03 +01:00
parent af958b2540
commit dd7d91bc6f
No known key found for this signature in database
4 changed files with 25 additions and 8 deletions

View File

@ -204,7 +204,7 @@ module Homebrew
"formula or cask if it or any of its dependencies is on this list.", "formula or cask if it or any of its dependencies is on this list.",
}, },
HOMEBREW_FORBIDDEN_LICENSES: { HOMEBREW_FORBIDDEN_LICENSES: {
description: "A space-separated list of licenses. Homebrew will refuse to install a " \ description: "A space-separated list of SPDX license identifiers. Homebrew will refuse to install a " \
"formula if it or any of its dependencies has a license on this list.", "formula if it or any of its dependencies has a license on this list.",
}, },
HOMEBREW_FORBIDDEN_OWNER: { HOMEBREW_FORBIDDEN_OWNER: {

View File

@ -1482,8 +1482,25 @@ on_request: installed_on_request?, options:)
pattern = /#{s.to_s.tr("_", " ")}/i pattern = /#{s.to_s.tr("_", " ")}/i
forbidden_licenses.sub!(pattern, s.to_s) forbidden_licenses.sub!(pattern, s.to_s)
end end
forbidden_licenses = forbidden_licenses.split.to_h do |license|
[license, SPDX.license_version_info(license)] invalid_licenses = []
forbidden_licenses = forbidden_licenses.split.each_with_object({}) do |license, hash|
unless SPDX.valid_license?(license)
invalid_licenses << license
next
end
hash[license] = SPDX.license_version_info(license)
end
if invalid_licenses.present?
opoo <<~EOS
HOMEBREW_FORBIDDEN_LICENSES contains invalid license identifiers: #{invalid_licenses.to_sentence}
These licenses will not be forbidden. See the valid SPDX license identifiers at:
#{Formatter.url("https://spdx.org/licenses/")}
And the licenses for a formula with:
brew info <formula>
EOS
end end
return if forbidden_licenses.blank? return if forbidden_licenses.blank?
@ -1501,7 +1518,7 @@ on_request: installed_on_request?, options:)
raise CannotInstallFormulaError, <<~EOS raise CannotInstallFormulaError, <<~EOS
The installation of #{formula.name} has a dependency on #{dep.name} where all The installation of #{formula.name} has a dependency on #{dep.name} where all
its licenses were forbidden by #{owner} in `HOMEBREW_FORBIDDEN_LICENSES`: its licenses were forbidden by #{owner} in `HOMEBREW_FORBIDDEN_LICENSES`:
#{SPDX.license_expression_to_string dep_f.license}.#{owner_contact} #{SPDX.license_expression_to_string dep_f.license}#{owner_contact}
EOS EOS
end end
end end
@ -1512,7 +1529,7 @@ on_request: installed_on_request?, options:)
raise CannotInstallFormulaError, <<~EOS raise CannotInstallFormulaError, <<~EOS
#{formula.name}'s licenses are all forbidden by #{owner} in `HOMEBREW_FORBIDDEN_LICENSES`: #{formula.name}'s licenses are all forbidden by #{owner} in `HOMEBREW_FORBIDDEN_LICENSES`:
#{SPDX.license_expression_to_string formula.license}.#{owner_contact} #{SPDX.license_expression_to_string formula.license}#{owner_contact}
EOS EOS
end end

View File

@ -3798,8 +3798,8 @@ command execution e.g. `$(cat file)`.
`HOMEBREW_FORBIDDEN_LICENSES` `HOMEBREW_FORBIDDEN_LICENSES`
: A space-separated list of licenses. Homebrew will refuse to install a formula : A space-separated list of SPDX license identifiers. Homebrew will refuse to
if it or any of its dependencies has a license on this list. install a formula if it or any of its dependencies has a license on this list.
`HOMEBREW_FORBIDDEN_OWNER` `HOMEBREW_FORBIDDEN_OWNER`

View File

@ -2470,7 +2470,7 @@ A space\-separated list of casks\. Homebrew will refuse to install a cask if it
A space\-separated list of formulae\. Homebrew will refuse to install a formula or cask if it or any of its dependencies is on this list\. A space\-separated list of formulae\. Homebrew will refuse to install a formula or cask if it or any of its dependencies is on this list\.
.TP .TP
\fBHOMEBREW_FORBIDDEN_LICENSES\fP \fBHOMEBREW_FORBIDDEN_LICENSES\fP
A space\-separated list of licenses\. Homebrew will refuse to install a formula if it or any of its dependencies has a license on this list\. A space\-separated list of SPDX license identifiers\. Homebrew will refuse to install a formula if it or any of its dependencies has a license on this list\.
.TP .TP
\fBHOMEBREW_FORBIDDEN_OWNER\fP \fBHOMEBREW_FORBIDDEN_OWNER\fP
The person who has set any \fBHOMEBREW_FORBIDDEN_*\fP variables\. The person who has set any \fBHOMEBREW_FORBIDDEN_*\fP variables\.