blob: cff1ff84ebff8e924e3a196db58914d0d7860b11 [file] [log] [blame]
Bob Badour6ea14572022-01-23 17:15:46 -08001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18 "bytes"
19 "compliance"
20 "flag"
21 "fmt"
22 "html"
23 "io"
24 "io/fs"
25 "os"
26 "path/filepath"
27 "strings"
28)
29
30var (
31 outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
32 includeTOC = flag.Bool("toc", true, "Whether to include a table of contents.")
33 stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
34 title = flag.String("title", "", "The title of the notice file.")
35
36 failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
37 failNoLicenses = fmt.Errorf("No licenses found")
38)
39
40type context struct {
41 stdout io.Writer
42 stderr io.Writer
43 rootFS fs.FS
44 includeTOC bool
45 stripPrefix string
46 title string
47}
48
49func init() {
50 flag.Usage = func() {
51 fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
52
53Outputs an html NOTICE.html file.
54
55Options:
56`, filepath.Base(os.Args[0]))
57 flag.PrintDefaults()
58 }
59}
60
61func main() {
62 flag.Parse()
63
64 // Must specify at least one root target.
65 if flag.NArg() == 0 {
66 flag.Usage()
67 os.Exit(2)
68 }
69
70 if len(*outputFile) == 0 {
71 flag.Usage()
72 fmt.Fprintf(os.Stderr, "must specify file for -o; use - for stdout\n")
73 os.Exit(2)
74 } else {
75 dir, err := filepath.Abs(filepath.Dir(*outputFile))
76 if err != nil {
77 fmt.Fprintf(os.Stderr, "cannot determine path to %q: %w\n", *outputFile, err)
78 os.Exit(1)
79 }
80 fi, err := os.Stat(dir)
81 if err != nil {
82 fmt.Fprintf(os.Stderr, "cannot read directory %q of %q: %w\n", dir, *outputFile, err)
83 os.Exit(1)
84 }
85 if !fi.IsDir() {
86 fmt.Fprintf(os.Stderr, "parent %q of %q is not a directory\n", dir, *outputFile)
87 os.Exit(1)
88 }
89 }
90
91 var ofile io.Writer
92 ofile = os.Stdout
93 if *outputFile != "-" {
94 ofile = &bytes.Buffer{}
95 }
96
97 ctx := &context{ofile, os.Stderr, os.DirFS("."), *includeTOC, *stripPrefix, *title}
98
99 err := htmlNotice(ctx, flag.Args()...)
100 if err != nil {
101 if err == failNoneRequested {
102 flag.Usage()
103 }
104 fmt.Fprintf(os.Stderr, "%s\n", err.Error())
105 os.Exit(1)
106 }
107 if *outputFile != "-" {
108 err := os.WriteFile(*outputFile, ofile.(*bytes.Buffer).Bytes(), 0666)
109 if err != nil {
110 fmt.Fprintf(os.Stderr, "could not write output to %q: %w\n", *outputFile, err)
111 os.Exit(1)
112 }
113 }
114 os.Exit(0)
115}
116
117// htmlNotice implements the htmlnotice utility.
118func htmlNotice(ctx *context, files ...string) error {
119 // Must be at least one root file.
120 if len(files) < 1 {
121 return failNoneRequested
122 }
123
124 // Read the license graph from the license metadata files (*.meta_lic).
125 licenseGraph, err := compliance.ReadLicenseGraph(ctx.rootFS, ctx.stderr, files)
126 if err != nil {
127 return fmt.Errorf("Unable to read license metadata file(s) %q: %v\n", files, err)
128 }
129 if licenseGraph == nil {
130 return failNoLicenses
131 }
132
133 // rs contains all notice resolutions.
134 rs := compliance.ResolveNotices(licenseGraph)
135
136 ni, err := compliance.IndexLicenseTexts(ctx.rootFS, licenseGraph, rs)
137 if err != nil {
138 return fmt.Errorf("Unable to read license text file(s) for %q: %v\n", files, err)
139 }
140
141 fmt.Fprintln(ctx.stdout, "<!DOCTYPE html>")
142 fmt.Fprintln(ctx.stdout, "<html><head>\n")
143 fmt.Fprintln(ctx.stdout, "<style type=\"text/css\">")
144 fmt.Fprintln(ctx.stdout, "body { padding: 2px; margin: 0; }")
145 fmt.Fprintln(ctx.stdout, "ul { list-style-type: none; margin: 0; padding: 0; }")
146 fmt.Fprintln(ctx.stdout, "li { padding-left: 1em; }")
147 fmt.Fprintln(ctx.stdout, ".file-list { margin-left: 1em; }")
148 fmt.Fprintln(ctx.stdout, "</style>\n")
149 if 0 < len(ctx.title) {
150 fmt.Fprintf(ctx.stdout, "<title>%s</title>\n", html.EscapeString(ctx.title))
151 }
152 fmt.Fprintln(ctx.stdout, "</head>")
153 fmt.Fprintln(ctx.stdout, "<body>")
154
155 if 0 < len(ctx.title) {
156 fmt.Fprintf(ctx.stdout, " <h1>%s</h1>\n", html.EscapeString(ctx.title))
157 }
158 ids := make(map[string]string)
159 if ctx.includeTOC {
160 fmt.Fprintln(ctx.stdout, " <ul class=\"toc\">")
161 i := 0
162 for installPath := range ni.InstallPaths() {
163 id := fmt.Sprintf("id%d", i)
164 i++
165 ids[installPath] = id
166 var p string
167 if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
168 p = installPath[len(ctx.stripPrefix):]
169 if 0 == len(p) {
170 if 0 < len(ctx.title) {
171 p = ctx.title
172 } else {
173 p = "root"
174 }
175 }
176 } else {
177 p = installPath
178 }
179 fmt.Fprintf(ctx.stdout, " <li id=\"%s\"><strong>%s</strong>\n <ul>\n", id, html.EscapeString(p))
180 for _, h := range ni.InstallHashes(installPath) {
181 libs := ni.InstallHashLibs(installPath, h)
182 fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", h.String(), html.EscapeString(strings.Join(libs, ", ")))
183 }
184 fmt.Fprintln(ctx.stdout, " </ul>")
185 }
186 fmt.Fprintln(ctx.stdout, " </ul><!-- toc -->")
187 }
188 for h := range ni.Hashes() {
189 fmt.Fprintln(ctx.stdout, " <hr>")
190 for _, libName := range ni.HashLibs(h) {
191 fmt.Fprintf(ctx.stdout, " <strong>%s</strong> used by:\n <ul class=\"file-list\">\n", html.EscapeString(libName))
192 for _, installPath := range ni.HashLibInstalls(h, libName) {
193 if id, ok := ids[installPath]; ok {
194 if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
195 fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(installPath[len(ctx.stripPrefix):]))
196 } else {
197 fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(installPath))
198 }
199 } else {
200 if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
201 fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(installPath[len(ctx.stripPrefix):]))
202 } else {
203 fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(installPath))
204 }
205 }
206 }
207 fmt.Fprintf(ctx.stdout, " </ul>\n")
208 }
209 fmt.Fprintf(ctx.stdout, " </ul>\n <a id=\"%s\"/><pre class=\"license-text\">", h.String())
210 fmt.Fprintln(ctx.stdout, html.EscapeString(string(ni.HashText(h))))
211 fmt.Fprintln(ctx.stdout, " </pre><!-- license-text -->")
212 }
213 fmt.Fprintln(ctx.stdout, "</body></html>")
214
215 return nil
216}