package main import ( "cmp" "fmt" "os" "path" "path/filepath" "slices" "strings" ) // header opens the notices. const header = `# Third-party licenses Überbringer is licensed under the European Union Public Licence v. 1.2 (EUPL-1.2). Its text is in LICENSE.txt, which comes with every release. The uberbringer binary also contains the Go standard library and runtime and the Go modules listed below. Their license and notice files follow, as each distribution ships them. A file in a subdirectory of a module covers the code in that directory. This file is generated by tools/genlicenses (go generate ./...) from the modules linked into the binary. Do not edit it by hand. ` // component is a part of the binary under licenses of its own. type component struct { name, version string // licenses are SPDX identifiers, in the order the files give them. licenses []string files []licenseText } // licenseText is a license or notice file as its distribution ships it. type licenseText struct { path, body string } // readComponent reads the license and notice files, named relative to dir, // that cover a component, and tells its licenses from them. func readComponent(name, version, dir string, files []string) (component, error) { c := component{name: name, version: version} for _, f := range files { b, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(f))) if err != nil { return component{}, fmt.Errorf("%s: %w", name, err) } body := normalize(string(b)) c.files = append(c.files, licenseText{path: f, body: body}) if isNoticeFile(path.Base(f)) { continue } ids := classify(body) if len(ids) == 0 { return component{}, fmt.Errorf("%s: %s holds no license that licenseMarkers knows; add its wording there", name, f) } for _, id := range ids { if !slices.Contains(c.licenses, id) { c.licenses = append(c.licenses, id) } } } if len(c.licenses) == 0 { return component{}, fmt.Errorf("%s ships no license file", name) } return c, nil } // normalize gives a file LF line endings and drops the blank lines around // it. Git keeps the generated files with LF endings, so a license file with // CRLF endings would otherwise never match its committed copy. func normalize(s string) string { s = strings.ReplaceAll(s, "\r\n", "\n") s = strings.TrimLeft(s, "\n") return strings.TrimRight(s, " \t\n") } // licenseMarkers are phrases, in lower case and with single spaces, that // the text of each license holds. "BSD" stands for BSD-2-Clause and // BSD-3-Clause, which classify tells apart. var licenseMarkers = []struct{ id, phrase string }{ {"Apache-2.0", "apache license version 2.0"}, {"Apache-2.0", "apache license, version 2.0"}, {"BSD", "redistribution and use in source and binary forms"}, {"ISC", "permission to use, copy, modify, and/or distribute this software for any purpose"}, {"MIT", "permission is hereby granted, free of charge, to any person obtaining a copy"}, {"MPL-2.0", "mozilla public license version 2.0"}, {"MPL-2.0", "mozilla public license, version 2.0"}, {"Unlicense", "this is free and unencumbered software released into the public domain"}, {"public domain", "all of the code and documentation in sqlite has been dedicated to the public domain"}, } // bsdThirdClause is in BSD-3-Clause and not in BSD-2-Clause. const bsdThirdClause = "endorse or promote products derived from this software" // classify returns the SPDX identifiers of the licenses a license file // holds, in the order they appear, or nil when it holds none it knows. A // file may hold several, such as a module's own license followed by those // of code it includes. func classify(body string) []string { text := strings.Join(strings.Fields(strings.ToLower(body)), " ") type hit struct { at int id string } var hits []hit for _, m := range licenseMarkers { for from := 0; ; { i := strings.Index(text[from:], m.phrase) if i < 0 { break } hits = append(hits, hit{from + i, m.id}) from += i + len(m.phrase) } } slices.SortFunc(hits, func(a, b hit) int { return cmp.Compare(a.at, b.at) }) var ids []string for i, h := range hits { id := h.id if id == "BSD" { end := len(text) if i+1 < len(hits) { end = hits[i+1].at } id = "BSD-2-Clause" if strings.Contains(text[h.at:end], bsdThirdClause) { id = "BSD-3-Clause" } } if !slices.Contains(ids, id) { ids = append(ids, id) } } return ids } // render writes the notices: the header, a table of the components, and // each component's files in full. func render(components []component) []byte { var b strings.Builder b.WriteString(header) b.WriteString("\n| Component | Version | License |\n| --- | --- | --- |\n") for _, c := range components { fmt.Fprintf(&b, "| %s | %s | %s |\n", c.name, c.version, strings.Join(c.licenses, ", ")) } for _, c := range components { b.WriteString("\n## " + c.name) if c.version != "" { b.WriteString(" " + c.version) } label := "License" if len(c.licenses) > 1 { label = "Licenses" } fmt.Fprintf(&b, "\n\n%s: %s\n", label, strings.Join(c.licenses, ", ")) for _, f := range c.files { fence := fence(f.body) fmt.Fprintf(&b, "\n### %s\n\n%s\n%s\n%s\n", f.path, fence, f.body, fence) } } return []byte(b.String()) } // fence returns a code fence longer than any run of backticks in body, so // that no text can close its block early. func fence(body string) string { longest, run := 0, 0 for _, r := range body { if r != '`' { run = 0 continue } run++ longest = max(longest, run) } return strings.Repeat("`", max(3, longest+1)) }