code.flowtr.dev/mirrors/u-root@v1.0.0/scripts/checklicenses/checklicenses.go (about) 1 // Copyright 2017-2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Run with `go run checklicenses.go`. This script has one drawback: 6 // - It does not correct the licenses; it simply outputs a list of files which 7 // do not conform and returns 1 if the list is non-empty. 8 package main 9 10 import ( 11 "flag" 12 "fmt" 13 "io/ioutil" 14 "log" 15 "os" 16 "regexp" 17 "strings" 18 "os/exec" 19 ) 20 21 var absPath = flag.Bool("a", false, "Print absolute paths") 22 23 const uroot = "$GOPATH/src/github.com/u-root/u-root" 24 25 // The first few lines of every go file is expected to contain this license. 26 var license = regexp.MustCompile( 27 `^// Copyright [\d\-, ]+ the u-root Authors\. All rights reserved 28 // Use of this source code is governed by a BSD-style 29 // license that can be found in the LICENSE file\. 30 `) 31 32 type rule struct { 33 *regexp.Regexp 34 invert bool 35 } 36 37 func accept(s string) rule { 38 return rule{ 39 regexp.MustCompile("^" + s + "$"), 40 false, 41 } 42 } 43 44 func reject(s string) rule { 45 return rule{ 46 regexp.MustCompile("^" + s + "$"), 47 true, 48 } 49 } 50 51 // A file is checked iff all the accepts and none of the rejects match. 52 var rules = []rule{ 53 accept(`.*\.go`), 54 reject(`vendor/.*`), // Various authors 55 reject(`cmds/dhcp/.*`), // Graham King 56 reject(`cmds/ldd/.*`), // Go authors 57 reject(`cmds/ping/.*`), // Go authors 58 reject(`xcmds/ectool/.*`), // Chromium authors 59 60 reject(`pkg/diskboot/entrytype_string.go`), // generated 61 } 62 63 func main() { 64 flag.Parse() 65 uroot := os.ExpandEnv(uroot) 66 incorrect := []string{} 67 68 // List files added to u-root. 69 out, err := exec.Command("git", "ls-files").Output() 70 if err != nil { 71 log.Fatalln("error running git ls-files:", err) 72 } 73 files := strings.Fields(string(out)) 74 75 // Iterate over files. 76 outer: 77 for _, file := range files { 78 // Test rules. 79 trimmedPath := strings.TrimPrefix(file, uroot) 80 for _, r := range rules { 81 if r.MatchString(trimmedPath) == r.invert { 82 continue outer 83 } 84 } 85 86 // Make sure it is not a directory. 87 info, err := os.Stat(file) 88 if err != nil { 89 log.Fatalln("cannot stat", file, err) 90 } 91 if info.IsDir() { 92 continue 93 } 94 95 // Read from the file. 96 r, err := os.Open(file) 97 if err != nil { 98 log.Fatalln("cannot open", file, err) 99 } 100 defer r.Close() 101 contents, err := ioutil.ReadAll(r) 102 if err != nil { 103 log.Fatalln("cannot read", file, err) 104 } 105 if !license.Match(contents) { 106 p := trimmedPath 107 if *absPath { 108 p = file 109 } 110 incorrect = append(incorrect, p) 111 } 112 } 113 if err != nil { 114 log.Fatal(err) 115 } 116 117 // Print files with incorrect licenses. 118 if len(incorrect) > 0 { 119 fmt.Println(strings.Join(incorrect, "\n")) 120 os.Exit(1) 121 } 122 }