copyrights.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // Copyright (C) 2025 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:build ignore
  7. // +build ignore
  8. // Updates the list of software copyrights in aboutModalView.html based on the
  9. // output of `go mod graph`.
  10. package main
  11. import (
  12. "bufio"
  13. "bytes"
  14. "encoding/base64"
  15. "encoding/json"
  16. "fmt"
  17. "io"
  18. "log"
  19. "net/http"
  20. "net/url"
  21. "os"
  22. "os/exec"
  23. "regexp"
  24. "slices"
  25. "strconv"
  26. "strings"
  27. "time"
  28. "golang.org/x/net/html"
  29. )
  30. var copyrightMap = map[string]string{
  31. // https://github.com/aws/aws-sdk-go/blob/main/NOTICE.txt#L2
  32. "aws/aws-sdk-go": "Copyright © 2015 Amazon.com, Inc. or its affiliates, Copyright 2014-2015 Stripe, Inc",
  33. // https://github.com/ccding/go-stun/blob/master/main.go#L1
  34. "ccding/go-stun": "Copyright © 2016 Cong Ding",
  35. // https://github.com/search?q=repo%3Acertifi%2Fgocertifi%20copyright&type=code
  36. // "certifi/gocertifi": "No copyrights found",
  37. // https://github.com/search?q=repo%3Aebitengine%2Fpurego%20copyright&type=code
  38. "ebitengine/purego": "Copyright © 2022 The Ebitengine Authors",
  39. // https://github.com/search?q=repo%3Agoogle%2Fpprof%20copyright&type=code
  40. "google/pprof": "Copyright © 2016 Google Inc",
  41. // https://github.com/greatroar/blobloom/blob/master/README.md?plain=1#L74
  42. "greatroar/blobloom": "Copyright © 2020-2024 the Blobloom authors",
  43. // https://github.com/jmespath/go-jmespath/blob/master/NOTICE#L2
  44. "jmespath/go-jmespath": "Copyright © 2015 James Saryerwinnie",
  45. // https://github.com/maxmind/geoipupdate/blob/main/README.md?plain=1#L140
  46. "maxmind/geoipupdate": "Copyright © 2018-2024 by MaxMind, Inc",
  47. // https://github.com/search?q=repo%3Apuzpuzpuz%2Fxsync%20copyright&type=code
  48. // "puzpuzpuz/xsync": "No copyrights found",
  49. // https://github.com/search?q=repo%3Atklauser%2Fnumcpus%20copyright&type=code
  50. "tklauser/numcpus": "Copyright © 2018-2024 Tobias Klauser",
  51. // https://github.com/search?q=repo%3Auber-go%2Fmock%20copyright&type=code
  52. "go.uber.org/mock": "Copyright © 2010-2022 Google LLC",
  53. }
  54. var urlMap = map[string]string{
  55. "fontawesome.io": "https://github.com/FortAwesome/Font-Awesome",
  56. "go.uber.org/automaxprocs": "https://github.com/uber-go/automaxprocs",
  57. "go.uber.org/mock": "https://github.com/uber-go/mock",
  58. "google.golang.org/protobuf": "https://github.com/protocolbuffers/protobuf-go",
  59. "gopkg.in/yaml.v2": "", // ignore, as gopkg.in/yaml.v3 supersedes
  60. "gopkg.in/yaml.v3": "https://github.com/go-yaml/yaml",
  61. "sigs.k8s.io/yaml": "https://github.com/kubernetes-sigs/yaml",
  62. }
  63. const htmlFile = "gui/default/syncthing/core/aboutModalView.html"
  64. type Type int
  65. const (
  66. // TypeJS defines non-Go copyright notices
  67. TypeJS Type = iota
  68. // TypeKeep defines Go copyright notices for packages that are still used.
  69. TypeKeep
  70. // TypeToss defines Go copyright notices for packages that are no longer used.
  71. TypeToss
  72. // TypeNew defines Go copyright notices for new packages found via `go mod graph`.
  73. TypeNew
  74. )
  75. type CopyrightNotice struct {
  76. Type Type
  77. Name string
  78. HTML string
  79. Module string
  80. URL string
  81. Copyright string
  82. RepoURL string
  83. RepoCopyrights []string
  84. }
  85. var copyrightRe = regexp.MustCompile(`(?s)id="copyright-notices">(.+?)</ul>`)
  86. func main() {
  87. bs := readAll(htmlFile)
  88. matches := copyrightRe.FindStringSubmatch(string(bs))
  89. if len(matches) <= 1 {
  90. log.Fatal("Cannot find id copyright-notices in ", htmlFile)
  91. }
  92. modules := getModules()
  93. notices := parseCopyrightNotices(matches[1])
  94. old := len(notices)
  95. // match up modules to notices
  96. matched := map[string]bool{}
  97. removes := 0
  98. for i, notice := range notices {
  99. if notice.Type == TypeJS {
  100. continue
  101. }
  102. found := ""
  103. for _, module := range modules {
  104. if strings.Contains(module, notice.Name) {
  105. found = module
  106. break
  107. }
  108. }
  109. if found != "" {
  110. matched[found] = true
  111. notices[i].Module = found
  112. continue
  113. }
  114. removes++
  115. fmt.Printf("Removing: %-40s %-55s %s\n", notice.Name, notice.URL, notice.Copyright)
  116. notices[i].Type = TypeToss
  117. }
  118. // add new modules to notices
  119. adds := 0
  120. for _, module := range modules {
  121. _, ok := matched[module]
  122. if ok {
  123. continue
  124. }
  125. adds++
  126. notice := CopyrightNotice{}
  127. notice.Name = module
  128. if strings.HasPrefix(notice.Name, "github.com/") {
  129. notice.Name = strings.ReplaceAll(notice.Name, "github.com/", "")
  130. }
  131. notice.Type = TypeNew
  132. url, ok := urlMap[module]
  133. if ok {
  134. notice.URL = url
  135. notice.RepoURL = url
  136. } else {
  137. notice.URL = "https://" + module
  138. notice.RepoURL = "https://" + module
  139. }
  140. notices = append(notices, notice)
  141. }
  142. if removes == 0 && adds == 0 {
  143. // authors.go is quiet, so let's be quiet too.
  144. // fmt.Printf("No changes detected in %d modules and %d notices\n", len(modules), len(notices))
  145. os.Exit(0)
  146. }
  147. // get copyrights via Github API for new modules
  148. notfound := 0
  149. for i, n := range notices {
  150. if n.Type != TypeNew {
  151. continue
  152. }
  153. copyright, ok := copyrightMap[n.Name]
  154. if ok {
  155. notices[i].Copyright = copyright
  156. continue
  157. }
  158. notices[i].Copyright = defaultCopyright(n)
  159. if strings.Contains(n.URL, "github.com/") {
  160. notices[i].RepoURL = notices[i].URL
  161. owner, repo := parseGitHubURL(n.URL)
  162. licenseText := getLicenseText(owner, repo)
  163. notices[i].RepoCopyrights = extractCopyrights(licenseText, n)
  164. if len(notices[i].RepoCopyrights) > 0 {
  165. notices[i].Copyright = notices[i].RepoCopyrights[0]
  166. }
  167. notices[i].HTML = fmt.Sprintf("<li><a href=\"%s\">%s</a>, %s.</li>", n.URL, n.Name, notices[i].Copyright)
  168. if len(notices[i].RepoCopyrights) > 0 {
  169. continue
  170. }
  171. }
  172. fmt.Printf("Copyright not found: %-30s : using %q\n", n.Name, notices[i].Copyright)
  173. notfound++
  174. }
  175. replacements := write(notices, bs)
  176. fmt.Printf("Removed: %3d\n", removes)
  177. fmt.Printf("Added: %3d\n", adds)
  178. fmt.Printf("Copyrights not found: %3d\n", notfound)
  179. fmt.Printf("Old package count: %3d\n", old)
  180. fmt.Printf("New package count: %3d\n", replacements)
  181. }
  182. func write(notices []CopyrightNotice, bs []byte) int {
  183. keys := make([]string, 0, len(notices))
  184. noticeMap := make(map[string]CopyrightNotice, 0)
  185. for _, n := range notices {
  186. if n.Type != TypeKeep && n.Type != TypeNew {
  187. continue
  188. }
  189. if n.Type == TypeNew {
  190. fmt.Printf("Adding: %-40s %-55s %s\n", n.Name, n.URL, n.Copyright)
  191. }
  192. keys = append(keys, n.Name)
  193. noticeMap[n.Name] = n
  194. }
  195. slices.Sort(keys)
  196. indent := " "
  197. replacements := []string{}
  198. for _, n := range notices {
  199. if n.Type != TypeJS {
  200. continue
  201. }
  202. replacements = append(replacements, indent+n.HTML)
  203. }
  204. for _, k := range keys {
  205. n := noticeMap[k]
  206. line := fmt.Sprintf("%s<li><a href=\"%s\">%s</a>, %s.</li>", indent, n.URL, n.Name, n.Copyright)
  207. replacements = append(replacements, line)
  208. }
  209. replacement := strings.Join(replacements, "\n")
  210. bs = copyrightRe.ReplaceAll(bs, []byte("id=\"copyright-notices\">\n"+replacement+"\n </ul>"))
  211. writeFile(htmlFile, string(bs))
  212. return len(replacements)
  213. }
  214. func readAll(path string) []byte {
  215. fd, err := os.Open(path)
  216. if err != nil {
  217. log.Fatal(err)
  218. }
  219. defer fd.Close()
  220. bs, err := io.ReadAll(fd)
  221. if err != nil {
  222. log.Fatal(err)
  223. }
  224. return bs
  225. }
  226. func writeFile(path string, data string) {
  227. err := os.WriteFile(path, []byte(data), 0o644)
  228. if err != nil {
  229. log.Fatal(err)
  230. }
  231. }
  232. func getModules() []string {
  233. cmd := exec.Command("go", "mod", "graph")
  234. output, err := cmd.Output()
  235. if err != nil {
  236. log.Fatal(err)
  237. }
  238. seen := make(map[string]struct{})
  239. scanner := bufio.NewScanner(bytes.NewReader(output))
  240. for scanner.Scan() {
  241. line := scanner.Text()
  242. fields := strings.Fields(line)
  243. if len(fields) == 0 {
  244. continue
  245. }
  246. if !strings.HasPrefix(fields[0], "github.com/syncthing/syncthing") {
  247. continue
  248. }
  249. // Get left-hand side of dependency pair (before '@')
  250. mod := strings.SplitN(fields[1], "@", 2)[0]
  251. // Keep only first 3 path components
  252. parts := strings.Split(mod, "/")
  253. if len(parts) == 1 {
  254. continue
  255. }
  256. short := strings.Join(parts[:min(len(parts), 3)], "/")
  257. if strings.HasPrefix(short, "golang.org/x") ||
  258. strings.HasPrefix(short, "github.com/prometheus") ||
  259. short == "go" {
  260. continue
  261. }
  262. seen[short] = struct{}{}
  263. }
  264. adds := make([]string, 0)
  265. for k := range seen {
  266. adds = append(adds, k)
  267. }
  268. slices.Sort(adds)
  269. return adds
  270. }
  271. func parseCopyrightNotices(input string) []CopyrightNotice {
  272. doc, err := html.Parse(strings.NewReader("<ul>" + input + "</ul>"))
  273. if err != nil {
  274. log.Fatal(err)
  275. }
  276. var notices []CopyrightNotice
  277. typ := TypeJS
  278. var f func(*html.Node)
  279. f = func(n *html.Node) {
  280. if n.Type == html.ElementNode && n.Data == "li" {
  281. var notice CopyrightNotice
  282. var aFound bool
  283. for c := n.FirstChild; c != nil; c = c.NextSibling {
  284. if c.Type == html.ElementNode && c.Data == "a" {
  285. aFound = true
  286. for _, attr := range c.Attr {
  287. if attr.Key == "href" {
  288. notice.URL = attr.Val
  289. }
  290. }
  291. if c.FirstChild != nil && c.FirstChild.Type == html.TextNode {
  292. notice.Name = strings.TrimSpace(c.FirstChild.Data)
  293. }
  294. } else if c.Type == html.TextNode && aFound {
  295. // Anything after <a> is considered the copyright
  296. notice.Copyright = strings.TrimSpace(html.UnescapeString(c.Data))
  297. notice.Copyright = strings.Trim(notice.Copyright, "., ")
  298. }
  299. if typ == TypeJS && strings.Contains(notice.URL, "AudriusButkevicius") {
  300. typ = TypeKeep
  301. }
  302. notice.Type = typ
  303. var buf strings.Builder
  304. _ = html.Render(&buf, n)
  305. notice.HTML = buf.String()
  306. }
  307. notice.Copyright = strings.ReplaceAll(notice.Copyright, "©", "&copy;")
  308. notice.HTML = strings.ReplaceAll(notice.HTML, "©", "&copy;")
  309. notices = append(notices, notice)
  310. }
  311. for c := n.FirstChild; c != nil; c = c.NextSibling {
  312. f(c)
  313. }
  314. }
  315. f(doc)
  316. return notices
  317. }
  318. func parseGitHubURL(u string) (string, string) {
  319. parsed, err := url.Parse(u)
  320. if err != nil {
  321. log.Fatal(err)
  322. }
  323. parts := strings.Split(strings.Trim(parsed.Path, "/"), "/")
  324. if len(parts) < 2 {
  325. log.Fatal(fmt.Errorf("invalid GitHub URL: %q", parsed.Path))
  326. }
  327. return parts[0], parts[1]
  328. }
  329. func getLicenseText(owner, repo string) string {
  330. url := fmt.Sprintf("https://api.github.com/repos/%s/%s/license", owner, repo)
  331. req, _ := http.NewRequest("GET", url, nil)
  332. req.Header.Set("Accept", "application/vnd.github.v3+json")
  333. if token := os.Getenv("GITHUB_TOKEN"); token != "" {
  334. req.Header.Set("Authorization", "Bearer "+token)
  335. }
  336. resp, err := http.DefaultClient.Do(req)
  337. if err != nil {
  338. log.Fatal(err)
  339. }
  340. defer resp.Body.Close()
  341. var result struct {
  342. Content string `json:"content"`
  343. Encoding string `json:"encoding"`
  344. }
  345. body, _ := io.ReadAll(resp.Body)
  346. err = json.Unmarshal(body, &result)
  347. if err != nil {
  348. log.Fatal(err)
  349. }
  350. if result.Encoding != "base64" {
  351. log.Fatal(fmt.Sprintf("unexpected encoding: %s", result.Encoding))
  352. }
  353. decoded, err := base64.StdEncoding.DecodeString(result.Content)
  354. if err != nil {
  355. log.Fatal(err)
  356. }
  357. return string(decoded)
  358. }
  359. func extractCopyrights(license string, notice CopyrightNotice) []string {
  360. lines := strings.Split(license, "\n")
  361. re := regexp.MustCompile(`(?i)^\s*(copyright\s*(?:©|\(c\)|&copy;|19|20).*)$`)
  362. copyrights := []string{}
  363. for _, line := range lines {
  364. if matches := re.FindStringSubmatch(strings.TrimSpace(line)); len(matches) == 2 {
  365. copyright := strings.TrimSpace(matches[1])
  366. re := regexp.MustCompile(`(?i)all rights reserved`)
  367. copyright = re.ReplaceAllString(copyright, "")
  368. copyright = strings.ReplaceAll(copyright, "©", "&copy;")
  369. copyright = strings.ReplaceAll(copyright, "(C)", "&copy;")
  370. copyright = strings.ReplaceAll(copyright, "(c)", "&copy;")
  371. copyright = strings.Trim(copyright, "., ")
  372. copyrights = append(copyrights, copyright)
  373. }
  374. }
  375. if len(copyrights) > 0 {
  376. return copyrights
  377. }
  378. return []string{}
  379. }
  380. func defaultCopyright(n CopyrightNotice) string {
  381. year := time.Now().Format("2006")
  382. return fmt.Sprintf("Copyright &copy; %v, the %s authors", year, n.Name)
  383. }
  384. func writeNotices(path string, notices []CopyrightNotice) {
  385. s := ""
  386. for i, n := range notices {
  387. s += "# : " + strconv.Itoa(i) + "\n" + n.String()
  388. }
  389. writeFile(path, s)
  390. }
  391. func (n CopyrightNotice) String() string {
  392. return fmt.Sprintf("Type : %v\nHTML : %v\nName : %v\nModule : %v\nURL : %v\nCopyright: %v\nRepoURL : %v\nRepoCopys: %v\n\n",
  393. n.Type, n.HTML, n.Name, n.Module, n.URL, n.Copyright, n.RepoURL, strings.Join(n.RepoCopyrights, ","))
  394. }
  395. func (t Type) String() string {
  396. switch t {
  397. case TypeJS:
  398. return "TypeJS"
  399. case TypeKeep:
  400. return "TypeKeep"
  401. case TypeToss:
  402. return "TypeToss"
  403. case TypeNew:
  404. return "TypeNew"
  405. default:
  406. return "unknown"
  407. }
  408. }