Commit 3ff5cfd0 authored by Felix Lange's avatar Felix Lange

build: new update-license.go

This version is less clever. All names are listed in a single file,
AUTHORS. All source files have the same header. This is an improvement
over the previous version, which attempted to list copyright holders in
each source file.
parent 3016f238
...@@ -7,12 +7,11 @@ whenever you feel like it. ...@@ -7,12 +7,11 @@ whenever you feel like it.
go run update-license.go go run update-license.go
The copyright in each file is assigned to any authors for which git All authors (people who have contributed code) are listed in the
can find commits in the file's history. It will try to follow renames AUTHORS file. The author names are mapped and deduplicated using the
throughout history. The author names are mapped and deduplicated using .mailmap file. You can use .mailmap to set the canonical name and
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
address for each author. See git-shortlog(1) for an explanation .mailmap format.
of the .mailmap format.
Please review the resulting diff to check whether the correct Please review the resulting diff to check whether the correct
copyright assignments are performed. copyright assignments are performed.
...@@ -24,15 +23,18 @@ import ( ...@@ -24,15 +23,18 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"text/template" "text/template"
"time"
) )
var ( var (
...@@ -40,7 +42,12 @@ var ( ...@@ -40,7 +42,12 @@ var (
extensions = []string{".go", ".js", ".qml"} extensions = []string{".go", ".js", ".qml"}
// paths with any of these prefixes will be skipped // paths with any of these prefixes will be skipped
skipPrefixes = []string{"Godeps/", "tests/files/", "cmd/mist/assets/ext/", "cmd/mist/assets/muted/"} skipPrefixes = []string{
// boring stuff
"Godeps/", "tests/files/", "build/",
// don't relicense vendored packages
"crypto/sha3/", "crypto/ecies/", "logger/glog/",
}
// paths with this prefix are licensed as GPL. all other files are LGPL. // paths with this prefix are licensed as GPL. all other files are LGPL.
gplPrefixes = []string{"cmd/"} gplPrefixes = []string{"cmd/"}
...@@ -49,54 +56,38 @@ var ( ...@@ -49,54 +56,38 @@ var (
// beginning of each file. // beginning of each file.
licenseCommentRE = regexp.MustCompile(`(?s)^/\*\s*(Copyright|This file is part of) .*?\*/\n*`) licenseCommentRE = regexp.MustCompile(`(?s)^/\*\s*(Copyright|This file is part of) .*?\*/\n*`)
// this line is used when git doesn't find any authors for a file // this text appears at the start of AUTHORS
defaultCopyright = "Copyright (C) 2014 Jeffrey Wilcke <jeffrey@ethereum.org>" authorsFileHeader = "# This is the official list of go-ethereum authors for copyright purposes.\n\n"
) )
// this template generates the license comment. // this template generates the license comment.
// its input is an info structure. // its input is an info structure.
var licenseT = template.Must(template.New("").Parse(`/* var licenseT = template.Must(template.New("").Parse(`
{{.Copyrights}} // Copyright {{.Year}} The go-ethereum Authors
// This file is part of go-ethereum.
This file is part of go-ethereum //
// go-ethereum is free software: you can redistribute it and/or modify
go-ethereum is free software: you can redistribute it and/or modify // it under the terms of the GNU {{.License}} as published by
it under the terms of the GNU {{.License}} as published by // the Free Software Foundation, either version 3 of the License, or
the Free Software Foundation, either version 3 of the License, or // (at your option) any later version.
(at your option) any later version. //
// go-ethereum 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 {{.License}} for more details.
//
// You should have received a copy of the GNU {{.License}}
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
go-ethereum is distributed in the hope that it will be useful, `[1:]))
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU {{.License}} for more details.
You should have received a copy of the GNU {{.License}}
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
`))
type info struct { type info struct {
file string file string
mode os.FileMode Year int64
authors map[string][]string // map keys are authors, values are years
gpl bool
}
func (i info) Copyrights() string {
var lines []string
for name, years := range i.authors {
lines = append(lines, "Copyright (C) "+strings.Join(years, ", ")+" "+name)
}
if len(lines) == 0 {
lines = []string{defaultCopyright}
}
sort.Strings(lines)
return strings.Join(lines, "\n\t")
} }
func (i info) License() string { func (i info) License() string {
if i.gpl { if i.gpl() {
return "General Public License" return "General Public License"
} else { } else {
return "Lesser General Public License" return "Lesser General Public License"
...@@ -104,40 +95,54 @@ func (i info) License() string { ...@@ -104,40 +95,54 @@ func (i info) License() string {
} }
func (i info) ShortLicense() string { func (i info) ShortLicense() string {
if i.gpl { if i.gpl() {
return "GPL" return "GPL"
} else { } else {
return "LGPL" return "LGPL"
} }
} }
func (i *info) addAuthorYear(name, year string) { func (i info) gpl() bool {
for _, y := range i.authors[name] { for _, p := range gplPrefixes {
if y == year { if strings.HasPrefix(i.file, p) {
return return true
} }
} }
i.authors[name] = append(i.authors[name], year) return false
sort.Strings(i.authors[name])
} }
func main() { func main() {
files := make(chan string) var (
infos := make(chan *info) files = getFiles()
wg := new(sync.WaitGroup) filec = make(chan string)
infoc = make(chan *info, 20)
wg sync.WaitGroup
)
go getFiles(files) writeAuthors(files)
go func() {
for _, f := range files {
filec <- f
}
close(filec)
}()
for i := runtime.NumCPU(); i >= 0; i-- { for i := runtime.NumCPU(); i >= 0; i-- {
// getting file info is slow and needs to be parallel // getting file info is slow and needs to be parallel.
// it traverses git history for each file.
wg.Add(1) wg.Add(1)
go getInfo(files, infos, wg) go getInfo(filec, infoc, &wg)
} }
go func() { wg.Wait(); close(infos) }() go func() {
writeLicenses(infos) wg.Wait()
close(infoc)
}()
writeLicenses(infoc)
} }
func getFiles(out chan<- string) { func getFiles() []string {
cmd := exec.Command("git", "ls-tree", "-r", "--name-only", "HEAD") cmd := exec.Command("git", "ls-tree", "-r", "--name-only", "HEAD")
var files []string
err := doLines(cmd, func(line string) { err := doLines(cmd, func(line string) {
for _, p := range skipPrefixes { for _, p := range skipPrefixes {
if strings.HasPrefix(line, p) { if strings.HasPrefix(line, p) {
...@@ -147,18 +152,101 @@ func getFiles(out chan<- string) { ...@@ -147,18 +152,101 @@ func getFiles(out chan<- string) {
ext := filepath.Ext(line) ext := filepath.Ext(line)
for _, wantExt := range extensions { for _, wantExt := range extensions {
if ext == wantExt { if ext == wantExt {
goto send goto keep
} }
} }
return return
keep:
files = append(files, line)
})
if err != nil {
log.Fatalf("error getting files:", err)
}
return files
}
send: var authorRegexp = regexp.MustCompile(`\s*[0-9]+\s*(.*)`)
out <- line
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 { if err != nil {
fmt.Println("error getting files:", err) 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)
} }
close(out)
} }
func getInfo(files <-chan string, out chan<- *info, wg *sync.WaitGroup) { func getInfo(files <-chan string, out chan<- *info, wg *sync.WaitGroup) {
...@@ -176,53 +264,63 @@ func getInfo(files <-chan string, out chan<- *info, wg *sync.WaitGroup) { ...@@ -176,53 +264,63 @@ func getInfo(files <-chan string, out chan<- *info, wg *sync.WaitGroup) {
fmt.Printf("ERROR %s: %v\n", file, err) fmt.Printf("ERROR %s: %v\n", file, err)
continue continue
} }
info.mode = stat.Mode()
out <- info out <- info
} }
wg.Done() wg.Done()
} }
// fileInfo finds the lowest year in which the given file was commited.
func fileInfo(file string) (*info, error) { func fileInfo(file string) (*info, error) {
info := &info{file: file, authors: make(map[string][]string)} info := &info{file: file, Year: int64(time.Now().Year())}
for _, p := range gplPrefixes { cmd := exec.Command("git", "log", "--follow", "--find-copies", "--pretty=format:%ai", "--", file)
if strings.HasPrefix(file, p) {
info.gpl = true
break
}
}
cmd := exec.Command("git", "log", "--follow", "--find-copies", "--pretty=format:%ai | %aN <%aE>", "--", file)
err := doLines(cmd, func(line string) { err := doLines(cmd, func(line string) {
sep := strings.IndexByte(line, '|') y, err := strconv.ParseInt(line[:4], 10, 64)
year, name := line[:4], line[sep+2:] if err != nil {
info.addAuthorYear(name, year) fmt.Printf("cannot parse year: %q", line[:4])
}
if y < info.Year {
info.Year = y
}
}) })
return info, err return info, err
} }
func writeLicenses(infos <-chan *info) { func writeLicenses(infos <-chan *info) {
buf := new(bytes.Buffer) for i := range infos {
for info := range infos { writeLicense(i)
content, err := ioutil.ReadFile(info.file) }
if err != nil { }
fmt.Printf("ERROR: couldn't read %s: %v\n", info.file, err)
continue
}
// construct new file content
buf.Reset()
licenseT.Execute(buf, info)
if m := licenseCommentRE.FindIndex(content); m != nil && m[0] == 0 {
buf.Write(content[m[1]:])
} else {
buf.Write(content)
}
if !bytes.Equal(content, buf.Bytes()) { func writeLicense(info *info) {
fmt.Println("writing", info.ShortLicense(), info.file) fi, err := os.Stat(info.file)
if err := ioutil.WriteFile(info.file, buf.Bytes(), info.mode); err != nil { if os.IsNotExist(err) {
fmt.Printf("ERROR: couldn't write %s: %v", info.file, 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)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment