Documentation
¶
Overview ¶
Package licenses matches text and scans repositories against the ScanCode license rule corpus. Matching is exact after token normalization, so edits within a license can prevent a match.
Index ¶
- Constants
- Variables
- func IsLegalPath(filePath string) bool
- func LegalFileRoles(filePath string) []string
- func ValidateScanOptions(options ScanOptions) error
- type CorpusInfo
- type CorpusRecord
- type DeclaredRecord
- type Detection
- type DetectionRecord
- type ExpressionRecord
- type FileRecord
- type Identification
- type Kind
- type Match
- type MatchRecord
- type Matcher
- type Method
- type Option
- type Result
- type SPDXDeclaration
- type ScanErrorRecord
- type ScanOptions
- type ScanReport
- type ScanSummary
- type ScannerRecord
- type SkipRecord
Constants ¶
const ( // ReportSchemaVersion is the additive JSON report schema version. ReportSchemaVersion = reportSchemaVersion // DefaultMaxDepth is the default maximum directory depth. DefaultMaxDepth = defaultMaxDepth // DefaultMaxFiles is the default maximum number of visited files. DefaultMaxFiles = defaultMaxFiles // DefaultMaxFileSize is the default maximum number of bytes per file. DefaultMaxFileSize = defaultMaxFileSize // ScannerName identifies this scanner in reports. ScannerName = scannerName // ScopeAll includes dependency, build, cache, and test-data directories. ScopeAll = scopeAll // ScopeProject skips dependency, build, cache, and test-data directories. ScopeProject = scopeProject )
Variables ¶
var ErrTooManyMatches = errors.New("licenses: too many exact-match candidates")
ErrTooManyMatches is returned when an input produces more exact-match candidates than the matcher can safely filter.
Functions ¶
func IsLegalPath ¶ added in v0.8.0
IsLegalPath reports whether a path has a license or notice role.
func LegalFileRoles ¶ added in v0.6.0
LegalFileRoles classifies a path as a license, notice, both, or neither.
func ValidateScanOptions ¶ added in v0.6.0
func ValidateScanOptions(options ScanOptions) error
ValidateScanOptions reports invalid limits or worker counts.
Types ¶
type CorpusInfo ¶
type CorpusInfo struct {
Version string // ScanCode version recorded in CORPUS_VERSION.
RuleCount int // Number of license texts and rules in the index.
SourceCommit string // Full ScanCode Toolkit source commit.
}
CorpusInfo identifies the ScanCode corpus used for a result.
type CorpusRecord ¶ added in v0.6.0
type CorpusRecord struct {
Version string `json:"version"`
RuleCount int `json:"rule_count"`
SourceCommit string `json:"source_commit"`
}
CorpusRecord identifies the ScanCode corpus used for a scan.
type DeclaredRecord ¶ added in v0.6.0
type DeclaredRecord struct {
Path string `json:"path"`
Raw []string `json:"raw"`
LicenseFile string `json:"license_file"`
NormalizedExpression string `json:"normalized_expression"`
}
DeclaredRecord contains license metadata read from a package manifest.
type Detection ¶
type Detection struct {
// Expression uses canonical SPDX identifiers where available and
// LicenseRef-scancode-* identifiers for other ScanCode license keys.
Expression string
// Identification is derived from the identifiers in Expression.
Identification Identification
// Matches contains the rule matches that state Expression.
Matches []Match
}
Detection groups matches that state the same license expression.
type DetectionRecord ¶ added in v0.6.0
type DetectionRecord struct {
Expression string `json:"expression"`
Identification Identification `json:"identification"`
Matches []MatchRecord `json:"matches"`
}
DetectionRecord groups matches that report the same license expression.
type ExpressionRecord ¶ added in v0.6.0
type ExpressionRecord struct {
Expression string `json:"expression"`
Identification Identification `json:"identification"`
Root bool `json:"root"`
Files int `json:"files"`
Matches int `json:"matches"`
}
ExpressionRecord summarizes a detected license expression across files.
type FileRecord ¶ added in v0.6.0
type FileRecord struct {
Path string `json:"path"`
Size int64 `json:"size"`
SHA256 string `json:"sha256"`
Encoding string `json:"encoding"`
Text string `json:"text,omitempty"`
Roles []string `json:"roles"`
LicenseTextCoverage float64 `json:"license_text_coverage"`
Detections []DetectionRecord `json:"detections"`
Clues []MatchRecord `json:"clues"`
}
FileRecord contains the license detections and clues reported for one file.
type Identification ¶
type Identification string
Identification states whether a detected expression names concrete licenses.
const ( // Identified means the expression contains no ScanCode placeholder // identifiers. Identified Identification = "identified" // Partial means the expression contains both non-placeholder and ScanCode // placeholder identifiers. Partial Identification = "partial" // NoAssertion uses SPDX's NOASSERTION term when the expression contains // only ScanCode placeholder identifiers. NoAssertion Identification = "NOASSERTION" )
type Kind ¶
type Kind string
Kind identifies the ScanCode category of a matched rule.
const ( // KindUnknown identifies a rule without an is_license_* category. KindUnknown Kind = "unknown" // KindText identifies a full license text rule. KindText Kind = "text" // KindNotice identifies a license notice rule. KindNotice Kind = "notice" // KindTag identifies a license tag rule. KindTag Kind = "tag" // KindReference identifies a license reference rule. KindReference Kind = "reference" // KindIntro identifies a license introduction rule. KindIntro Kind = "intro" // KindClue identifies a weak license clue rule. KindClue Kind = "clue" )
type Match ¶
type Match struct {
// RuleID is the ScanCode rule identifier.
RuleID string
// LicenseIDs contains the SPDX-compatible identifiers in the expression.
LicenseIDs []string
// Kind identifies the ScanCode category of the matched rule.
Kind Kind
// Method identifies the exact matching stage that produced the match.
Method Method
// Score is the rule's 0-100 relevance, not a similarity score.
Score float64
// Coverage is 100 for every exact match.
Coverage float64
// Start is the inclusive byte offset into the input.
Start int
// End is the exclusive byte offset into the input.
End int
// Matched is a copy of input[Start:End] when WithMatchedText is set.
// It is nil otherwise.
Matched []byte
}
Match describes one rule match in the input.
type MatchRecord ¶ added in v0.6.0
type MatchRecord struct {
RuleID string `json:"rule_id"`
LicenseIDs []string `json:"license_ids,omitempty"`
Kind Kind `json:"kind"`
Method Method `json:"method"`
Score float64 `json:"score"`
Coverage float64 `json:"coverage"`
Start int `json:"start"`
End int `json:"end"`
Matched string `json:"matched,omitempty"`
}
MatchRecord describes one rule match in a scanned file.
type Matcher ¶
type Matcher struct {
// contains filtered or unexported fields
}
Matcher matches byte slices against an immutable corpus.
func New ¶
New loads the embedded corpus. The decoded corpus is shared by every Matcher in the process.
func NewFromReader ¶ added in v0.9.0
NewFromReader loads a corpus produced by cmd/corpusgen from r. Each call builds an independent engine; reuse the returned Matcher across calls to Match. It does not close r or initialize the shared embedded corpus. The corpus must use the format supported by this version of the package. Programs using only NewFromReader can omit the full embedded corpus from their linked binary.
func (*Matcher) Corpus ¶
func (m *Matcher) Corpus() CorpusInfo
Corpus returns information about the corpus used by m. It returns the zero value for a nil or uninitialized Matcher.
type Option ¶
type Option func(*matcherOptions)
Option configures a Matcher.
func WithMatchedText ¶
func WithMatchedText() Option
WithMatchedText includes a copy of each matched input range in Match.Matched.
type Result ¶
type Result struct {
Detections []Detection
Clues []Match
Corpus CorpusInfo
// SPDXDeclarations contains parsed SPDX-License-Identifier tag occurrences.
SPDXDeclarations []SPDXDeclaration
}
Result contains conclusive detections and weaker clue matches.
type SPDXDeclaration ¶ added in v0.8.0
type SPDXDeclaration struct {
// Expression is normalized from the tag alone.
Expression string
// Start and End delimit the tag in input bytes, excluding End.
Start int
End int
}
SPDXDeclaration describes one parsed SPDX-License-Identifier tag.
type ScanErrorRecord ¶ added in v0.6.0
ScanErrorRecord describes an error associated with one path.
type ScanOptions ¶ added in v0.6.0
type ScanOptions struct {
MaxDepth int // Maximum directory depth; zero disables the limit.
MaxFiles int // Maximum number of visited files; zero disables the limit.
MaxFileSize int64 // Maximum bytes read per file; zero disables the limit.
Workers int // Requested concurrent file scans, capped at 16.
SkipDirs map[string]bool // Directory base names to skip.
NoDefaultSkip bool // Include hidden, dependency, build, cache, and test-data directories.
IncludeLegalFiles bool // Report legal files even when they contain no matches.
ScannerVersion string // Scanner build version recorded in the report.
}
ScanOptions controls repository traversal and file scanning.
func DefaultScanOptions ¶ added in v0.6.0
func DefaultScanOptions() ScanOptions
DefaultScanOptions returns the default traversal limits and worker count.
type ScanReport ¶ added in v0.6.0
type ScanReport struct {
Schema int `json:"schema"`
Root string `json:"root"`
Scope string `json:"scope"`
Scanner ScannerRecord `json:"scanner"`
Corpus CorpusRecord `json:"corpus"`
Summary ScanSummary `json:"summary"`
Declared []DeclaredRecord `json:"declared"`
Expressions []ExpressionRecord `json:"expressions"`
Files []FileRecord `json:"files"`
Skipped []SkipRecord `json:"skipped"`
Errors []ScanErrorRecord `json:"errors"`
}
ScanReport contains the deterministic result of scanning a file or directory.
func ScanRepository ¶ added in v0.6.0
func ScanRepository( ctx context.Context, matcher *Matcher, root string, options ScanOptions, ) (ScanReport, error)
ScanRepository scans a file or directory with matcher.
type ScanSummary ¶ added in v0.6.0
type ScanSummary struct {
FilesVisited int `json:"files_visited"`
FilesScanned int `json:"files_scanned"`
FilesWithDetections int `json:"files_with_detections"`
FilesWithIdentifiedDetections int `json:"files_with_identified_detections"`
FilesWithPartialDetections int `json:"files_with_partial_detections"`
FilesWithNoAssertionDetections int `json:"files_with_noassertion_detections"`
FilesWithClues int `json:"files_with_clues"`
BytesScanned int64 `json:"bytes_scanned"`
DirectoriesSkipped int `json:"directories_skipped"`
FilesSkippedBinary int `json:"files_skipped_binary"`
FilesSkippedSize int `json:"files_skipped_size"`
FilesSkippedOther int `json:"files_skipped_other"`
ErrorCount int `json:"error_count"`
Truncated bool `json:"truncated"`
}
ScanSummary contains file, byte, skip, and error counts for a scan.
type ScannerRecord ¶ added in v0.6.0
ScannerRecord identifies the scanner and its build version.
type SkipRecord ¶ added in v0.6.0
SkipRecord describes a path omitted from a scan and the reason it was omitted.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
corpusgen
command
Command corpusgen builds the embedded license corpus from a ScanCode checkout.
|
Command corpusgen builds the embedded license corpus from a ScanCode checkout. |
|
corpusreport
command
Command corpusreport compares a regenerated corpus with the previously checked-in corpus and writes a stable Markdown report.
|
Command corpusreport compares a regenerated corpus with the previously checked-in corpus and writes a stable Markdown report. |
|
licenses
command
Command licenses scans files and repositories for exact ScanCode license rule matches.
|
Command licenses scans files and repositories for exact ScanCode license rule matches. |
|
internal
|
|
|
aho
Package aho implements a compact Aho-Corasick automaton over integer tokens.
|
Package aho implements a compact Aho-Corasick automaton over integer tokens. |
|
tokenize
Package tokenize converts license text into normalized integer tokens.
|
Package tokenize converts license text into normalized integer tokens. |