1
+ //go:build tools
2
+ // +build tools
3
+
1
4
/*
2
5
Copyright (c) 2019, Percona LLC.
3
6
All rights reserved.
@@ -28,59 +31,140 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
31
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
32
*/
30
33
31
- // +build ignore
32
-
33
- // check-license checks that AGPL license header in all files matches header in this file.
34
+ // check-license checks that the license header in all files matches the copyright text below.
34
35
package main
35
36
36
37
import (
37
- "bufio"
38
38
"flag"
39
39
"fmt"
40
40
"io"
41
41
"log"
42
42
"os"
43
43
"path/filepath"
44
44
"regexp"
45
- "runtime"
46
45
)
47
46
48
- func getHeader () string {
49
- _ , file , _ , ok := runtime .Caller (0 )
50
- if ! ok {
51
- panic ("runtime.Caller(0) failed" )
52
- }
53
- f , err := os .Open (file )
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 )
54
116
if err != nil {
55
117
log .Fatal (err )
56
118
}
57
119
defer f .Close ()
58
120
59
- var header string
60
- s := bufio .NewScanner (f )
61
- for s .Scan () {
62
- if s .Text () == "" {
63
- break
64
- }
65
- header += s .Text () + "\n "
121
+ actual := make ([]byte , len (copyrightText ))
122
+ _ , err = io .ReadFull (f , actual )
123
+ if err == io .ErrUnexpectedEOF {
124
+ err = nil // some files are shorter than license header
66
125
}
67
- header += " \n "
68
- if err := s . Err (); err != nil {
69
- log . Fatal ( err )
126
+ if err != nil {
127
+ log . Printf ( "%s - %s" , path , err )
128
+ return false
70
129
}
71
- return header
72
- }
73
130
74
- var generatedHeader = regexp .MustCompile (`^// Code generated .* DO NOT EDIT\.` )
131
+ if string (copyrightText ) == string (actual ) {
132
+ return true
133
+ }
134
+
135
+ if generatedHeader .Match (actual ) {
136
+ return true
137
+ }
75
138
76
- func checkHeader (path string , header string ) bool {
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 {
77
151
f , err := os .Open (path )
78
152
if err != nil {
79
153
log .Fatal (err )
80
154
}
81
155
defer f .Close ()
82
156
83
- actual := make ([]byte , len (header ))
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 ))
84
168
_ , err = io .ReadFull (f , actual )
85
169
if err == io .ErrUnexpectedEOF {
86
170
err = nil // some files are shorter than license header
@@ -90,14 +174,15 @@ func checkHeader(path string, header string) bool {
90
174
return false
91
175
}
92
176
93
- if generatedHeader . Match (actual ) {
177
+ if string ( headerText ) == string (actual ) {
94
178
return true
95
179
}
96
180
97
- if header != string (actual ) {
181
+ if ! headerPattern . Match (actual ) {
98
182
log .Print (path )
99
183
return false
100
184
}
185
+
101
186
return true
102
187
}
103
188
@@ -109,8 +194,6 @@ func main() {
109
194
}
110
195
flag .Parse ()
111
196
112
- header := getHeader ()
113
-
114
197
ok := true
115
198
filepath .Walk ("." , func (path string , info os.FileInfo , err error ) error {
116
199
if err != nil {
@@ -126,7 +209,7 @@ func main() {
126
209
}
127
210
128
211
if filepath .Ext (info .Name ()) == ".go" {
129
- if ! checkHeader (path , header ) {
212
+ if ! checkHeader (path ) {
130
213
ok = false
131
214
}
132
215
}
0 commit comments