| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
package main |
| 36 |
|
| 37 |
import ( |
| 38 |
"flag" |
| 39 |
"fmt" |
| 40 |
"io" |
| 41 |
"log" |
| 42 |
"os" |
| 43 |
"path/filepath" |
| 44 |
"regexp" |
| 45 |
) |
| 46 |
|
| 47 |
var ( |
| 48 |
generatedHeader = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`) |
| 49 |
|
| 50 |
copyrightText = `/* |
| 51 |
Copyright (c) 2019, Percona LLC. |
| 52 |
All rights reserved. |
| 53 |
|
| 54 |
Redistribution and use in source and binary forms, with or without |
| 55 |
modification, are permitted provided that the following conditions are met: |
| 56 |
|
| 57 |
* Redistributions of source code must retain the above copyright notice, this |
| 58 |
list of conditions and the following disclaimer. |
| 59 |
|
| 60 |
* Redistributions in binary form must reproduce the above copyright notice, |
| 61 |
this list of conditions and the following disclaimer in the documentation |
| 62 |
and/or other materials provided with the distribution. |
| 63 |
|
| 64 |
* Neither the name of the copyright holder nor the names of its |
| 65 |
contributors may be used to endorse or promote products derived from |
| 66 |
this software without specific prior written permission. |
| 67 |
|
| 68 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 69 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 70 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 71 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 72 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 73 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 74 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 75 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 76 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 77 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 78 |
*/ |
| 79 |
` |
| 80 |
|
| 81 |
copyrightPattern = regexp.MustCompile(`^/\* |
| 82 |
Copyright \(c\) 20\d{2}, Percona LLC\. |
| 83 |
All rights reserved\. |
| 84 |
|
| 85 |
Redistribution and use in source and binary forms, with or without |
| 86 |
modification, are permitted provided that the following conditions are met: |
| 87 |
|
| 88 |
\* Redistributions of source code must retain the above copyright notice, this |
| 89 |
{2}list of conditions and the following disclaimer. |
| 90 |
|
| 91 |
\* Redistributions in binary form must reproduce the above copyright notice, |
| 92 |
{2}this list of conditions and the following disclaimer in the documentation |
| 93 |
{2}and/or other materials provided with the distribution. |
| 94 |
|
| 95 |
\* Neither the name of the copyright holder nor the names of its |
| 96 |
{2}contributors may be used to endorse or promote products derived from |
| 97 |
{2}this software without specific prior written permission. |
| 98 |
|
| 99 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 100 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 101 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 102 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 103 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 104 |
DAMAGES \(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 105 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION\) HOWEVER |
| 106 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 107 |
OR TORT \(INCLUDING NEGLIGENCE OR OTHERWISE\) ARISING IN ANY WAY OUT OF THE USE |
| 108 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\. |
| 109 |
\*/ |
| 110 |
`, |
| 111 |
) |
| 112 |
) |
| 113 |
|
| 114 |
func checkHeader(path string) bool { |
| 115 |
f, err := os.Open(path) |
| 116 |
if err != nil { |
| 117 |
log.Fatal(err) |
| 118 |
} |
| 119 |
defer f.Close() |
| 120 |
|
| 121 |
actual := make([]byte, len(copyrightText)) |
| 122 |
_, err = io.ReadFull(f, actual) |
| 123 |
if err == io.ErrUnexpectedEOF { |
| 124 |
err = nil |
| 125 |
} |
| 126 |
if err != nil { |
| 127 |
log.Printf("%s - %s", path, err) |
| 128 |
return false |
| 129 |
} |
| 130 |
|
| 131 |
if string(copyrightText) == string(actual) { |
| 132 |
return true |
| 133 |
} |
| 134 |
|
| 135 |
if generatedHeader.Match(actual) { |
| 136 |
return true |
| 137 |
} |
| 138 |
|
| 139 |
if !copyrightPattern.Match(actual) { |
| 140 |
if !checkBuildIgnoreHeader(path) { |
| 141 |
log.Print(path) |
| 142 |
return false |
| 143 |
} |
| 144 |
return true |
| 145 |
} |
| 146 |
|
| 147 |
return true |
| 148 |
} |
| 149 |
|
| 150 |
func checkBuildIgnoreHeader(path string) bool { |
| 151 |
f, err := os.Open(path) |
| 152 |
if err != nil { |
| 153 |
log.Fatal(err) |
| 154 |
} |
| 155 |
defer f.Close() |
| 156 |
|
| 157 |
headerPattern := regexp.MustCompile(`^//go:build tools |
| 158 |
// \+build tools |
| 159 |
|
| 160 |
` + copyrightPattern.String()) |
| 161 |
|
| 162 |
headerText := `//go:build tools |
| 163 |
// +build tools |
| 164 |
|
| 165 |
` + copyrightText |
| 166 |
|
| 167 |
actual := make([]byte, len(headerText)) |
| 168 |
_, err = io.ReadFull(f, actual) |
| 169 |
if err == io.ErrUnexpectedEOF { |
| 170 |
err = nil |
| 171 |
} |
| 172 |
if err != nil { |
| 173 |
log.Printf("%s - %s", path, err) |
| 174 |
return false |
| 175 |
} |
| 176 |
|
| 177 |
if string(headerText) == string(actual) { |
| 178 |
return true |
| 179 |
} |
| 180 |
|
| 181 |
if !headerPattern.Match(actual) { |
| 182 |
log.Print(path) |
| 183 |
return false |
| 184 |
} |
| 185 |
|
| 186 |
return true |
| 187 |
} |
| 188 |
|
| 189 |
func main() { |
| 190 |
log.SetFlags(0) |
| 191 |
flag.Usage = func() { |
| 192 |
fmt.Fprintln(flag.CommandLine.Output(), "Usage: go run .github/check-license.go") |
| 193 |
flag.CommandLine.PrintDefaults() |
| 194 |
} |
| 195 |
flag.Parse() |
| 196 |
|
| 197 |
ok := true |
| 198 |
filepath.Walk(".", func(path string, info os.FileInfo, err error) error { |
| 199 |
if err != nil { |
| 200 |
return err |
| 201 |
} |
| 202 |
if info.IsDir() { |
| 203 |
switch info.Name() { |
| 204 |
case ".git", "vendor": |
| 205 |
return filepath.SkipDir |
| 206 |
default: |
| 207 |
return nil |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if filepath.Ext(info.Name()) == ".go" { |
| 212 |
if !checkHeader(path) { |
| 213 |
ok = false |
| 214 |
} |
| 215 |
} |
| 216 |
return nil |
| 217 |
}) |
| 218 |
|
| 219 |
if ok { |
| 220 |
os.Exit(0) |
| 221 |
} |
| 222 |
log.Print("Please update license header in those files.") |
| 223 |
os.Exit(1) |
| 224 |
} |