Files
57_WolveandClaude Opus 5.5 341353f702 Read the license files of the SQLite driver's modules
The license notices stopped on two files of the modules that
modernc.org/sqlite brings, which the daemon will link:

- modernc.org/sqlite ships LICENSE-SQLITE, SQLite's dedication to the
  public domain, whose wording the generator did not know. It now reads
  as "public domain", the term modernc's own notices use.
- modernc.org/memory ships LICENSE-LOGO, which holds only the address
  of the image its logo.png comes from. No package links that image, so
  LICENSE-LOGO no longer counts as a license file.

Nothing links these modules yet, so the notices do not change here.

Part of stage 1d of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 16:39:48 -06:00

210 lines
5.9 KiB
Go

package main
import (
"bytes"
"cmp"
"encoding/json"
"fmt"
"maps"
"os"
"path"
"path/filepath"
"slices"
"strings"
)
// mainPackage is the binary the notices are for.
const mainPackage = "./cmd/uberbringer"
// platform is a target the binary is built for.
type platform struct {
goos, goarch, goarm string
}
// platforms are the targets of the release archives in .goreleaser.yaml.
// Build constraints can link a module on one platform and not another, so
// the notices cover them all.
var platforms = []platform{
{"linux", "amd64", ""},
{"linux", "arm", "6"},
{"linux", "arm", "7"},
{"linux", "arm64", ""},
}
// module is a third-party module linked into the binary.
type module struct {
path, version string
// dir is where the module's files are, in the module cache.
dir string
// files are the license and notice files covering the linked
// packages, as slash-separated paths relative to dir, those nearest the
// module root first.
files []string
}
// listedPackage is the part of a package's `go list -json` record read here.
type listedPackage struct {
Dir string
Standard bool
Module *struct {
Path string
Version string
Dir string
Main bool
}
}
// linkedModules returns, sorted by path, the third-party modules that
// mainPackage links on any of the platforms.
func linkedModules(root string) ([]module, error) {
byPath := make(map[string]*module)
found := make(map[string]map[string]bool)
names := make(map[string][]string)
for _, p := range platforms {
packages, err := listDeps(root, p)
if err != nil {
return nil, err
}
for _, pkg := range packages {
if pkg.Standard || pkg.Module == nil || pkg.Module.Main {
continue
}
if pkg.Module.Dir == "" {
return nil, fmt.Errorf("module %s %s has no directory in the module cache", pkg.Module.Path, pkg.Module.Version)
}
m := byPath[pkg.Module.Path]
if m == nil {
m = &module{path: pkg.Module.Path, version: pkg.Module.Version, dir: pkg.Module.Dir}
byPath[m.path] = m
found[m.path] = make(map[string]bool)
}
files, err := coveringFiles(m.dir, pkg.Dir, names)
if err != nil {
return nil, fmt.Errorf("module %s: %w", m.path, err)
}
for _, f := range files {
found[m.path][f] = true
}
}
}
modules := make([]module, 0, len(byPath))
for _, m := range byPath {
m.files = slices.SortedFunc(maps.Keys(found[m.path]), nearestFirst)
modules = append(modules, *m)
}
slices.SortFunc(modules, func(a, b module) int { return strings.Compare(a.path, b.path) })
return modules, nil
}
// listDeps returns the packages mainPackage links on platform p.
func listDeps(root string, p platform) ([]listedPackage, error) {
env := []string{"GOOS=" + p.goos, "GOARCH=" + p.goarch, "GOARM=" + p.goarm, "CGO_ENABLED=0"}
out, err := goOutput(root, env, "list", "-deps", "-json=Dir,Standard,Module", mainPackage)
if err != nil {
return nil, err
}
var packages []listedPackage
dec := json.NewDecoder(bytes.NewReader([]byte(out)))
for dec.More() {
var pkg listedPackage
if err := dec.Decode(&pkg); err != nil {
return nil, fmt.Errorf("reading go list output for %s/%s: %w", p.goos, p.goarch, err)
}
packages = append(packages, pkg)
}
return packages, nil
}
// coveringFiles returns the license and notice files in dir and in each of
// its parents up to moduleDir, as slash-separated paths relative to
// moduleDir. names caches the files found in each directory, by its path.
func coveringFiles(moduleDir, dir string, names map[string][]string) ([]string, error) {
rel, err := filepath.Rel(moduleDir, dir)
if err != nil {
return nil, err
}
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return nil, fmt.Errorf("package directory %s is outside the module", dir)
}
var files []string
for {
abs := filepath.Join(moduleDir, rel)
inDir, ok := names[abs]
if !ok {
inDir, err = licenseFiles(abs)
if err != nil {
return nil, err
}
names[abs] = inDir
}
for _, name := range inDir {
files = append(files, path.Join(filepath.ToSlash(rel), name))
}
if rel == "." {
return files, nil
}
rel = filepath.Dir(rel)
}
}
// licenseFiles returns the names of the license and notice files in dir.
func licenseFiles(dir string) ([]string, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var names []string
for _, e := range entries {
if e.Type().IsRegular() && (isLicenseFile(e.Name()) || isNoticeFile(e.Name())) {
names = append(names, e.Name())
}
}
return names, nil
}
// isLicenseFile reports whether a file name, in any letter case, is that of
// a license: LICENSE, LICENCE, COPYING or UNLICENSE, perhaps with a suffix
// such as -MIT, and with no extension or that of a text file. LICENSE-LOGO
// is not one: it covers a project's logo image, which no package links.
func isLicenseFile(name string) bool {
stem := textStem(name)
if stem == "license-logo" || stem == "licence-logo" {
return false
}
for _, base := range []string{"license", "licence", "copying", "unlicense"} {
if stem == base || strings.HasPrefix(stem, base+"-") {
return true
}
}
return false
}
// isNoticeFile reports whether a file name, in any letter case, is that of
// the NOTICE file whose text the Apache License requires to be passed on.
func isNoticeFile(name string) bool {
return textStem(name) == "notice"
}
// textStem returns a file name in lower case, without its extension if that
// is the extension of a text file.
func textStem(name string) string {
lower := strings.ToLower(name)
switch ext := path.Ext(lower); ext {
case ".md", ".txt", ".rst":
return strings.TrimSuffix(lower, ext)
}
return lower
}
// nearestFirst orders slash-separated paths by depth, then by name, so a
// module's own license comes before those of code it vendors.
func nearestFirst(a, b string) int {
if c := cmp.Compare(strings.Count(a, "/"), strings.Count(b, "/")); c != 0 {
return c
}
return strings.Compare(a, b)
}