From 1318ce36dfe7efdc80a704336722f7eb80957151 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Wed, 22 Jul 2026 15:47:53 -0400 Subject: [PATCH 1/3] feat(license): SPDX expression parser (rstudio/package-manager#19135) --- NOTICE | 6 ++ license/spdx_expression.go | 139 ++++++++++++++++++++++++++++++++ license/spdx_expression_test.go | 88 ++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 license/spdx_expression.go create mode 100644 license/spdx_expression_test.go diff --git a/NOTICE b/NOTICE index c285e24..c8062f0 100644 --- a/NOTICE +++ b/NOTICE @@ -76,3 +76,9 @@ pipeline, `break_args_options`, `COMMENT_RE`, `ENV_VAR_RE`, and and from Astral's uv (https://github.com/astral-sh/uv), specifically its uv-requirements crate, dual-licensed under the Apache License, Version 2.0, or the MIT license. + +The license/ package is derived from rstudio/package-manager's PyPI license +derivation (getLicense/getLicenseTypes and the licenses package's PyPI path), +Copyright (c) Posit Software, PBC, contributed under the Apache License, Version +2.0, or the MIT license. The embedded data/pypi_licenses.json maps public PyPI +Trove classifiers to SPDX identifiers. diff --git a/license/spdx_expression.go b/license/spdx_expression.go new file mode 100644 index 0000000..3944859 --- /dev/null +++ b/license/spdx_expression.go @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package license + +import ( + "strings" + "unicode" +) + +// ParseSPDXExpression parses an SPDX license expression and returns the individual +// license identifiers. This follows the SPDX License Expression syntax specification: +// https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/ +// +// The function extracts all license identifiers from expressions like: +// - Simple: "MIT" -> ["MIT"] +// - OR (disjunctive): "MIT OR Apache-2.0" -> ["MIT", "Apache-2.0"] +// - AND (conjunctive): "MIT AND Apache-2.0" -> ["MIT", "Apache-2.0"] +// - WITH (exception): "GPL-3.0-only WITH Classpath-exception-2.0" -> ["GPL-3.0-only"] +// - Or-later (+): "GPL-2.0+" -> ["GPL-2.0-or-later"] +// - Custom: "LicenseRef-Proprietary" -> ["LicenseRef-Proprietary"] +// - Nested: "(MIT OR Apache-2.0) AND BSD-3-Clause" -> ["MIT", "Apache-2.0", "BSD-3-Clause"] +// +// Note: Per SPDX spec, operators AND, OR, WITH are case-sensitive (must be uppercase). +// This parser is lenient and accepts lowercase for compatibility with real-world usage. +func ParseSPDXExpression(expr string) []string { + if expr == "" { + return nil + } + + expr = strings.TrimSpace(expr) + if expr == "" { + return nil + } + + tokens := tokenizeSPDX(expr) + return extractLicenseIdentifiers(tokens) +} + +// tokenizeSPDX splits an SPDX expression into tokens. +// Tokens are: identifiers (including +), AND, OR, WITH, (, ) +func tokenizeSPDX(expr string) []string { + var tokens []string + var current strings.Builder + + flushCurrent := func() { + if current.Len() > 0 { + tokens = append(tokens, current.String()) + current.Reset() + } + } + + i := 0 + for i < len(expr) { + ch := rune(expr[i]) + + switch { + case ch == '(' || ch == ')': + flushCurrent() + tokens = append(tokens, string(ch)) + i++ + + case ch == '+': + // + suffix attaches to the previous identifier (no whitespace per spec) + // but we handle it as part of the identifier + current.WriteRune(ch) + i++ + + case ch == ':': + // Part of DocumentRef-X:LicenseRef-Y format + current.WriteRune(ch) + i++ + + case unicode.IsSpace(ch): + flushCurrent() + i++ + + default: + current.WriteRune(ch) + i++ + } + } + + flushCurrent() + return tokens +} + +// extractLicenseIdentifiers extracts license identifiers from tokens, +// filtering out operators (AND, OR, WITH) and exception identifiers. +func extractLicenseIdentifiers(tokens []string) []string { + var licenses []string + seen := make(map[string]bool) + skipNext := false + + for _, token := range tokens { + // Skip parentheses + if token == "(" || token == ")" { + continue + } + + // Check for operators - per SPDX spec these are case-sensitive + // but we accept both for compatibility + upper := strings.ToUpper(token) + if upper == "AND" || upper == "OR" { + continue + } + + // WITH is followed by an exception identifier, skip both + if upper == "WITH" { + skipNext = true + continue + } + + if skipNext { + skipNext = false + continue + } + + // Handle + suffix (or-later) + // Per SPDX spec: "GPL-2.0+" means "GPL-2.0-or-later" + cleanToken := token + if strings.HasSuffix(token, "+") { + base := strings.TrimSuffix(token, "+") + // Convert to -or-later form if not already + if !strings.HasSuffix(base, "-or-later") { + cleanToken = base + "-or-later" + } else { + cleanToken = base + } + } + + // Deduplicate + if !seen[cleanToken] { + seen[cleanToken] = true + licenses = append(licenses, cleanToken) + } + } + + return licenses +} diff --git a/license/spdx_expression_test.go b/license/spdx_expression_test.go new file mode 100644 index 0000000..4b08852 --- /dev/null +++ b/license/spdx_expression_test.go @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package license + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseSPDXExpression(t *testing.T) { + cases := []struct { + name string + in string + want []string + }{ + // Empty and whitespace + {"empty string", "", nil}, + {"whitespace only", " ", nil}, + + // Simple identifiers + {"single identifier", "MIT", []string{"MIT"}}, + {"single identifier with whitespace", " MIT ", []string{"MIT"}}, + {"identifier with version", "GPL-2.0-only", []string{"GPL-2.0-only"}}, + {"identifier with dots", "BSD-3-Clause", []string{"BSD-3-Clause"}}, + {"Apache license", "Apache-2.0", []string{"Apache-2.0"}}, + + // Or-later (+) suffix + {"or-later suffix converts to -or-later", "GPL-2.0+", []string{"GPL-2.0-or-later"}}, + {"LGPL or-later", "LGPL-2.1+", []string{"LGPL-2.1-or-later"}}, + {"already has -or-later suffix", "GPL-3.0-or-later", []string{"GPL-3.0-or-later"}}, + + // OR expressions - disjunctive + {"two licenses with OR", "MIT OR Apache-2.0", []string{"MIT", "Apache-2.0"}}, + {"three licenses with OR", "MIT OR Apache-2.0 OR BSD-3-Clause", []string{"MIT", "Apache-2.0", "BSD-3-Clause"}}, + {"OR is case-sensitive but we accept lowercase", "MIT or Apache-2.0", []string{"MIT", "Apache-2.0"}}, + + // AND expressions - conjunctive + {"two licenses with AND", "MIT AND Apache-2.0", []string{"MIT", "Apache-2.0"}}, + {"three licenses with AND", "MIT AND Apache-2.0 AND BSD-3-Clause", []string{"MIT", "Apache-2.0", "BSD-3-Clause"}}, + {"AND is case-sensitive but we accept lowercase", "MIT and Apache-2.0", []string{"MIT", "Apache-2.0"}}, + + // WITH expressions - exceptions + {"license with exception", "GPL-2.0-only WITH Classpath-exception-2.0", []string{"GPL-2.0-only"}}, + {"GPL-3.0 with exception", "GPL-3.0-or-later WITH Bison-exception-2.2", []string{"GPL-3.0-or-later"}}, + {"WITH is case-sensitive but we accept lowercase", "GPL-2.0-only with Classpath-exception-2.0", []string{"GPL-2.0-only"}}, + + // Mixed expressions + {"AND and OR mixed", "MIT AND Apache-2.0 OR GPL-3.0-only", []string{"MIT", "Apache-2.0", "GPL-3.0-only"}}, + {"OR with exception", "MIT OR GPL-3.0-only WITH Classpath-exception-2.0", []string{"MIT", "GPL-3.0-only"}}, + + // Parentheses + {"parentheses around single license", "(MIT)", []string{"MIT"}}, + {"parentheses around OR expression", "(MIT OR Apache-2.0)", []string{"MIT", "Apache-2.0"}}, + {"nested parentheses", "((MIT OR Apache-2.0) AND BSD-3-Clause)", []string{"MIT", "Apache-2.0", "BSD-3-Clause"}}, + {"complex nested expression", "(MIT AND Apache-2.0) OR (GPL-2.0-only AND BSD-3-Clause)", []string{"MIT", "Apache-2.0", "GPL-2.0-only", "BSD-3-Clause"}}, + {"precedence override with parens", "LGPL-2.1-only OR (MIT AND BSD-2-Clause)", []string{"LGPL-2.1-only", "MIT", "BSD-2-Clause"}}, + + // LicenseRef + {"LicenseRef custom license", "LicenseRef-Proprietary", []string{"LicenseRef-Proprietary"}}, + {"LicenseRef with numbers", "LicenseRef-Company-1.0", []string{"LicenseRef-Company-1.0"}}, + {"LicenseRef with standard license", "MIT OR LicenseRef-Company-License", []string{"MIT", "LicenseRef-Company-License"}}, + + // DocumentRef:LicenseRef + {"DocumentRef with LicenseRef", "DocumentRef-ext:LicenseRef-custom", []string{"DocumentRef-ext:LicenseRef-custom"}}, + + // Deduplication + {"duplicate licenses are deduplicated", "MIT AND MIT", []string{"MIT"}}, + {"duplicates across OR branches", "MIT OR MIT OR Apache-2.0", []string{"MIT", "Apache-2.0"}}, + + // Real-world examples + {"numpy-style expression", "BSD-3-Clause", []string{"BSD-3-Clause"}}, + {"dual-license expression", "Apache-2.0 OR MIT", []string{"Apache-2.0", "MIT"}}, + {"requests-style", "Apache-2.0", []string{"Apache-2.0"}}, + {"GPL with exception (common pattern)", "GPL-2.0-or-later WITH Bison-exception-2.2", []string{"GPL-2.0-or-later"}}, + {"complex real-world", "(Apache-2.0 OR MIT) AND BSD-3-Clause", []string{"Apache-2.0", "MIT", "BSD-3-Clause"}}, + + // Edge cases + {"extra whitespace", " MIT OR Apache-2.0 ", []string{"MIT", "Apache-2.0"}}, + {"mixed case operators", "MIT Or Apache-2.0", []string{"MIT", "Apache-2.0"}}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, ParseSPDXExpression(tc.in)) + }) + } +} From 87254dce84305a9cab1cdf9dd4c02e2072709b33 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Wed, 22 Jul 2026 15:52:54 -0400 Subject: [PATCH 2/3] feat(license): PyPI classifier standardization + embedded mapping (rstudio/package-manager#19135) --- license/classifiers.go | 112 +++++++ license/classifiers_test.go | 38 +++ license/data/pypi_licenses.json | 506 ++++++++++++++++++++++++++++++++ license/license.go | 7 + 4 files changed, 663 insertions(+) create mode 100644 license/classifiers.go create mode 100644 license/classifiers_test.go create mode 100644 license/data/pypi_licenses.json create mode 100644 license/license.go diff --git a/license/classifiers.go b/license/classifiers.go new file mode 100644 index 0000000..1ebf2a9 --- /dev/null +++ b/license/classifiers.go @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package license + +import ( + _ "embed" + "encoding/json" + "fmt" + "strings" +) + +//go:embed data/pypi_licenses.json +var pypiLicensesJSON []byte + +// PyPILicense represents a license from PyPI's Trove classifiers. +// See: https://pypi.org/classifiers/ +type PyPILicense struct { + // Full license classifier. E.g., "License :: OSI Approved :: MIT License" + Classifier string `json:"Classifier"` + // License name. The last component of the classifier, e.g., "MIT License" + Name string `json:"Name"` + + // SPDX identifier mapping. If the license is not in SPDX, SPDXIdentifier should + // be empty with SPDXNotIncluded set to true. SPDXNotIncluded distinguishes + // newly added, unreviewed licenses from licenses without an SPDX identifier. + SPDXIdentifier string `json:"SPDXIdentifier"` + SPDXNotIncluded bool `json:"SPDXNotIncluded"` +} + +func init() { + buildPyPILicenseDB() +} + +var pypiLicenseList []*PyPILicense +var pypiLicenseByName map[string]*PyPILicense + +func buildPyPILicenseDB() { + if err := json.Unmarshal(pypiLicensesJSON, &pypiLicenseList); err != nil { + panic(fmt.Sprintf("license: embedded pypi_licenses.json is malformed: %v", err)) + } + + pypiLicenseByName = make(map[string]*PyPILicense) + + for _, license := range pypiLicenseList { + if license.SPDXNotIncluded { + license.SPDXIdentifier = LicenseUnknown + } + pypiLicenseByName[license.Name] = license + } +} + +func ListPyPILicenseTypes() []string { + var types []string + for _, lic := range pypiLicenseList { + if lic.SPDXIdentifier != LicenseUnknown { + types = append(types, lic.SPDXIdentifier) + } + } + return types +} + +// StandardizeLicensePyPIClassifiers standardizes PyPI classifiers into SPDX license identifiers. +// LicenseUnknown is returned if a license can't be standardized. +// +// Examples of standardizable PyPI license classifiers: +// +// - License :: OSI Approved :: BSD License +// - License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+) +func StandardizeLicensePyPIClassifiers(classifiers []string) []string { + var standardized []string + dedupLicenses := make(map[string]bool) + + licenses := GetLicensesFromPyPIClassifiers(classifiers) + for _, license := range licenses { + // "License :: OSI Approved" is a special case. It's an umbrella classifier + // for valid and standardizable licenses, rather than a license itself, and + // should not be classified as an unknown license. Just ignore these, as + // "License :: OSI Approved" alone would be considered a missing license. + // Also see: https://pypi.org/search/?c=License+%3A%3A+OSI+Approved + if license == "OSI Approved" { + continue + } + if foundLic, ok := pypiLicenseByName[license]; ok { + if _, duplicated := dedupLicenses[foundLic.SPDXIdentifier]; duplicated { + continue + } + dedupLicenses[foundLic.SPDXIdentifier] = true + standardized = append(standardized, foundLic.SPDXIdentifier) + } + } + if len(standardized) == 0 { + standardized = []string{LicenseUnknown} + } + return standardized +} + +// GetLicensesFromPyPIClassifiers filters PyPI classifiers for license classifiers +// and returns just the license components of the classifiers. +// +// For example: +// - "License :: OSI Approved :: BSD License" -> "BSD License" +// - "License :: Free For Home Use" -> "Free For Home Use" +func GetLicensesFromPyPIClassifiers(classifiers []string) []string { + var licenses []string + for _, classifier := range classifiers { + if strings.HasPrefix(classifier, "License ::") { + fields := strings.Split(classifier, "::") + licenses = append(licenses, strings.TrimSpace(fields[len(fields)-1])) + } + } + return licenses +} diff --git a/license/classifiers_test.go b/license/classifiers_test.go new file mode 100644 index 0000000..03c2970 --- /dev/null +++ b/license/classifiers_test.go @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package license + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetLicensesFromPyPIClassifiers(t *testing.T) { + got := GetLicensesFromPyPIClassifiers([]string{ + "License :: OSI Approved :: BSD License", + "Programming Language :: Python :: 3", + "License :: OSI Approved", + "License :: OSI Approved :: BSD License", + }) + assert.Equal(t, []string{"BSD License", "OSI Approved", "BSD License"}, got) +} + +func TestStandardizeLicensePyPIClassifiers(t *testing.T) { + cases := []struct { + name string + in []string + want []string + }{ + {"bsd", []string{"License :: OSI Approved :: BSD License"}, []string{"BSD-3-Clause"}}, + {"umbrella-skipped", []string{"License :: OSI Approved"}, []string{"Unknown"}}, + {"dedup", []string{"License :: OSI Approved :: BSD License", "License :: OSI Approved :: BSD License"}, []string{"BSD-3-Clause"}}, + {"none", []string{"Programming Language :: Python :: 3"}, []string{"Unknown"}}, + {"not-included", []string{"License :: Public Domain"}, []string{"Unknown"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, StandardizeLicensePyPIClassifiers(tc.in)) + }) + } +} diff --git a/license/data/pypi_licenses.json b/license/data/pypi_licenses.json new file mode 100644 index 0000000..0dfb95a --- /dev/null +++ b/license/data/pypi_licenses.json @@ -0,0 +1,506 @@ +[ + { + "Classifier": "License :: Free For Home Use", + "Name": "Free For Home Use", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: OSI Approved :: Eclipse Public License 1.0 (EPL-1.0)", + "Name": "Eclipse Public License 1.0 (EPL-1.0)", + "SPDXIdentifier": "EPL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU General Public License v2 (GPLv2)", + "Name": "GNU General Public License v2 (GPLv2)", + "SPDXIdentifier": "GPL-2.0-only", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Universal Permissive License (UPL)", + "Name": "Universal Permissive License (UPL)", + "SPDXIdentifier": "UPL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: Free For Educational Use", + "Name": "Free For Educational Use", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: OSI Approved :: BSD License", + "Name": "BSD License", + "SPDXIdentifier": "BSD-3-Clause", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Common Public License", + "Name": "Common Public License", + "SPDXIdentifier": "CPL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Eclipse Public License 2.0 (EPL-2.0)", + "Name": "Eclipse Public License 2.0 (EPL-2.0)", + "SPDXIdentifier": "EPL-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Attribution Assurance License", + "Name": "Attribution Assurance License", + "SPDXIdentifier": "AAL", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)", + "Name": "GNU Lesser General Public License v2 or later (LGPLv2+)", + "SPDXIdentifier": "LGPL-2.0-or-later", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication", + "Name": "CC0 1.0 Universal (CC0 1.0) Public Domain Dedication", + "SPDXIdentifier": "CC0-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: MIT License", + "Name": "MIT License", + "SPDXIdentifier": "MIT", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Qt Public License (QPL)", + "Name": "Qt Public License (QPL)", + "SPDXIdentifier": "QPL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: SIL Open Font License 1.1 (OFL-1.1)", + "Name": "SIL Open Font License 1.1 (OFL-1.1)", + "SPDXIdentifier": "OFL-1.1", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)", + "Name": "Mozilla Public License 2.0 (MPL 2.0)", + "SPDXIdentifier": "MPL-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: The Unlicense (Unlicense)", + "Name": "The Unlicense (Unlicense)", + "SPDXIdentifier": "Unlicense", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: IBM Public License", + "Name": "IBM Public License", + "SPDXIdentifier": "IPL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", + "Name": "GNU General Public License v3 (GPLv3)", + "SPDXIdentifier": "GPL-3.0-only", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Mozilla Public License 1.1 (MPL 1.1)", + "Name": "Mozilla Public License 1.1 (MPL 1.1)", + "SPDXIdentifier": "MPL-1.1", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)", + "Name": "GNU Lesser General Public License v2 (LGPLv2)", + "SPDXIdentifier": "LGPL-2.0-only", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Boost Software License 1.0 (BSL-1.0)", + "Name": "Boost Software License 1.0 (BSL-1.0)", + "SPDXIdentifier": "BSL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: PostgreSQL License", + "Name": "PostgreSQL License", + "SPDXIdentifier": "PostgreSQL", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: University of Illinois/NCSA Open Source License", + "Name": "University of Illinois/NCSA Open Source License", + "SPDXIdentifier": "NCSA", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Ricoh Source Code Public License", + "Name": "Ricoh Source Code Public License", + "SPDXIdentifier": "RSCPL", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Nethack General Public License", + "Name": "Nethack General Public License", + "SPDXIdentifier": "NGPL", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU Affero General Public License v3", + "Name": "GNU Affero General Public License v3", + "SPDXIdentifier": "AGPL-3.0-only", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: ISC License (ISCL)", + "Name": "ISC License (ISCL)", + "SPDXIdentifier": "ISC", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: Public Domain", + "Name": "Public Domain", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)", + "Name": "GNU Lesser General Public License v3 (LGPLv3)", + "SPDXIdentifier": "LGPL-3.0-or-later", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Zope Public License", + "Name": "Zope Public License", + "SPDXIdentifier": "ZPL-2.1", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: X.Net License", + "Name": "X.Net License", + "SPDXIdentifier": "Xnet", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Jabber Open Source License", + "Name": "Jabber Open Source License", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: OSI Approved :: Eiffel Forum License", + "Name": "Eiffel Forum License", + "SPDXIdentifier": "EFL-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Motosoto License", + "Name": "Motosoto License", + "SPDXIdentifier": "Motosoto", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)", + "Name": "GNU Affero General Public License v3 or later (AGPLv3+)", + "SPDXIdentifier": "AGPL-3.0-or-later", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved", + "Name": "OSI Approved", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: OSI Approved :: Mulan Permissive Software License v2 (MulanPSL-2.0)", + "Name": "Mulan Permissive Software License v2 (MulanPSL-2.0)", + "SPDXIdentifier": "MulanPSL-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Python Software Foundation License", + "Name": "Python Software Foundation License", + "SPDXIdentifier": "PSF-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: GUST Font License 2006-09-30", + "Name": "GUST Font License 2006-09-30", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: Freely Distributable", + "Name": "Freely Distributable", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: Repoze Public License", + "Name": "Repoze Public License", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: Netscape Public License (NPL)", + "Name": "Netscape Public License (NPL)", + "SPDXIdentifier": "NPL-1.1", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: European Union Public Licence 1.2 (EUPL 1.2)", + "Name": "European Union Public Licence 1.2 (EUPL 1.2)", + "SPDXIdentifier": "EUPL-1.2", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)", + "Name": "GNU General Public License v2 or later (GPLv2+)", + "SPDXIdentifier": "GPL-2.0-or-later", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: Nokia Open Source License (NOKOS)", + "Name": "Nokia Open Source License (NOKOS)", + "SPDXIdentifier": "Nokia", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU Free Documentation License (FDL)", + "Name": "GNU Free Documentation License (FDL)", + "SPDXIdentifier": "GFDL-1.3-only", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: MITRE Collaborative Virtual Workspace License (CVW)", + "Name": "MITRE Collaborative Virtual Workspace License (CVW)", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: OSI Approved :: W3C License", + "Name": "W3C License", + "SPDXIdentifier": "W3C", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Apache Software License", + "Name": "Apache Software License", + "SPDXIdentifier": "Apache-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Mozilla Public License 1.0 (MPL)", + "Name": "Mozilla Public License 1.0 (MPL)", + "SPDXIdentifier": "MPL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Artistic License", + "Name": "Artistic License", + "SPDXIdentifier": "Artistic-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: CEA CNRS Inria Logiciel Libre License, version 2.1 (CeCILL-2.1)", + "Name": "CEA CNRS Inria Logiciel Libre License, version 2.1 (CeCILL-2.1)", + "SPDXIdentifier": "CECILL-2.1", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Open Software License 3.0 (OSL-3.0)", + "Name": "Open Software License 3.0 (OSL-3.0)", + "SPDXIdentifier": "OSL-3.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Academic Free License (AFL)", + "Name": "Academic Free License (AFL)", + "SPDXIdentifier": "AFL-3.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: DFSG approved", + "Name": "DFSG approved", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: Eiffel Forum License (EFL)", + "Name": "Eiffel Forum License (EFL)", + "SPDXIdentifier": "EFL-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: zlib/libpng License", + "Name": "zlib/libpng License", + "SPDXIdentifier": "Zlib", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Historical Permission Notice and Disclaimer (HPND)", + "Name": "Historical Permission Notice and Disclaimer (HPND)", + "SPDXIdentifier": "HPND", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Sleepycat License", + "Name": "Sleepycat License", + "SPDXIdentifier": "Sleepycat", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU General Public License (GPL)", + "Name": "GNU General Public License (GPL)", + "SPDXIdentifier": "GPL-3.0-only", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Nokia Open Source License", + "Name": "Nokia Open Source License", + "SPDXIdentifier": "Nokia", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: Freeware", + "Name": "Freeware", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: Other/Proprietary License", + "Name": "Other/Proprietary License", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: CeCILL-C Free Software License Agreement (CECILL-C)", + "Name": "CeCILL-C Free Software License Agreement (CECILL-C)", + "SPDXIdentifier": "CECILL-C", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Common Development and Distribution License 1.0 (CDDL-1.0)", + "Name": "Common Development and Distribution License 1.0 (CDDL-1.0)", + "SPDXIdentifier": "CDDL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: CeCILL-B Free Software License Agreement (CECILL-B)", + "Name": "CeCILL-B Free Software License Agreement (CECILL-B)", + "SPDXIdentifier": "CECILL-B", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: Aladdin Free Public License (AFPL)", + "Name": "Aladdin Free Public License (AFPL)", + "SPDXIdentifier": "Aladdin", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Sun Public License", + "Name": "Sun Public License", + "SPDXIdentifier": "SPL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Apple Public Source License", + "Name": "Apple Public Source License", + "SPDXIdentifier": "APSL-2.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)", + "Name": "GNU Library or Lesser General Public License (LGPL)", + "SPDXIdentifier": "LGPL-3.0-only", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: MIT No Attribution License (MIT-0)", + "Name": "MIT No Attribution License (MIT-0)", + "SPDXIdentifier": "MIT-0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Intel Open Source License", + "Name": "Intel Open Source License", + "SPDXIdentifier": "Intel", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)", + "Name": "GNU Lesser General Public License v3 or later (LGPLv3+)", + "SPDXIdentifier": "LGPL-3.0-or-later", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Python License (CNRI Python License)", + "Name": "Python License (CNRI Python License)", + "SPDXIdentifier": "CNRI-Python", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: Free for non-commercial use", + "Name": "Free for non-commercial use", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: OSI Approved :: Open Group Test Suite License", + "Name": "Open Group Test Suite License", + "SPDXIdentifier": "OGTSL", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: European Union Public Licence 1.1 (EUPL 1.1)", + "Name": "European Union Public Licence 1.1 (EUPL 1.1)", + "SPDXIdentifier": "EUPL-1.1", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Sun Industry Standards Source License (SISSL)", + "Name": "Sun Industry Standards Source License (SISSL)", + "SPDXIdentifier": "SISSL-1.2", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: MirOS License (MirOS)", + "Name": "MirOS License (MirOS)", + "SPDXIdentifier": "MirOS", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", + "Name": "GNU General Public License v3 or later (GPLv3+)", + "SPDXIdentifier": "GPL-3.0-or-later", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: GUST Font License 1.0", + "Name": "GUST Font License 1.0", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: Free To Use But Restricted", + "Name": "Free To Use But Restricted", + "SPDXIdentifier": "", + "SPDXNotIncluded": true + }, + { + "Classifier": "License :: OSI Approved :: European Union Public Licence 1.0 (EUPL 1.0)", + "Name": "European Union Public Licence 1.0 (EUPL 1.0)", + "SPDXIdentifier": "EUPL-1.0", + "SPDXNotIncluded": false + }, + { + "Classifier": "License :: OSI Approved :: Vovida Software License 1.0", + "Name": "Vovida Software License 1.0", + "SPDXIdentifier": "VSL-1.0", + "SPDXNotIncluded": false + } +] \ No newline at end of file diff --git a/license/license.go b/license/license.go new file mode 100644 index 0000000..8e062d2 --- /dev/null +++ b/license/license.go @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package license + +// LicenseUnknown is the SPDX-side sentinel returned by Types when a license +// cannot be standardized. Distinct (by case) from Names's "UNKNOWN" literal. +const LicenseUnknown = "Unknown" From ef4a880280da5904766f8d345e2d1835345260c8 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Wed, 22 Jul 2026 15:56:27 -0400 Subject: [PATCH 3/3] feat(license): Names/Types top-level derivation + golden vectors (rstudio/package-manager#19135) --- license/license.go | 37 +++++++++++++++++++++++++++ license/license_test.go | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 license/license_test.go diff --git a/license/license.go b/license/license.go index 8e062d2..cdcdf27 100644 --- a/license/license.go +++ b/license/license.go @@ -5,3 +5,40 @@ package license // LicenseUnknown is the SPDX-side sentinel returned by Types when a license // cannot be standardized. Distinct (by case) from Names's "UNKNOWN" literal. const LicenseUnknown = "Unknown" + +// Names reproduces PPM getLicense: display names via the priority ladder +// (License-Expression -> classifiers -> freeform License -> "UNKNOWN"). +func Names(expression string, classifiers []string, license string) []string { + // Priority 1: License-Expression (PEP 639 SPDX format) + if expression != "" { + lics := ParseSPDXExpression(expression) + if len(lics) > 0 { + return lics + } + } + // Priority 2: Classifiers (raw names; no umbrella skip, no dedup) + lics := GetLicensesFromPyPIClassifiers(classifiers) + if len(lics) > 0 { + return lics + } + // Priority 3: freeform License field + if license != "" { + return []string{license} + } + // Priority 4: unknown + return []string{"UNKNOWN"} +} + +// Types reproduces PPM getLicenseTypes: SPDX-id set via +// (License-Expression -> classifiers->SPDX -> "Unknown"). +func Types(expression string, classifiers []string) []string { + // Priority 1: License-Expression is already SPDX format + if expression != "" { + lics := ParseSPDXExpression(expression) + if len(lics) > 0 { + return lics + } + } + // Priority 2: map classifiers to SPDX (umbrella skip + dedup) + return StandardizeLicensePyPIClassifiers(classifiers) +} diff --git a/license/license_test.go b/license/license_test.go new file mode 100644 index 0000000..ce4c40c --- /dev/null +++ b/license/license_test.go @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package license + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNamesAndTypes(t *testing.T) { + cases := []struct { + name string + expression string + classifiers []string + license string + wantNames []string + wantTypes []string + }{ + { + name: "expression-wins", expression: "MIT OR Apache-2.0", + classifiers: []string{"License :: OSI Approved :: BSD License"}, license: "ignored", + wantNames: []string{"MIT", "Apache-2.0"}, wantTypes: []string{"MIT", "Apache-2.0"}, + }, + { + name: "classifiers-second", classifiers: []string{"License :: OSI Approved :: BSD License"}, + wantNames: []string{"BSD License"}, wantTypes: []string{"BSD-3-Clause"}, + }, + { + name: "names-vs-types-classifiers", + classifiers: []string{"License :: OSI Approved", "License :: OSI Approved :: BSD License", "License :: OSI Approved :: BSD License"}, + wantNames: []string{"OSI Approved", "BSD License", "BSD License"}, + wantTypes: []string{"BSD-3-Clause"}, + }, + { + name: "freeform-third", license: "My Custom License 1.0", + wantNames: []string{"My Custom License 1.0"}, wantTypes: []string{"Unknown"}, + }, + { + name: "none-declared", + wantNames: []string{"UNKNOWN"}, wantTypes: []string{"Unknown"}, + }, + { + name: "long-dump-freeform", license: "Copyright (c) 2020 ... 400 chars of license text ... permission is hereby granted", + wantNames: []string{"Copyright (c) 2020 ... 400 chars of license text ... permission is hereby granted"}, + wantTypes: []string{"Unknown"}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.wantNames, Names(tc.expression, tc.classifiers, tc.license)) + assert.Equal(t, tc.wantTypes, Types(tc.expression, tc.classifiers)) + }) + } +}