From 608bdff0a725eb0a4d1d0ce50620d7ab2a7376bb Mon Sep 17 00:00:00 2001 From: Bob Badour Date: Tue, 1 Feb 2022 12:02:30 -0800 Subject: [PATCH] Add support for gzipping html output. Bug: 68860345 Bug: 151177513 Bug: 151953481 Bug: 213388645 Bug: 210912771 Test: m all Test: m systemlicense Test: m htmlnotice; out/soong/host/linux-x85/htmlnotice ... where ... is -o=html.gz followed by the path to the .meta_lic file for the system image. In my case if $ export PRODUCT=$(realpath $ANDROID_PRODUCT_OUT --relative-to=$PWD) the rest of ... can be expressed as: ${PRODUCT}/gen/META/lic_intermediates/${PRODUCT}/system.img.meta_lic Change-Id: I7a42d5186876609a401956754e3dcff64211fb15 --- tools/compliance/cmd/htmlnotice/htmlnotice.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tools/compliance/cmd/htmlnotice/htmlnotice.go b/tools/compliance/cmd/htmlnotice/htmlnotice.go index 1f555467b3..6363acfc02 100644 --- a/tools/compliance/cmd/htmlnotice/htmlnotice.go +++ b/tools/compliance/cmd/htmlnotice/htmlnotice.go @@ -16,6 +16,7 @@ package main import ( "bytes" + "compress/gzip" "flag" "fmt" "html" @@ -55,7 +56,8 @@ func init() { flag.Usage = func() { fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...} -Outputs an html NOTICE.html file. +Outputs an html NOTICE.html or gzipped NOTICE.html.gz file if the -o filename +ends with ".gz". Options: `, filepath.Base(os.Args[0])) @@ -94,9 +96,16 @@ func main() { } var ofile io.Writer + var closer io.Closer ofile = os.Stdout + var obuf *bytes.Buffer if *outputFile != "-" { - ofile = &bytes.Buffer{} + obuf = &bytes.Buffer{} + ofile = obuf + } + if strings.HasSuffix(*outputFile, ".gz") { + ofile, _ = gzip.NewWriterLevel(obuf, gzip.BestCompression) + closer = ofile.(io.Closer) } var deps []string @@ -111,8 +120,12 @@ func main() { fmt.Fprintf(os.Stderr, "%s\n", err.Error()) os.Exit(1) } + if closer != nil { + closer.Close() + } + if *outputFile != "-" { - err := os.WriteFile(*outputFile, ofile.(*bytes.Buffer).Bytes(), 0666) + err := os.WriteFile(*outputFile, obuf.Bytes(), 0666) if err != nil { fmt.Fprintf(os.Stderr, "could not write output to %q: %s\n", *outputFile, err) os.Exit(1)