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
1 change: 1 addition & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ spdxexp/license.go file.

Command to run all extractions (run command from the /cmd directory):

cd cmd
go run . extract -l -e

Usage options:
Expand Down
38 changes: 38 additions & 0 deletions cmd/exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"go/format"
"os"
"strings"
)

type ExceptionData struct {
Expand Down Expand Up @@ -54,6 +56,27 @@ func extractExceptionLicenseIDs() error {
// Code generated by go-spdx cmd/exceptions.go. DO NOT EDIT.
// Source: https://github.com/spdx/license-list-data specifies official SPDX license list.

import "strings"

// IsException does a case-insensitive lookup for the exception id in the exceptions map.
// It returns true and the case-sensitive ID if found, otherwise false and the original id.
func IsException(id string) (bool, string) {
foundID, ok := exceptionsMap[strings.ToUpper(id)]
if ok {
return true, foundID
}
return false, id
}

// GetExceptionsMap returns a map of exception license IDs keyed by uppercase ID.
func GetExceptionsMap() map[string]string {
copied := make(map[string]string, len(exceptionsMap))
for k, v := range exceptionsMap {
copied[k] = v
}
return copied
}

// GetExceptions returns a slice of exception license IDs.
func GetExceptions() []string {
return []string{
Expand All @@ -64,8 +87,23 @@ func GetExceptions() []string {
}
getExceptionsContents = append(getExceptionsContents, ` }
}

`...)

getExceptionsContents = append(getExceptionsContents, `var exceptionsMap = map[string]string{
`...)
for _, id := range exceptionLicenseIDs {
getExceptionsContents = append(getExceptionsContents, ` "`+strings.ToUpper(id)+`": "`+id+`",
`...)
Comment thread
elrayle marked this conversation as resolved.
}
getExceptionsContents = append(getExceptionsContents, `}
`...)

getExceptionsContents, err = format.Source(getExceptionsContents)
if err != nil {
return fmt.Errorf("format generated get_exceptions.go: %w", err)
}

err = os.WriteFile("../spdxexp/spdxlicenses/get_exceptions.go", getExceptionsContents, 0600)
if err != nil {
return err
Expand Down
74 changes: 74 additions & 0 deletions cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"go/format"
"os"
"strings"
)

type LicenseData struct {
Expand Down Expand Up @@ -59,6 +61,27 @@ func extractLicenseIDs() error {
// Code generated by go-spdx cmd/license.go. DO NOT EDIT.
// Source: https://github.com/spdx/license-list-data specifies official SPDX license list.

import "strings"

// IsActiveLicense does a case-insensitive lookup for the license id in the active licenses map.
// It returns true and the case-sensitive ID if found, otherwise false and the original id.
func IsActiveLicense(id string) (bool, string) {
foundID, ok := licensesMap[strings.ToUpper(id)]
if ok {
return true, foundID
}
return false, id
}

// GetLicensesMap returns a map of active license IDs keyed by uppercase ID.
func GetLicensesMap() map[string]string {
copied := make(map[string]string, len(licensesMap))
for k, v := range licensesMap {
copied[k] = v
}
return copied
}

// GetLicenses returns a slice of active license IDs.
func GetLicenses() []string {
return []string{
Expand All @@ -69,8 +92,23 @@ func GetLicenses() []string {
}
getLicensesContents = append(getLicensesContents, ` }
}

`...)

getLicensesContents = append(getLicensesContents, `var licensesMap = map[string]string{
`...)
for _, id := range activeLicenseIDs {
getLicensesContents = append(getLicensesContents, ` "`+strings.ToUpper(id)+`": "`+id+`",
`...)
Comment thread
elrayle marked this conversation as resolved.
}
getLicensesContents = append(getLicensesContents, `}
`...)

getLicensesContents, err = format.Source(getLicensesContents)
if err != nil {
return fmt.Errorf("format generated get_licenses.go: %w", err)
}

err = os.WriteFile("../spdxexp/spdxlicenses/get_licenses.go", getLicensesContents, 0600)
if err != nil {
return err
Expand All @@ -83,6 +121,27 @@ func GetLicenses() []string {
// Code generated by go-spdx cmd/license.go. DO NOT EDIT.
// Source: https://github.com/spdx/license-list-data specifies official SPDX license list.

import "strings"

// IsDeprecatedLicense does a case-insensitive lookup for the license id in the deprecated licenses map.
// It returns true and the case-sensitive ID if found, otherwise false and the original id.
func IsDeprecatedLicense(id string) (bool, string) {
foundID, ok := deprecatedMap[strings.ToUpper(id)]
if ok {
return true, foundID
}
return false, id
}

// GetDeprecatedMap returns a map of deprecated license IDs keyed by uppercase ID.
func GetDeprecatedMap() map[string]string {
copied := make(map[string]string, len(deprecatedMap))
for k, v := range deprecatedMap {
copied[k] = v
}
return copied
}

// GetDeprecated returns a slice of deprecated license IDs.
func GetDeprecated() []string {
return []string{
Expand All @@ -93,8 +152,23 @@ func GetDeprecated() []string {
}
getDeprecatedContents = append(getDeprecatedContents, ` }
}

`...)

getDeprecatedContents = append(getDeprecatedContents, `var deprecatedMap = map[string]string{
`...)
for _, id := range deprecatedLicenseIDs {
getDeprecatedContents = append(getDeprecatedContents, ` "`+strings.ToUpper(id)+`": "`+id+`",
`...)
}
getDeprecatedContents = append(getDeprecatedContents, `}
`...)

getDeprecatedContents, err = format.Source(getDeprecatedContents)
if err != nil {
return fmt.Errorf("format generated get_deprecated.go: %w", err)
}

err = os.WriteFile("../spdxexp/spdxlicenses/get_deprecated.go", getDeprecatedContents, 0600)
if err != nil {
return err
Expand Down
20 changes: 12 additions & 8 deletions spdxexp/benchmark_satisfies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ type satisfiesBenchmarkScenario struct {

var satisfiesBenchmarkScenarios = []satisfiesBenchmarkScenario{
// Scenario order is used as-is in the summary table.
{"MIT", "MIT"},
{"mit", "mit"},
{"Apache-2.0", "Apache-2.0"},
{"Zed", "Zed"},
{"MIT AND Apache-2.0", "MIT AND Apache-2.0"},
{"MIT AND Apache-2.0 OR Zed", "MIT AND Apache-2.0 OR Zed"},
{"GPL-2.0-or-later", "GPL-2.0-or-later"},
{"GPL-2.0+", "GPL-2.0+"},
{"MIT--exact", "MIT"},
{"mit--caseinsensitive", "mit"},
{"mit--extra-space", " MIT "},
{"Apache-2.0--active-early", "Apache-2.0"},
{"Zed--active-end", "Zed"},
{"MIT AND Apache-2.0--complex", "MIT AND Apache-2.0"},
{"MIT AND Apache-2.0 OR Zed--complex", "MIT AND Apache-2.0 OR Zed"},
{"BSD-2-Clause-FreeBSD--deprecated", "BSD-2-Clause-FreeBSD"},
{"GPL-2.0-or-later--range", "GPL-2.0-or-later"},
{"Apache-1.0+--plus-range", "Apache-1.0+"},
{"LicenseRef-scancode-adobe-postscript", "LicenseRef-scancode-adobe-postscript"},
{"DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", "DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2"},
}

func BenchmarkSatisfies(b *testing.B) {
Expand Down
20 changes: 12 additions & 8 deletions spdxexp/benchmark_validate_licenses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ type validateLicensesBenchmarkScenario struct {

var validateLicensesBenchmarkScenarios = []validateLicensesBenchmarkScenario{
// Scenario order is used as-is in the summary table.
{"MIT", []string{"MIT"}},
{"mit", []string{"mit"}},
{"Apache-2.0", []string{"Apache-2.0"}},
{"Zed", []string{"Zed"}},
{"MIT AND Apache-2.0", []string{"MIT", "Apache-2.0"}},
{"MIT AND Apache-2.0 OR Zed", []string{"MIT", "Apache-2.0", "Zed"}},
{"GPL-2.0-or-later", []string{"GPL-2.0-or-later"}},
{"GPL-2.0+", []string{"GPL-2.0+"}},
{"MIT--exact", []string{"MIT"}},
{"mit--caseinsensitive", []string{"mit"}},
{"mit--extra-space", []string{" MIT "}},
{"Apache-2.0--active-early", []string{"Apache-2.0"}},
{"Zed--active-end", []string{"Zed"}},
{"MIT AND Apache-2.0--complex", []string{"MIT", "Apache-2.0"}},
{"MIT AND Apache-2.0 OR Zed--complex", []string{"MIT", "Apache-2.0", "Zed"}},
{"BSD-2-Clause-FreeBSD--deprecated", []string{"BSD-2-Clause-FreeBSD"}},
{"GPL-2.0-or-later--range", []string{"GPL-2.0-or-later"}},
{"Apache-1.0+--plus-range", []string{"Apache-1.0+"}},
{"LicenseRef-scancode-adobe-postscript", []string{"LicenseRef-scancode-adobe-postscript"}},
{"DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", []string{"DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2"}},
}

func BenchmarkValidateLicenses(b *testing.B) {
Expand Down
16 changes: 3 additions & 13 deletions spdxexp/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// activeLicense returns true if the id is an active license.
func activeLicense(id string) (bool, string) {
return inLicenseList(spdxlicenses.GetLicenses(), id)
return spdxlicenses.IsActiveLicense(id)
}

// ActiveLicense returns true if the id is an active license.
Expand All @@ -18,22 +18,12 @@ func ActiveLicense(id string) (bool, string) {

// deprecatedLicense returns true if the id is a deprecated license.
func deprecatedLicense(id string) (bool, string) {
return inLicenseList(spdxlicenses.GetDeprecated(), id)
return spdxlicenses.IsDeprecatedLicense(id)
}

// exceptionLicense returns true if the id is an exception license.
func exceptionLicense(id string) (bool, string) {
return inLicenseList(spdxlicenses.GetExceptions(), id)
}

// inLicenseList looks for id in the list of licenses. The check is case-insensitive (e.g. "mit" will match "MIT").
func inLicenseList(licenses []string, id string) (bool, string) {
for _, license := range licenses {
if strings.EqualFold(license, id) {
return true, license
}
}
return false, id
return spdxlicenses.IsException(id)
}

const (
Expand Down
Loading
Loading