update-license.go 8.2 KB

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