update-license.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. //go:build none
  17. // +build none
  18. /*
  19. This command generates GPL license headers on top of all source files.
  20. You can run it once per month, before cutting a release or just
  21. whenever you feel like it.
  22. go run update-license.go
  23. All authors (people who have contributed code) are listed in the
  24. AUTHORS file. The author names are mapped and deduplicated using the
  25. .mailmap file. You can use .mailmap to set the canonical name and
  26. address for each author. See git-shortlog(1) for an explanation of the
  27. .mailmap format.
  28. Please review the resulting diff to check whether the correct
  29. copyright assignments are performed.
  30. */
  31. package main
  32. import (
  33. "bufio"
  34. "bytes"
  35. "fmt"
  36. "log"
  37. "os"
  38. "os/exec"
  39. "path/filepath"
  40. "regexp"
  41. "runtime"
  42. "sort"
  43. "strconv"
  44. "strings"
  45. "sync"
  46. "text/template"
  47. "time"
  48. )
  49. var (
  50. // only files with these extensions will be considered
  51. extensions = []string{".go", ".js", ".qml"}
  52. // paths with any of these prefixes will be skipped
  53. skipPrefixes = []string{
  54. // boring stuff
  55. "vendor/", "tests/testdata/", "build/",
  56. // don't relicense vendored sources
  57. "cmd/internal/browser",
  58. "common/bitutil/bitutil",
  59. "common/prque/",
  60. "consensus/ethash/xor.go",
  61. "crypto/blake2b/",
  62. "crypto/bn256/",
  63. "crypto/bls12381/",
  64. "crypto/ecies/",
  65. "graphql/graphiql.go",
  66. "internal/jsre/deps",
  67. "log/",
  68. "metrics/",
  69. "signer/rules/deps",
  70. // skip special licenses
  71. "crypto/secp256k1", // Relicensed to BSD-3 via https://github.com/ethereum/go-ethereum/pull/17225
  72. }
  73. // paths with this prefix are licensed as GPL. all other files are LGPL.
  74. gplPrefixes = []string{"cmd/"}
  75. // this regexp must match the entire license comment at the
  76. // beginning of each file.
  77. licenseCommentRE = regexp.MustCompile(`^//\s*(Copyright|This file is part of).*?\n(?://.*?\n)*\n*`)
  78. // this text appears at the start of AUTHORS
  79. authorsFileHeader = "# This is the official list of go-ethereum authors for copyright purposes.\n\n"
  80. )
  81. // this template generates the license comment.
  82. // its input is an info structure.
  83. var licenseT = template.Must(template.New("").Parse(`
  84. // Copyright {{.Year}} The go-ethereum Authors
  85. // This file is part of {{.Whole false}}.
  86. //
  87. // {{.Whole true}} is free software: you can redistribute it and/or modify
  88. // it under the terms of the GNU {{.License}} as published by
  89. // the Free Software Foundation, either version 3 of the License, or
  90. // (at your option) any later version.
  91. //
  92. // {{.Whole true}} is distributed in the hope that it will be useful,
  93. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  94. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  95. // GNU {{.License}} for more details.
  96. //
  97. // You should have received a copy of the GNU {{.License}}
  98. // along with {{.Whole false}}. If not, see <http://www.gnu.org/licenses/>.
  99. `[1:]))
  100. type info struct {
  101. file string
  102. Year int64
  103. }
  104. func (i info) License() string {
  105. if i.gpl() {
  106. return "General Public License"
  107. }
  108. return "Lesser General Public License"
  109. }
  110. func (i info) ShortLicense() string {
  111. if i.gpl() {
  112. return "GPL"
  113. }
  114. return "LGPL"
  115. }
  116. func (i info) Whole(startOfSentence bool) string {
  117. if i.gpl() {
  118. return "go-ethereum"
  119. }
  120. if startOfSentence {
  121. return "The go-ethereum library"
  122. }
  123. return "the go-ethereum library"
  124. }
  125. func (i info) gpl() bool {
  126. for _, p := range gplPrefixes {
  127. if strings.HasPrefix(i.file, p) {
  128. return true
  129. }
  130. }
  131. return false
  132. }
  133. // authors implements the sort.Interface for strings in case-insensitive mode.
  134. type authors []string
  135. func (as authors) Len() int { return len(as) }
  136. func (as authors) Less(i, j int) bool { return strings.ToLower(as[i]) < strings.ToLower(as[j]) }
  137. func (as authors) Swap(i, j int) { as[i], as[j] = as[j], as[i] }
  138. func main() {
  139. var (
  140. files = getFiles()
  141. filec = make(chan string)
  142. infoc = make(chan *info, 20)
  143. wg sync.WaitGroup
  144. )
  145. writeAuthors(files)
  146. go func() {
  147. for _, f := range files {
  148. filec <- f
  149. }
  150. close(filec)
  151. }()
  152. for i := runtime.NumCPU(); i >= 0; i-- {
  153. // getting file info is slow and needs to be parallel.
  154. // it traverses git history for each file.
  155. wg.Add(1)
  156. go getInfo(filec, infoc, &wg)
  157. }
  158. go func() {
  159. wg.Wait()
  160. close(infoc)
  161. }()
  162. writeLicenses(infoc)
  163. }
  164. func skipFile(path string) bool {
  165. if strings.Contains(path, "/testdata/") {
  166. return true
  167. }
  168. for _, p := range skipPrefixes {
  169. if strings.HasPrefix(path, p) {
  170. return true
  171. }
  172. }
  173. return false
  174. }
  175. func getFiles() []string {
  176. cmd := exec.Command("git", "ls-tree", "-r", "--name-only", "HEAD")
  177. var files []string
  178. err := doLines(cmd, func(line string) {
  179. if skipFile(line) {
  180. return
  181. }
  182. ext := filepath.Ext(line)
  183. for _, wantExt := range extensions {
  184. if ext == wantExt {
  185. goto keep
  186. }
  187. }
  188. return
  189. keep:
  190. files = append(files, line)
  191. })
  192. if err != nil {
  193. log.Fatal("error getting files:", err)
  194. }
  195. return files
  196. }
  197. var authorRegexp = regexp.MustCompile(`\s*[0-9]+\s*(.*)`)
  198. func gitAuthors(files []string) []string {
  199. cmds := []string{"shortlog", "-s", "-n", "-e", "HEAD", "--"}
  200. cmds = append(cmds, files...)
  201. cmd := exec.Command("git", cmds...)
  202. var authors []string
  203. err := doLines(cmd, func(line string) {
  204. m := authorRegexp.FindStringSubmatch(line)
  205. if len(m) > 1 {
  206. authors = append(authors, m[1])
  207. }
  208. })
  209. if err != nil {
  210. log.Fatalln("error getting authors:", err)
  211. }
  212. return authors
  213. }
  214. func readAuthors() []string {
  215. content, err := os.ReadFile("AUTHORS")
  216. if err != nil && !os.IsNotExist(err) {
  217. log.Fatalln("error reading AUTHORS:", err)
  218. }
  219. var authors []string
  220. for _, a := range bytes.Split(content, []byte("\n")) {
  221. if len(a) > 0 && a[0] != '#' {
  222. authors = append(authors, string(a))
  223. }
  224. }
  225. // Retranslate existing authors through .mailmap.
  226. // This should catch email address changes.
  227. authors = mailmapLookup(authors)
  228. return authors
  229. }
  230. func mailmapLookup(authors []string) []string {
  231. if len(authors) == 0 {
  232. return nil
  233. }
  234. cmds := []string{"check-mailmap", "--"}
  235. cmds = append(cmds, authors...)
  236. cmd := exec.Command("git", cmds...)
  237. var translated []string
  238. err := doLines(cmd, func(line string) {
  239. translated = append(translated, line)
  240. })
  241. if err != nil {
  242. log.Fatalln("error translating authors:", err)
  243. }
  244. return translated
  245. }
  246. func writeAuthors(files []string) {
  247. var (
  248. dedup = make(map[string]bool)
  249. list []string
  250. )
  251. // Add authors that Git reports as contributors.
  252. // This is the primary source of author information.
  253. for _, a := range gitAuthors(files) {
  254. if la := strings.ToLower(a); !dedup[la] {
  255. list = append(list, a)
  256. dedup[la] = true
  257. }
  258. }
  259. // Add existing authors from the file. This should ensure that we
  260. // never lose authors, even if Git stops listing them. We can also
  261. // add authors manually this way.
  262. for _, a := range readAuthors() {
  263. if la := strings.ToLower(a); !dedup[la] {
  264. list = append(list, a)
  265. dedup[la] = true
  266. }
  267. }
  268. // Write sorted list of authors back to the file.
  269. sort.Sort(authors(list))
  270. content := new(bytes.Buffer)
  271. content.WriteString(authorsFileHeader)
  272. for _, a := range list {
  273. content.WriteString(a)
  274. content.WriteString("\n")
  275. }
  276. fmt.Println("writing AUTHORS")
  277. if err := os.WriteFile("AUTHORS", content.Bytes(), 0644); err != nil {
  278. log.Fatalln(err)
  279. }
  280. }
  281. func getInfo(files <-chan string, out chan<- *info, wg *sync.WaitGroup) {
  282. for file := range files {
  283. stat, err := os.Lstat(file)
  284. if err != nil {
  285. fmt.Printf("ERROR %s: %v\n", file, err)
  286. continue
  287. }
  288. if !stat.Mode().IsRegular() {
  289. continue
  290. }
  291. if isGenerated(file) {
  292. continue
  293. }
  294. info, err := fileInfo(file)
  295. if err != nil {
  296. fmt.Printf("ERROR %s: %v\n", file, err)
  297. continue
  298. }
  299. out <- info
  300. }
  301. wg.Done()
  302. }
  303. func isGenerated(file string) bool {
  304. fd, err := os.Open(file)
  305. if err != nil {
  306. return false
  307. }
  308. defer fd.Close()
  309. buf := make([]byte, 2048)
  310. n, _ := fd.Read(buf)
  311. buf = buf[:n]
  312. for _, l := range bytes.Split(buf, []byte("\n")) {
  313. if bytes.HasPrefix(l, []byte("// Code generated")) {
  314. return true
  315. }
  316. }
  317. return false
  318. }
  319. // fileInfo finds the lowest year in which the given file was committed.
  320. func fileInfo(file string) (*info, error) {
  321. info := &info{file: file, Year: int64(time.Now().Year())}
  322. cmd := exec.Command("git", "log", "--follow", "--find-renames=80", "--find-copies=80", "--pretty=format:%ai", "--", file)
  323. err := doLines(cmd, func(line string) {
  324. y, err := strconv.ParseInt(line[:4], 10, 64)
  325. if err != nil {
  326. fmt.Printf("cannot parse year: %q", line[:4])
  327. }
  328. if y < info.Year {
  329. info.Year = y
  330. }
  331. })
  332. return info, err
  333. }
  334. func writeLicenses(infos <-chan *info) {
  335. for i := range infos {
  336. writeLicense(i)
  337. }
  338. }
  339. func writeLicense(info *info) {
  340. fi, err := os.Stat(info.file)
  341. if os.IsNotExist(err) {
  342. fmt.Println("skipping (does not exist)", info.file)
  343. return
  344. }
  345. if err != nil {
  346. log.Fatalf("error stat'ing %s: %v\n", info.file, err)
  347. }
  348. content, err := os.ReadFile(info.file)
  349. if err != nil {
  350. log.Fatalf("error reading %s: %v\n", info.file, err)
  351. }
  352. // Construct new file content.
  353. buf := new(bytes.Buffer)
  354. licenseT.Execute(buf, info)
  355. if m := licenseCommentRE.FindIndex(content); m != nil && m[0] == 0 {
  356. buf.Write(content[:m[0]])
  357. buf.Write(content[m[1]:])
  358. } else {
  359. buf.Write(content)
  360. }
  361. // Write it to the file.
  362. if bytes.Equal(content, buf.Bytes()) {
  363. fmt.Println("skipping (no changes)", info.file)
  364. return
  365. }
  366. fmt.Println("writing", info.ShortLicense(), info.file)
  367. if err := os.WriteFile(info.file, buf.Bytes(), fi.Mode()); err != nil {
  368. log.Fatalf("error writing %s: %v", info.file, err)
  369. }
  370. }
  371. func doLines(cmd *exec.Cmd, f func(string)) error {
  372. stdout, err := cmd.StdoutPipe()
  373. if err != nil {
  374. return err
  375. }
  376. if err := cmd.Start(); err != nil {
  377. return err
  378. }
  379. s := bufio.NewScanner(stdout)
  380. for s.Scan() {
  381. f(s.Text())
  382. }
  383. if s.Err() != nil {
  384. return s.Err()
  385. }
  386. if err := cmd.Wait(); err != nil {
  387. return fmt.Errorf("%v (for %s)", err, strings.Join(cmd.Args, " "))
  388. }
  389. return nil
  390. }