github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+incompatible/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 "os/exec" 17 "regexp" 18 "strings" 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 var oklicenses = []*regexp.Regexp{ 26 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 regexp.MustCompile( 33 `^// Copyright [\d\-, ]+ Google LLC. 34 // 35 // Licensed under the Apache License, Version 2.0.* 36 `), 37 } 38 39 type rule struct { 40 *regexp.Regexp 41 invert bool 42 } 43 44 func accept(s string) rule { 45 return rule{ 46 regexp.MustCompile("^" + s + "$"), 47 false, 48 } 49 } 50 51 func reject(s string) rule { 52 return rule{ 53 regexp.MustCompile("^" + s + "$"), 54 true, 55 } 56 } 57 58 // A file is checked iff all the accepts and none of the rejects match. 59 var rules = []rule{ 60 accept(`.*\.go`), 61 reject(`vendor/.*`), // Various authors 62 reject(`cmds/dhcp/.*`), // Graham King 63 reject(`cmds/elvish/.*`), // elvish developers and contributors 64 reject(`cmds/ldd/.*`), // Go authors 65 reject(`cmds/ping/.*`), // Go authors 66 reject(`xcmds/ectool/.*`), // Chromium authors 67 68 reject(`pkg/diskboot/entrytype_string.go`), // generated 69 } 70 71 func main() { 72 flag.Parse() 73 uroot := os.ExpandEnv(uroot) 74 incorrect := []string{} 75 76 // List files added to u-root. 77 out, err := exec.Command("git", "ls-files").Output() 78 if err != nil { 79 log.Fatalln("error running git ls-files:", err) 80 } 81 files := strings.Fields(string(out)) 82 83 // Iterate over files. 84 outer: 85 for _, file := range files { 86 // Test rules. 87 trimmedPath := strings.TrimPrefix(file, uroot) 88 for _, r := range rules { 89 if r.MatchString(trimmedPath) == r.invert { 90 continue outer 91 } 92 } 93 94 // Make sure it is not a directory. 95 info, err := os.Stat(file) 96 if err != nil { 97 log.Fatalln("cannot stat", file, err) 98 } 99 if info.IsDir() { 100 continue 101 } 102 103 // Read from the file. 104 r, err := os.Open(file) 105 if err != nil { 106 log.Fatalln("cannot open", file, err) 107 } 108 defer r.Close() 109 contents, err := ioutil.ReadAll(r) 110 if err != nil { 111 log.Fatalln("cannot read", file, err) 112 } 113 var foundone bool 114 for _, l := range oklicenses { 115 if l.Match(contents) { 116 foundone = true 117 break 118 } 119 } 120 if !foundone { 121 p := trimmedPath 122 if *absPath { 123 p = file 124 } 125 incorrect = append(incorrect, p) 126 } 127 } 128 if err != nil { 129 log.Fatal(err) 130 } 131 132 // Print files with incorrect licenses. 133 if len(incorrect) > 0 { 134 fmt.Println(strings.Join(incorrect, "\n")) 135 os.Exit(1) 136 } 137 }