licenses

package
v0.0.0-...-ddbbf7b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package licenses detects licenses and determines whether they are redistributable. The functions in this package do not return errors; instead, they log any problems they encounter and fail closed by reporting that the module or package is not redistributable.

Example (modproxy):

d := licenses.NewDetector(modulePath, version, zipReader, log.Infof)
modRedist := d.ModuleIsRedistributable()

Example (discovery):

d := licenses.NewDetector(modulePath, version, zipReader, log.Infof)
modRedist := d.ModuleIsRedistributable()
lics := d.AllLicenses()
pkgRedist, pkgMetas := d.PackageInfo(pkgSubdir)

Index

Constants

This section is empty.

Variables

View Source
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",
	}
)
View Source
var (
	// OmitExceptions causes the list of exceptions to be omitted from license detection.
	// It is intended only to speed up testing, and must be set before the first use
	// of this package.
	OmitExceptions bool
)

Functions

func DetectFile

func DetectFile(contents []byte, filename string, logf func(string, ...any)) ([]string, licensecheck.Coverage)

DetectFile return the set of license types for the given file contents. It also returns the licensecheck coverage information. The filename is used solely for logging.

func Redistributable

func Redistributable(licenseTypes []string) bool

Redistributable reports whether the set of license types establishes that a module or package is redistributable. All the licenses we see that are relevant must be redistributable, and we must see at least one such license.

Types

type AcceptedLicenseInfo

type AcceptedLicenseInfo struct {
	Name string
	URL  string
}

AcceptedLicenseInfo describes a license that is accepted by the discovery site.

func AcceptedLicenses

func AcceptedLicenses() []AcceptedLicenseInfo

AcceptedLicenses returns a sorted slice of license types that are accepted as redistributable. Its result is intended to be displayed to users.

type Detector

type Detector struct {
	// contains filtered or unexported fields
}

A Detector detects licenses in a module and its packages.

func NewDetector

func NewDetector(modulePath, version string, zr *zip.Reader, logf func(string, ...any)) *Detector

NewDetector returns a Detector for the given module and version. zr should be the zip file for that module and version. logf is for logging; if nil, no logging is done. Deprecated: use NewDetectorFS.

func NewDetectorFS

func NewDetectorFS(modulePath, version string, fsys fs.FS, logf func(string, ...any)) *Detector

NewDetectorFS returns a Detector for the given module and version. fsys should represent the content directory of the module (not the zip root). logf is for logging; if nil, no logging is done.

func (*Detector) AllLicenses

func (d *Detector) AllLicenses() []*License

AllLicenses returns all the licenses detected in the entire module, including package licenses.

func (*Detector) ModuleIsRedistributable

func (d *Detector) ModuleIsRedistributable() bool

ModuleIsRedistributable reports whether the given module is redistributable.

func (*Detector) ModuleLicenses

func (d *Detector) ModuleLicenses() []*License

ModuleLicenses returns the licenses that apply to the module.

func (*Detector) PackageInfo

func (d *Detector) PackageInfo(dir string) (isRedistributable bool, lics []*License)

PackageInfo reports whether the package at dir, a directory relative to the module root, is redistributable. It also returns all the licenses that apply to the package.

type License

type License struct {
	*Metadata
	Contents []byte
}

A License is a classified license file path and its contents.

func (*License) RemoveNonRedistributableData

func (l *License) RemoveNonRedistributableData()

RemoveNonRedistributableData methods removes the license contents if the license is non-redistributable.

type Metadata

type Metadata struct {
	// Types is the set of license types, as determined by the licensecheck package.
	Types []string
	// FilePath is the '/'-separated path to the license file in the module zip,
	// relative to the contents directory.
	FilePath string
	Coverage licensecheck.Coverage
}

Metadata holds information extracted from a license file.

type WhichFiles

type WhichFiles int

WhichFiles describes which files from the zip should be returned by Detector.Files.

const (
	// Only files from the root (contents) directory.
	RootFiles WhichFiles = iota
	// Only files that are not in the root directory.
	NonRootFiles
	// All files; the union of root and non-root.
	AllFiles
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL