Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
112 changes: 112 additions & 0 deletions license/classifiers.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions license/classifiers_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
Loading