2019-01-03 11:09:04 +03:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-05-24 21:39:40 +03:00
|
|
|
//go:build none
|
2015-07-07 03:40:55 +03:00
|
|
|
// +build none
|
|
|
|
|
|
|
|
/*
|
|
|
|
This command generates GPL license headers on top of all source files.
|
|
|
|
You can run it once per month, before cutting a release or just
|
|
|
|
whenever you feel like it.
|
|
|
|
|
|
|
|
go run update-license.go
|
|
|
|
|
|
|
|
All authors (people who have contributed code) are listed in the
|
|
|
|
AUTHORS file. The author names are mapped and deduplicated using the
|
|
|
|
.mailmap file. You can use .mailmap to set the canonical name and
|
|
|
|
address for each author. See git-shortlog(1) for an explanation of the
|
|
|
|
.mailmap format.
|
|
|
|
|
|
|
|
Please review the resulting diff to check whether the correct
|
|
|
|
copyright assignments are performed.
|
|
|
|
*/
|
2015-07-07 06:08:16 +03:00
|
|
|
|
2015-07-07 03:40:55 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
2023-06-18 16:09:00 +03:00
|
|
|
|
|
|
|
"golang.org/x/exp/slices"
|
2015-07-07 03:40:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// only files with these extensions will be considered
|
|
|
|
extensions = []string{".go", ".js", ".qml"}
|
|
|
|
|
|
|
|
// paths with any of these prefixes will be skipped
|
|
|
|
skipPrefixes = []string{
|
|
|
|
// boring stuff
|
2017-07-11 14:49:14 +03:00
|
|
|
"vendor/", "tests/testdata/", "build/",
|
2019-07-22 12:16:51 +03:00
|
|
|
|
2015-07-06 02:19:48 +03:00
|
|
|
// don't relicense vendored sources
|
2017-05-24 16:40:02 +03:00
|
|
|
"cmd/internal/browser",
|
2019-07-22 12:16:51 +03:00
|
|
|
"common/bitutil/bitutil",
|
|
|
|
"common/prque/",
|
2017-04-14 11:29:00 +03:00
|
|
|
"consensus/ethash/xor.go",
|
2022-05-24 21:39:40 +03:00
|
|
|
"crypto/blake2b/",
|
2017-05-24 16:40:02 +03:00
|
|
|
"crypto/bn256/",
|
2022-05-24 21:39:40 +03:00
|
|
|
"crypto/bls12381/",
|
2017-05-24 16:40:02 +03:00
|
|
|
"crypto/ecies/",
|
2019-07-22 12:16:51 +03:00
|
|
|
"graphql/graphiql.go",
|
2017-04-14 11:29:00 +03:00
|
|
|
"internal/jsre/deps",
|
2017-05-24 16:40:02 +03:00
|
|
|
"log/",
|
2019-07-22 12:16:51 +03:00
|
|
|
"metrics/",
|
|
|
|
"signer/rules/deps",
|
|
|
|
|
|
|
|
// skip special licenses
|
|
|
|
"crypto/secp256k1", // Relicensed to BSD-3 via https://github.com/ethereum/go-ethereum/pull/17225
|
2015-07-07 03:40:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// paths with this prefix are licensed as GPL. all other files are LGPL.
|
|
|
|
gplPrefixes = []string{"cmd/"}
|
|
|
|
|
|
|
|
// this regexp must match the entire license comment at the
|
|
|
|
// beginning of each file.
|
2015-07-07 05:28:01 +03:00
|
|
|
licenseCommentRE = regexp.MustCompile(`^//\s*(Copyright|This file is part of).*?\n(?://.*?\n)*\n*`)
|
2015-07-07 03:40:55 +03:00
|
|
|
|
|
|
|
// this text appears at the start of AUTHORS
|
|
|
|
authorsFileHeader = "# This is the official list of go-ethereum 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 {{.Year}} The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of {{.Whole false}}.
|
2015-07-07 03:40:55 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// {{.Whole true}} is free software: you can redistribute it and/or modify
|
2015-07-07 03:40:55 +03:00
|
|
|
// it under the terms of the GNU {{.License}} as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// {{.Whole true}} is distributed in the hope that it will be useful,
|
2015-07-07 03:40:55 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:40:55 +03:00
|
|
|
// GNU {{.License}} for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU {{.License}}
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with {{.Whole false}}. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:40:55 +03:00
|
|
|
|
|
|
|
`[1:]))
|
|
|
|
|
|
|
|
type info struct {
|
|
|
|
file string
|
|
|
|
Year int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i info) License() string {
|
|
|
|
if i.gpl() {
|
|
|
|
return "General Public License"
|
|
|
|
}
|
2015-07-22 19:48:40 +03:00
|
|
|
return "Lesser General Public License"
|
2015-07-07 03:40:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i info) ShortLicense() string {
|
|
|
|
if i.gpl() {
|
|
|
|
return "GPL"
|
|
|
|
}
|
2015-07-22 19:48:40 +03:00
|
|
|
return "LGPL"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i info) Whole(startOfSentence bool) string {
|
|
|
|
if i.gpl() {
|
|
|
|
return "go-ethereum"
|
|
|
|
}
|
|
|
|
if startOfSentence {
|
|
|
|
return "The go-ethereum library"
|
|
|
|
}
|
|
|
|
return "the go-ethereum library"
|
2015-07-07 03:40:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i info) gpl() bool {
|
|
|
|
for _, p := range gplPrefixes {
|
|
|
|
if strings.HasPrefix(i.file, p) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:16:51 +03:00
|
|
|
func skipFile(path string) bool {
|
|
|
|
if strings.Contains(path, "/testdata/") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, p := range skipPrefixes {
|
|
|
|
if strings.HasPrefix(path, p) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-07 03:40:55 +03:00
|
|
|
func getFiles() []string {
|
|
|
|
cmd := exec.Command("git", "ls-tree", "-r", "--name-only", "HEAD")
|
|
|
|
var files []string
|
|
|
|
err := doLines(cmd, func(line string) {
|
2016-04-14 19:16:51 +03:00
|
|
|
if skipFile(line) {
|
|
|
|
return
|
2015-07-07 03:40:55 +03:00
|
|
|
}
|
|
|
|
ext := filepath.Ext(line)
|
|
|
|
for _, wantExt := range extensions {
|
|
|
|
if ext == wantExt {
|
|
|
|
goto keep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
keep:
|
|
|
|
files = append(files, line)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-01-09 11:52:26 +03:00
|
|
|
log.Fatal("error getting files:", err)
|
2015-07-07 03:40:55 +03:00
|
|
|
}
|
|
|
|
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 {
|
2022-05-16 12:59:35 +03:00
|
|
|
content, err := os.ReadFile("AUTHORS")
|
2015-07-07 03:40:55 +03:00
|
|
|
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) {
|
2019-07-22 12:31:11 +03:00
|
|
|
var (
|
|
|
|
dedup = make(map[string]bool)
|
|
|
|
list []string
|
|
|
|
)
|
2019-07-22 12:16:51 +03:00
|
|
|
// Add authors that Git reports as contributors.
|
2015-07-07 03:40:55 +03:00
|
|
|
// This is the primary source of author information.
|
|
|
|
for _, a := range gitAuthors(files) {
|
2019-07-22 12:31:11 +03:00
|
|
|
if la := strings.ToLower(a); !dedup[la] {
|
|
|
|
list = append(list, a)
|
|
|
|
dedup[la] = true
|
|
|
|
}
|
2015-07-07 03:40:55 +03:00
|
|
|
}
|
|
|
|
// 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() {
|
2019-07-22 12:31:11 +03:00
|
|
|
if la := strings.ToLower(a); !dedup[la] {
|
|
|
|
list = append(list, a)
|
|
|
|
dedup[la] = true
|
|
|
|
}
|
2015-07-07 03:40:55 +03:00
|
|
|
}
|
|
|
|
// Write sorted list of authors back to the file.
|
2023-06-18 16:09:00 +03:00
|
|
|
slices.SortFunc(list, func(a, b string) bool {
|
|
|
|
return strings.ToLower(a) < strings.ToLower(b)
|
|
|
|
})
|
2015-07-07 03:40:55 +03:00
|
|
|
content := new(bytes.Buffer)
|
|
|
|
content.WriteString(authorsFileHeader)
|
2019-07-22 12:31:11 +03:00
|
|
|
for _, a := range list {
|
2015-07-07 03:40:55 +03:00
|
|
|
content.WriteString(a)
|
|
|
|
content.WriteString("\n")
|
|
|
|
}
|
|
|
|
fmt.Println("writing AUTHORS")
|
2022-05-16 12:59:35 +03:00
|
|
|
if err := os.WriteFile("AUTHORS", content.Bytes(), 0644); err != nil {
|
2015-07-07 03:40:55 +03:00
|
|
|
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
|
|
|
|
}
|
2017-04-14 11:29:00 +03:00
|
|
|
if isGenerated(file) {
|
|
|
|
continue
|
|
|
|
}
|
2015-07-07 03:40:55 +03:00
|
|
|
info, err := fileInfo(file)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR %s: %v\n", file, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out <- info
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
2017-04-14 11:29:00 +03:00
|
|
|
func isGenerated(file string) bool {
|
|
|
|
fd, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
buf := make([]byte, 2048)
|
2022-08-30 15:40:15 +03:00
|
|
|
n, err := fd.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-04-14 11:29:00 +03:00
|
|
|
buf = buf[:n]
|
|
|
|
for _, l := range bytes.Split(buf, []byte("\n")) {
|
|
|
|
if bytes.HasPrefix(l, []byte("// Code generated")) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-06 20:44:35 +03:00
|
|
|
// fileInfo finds the lowest year in which the given file was committed.
|
2015-07-07 03:40:55 +03:00
|
|
|
func fileInfo(file string) (*info, error) {
|
|
|
|
info := &info{file: file, Year: int64(time.Now().Year())}
|
2016-04-14 19:16:51 +03:00
|
|
|
cmd := exec.Command("git", "log", "--follow", "--find-renames=80", "--find-copies=80", "--pretty=format:%ai", "--", file)
|
2015-07-07 03:40:55 +03:00
|
|
|
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)
|
|
|
|
}
|
2022-05-16 12:59:35 +03:00
|
|
|
content, err := os.ReadFile(info.file)
|
2015-07-07 03:40:55 +03:00
|
|
|
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)
|
2022-05-16 12:59:35 +03:00
|
|
|
if err := os.WriteFile(info.file, buf.Bytes(), fi.Mode()); err != nil {
|
2015-07-07 03:40:55 +03:00
|
|
|
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
|
|
|
|
}
|