func()

in backend/licenseclassifier.go [74:148]


func (obj *LicenseClassifier) ScanPath(ctx context.Context, path safepath.Path, info *interfaces.Info) (*interfaces.Result, error) {

	if info.FileInfo.IsDir() { // path.IsDir() should be the same.
		return nil, nil // skip
	}

	filenames := []string{path.Path()}

	threshold := 0.0 // we decide acceptability downstream
	if obj.UseDefaultConfidence {
		threshold = licenseclassifier.DefaultConfidenceThreshold
	}
	forbiddenOnly := true // identify using forbidden licenses archive
	be, err := backend.New(threshold, forbiddenOnly)
	if err != nil {
		be.Close()
		return nil, errwrap.Wrapf(err, "cannot create license classifier")
	}

	// XXX: bug: https://github.com/google/licenseclassifier/issues/28
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	if errs := be.ClassifyLicensesWithContext(ctx, filenames, obj.IncludeHeaders); errs != nil {
		be.Close()
		for _, err := range errs {
			if obj.Debug {
				obj.Logf("classify license failed: %v", err)
			}
		}
		return nil, fmt.Errorf("cannot classify licenses")
	}

	results := be.GetResults()
	if len(results) == 0 {
		be.Close()
		if obj.SkipZeroResults {
			return nil, nil
		}
		//return nil, fmt.Errorf("couldn't classify license(s)")
		return nil, interfaces.ErrUnknownLicense
	}

	sort.Sort(results)
	// A match identifies the result of matching a string against a known value.
	// Name		string	// Name of known value that was matched.
	// Confidence	float64	// Confidence percentage.
	// Offset	int	// The offset into the unknown string the match was made.
	// Extent	int	// The length from the offset into the unknown string.
	//for _, r := range results {
	//	log.Printf("%s: %s (confidence: %v, offset: %v, extent: %v)",
	//		r.Filename, r.Name, r.Confidence, r.Offset, r.Extent)
	//	// licenses/AGPL-3.0.txt: AGPL-3.0 (confidence: 0.9999677086024283, offset: 0, extent: 30968)
	//}
	be.Close()
	// This can give us multiple results, sorted by most confident.
	result, err := resultHelper(results[0])
	if err != nil {
		return nil, err
	}

	// Add more info about the others possibilities to the result.
	more := []*interfaces.Result{}
	for i := 1; i < len(results); i++ {
		r, err := resultHelper(results[i])
		if err != nil {
			return nil, err
		}
		more = append(more, r)
	}
	if len(more) > 0 {
		result.More = more
	}

	return result, nil
}