// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package main import ( "compress/gzip" "crypto/md5" "encoding/json" "flag" "fmt" "html/template" "io" "os" "strings" ) var ( inputFile string outputFile = flag.String("o", "", "output file") listTargets = flag.Bool("list_targets", false, "list targets using each license") ) type LicenseKind struct { Target string `json:"target"` Name string `json:"name"` Conditions []string `json:"conditions"` } type License struct { Rule string `json:"rule"` CopyrightNotice string `json:"copyright_notice"` PackageName string `json:"package_name"` PackageUrl string `json:"package_url"` PackageVersion string `json:"package_version"` LicenseFile string `json:"license_text"` LicenseKinds []LicenseKind `json:"license_kinds"` Licensees []string `json:"licensees"` } type LicenseTextHash string // generator generates the notices for the given set of licenses read from the JSON-encoded string. // As the contents of the license files is often the same, they are read into the map by their hash. type generator struct { Licenses []License LicenseTextHash map[string]LicenseTextHash // License.rule->hash of license text contents LicenseTextIndex map[LicenseTextHash]string } func newGenerator(in string) *generator { g := generator{} decoder := json.NewDecoder(strings.NewReader(in)) decoder.DisallowUnknownFields() //useful to detect typos, e.g. in unit tests err := decoder.Decode(&g.Licenses) maybeQuit(err) return &g } func (g *generator) buildLicenseTextIndex() { g.LicenseTextHash = make(map[string]LicenseTextHash, len(g.Licenses)) g.LicenseTextIndex = make(map[LicenseTextHash]string) for _, l := range g.Licenses { if l.LicenseFile == "" { continue } data, err := os.ReadFile(l.LicenseFile) if err != nil { fmt.Fprintf(os.Stderr, "%s: bad license file %s: %s\n", l.Rule, l.LicenseFile, err) os.Exit(1) } h := LicenseTextHash(fmt.Sprintf("%x", md5.Sum(data))) g.LicenseTextHash[l.Rule] = h if _, found := g.LicenseTextIndex[h]; !found { g.LicenseTextIndex[h] = string(data) } } } func (g *generator) generate(sink io.Writer, listTargets bool) { const tpl = `
The following software has been included in this product and contains the license and notice as shown below.
{{- $x := . }}
{{- range .Licenses }}
{{ if .PackageName }}{{.PackageName}}{{- else }}Rule: {{.Rule}}{{ end }}
{{- if .CopyrightNotice }}
Copyright Notice: {{.CopyrightNotice}}{{ end }}
{{- $v := index $x.LicenseTextHash .Rule }}{{- if $v }}
License{{- end }}
{{- if list_targets }}
Used by: {{- range .Licensees }} {{.}} {{- end }}
{{$v}}