mirror of https://github.com/knative/pkg.git
[master] Auto-update dependencies (#1201)
Produced via: `./hack/update-deps.sh --upgrade && ./hack/update-codegen.sh` /assign n3wscott vagababov /cc n3wscott vagababov
This commit is contained in:
parent
466c676678
commit
db0702fc7c
|
@ -1369,14 +1369,14 @@
|
|||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:d8858077778bca77705b26d5b5262bf33a6bfbaa701fffca1578fd7ef4c4b975"
|
||||
digest = "1:ce8214f24f811aa582a58b2680e3380ff3fac7668a00d490fb23349a21051dcb"
|
||||
name = "knative.dev/test-infra"
|
||||
packages = [
|
||||
"scripts",
|
||||
"tools/dep-collector",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "cdec09517ea85b37691ab4a81ee85d74e7e91b7e"
|
||||
revision = "a7b18bf0c37bf1a10743fba5c1ce988b7c2e0e30"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:8730e0150dfb2b7e173890c8b9868e7a273082ef8e39f4940e3506a481cf895c"
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
Copyright 2020 The Knative Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
gb "go/build"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"knative.dev/pkg/test/cmd"
|
||||
)
|
||||
|
||||
// https://golang.org/pkg/cmd/go/internal/modinfo/#ModulePublic
|
||||
type modInfo struct {
|
||||
Path string
|
||||
Dir string
|
||||
}
|
||||
|
||||
type gobuild struct {
|
||||
mod *modInfo
|
||||
}
|
||||
|
||||
// moduleInfo returns the module path and module root directory for a project
|
||||
// using go modules, otherwise returns nil.
|
||||
// If there is something wrong in getting the module info, it will return an error.
|
||||
//
|
||||
// Related: https://github.com/golang/go/issues/26504
|
||||
func moduleInfo() (*modInfo, error) {
|
||||
// If `go list -m` returns an error, the project is not using Go modules.
|
||||
_, err := cmd.RunCommands("go list -m")
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
output, err := cmd.RunCommand("go list -mod=readonly -m -json")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed getting module info: %v", err)
|
||||
}
|
||||
var info modInfo
|
||||
if err := json.Unmarshal([]byte(output), &info); err != nil {
|
||||
return nil, fmt.Errorf("failed parsing module info %q: %v", output, err)
|
||||
}
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// importPackage wraps go/build.Import to handle go modules.
|
||||
//
|
||||
// Note that we will fall back to GOPATH if the project isn't using go modules.
|
||||
func (g *gobuild) importPackage(s string) (*gb.Package, error) {
|
||||
if g.mod == nil {
|
||||
return gb.Import(s, WorkingDir, gb.ImportComment)
|
||||
}
|
||||
|
||||
// If we're inside a go modules project, try to use the module's directory
|
||||
// as our source root to import:
|
||||
// * paths that match module path prefix (they should be in this project)
|
||||
// * relative paths (they should also be in this project)
|
||||
gp, err := gb.Import(s, g.mod.Dir, gb.ImportComment)
|
||||
return gp, err
|
||||
}
|
||||
|
||||
func (g *gobuild) qualifyLocalImport(ip string) (string, error) {
|
||||
if g.mod == nil {
|
||||
gopathsrc := filepath.Join(gb.Default.GOPATH, "src")
|
||||
if !strings.HasPrefix(WorkingDir, gopathsrc) {
|
||||
return "", fmt.Errorf("working directory must be on ${GOPATH}/src = %s", gopathsrc)
|
||||
}
|
||||
return filepath.Join(strings.TrimPrefix(WorkingDir, gopathsrc+string(filepath.Separator)), ip), nil
|
||||
}
|
||||
return filepath.Join(g.mod.Path, ip), nil
|
||||
}
|
|
@ -19,66 +19,72 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
gb "go/build"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CollectTransitiveImports(binaries []string) ([]string, error) {
|
||||
type ImportInfo struct {
|
||||
ImportPath string
|
||||
Dir string
|
||||
}
|
||||
|
||||
func CollectTransitiveImports(binaries []string) ([]ImportInfo, error) {
|
||||
// Perform a simple DFS to collect the binaries' transitive dependencies.
|
||||
visited := make(map[string]struct{})
|
||||
visited := make(map[string]ImportInfo)
|
||||
mi, err := moduleInfo()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed getting Go module info: %v", err)
|
||||
}
|
||||
g := &gobuild{mi}
|
||||
for _, importpath := range binaries {
|
||||
if gb.IsLocalImport(importpath) {
|
||||
ip, err := qualifyLocalImport(importpath)
|
||||
ip, err := g.qualifyLocalImport(importpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
importpath = ip
|
||||
}
|
||||
|
||||
pkg, err := gb.Import(importpath, WorkingDir, gb.ImportComment)
|
||||
pkg, err := g.importPackage(importpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := visit(pkg, visited); err != nil {
|
||||
if err := visit(g, pkg, visited); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the dependencies deterministically.
|
||||
var list sort.StringSlice
|
||||
for ip := range visited {
|
||||
if !strings.Contains(ip, "/vendor/") {
|
||||
for dir := range visited {
|
||||
if !strings.Contains(dir, "/vendor/") {
|
||||
// Skip files outside of vendor
|
||||
continue
|
||||
}
|
||||
list = append(list, ip)
|
||||
list = append(list, dir)
|
||||
}
|
||||
list.Sort()
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func qualifyLocalImport(ip string) (string, error) {
|
||||
gopathsrc := filepath.Join(gb.Default.GOPATH, "src")
|
||||
if !strings.HasPrefix(WorkingDir, gopathsrc) {
|
||||
return "", fmt.Errorf("working directory must be on ${GOPATH}/src = %s", gopathsrc)
|
||||
iiList := make([]ImportInfo, len(list))
|
||||
for i := range iiList {
|
||||
iiList[i] = visited[list[i]]
|
||||
}
|
||||
return filepath.Join(strings.TrimPrefix(WorkingDir, gopathsrc+string(filepath.Separator)), ip), nil
|
||||
|
||||
return iiList, nil
|
||||
}
|
||||
|
||||
func visit(pkg *gb.Package, visited map[string]struct{}) error {
|
||||
if _, ok := visited[pkg.ImportPath]; ok {
|
||||
func visit(g *gobuild, pkg *gb.Package, visited map[string]ImportInfo) error {
|
||||
if _, ok := visited[pkg.Dir]; ok {
|
||||
return nil
|
||||
}
|
||||
visited[pkg.ImportPath] = struct{}{}
|
||||
visited[pkg.Dir] = ImportInfo{Dir: pkg.Dir, ImportPath: pkg.ImportPath}
|
||||
|
||||
for _, ip := range pkg.Imports {
|
||||
if ip == "C" {
|
||||
// skip cgo
|
||||
continue
|
||||
}
|
||||
subpkg, err := gb.Import(ip, WorkingDir, gb.ImportComment)
|
||||
subpkg, err := g.importPackage(ip)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v\n -> %v", pkg.ImportPath, err)
|
||||
}
|
||||
|
@ -86,7 +92,7 @@ func visit(pkg *gb.Package, visited map[string]struct{}) error {
|
|||
// Skip import paths outside of our workspace (std library)
|
||||
continue
|
||||
}
|
||||
if err := visit(subpkg, visited); err != nil {
|
||||
if err := visit(g, subpkg, visited); err != nil {
|
||||
return fmt.Errorf("%v (%v)\n -> %v", pkg.ImportPath, pkg.Dir, err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
gb "go/build"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -72,7 +71,7 @@ func (lt *LicenseFile) Check(classifier *licenseclassifier.License) error {
|
|||
}
|
||||
ms := classifier.MultipleMatch(body, false)
|
||||
for _, m := range ms {
|
||||
return fmt.Errorf("Found matching forbidden license in %q: %v", lt.EnclosingImportPath, m.Name)
|
||||
return fmt.Errorf("found matching forbidden license in %q: %v", lt.EnclosingImportPath, m.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -108,17 +107,13 @@ func (lt *LicenseFile) CSVRow(classifier *licenseclassifier.License) (string, er
|
|||
}, ","), nil
|
||||
}
|
||||
|
||||
func findLicense(ip string) (*LicenseFile, error) {
|
||||
pkg, err := gb.Import(ip, WorkingDir, gb.ImportComment)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dir := pkg.Dir
|
||||
|
||||
func findLicense(ii ImportInfo) (*LicenseFile, error) {
|
||||
dir := ii.Dir
|
||||
ip := ii.ImportPath
|
||||
for {
|
||||
// When we reach the root of our workspace, stop searching.
|
||||
if dir == WorkingDir {
|
||||
return nil, fmt.Errorf("unable to find license for %q", pkg.ImportPath)
|
||||
return nil, fmt.Errorf("unable to find license for %q", ip)
|
||||
}
|
||||
|
||||
for _, name := range LicenseNames {
|
||||
|
@ -178,11 +173,12 @@ func (lc LicenseCollection) Check(classifier *licenseclassifier.License) error {
|
|||
return fmt.Errorf("Errors validating licenses:\n%v", strings.Join(errors, "\n"))
|
||||
}
|
||||
|
||||
func CollectLicenses(imports []string) (LicenseCollection, error) {
|
||||
// CollectLicenses collects a list of licenses for the given imports.
|
||||
func CollectLicenses(importInfos []ImportInfo) (LicenseCollection, error) {
|
||||
// for each of the import paths, search for a license file.
|
||||
licenseFiles := make(map[string]*LicenseFile)
|
||||
for _, ip := range imports {
|
||||
lf, err := findLicense(ip)
|
||||
for _, info := range importInfos {
|
||||
lf, err := findLicense(info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue