copyrights.go 13 KB

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