Source File
licenses.go
Belonging Package
golang.org/x/pkgsite/internal/licenses
package licenses
import (
oldlicensecheck
modzip
)
coverageThreshold = 75
unknownLicenseType = "UNKNOWN"
)
var maxLicenseSize uint64 = modzip.MaxLICENSE
func ( *License) () {
if !Redistributable(.Types) {
.Contents = nil
}
}
var (
FileNames = []string{
"COPYING",
"COPYING.md",
"COPYING.markdown",
"COPYING.txt",
"LICENCE",
"LICENCE.md",
"LICENCE.markdown",
"LICENCE.txt",
"LICENSE",
"LICENSE.md",
"LICENSE.markdown",
"LICENSE.txt",
"LICENSE-2.0.txt",
"LICENCE-2.0.txt",
"LICENSE-APACHE",
"LICENCE-APACHE",
"LICENSE-APACHE-2.0.txt",
"LICENCE-APACHE-2.0.txt",
"LICENSE-MIT",
"LICENCE-MIT",
"LICENSE.MIT",
"LICENCE.MIT",
"LICENSE.code",
"LICENCE.code",
"LICENSE.docs",
"LICENCE.docs",
"LICENSE.rst",
"LICENCE.rst",
"MIT-LICENSE",
"MIT-LICENCE",
"MIT-LICENSE.md",
"MIT-LICENCE.md",
"MIT-LICENSE.markdown",
"MIT-LICENCE.markdown",
"MIT-LICENSE.txt",
"MIT-LICENCE.txt",
"MIT_LICENSE",
"MIT_LICENCE",
"UNLICENSE",
"UNLICENCE",
}
"AFL-3.0",
"AGPL-3.0",
"AGPL-3.0-only",
"AGPL-3.0-or-later",
"Apache-1.1",
"Apache-2.0",
"Artistic-2.0",
"BlueOak-1.0.0",
"0BSD",
"BSD-1-Clause",
"BSD-2-Clause",
"BSD-2-Clause-Patent",
"BSD-2-Clause-Views",
"BSD-3-Clause",
"BSD-3-Clause-Clear",
"BSD-3-Clause-Open-MPI",
"BSD-4-Clause",
"BSD-4-Clause-UC",
"BSL-1.0",
"CC-BY-3.0",
"CC-BY-4.0",
"CC-BY-SA-3.0",
"CC-BY-SA-4.0",
"CC0-1.0",
"EPL-1.0",
"EPL-2.0",
"EUPL-1.2",
"GPL-2.0",
"GPL-2.0-only",
"GPL-2.0-or-later",
"GPL-3.0",
"GPL-3.0-only",
"GPL-3.0-or-later",
"HPND",
"ISC",
"JSON",
"LGPL-2.1",
"LGPL-2.1-or-later",
"LGPL-3.0",
"LGPL-3.0-or-later",
"MIT",
"MIT-0",
"MPL-2.0",
"MPL-2.0-no-copyleft-exception",
"NIST-PD",
"NIST-PD-fallback",
"NCSA",
"OpenSSL",
"OSL-3.0",
"PostgreSQL", // TODO: ask legal
"Python-2.0",
"Unlicense",
"UPL-1.0",
"Zlib",
}
redistributableLicenseTypes = map[string]bool{}
)
func () {
for , := range standardRedistributableLicenseTypes {
redistributableLicenseTypes[] = true
redistributableLicenseTypes["Freetype"] = true
for , := range exceptionTypes {
for , := range {
if !redistributableLicenseTypes[] {
log.Fatalf(context.Background(), "%s is an exception type that is not redistributable.", )
}
}
}
}
var fileNamesLowercase = map[string]bool{}
func () {
for , := range FileNames {
fileNamesLowercase[strings.ToLower()] = true
}
}
type AcceptedLicenseInfo struct {
Name string
URL string
}
func () []AcceptedLicenseInfo {
var []AcceptedLicenseInfo
for , := range standardRedistributableLicenseTypes {
var string
if nonOSILicenses[] {
= fmt.Sprintf("https://spdx.org/licenses/%s.html", )
} else {
= fmt.Sprintf("https://opensource.org/licenses/%s", )
}
= append(, AcceptedLicenseInfo{, })
}
sort.Slice(, func(, int) bool { return [].Name < [].Name })
return
}
OmitExceptions bool
_scanner *licensecheck.Scanner
scannerOnce sync.Once
)
func () *licensecheck.Scanner {
scannerOnce.Do(func() {
if OmitExceptions {
exceptionLicenses = nil
}
var error
_scanner, = licensecheck.NewScanner(append(exceptionLicenses, licensecheck.BuiltinLicenses()...))
if != nil {
log.Fatalf(context.Background(), "licensecheck.NewScanner: %v", )
}
})
return _scanner
}
type Detector struct {
modulePath string
version string
zr *zip.Reader
logf func(string, ...interface{})
moduleRedist bool
moduleLicenses []*License // licenses at module root directory, or list from exceptions
allLicenses []*License
licsByDir map[string][]*License // from directory to list of licenses
}
func ( *Detector) () bool {
return .moduleRedist
}
func ( *Detector) () []*License {
return .moduleLicenses
}
func ( *Detector) () []*License {
if .allLicenses == nil {
.computeAllLicenseInfo()
}
return .allLicenses
}
:= types()
return , append(, .moduleLicenses...)
}
.moduleLicenses = .detectFiles(.Files(RootFiles))
.moduleRedist = Redistributable(types(.moduleLicenses))
}
func ( *Detector) () {
.allLicenses = []*License{}
.allLicenses = append(.allLicenses, .moduleLicenses...)
:= .detectFiles(.Files(NonRootFiles))
.allLicenses = append(.allLicenses, ...)
.licsByDir = map[string][]*License{}
for , := range {
:= path.Dir(.FilePath)
.licsByDir[] = append(.licsByDir[], )
}
}
type WhichFiles int
AllFiles
)
func ( *Detector) ( WhichFiles) []*zip.File {
:= contentsDir(.modulePath, .version)
:= pathPrefix()
var []*zip.File
for , := range .zr.File {
if !fileNamesLowercase[strings.ToLower(path.Base(.Name))] {
continue
}
if !strings.HasPrefix(.Name, ) {
.logf("potential license file %q found outside of the expected path %q", .Name, )
continue
if ignoreFiles[.modulePath+" "+strings.TrimPrefix(.Name, )] {
continue
}
continue
}
continue
}
continue
}
return false
func ( *Detector) ( []*zip.File) []*License {
:= pathPrefix(contentsDir(.modulePath, .version))
var []*License
for , := range {
, := readZipFile()
if != nil {
.logf("reading zip file %s: %v", .Name, )
= append(, &License{
Metadata: &Metadata{
Types: []string{unknownLicenseType},
FilePath: strings.TrimPrefix(.Name, ),
},
})
continue
}
, := DetectFile(, .Name, .logf)
= append(, &License{
Metadata: &Metadata{
Types: ,
FilePath: strings.TrimPrefix(.Name, ),
Coverage: ,
},
Contents: ,
})
}
return
}
func ( []byte, string, func(string, ...interface{})) ([]string, licensecheck.Coverage) {
if == nil {
= func(string, ...interface{}) {}
}
:= scanner().Scan()
if .Percent < float64(coverageThreshold) {
("%s license coverage too low (%+v), skipping", , )
return []string{unknownLicenseType},
}
:= make(map[string]bool)
for , := range .Match {
:= exceptionTypes[.ID]
if == nil {
= []string{.ID}
}
for , := range {
[] = true
}
}
if len() == 0 {
("%s failed to classify license (%+v), skipping", , )
return []string{unknownLicenseType},
}
return setToSortedSlice(),
}
func ( []string) bool {
:= false
for , := range {
if ignorableLicenseTypes[] {
continue
}
if !redistributableLicenseTypes[] {
return false
}
= true
}
return
}
func ( []*License) []string {
var []string
for , := range {
= append(, .Types...)
}
return
}
func ( map[string]bool) []string {
var []string
for := range {
= append(, )
}
sort.Strings()
return
}
func ( *zip.File) ([]byte, error) {
if .UncompressedSize64 > maxLicenseSize {
return nil, fmt.Errorf("file size %d exceeds max license size %d", .UncompressedSize64, maxLicenseSize)
}
, := .Open()
if != nil {
return nil,
}
defer .Close()
return ioutil.ReadAll(io.LimitReader(, int64(maxLicenseSize)))
}
func (, string) string {
return + "@" +
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |