-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for scanning license files in golang packages
Signed-off-by: Avi Deitcher <avi@deitcher.net>
- Loading branch information
Showing
8 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package licenses | ||
|
||
// all of these taken from https://github.com/golang/pkgsite/blob/8996ff632abee854aef1b764ca0501f262f8f523/internal/licenses/licenses.go#L338 | ||
// which unfortunately is not exported. But fortunately is under BSD-style license. | ||
|
||
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", | ||
} | ||
) | ||
|
||
var fileNames map[string]bool | ||
|
||
func init() { | ||
fileNames = make(map[string]bool) | ||
for _, name := range FileNames { | ||
fileNames[name] = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package licenses | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/google/licensecheck" | ||
) | ||
|
||
const ( | ||
coverageThreshold = 75 | ||
unknownLicenseType = "UNKNOWN" | ||
) | ||
|
||
// ScanLicenses scan an fs.FS for licenses, First finds files that fit with the list | ||
// in FileNames, and then uses github.com/google/licensecheck to scan the contents. | ||
func ScanLicenses(fsys fs.FS) []string { | ||
var ( | ||
licenses []string | ||
isVendor bool | ||
) | ||
_ = fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return nil | ||
} | ||
filename := filepath.Base(p) | ||
// ignore any tat are not a known filetype | ||
if _, ok := fileNames[filename]; !ok { | ||
return nil | ||
} | ||
// make sure it is not in a vendored path | ||
parts := strings.Split(filepath.Dir(p), string(filepath.Separator)) | ||
for _, part := range parts { | ||
if part == "vendor" { | ||
isVendor = true | ||
break | ||
} | ||
} | ||
if isVendor { | ||
return nil | ||
} | ||
// read the file contents | ||
rc, err := fsys.Open(p) | ||
if err != nil { | ||
return nil | ||
} | ||
defer rc.Close() | ||
contents, err := io.ReadAll(rc) | ||
if err != nil { | ||
return nil | ||
} | ||
cov := licensecheck.Scan(contents) | ||
|
||
if cov.Percent < float64(coverageThreshold) { | ||
licenses = append(licenses, unknownLicenseType) | ||
} | ||
for _, m := range cov.Match { | ||
licenses = append(licenses, m.ID) | ||
} | ||
return nil | ||
}) | ||
return licenses | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters