Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
support manual overrides when third party licenses cannot be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
jlegrone committed Sep 15, 2021
1 parent 9cbf0fb commit d84802f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ github.com/aws/aws-sdk-go/internal/sync/singleflight,https://github.com/aws/aws-
github.com/benbjohnson/clock,https://github.com/benbjohnson/clock,MIT,Ben Johnson
github.com/beorn7/perks/quantile,https://github.com/beorn7/perks,MIT,Blake Mizerany
github.com/blang/semver/v4,https://github.com/blang/semver,MIT,Benedikt Lang <github at benediktlang.de>
github.com/cactus/go-statsd-client/statsd,,,
github.com/cactus/go-statsd-client/statsd,https://github.com/cactus/go-statsd-client,MIT,Eli Janssen
github.com/cespare/xxhash/v2,https://github.com/cespare/xxhash,MIT,Caleb Spare
github.com/cpuguy83/go-md2man/v2/md2man,https://github.com/cpuguy83/go-md2man,MIT,Brian Goff
github.com/davecgh/go-spew/spew,https://github.com/davecgh/go-spew,ISC,Dave Collins <dave@davec.name>
Expand Down
70 changes: 51 additions & 19 deletions internal/licensecheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"context"
_ "embed"
"encoding/csv"
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
Expand All @@ -19,6 +22,15 @@ func main() {
}
}

type Component struct {
Origin string `json:"origin"`
License string `json:"license"`
Copyright string `json:"copyright"`
}

//go:embed overrides.json
var componentOverrides []byte

func execute() error {
classifier, err := licenses.NewClassifier(0.9)
if err != nil {
Expand All @@ -36,50 +48,70 @@ func execute() error {
return err
}

// Sort libs so csv diff is neater
sort.SliceStable(libs, func(i, j int) bool {
return strings.Compare(libs[i].Name(), libs[j].Name()) < 0
})

w := csv.NewWriter(f)
defer w.Flush()

if err := w.Write([]string{"Component", "Origin", "License", "Copyright"}); err != nil {
components := map[string]Component{}
var overrides map[string]Component
if err := json.Unmarshal(componentOverrides, &overrides); err != nil {
return err
}

for _, lib := range libs {
var (
origin, licenseName, copyright string
)
var component Component
var licenseUrl string
if lib.LicensePath != "" {
// Find a URL for the license file, based on the URL of a remote for the Git repository.
repo, err := licenses.FindGitRepo(lib.LicensePath)
if err != nil {
// Can't find Git repo (possibly a Go Module?) - derive URL from lib name instead.
if lURL, err := lib.FileURL(lib.LicensePath); err == nil {
origin = lURL.String()
licenseUrl = lURL.String()
}
if b, err := ioutil.ReadFile(lib.LicensePath); err == nil {
copyright = licenseclassifier.CopyrightHolder(string(b))
component.Copyright = licenseclassifier.CopyrightHolder(string(b))
}
} else {
for _, remote := range []string{"origin", "upstream"} {
url, err := repo.FileURL(lib.LicensePath, remote)
if err != nil {
continue
}
origin = url.String()
licenseUrl = url.String()
break
}
}
if ln, _, err := classifier.Identify(lib.LicensePath); err == nil {
licenseName = ln
component.License = ln
}
}
// Parse the project URL
originUrlParts := strings.Split(origin, "/blob/")
if err := w.Write([]string{lib.Name(), originUrlParts[0], licenseName, copyright}); err != nil {
component.Origin = strings.Split(licenseUrl, "/blob/")[0]

components[lib.Name()] = component
}

for k, v := range overrides {
components[k] = v
}

return writeComponents(f, components)
}

func writeComponents(w io.Writer, components map[string]Component) error {
c := csv.NewWriter(w)
defer c.Flush()

if err := c.Write([]string{"Component", "Origin", "License", "Copyright"}); err != nil {
return err
}

// Sort component names
var keys []string
for k, _ := range components {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
component := components[k]
if err := c.Write([]string{k, component.Origin, component.License, component.Copyright}); err != nil {
return err
}
}
Expand Down
7 changes: 7 additions & 0 deletions internal/licensecheck/overrides.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"github.com/cactus/go-statsd-client/statsd": {
"license": "MIT",
"copyright": "Eli Janssen",
"origin": "https://github.com/cactus/go-statsd-client"
}
}

0 comments on commit d84802f

Please sign in to comment.