-
Notifications
You must be signed in to change notification settings - Fork 6
/
update-license.go
393 lines (357 loc) · 8.44 KB
/
update-license.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright The go-okchain Authors 2019, All rights reserved.
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"text/template"
"time"
)
var (
// only files with these extensions will be considered
extensions = []string{".go"}
// paths with any of these prefixes will be skipped
skipPrefixes = []string{
// boring stuff
"vendor/",
"build/",
//quadratic develop
"accounts/",
"crypto/",
"rpc/",
"common/",
"p2p/",
"config/",
"core/",
//test scripts
"scripts/",
"dev/",
"testnet/",
//?
"util/",
}
includeFiles = []string{
//quadratic-dev
"accounts/keystore/key.go",
"accounts/keystore/keystore_passphrase.go",
"accounts/keystore/keystore_plain_test.go",
"accounts/keystore/keystore.go",
"accounts/manager.go",
"crypto/ecschnorr/",
"crypto/p256/",
"crypto/merkel/",
"crypto/multibls/",
"crypto/sortition",
"crypto/bccsp.go",
"crypto/bccsp_test.go",
"core/blockchain/",
"gossip/comm/comm.go",
"gossip/comm/comm_impl.go",
"gossip/comm/conn.go",
"gossip/discovery/discovery_impl.go",
"gossip/gossip/gossip.go",
"gossip/gossip/gossip_impl.go",
"gossip/util/config.go", //add
"p2p/gossip/state/payloads_buffer.go",
"p2p/gossip/state/state.go",
"rpc/client.go",
"rpc/client_example_test.go",
"rpc/client_test.go",
"rpc/endpoints.go",
"rpc/http.go",
"rpc/ipc.go",
"rpc/json.go",
"rpc/server.go",
"rpc/types.go",
"rpc/types_test.go",
"rpc/websocket.go",
}
// this regexp must match the entire license comment at the
// beginning of each file.
// licenseCommentRE = regexp.MustCompile(`^//\s*(Copyright|This file is part of).*?\n(?://.*?\n)*\n*`)
licenseCommentRE = regexp.MustCompile(`^//\s*(Copyright The go-okchain Authors).*?\n(?://.*?\n)*\n*`)
// this text appears at the start of AUTHORS
authorsFileHeader = "# This is the official list of go-okchain authors for copyright purposes.\n\n"
)
// this template generates the license comment.
// its input is an info structure.
var licenseT = template.Must(template.New("").Parse(`
// Copyright The go-okchain Authors {{.Year}}, All rights reserved.
`[1:]))
type info struct {
file string
Year int64
}
func (i info) License() string {
return "Lesser General Public License"
}
func (i info) ShortLicense() string {
return "LGPL"
}
func main() {
var (
files = getFiles()
filec = make(chan string)
infoc = make(chan *info, 20)
wg sync.WaitGroup
)
writeAuthors(files)
go func() {
for _, f := range files {
filec <- f
}
close(filec)
}()
for i := runtime.NumCPU(); i >= 0; i-- {
// getting file info is slow and needs to be parallel.
// it traverses git history for each file.
wg.Add(1)
go getInfo(filec, infoc, &wg)
}
go func() {
wg.Wait()
close(infoc)
}()
writeLicenses(infoc)
}
func mustFile(path string) bool {
for _, p := range includeFiles {
if strings.HasPrefix(path, p) {
return true
}
}
return false
}
func skipFile(path string) bool {
if strings.Contains(path, "/testdata/") {
return true
}
for _, p := range skipPrefixes {
if strings.HasPrefix(path, p) && !mustFile(path) {
return true
}
}
return false
}
func getFiles() []string {
cmd := exec.Command("git", "ls-tree", "-r", "--name-only", "HEAD")
var files []string
err := doLines(cmd, func(line string) {
if skipFile(line) {
return
}
ext := filepath.Ext(line)
for _, wantExt := range extensions {
if ext == wantExt {
goto keep
}
}
return
keep:
files = append(files, line)
})
if err != nil {
log.Fatal("error getting files:", err)
}
return files
}
var authorRegexp = regexp.MustCompile(`\s*[0-9]+\s*(.*)`)
func gitAuthors(files []string) []string {
cmds := []string{"shortlog", "-s", "-n", "-e", "HEAD", "--"}
cmds = append(cmds, files...)
cmd := exec.Command("git", cmds...)
var authors []string
err := doLines(cmd, func(line string) {
m := authorRegexp.FindStringSubmatch(line)
if len(m) > 1 {
authors = append(authors, m[1])
}
})
if err != nil {
log.Fatalln("error getting authors:", err)
}
return authors
}
func readAuthors() []string {
content, err := ioutil.ReadFile("AUTHORS")
if err != nil && !os.IsNotExist(err) {
log.Fatalln("error reading AUTHORS:", err)
}
var authors []string
for _, a := range bytes.Split(content, []byte("\n")) {
if len(a) > 0 && a[0] != '#' {
authors = append(authors, string(a))
}
}
// Retranslate existing authors through .mailmap.
// This should catch email address changes.
authors = mailmapLookup(authors)
return authors
}
func mailmapLookup(authors []string) []string {
if len(authors) == 0 {
return nil
}
cmds := []string{"check-mailmap", "--"}
cmds = append(cmds, authors...)
cmd := exec.Command("git", cmds...)
var translated []string
err := doLines(cmd, func(line string) {
translated = append(translated, line)
})
if err != nil {
log.Fatalln("error translating authors:", err)
}
return translated
}
func writeAuthors(files []string) {
merge := make(map[string]bool)
// Add authors that Git reports as contributorxs.
// This is the primary source of author information.
for _, a := range gitAuthors(files) {
merge[a] = true
}
// Add existing authors from the file. This should ensure that we
// never lose authors, even if Git stops listing them. We can also
// add authors manually this way.
for _, a := range readAuthors() {
merge[a] = true
}
// Write sorted list of authors back to the file.
var result []string
for a := range merge {
result = append(result, a)
}
sort.Strings(result)
content := new(bytes.Buffer)
content.WriteString(authorsFileHeader)
for _, a := range result {
content.WriteString(a)
content.WriteString("\n")
}
fmt.Println("writing AUTHORS")
if err := ioutil.WriteFile("AUTHORS", content.Bytes(), 0644); err != nil {
log.Fatalln(err)
}
}
func getInfo(files <-chan string, out chan<- *info, wg *sync.WaitGroup) {
for file := range files {
stat, err := os.Lstat(file)
if err != nil {
fmt.Printf("ERROR %s: %v\n", file, err)
continue
}
if !stat.Mode().IsRegular() {
continue
}
if isGenerated(file) {
continue
}
info, err := fileInfo(file)
if err != nil {
fmt.Printf("ERROR %s: %v\n", file, err)
continue
}
out <- info
}
wg.Done()
}
func isGenerated(file string) bool {
fd, err := os.Open(file)
if err != nil {
return false
}
defer fd.Close()
buf := make([]byte, 2048)
n, _ := fd.Read(buf)
buf = buf[:n]
for _, l := range bytes.Split(buf, []byte("\n")) {
if bytes.HasPrefix(l, []byte("// Code generated")) {
return true
}
}
return false
}
// fileInfo finds the lowest year in which the given file was committed.
func fileInfo(file string) (*info, error) {
info := &info{file: file, Year: int64(time.Now().Year())}
cmd := exec.Command("git", "log", "--follow", "--find-renames=80", "--find-copies=80", "--pretty=format:%ai", "--", file)
err := doLines(cmd, func(line string) {
y, err := strconv.ParseInt(line[:4], 10, 64)
if err != nil {
fmt.Printf("cannot parse year: %q", line[:4])
}
if y < info.Year {
info.Year = y
}
})
return info, err
}
func writeLicenses(infos <-chan *info) {
for i := range infos {
writeLicense(i)
}
}
func writeLicense(info *info) {
fi, err := os.Stat(info.file)
if os.IsNotExist(err) {
fmt.Println("skipping (does not exist)", info.file)
return
}
if err != nil {
log.Fatalf("error stat'ing %s: %v\n", info.file, err)
}
content, err := ioutil.ReadFile(info.file)
if err != nil {
log.Fatalf("error reading %s: %v\n", info.file, err)
}
// Construct new file content.
buf := new(bytes.Buffer)
licenseT.Execute(buf, info)
if m := licenseCommentRE.FindIndex(content); m != nil && m[0] == 0 {
buf.Write(content[:m[0]])
buf.Write(content[m[1]:])
} else {
buf.Write(content)
}
// Write it to the file.
if bytes.Equal(content, buf.Bytes()) {
fmt.Println("skipping (no changes)", info.file)
return
}
fmt.Println("writing", info.ShortLicense(), info.file)
if err := ioutil.WriteFile(info.file, buf.Bytes(), fi.Mode()); err != nil {
log.Fatalf("error writing %s: %v", info.file, err)
}
}
func doLines(cmd *exec.Cmd, f func(string)) error {
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
s := bufio.NewScanner(stdout)
for s.Scan() {
f(s.Text())
}
if s.Err() != nil {
return s.Err()
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("%v (for %s)", err, strings.Join(cmd.Args, " "))
}
return nil
}