Regularize command-line flags.

All the notice binaries have -title

All the binaries that can -stripPrefix can strip multiple.

Bug: 68860345
Bug: 151177513
Bug: 151953481
Bug: 213388645
Bug: 210912771

Test: m all
Test: m systemlicense
Test: m bom; out/soong/host/linux-x85/bom ...
Test: m dumpgraph; out/soong/host/linux-x85/dumpgraph ...
Test: m dumpresolutions; out/soong/host/linux-x85/dumpresolutions ...
Test: m htmlnotice; out/soong/host/linux-x85/htmlnotice ...
Test: m rtrace; out/soong/host/linux-x85/rtrace ...
Test: m textnotice; out/soong/host/linux-x85/textnotice ...
Test: m xmlnotice; out/soong/host/linux-x85/xmlnotice ...

where ... is the path to the .meta_lic file for the system image. In my
case if

$ export PRODUCT=$(realpath $ANDROID_PRODUCT_OUT --relative-to=$PWD)

... can be expressed as:

${PRODUCT}/gen/META/lic_intermediates/${PRODUCT}/system.img.meta_lic

Change-Id: I08357bf1adb048abba6563cf3cea6ee6d60405e0
This commit is contained in:
Bob Badour 2022-02-02 15:15:56 -08:00
parent f87922450e
commit 682e1bae57
14 changed files with 299 additions and 155 deletions

View file

@ -29,7 +29,7 @@ import (
var (
outputFile = flag.String("o", "-", "Where to write the bill of materials. (default stdout)")
stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
failNoLicenses = fmt.Errorf("No licenses found")
@ -39,7 +39,20 @@ type context struct {
stdout io.Writer
stderr io.Writer
rootFS fs.FS
stripPrefix string
stripPrefix []string
}
func (ctx context) strip(installPath string) string {
for _, prefix := range ctx.stripPrefix {
if strings.HasPrefix(installPath, prefix) {
p := strings.TrimPrefix(installPath, prefix)
if 0 == len(p) {
continue
}
return p
}
}
return installPath
}
func init() {
@ -54,6 +67,19 @@ Options:
}
}
// newMultiString creates a flag that allows multiple values in an array.
func newMultiString(name, usage string) *multiString {
var f multiString
flag.Var(&f, name, usage)
return &f
}
// multiString implements the flag `Value` interface for multiple strings.
type multiString []string
func (ms *multiString) String() string { return strings.Join(*ms, ", ") }
func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
func main() {
flag.Parse()
@ -135,11 +161,7 @@ func billOfMaterials(ctx *context, files ...string) error {
}
for path := range ni.InstallPaths() {
if 0 < len(ctx.stripPrefix) && strings.HasPrefix(path, ctx.stripPrefix) {
fmt.Fprintln(ctx.stdout, path[len(ctx.stripPrefix):])
} else {
fmt.Fprintln(ctx.stdout, path)
}
fmt.Fprintln(ctx.stdout, ctx.strip(path))
}
return nil
}

View file

@ -282,7 +282,7 @@ func Test(t *testing.T) {
rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r)
}
ctx := context{stdout, stderr, os.DirFS("."), tt.stripPrefix}
ctx := context{stdout, stderr, os.DirFS("."), []string{tt.stripPrefix}}
err := billOfMaterials(&ctx, rootFiles...)
if err != nil {

View file

@ -29,7 +29,7 @@ import (
var (
graphViz = flag.Bool("dot", false, "Whether to output graphviz (i.e. dot) format.")
labelConditions = flag.Bool("label_conditions", false, "Whether to label target nodes with conditions.")
stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
failNoLicenses = fmt.Errorf("No licenses found")
@ -38,7 +38,20 @@ var (
type context struct {
graphViz bool
labelConditions bool
stripPrefix string
stripPrefix []string
}
func (ctx context) strip(installPath string) string {
for _, prefix := range ctx.stripPrefix {
if strings.HasPrefix(installPath, prefix) {
p := strings.TrimPrefix(installPath, prefix)
if 0 == len(p) {
continue
}
return p
}
}
return installPath
}
func init() {
@ -60,6 +73,19 @@ Options:
}
}
// newMultiString creates a flag that allows multiple values in an array.
func newMultiString(name, usage string) *multiString {
var f multiString
flag.Var(&f, name, usage)
return &f
}
// multiString implements the flag `Value` interface for multiple strings.
type multiString []string
func (ms *multiString) String() string { return strings.Join(*ms, ", ") }
func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
func main() {
flag.Parse()
@ -107,7 +133,7 @@ func dumpGraph(ctx *context, stdout, stderr io.Writer, files ...string) error {
// targetOut calculates the string to output for `target` separating conditions as needed using `sep`.
targetOut := func(target *compliance.TargetNode, sep string) string {
tOut := strings.TrimPrefix(target.Name(), ctx.stripPrefix)
tOut := ctx.strip(target.Name())
if ctx.labelConditions {
conditions := target.LicenseConditions().Names()
sort.Strings(conditions)

View file

@ -59,7 +59,7 @@ func Test_plaintext(t *testing.T) {
condition: "firstparty",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/firstparty/"},
ctx: context{stripPrefix: []string{"testdata/firstparty/"}},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic static",
"bin/bin1.meta_lic lib/libc.a.meta_lic static",
@ -75,7 +75,7 @@ func Test_plaintext(t *testing.T) {
condition: "firstparty",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/firstparty/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/firstparty/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:notice static",
"bin/bin1.meta_lic:notice lib/libc.a.meta_lic:notice static",
@ -146,7 +146,7 @@ func Test_plaintext(t *testing.T) {
condition: "notice",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/notice/"},
ctx: context{stripPrefix: []string{"testdata/notice/"}},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic static",
"bin/bin1.meta_lic lib/libc.a.meta_lic static",
@ -162,7 +162,7 @@ func Test_plaintext(t *testing.T) {
condition: "notice",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/notice/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/notice/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:notice static",
"bin/bin1.meta_lic:notice lib/libc.a.meta_lic:notice static",
@ -233,7 +233,7 @@ func Test_plaintext(t *testing.T) {
condition: "reciprocal",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/reciprocal/"},
ctx: context{stripPrefix: []string{"testdata/reciprocal/"}},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic static",
"bin/bin1.meta_lic lib/libc.a.meta_lic static",
@ -249,7 +249,7 @@ func Test_plaintext(t *testing.T) {
condition: "reciprocal",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/reciprocal/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/reciprocal/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:reciprocal static",
"bin/bin1.meta_lic:notice lib/libc.a.meta_lic:reciprocal static",
@ -320,7 +320,7 @@ func Test_plaintext(t *testing.T) {
condition: "restricted",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/restricted/"},
ctx: context{stripPrefix: []string{"testdata/restricted/"}},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic static",
"bin/bin1.meta_lic lib/libc.a.meta_lic static",
@ -336,7 +336,7 @@ func Test_plaintext(t *testing.T) {
condition: "restricted",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/restricted/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/restricted/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:restricted_allows_dynamic_linking static",
"bin/bin1.meta_lic:notice lib/libc.a.meta_lic:reciprocal static",
@ -407,7 +407,7 @@ func Test_plaintext(t *testing.T) {
condition: "proprietary",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/proprietary/"},
ctx: context{stripPrefix: []string{"testdata/proprietary/"}},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic static",
"bin/bin1.meta_lic lib/libc.a.meta_lic static",
@ -423,7 +423,7 @@ func Test_plaintext(t *testing.T) {
condition: "proprietary",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/proprietary/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/proprietary/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:by_exception_only:proprietary static",
"bin/bin1.meta_lic:notice lib/libc.a.meta_lic:by_exception_only:proprietary static",
@ -613,7 +613,7 @@ func Test_graphviz(t *testing.T) {
condition: "firstparty",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/firstparty/"},
ctx: context{stripPrefix: []string{"testdata/firstparty/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("bin/bin2.meta_lic"),
@ -636,7 +636,7 @@ func Test_graphviz(t *testing.T) {
condition: "firstparty",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/firstparty/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/firstparty/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("bin/bin2.meta_lic", "notice"),
@ -735,7 +735,7 @@ func Test_graphviz(t *testing.T) {
condition: "notice",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/notice/"},
ctx: context{stripPrefix: []string{"testdata/notice/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("bin/bin2.meta_lic"),
@ -758,7 +758,7 @@ func Test_graphviz(t *testing.T) {
condition: "notice",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/notice/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/notice/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("bin/bin2.meta_lic", "notice"),
@ -857,7 +857,7 @@ func Test_graphviz(t *testing.T) {
condition: "reciprocal",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/reciprocal/"},
ctx: context{stripPrefix: []string{"testdata/reciprocal/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("bin/bin2.meta_lic"),
@ -880,7 +880,7 @@ func Test_graphviz(t *testing.T) {
condition: "reciprocal",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/reciprocal/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/reciprocal/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("bin/bin2.meta_lic", "notice"),
@ -979,7 +979,7 @@ func Test_graphviz(t *testing.T) {
condition: "restricted",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/restricted/"},
ctx: context{stripPrefix: []string{"testdata/restricted/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("bin/bin2.meta_lic"),
@ -1002,7 +1002,7 @@ func Test_graphviz(t *testing.T) {
condition: "restricted",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/restricted/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/restricted/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("bin/bin2.meta_lic", "notice"),
@ -1101,7 +1101,7 @@ func Test_graphviz(t *testing.T) {
condition: "proprietary",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/proprietary/"},
ctx: context{stripPrefix: []string{"testdata/proprietary/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("bin/bin2.meta_lic"),
@ -1124,7 +1124,7 @@ func Test_graphviz(t *testing.T) {
condition: "proprietary",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/proprietary/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/proprietary/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("bin/bin2.meta_lic", "by_exception_only", "proprietary"),

View file

@ -30,7 +30,7 @@ var (
conditions = newMultiString("c", "License condition to resolve. (may be given multiple times)")
graphViz = flag.Bool("dot", false, "Whether to output graphviz (i.e. dot) format.")
labelConditions = flag.Bool("label_conditions", false, "Whether to label target nodes with conditions.")
stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
failNoLicenses = fmt.Errorf("No licenses found")
@ -40,7 +40,20 @@ type context struct {
conditions []compliance.LicenseCondition
graphViz bool
labelConditions bool
stripPrefix string
stripPrefix []string
}
func (ctx context) strip(installPath string) string {
for _, prefix := range ctx.stripPrefix {
if strings.HasPrefix(installPath, prefix) {
p := strings.TrimPrefix(installPath, prefix)
if 0 == len(p) {
continue
}
return p
}
}
return installPath
}
func init() {
@ -140,7 +153,7 @@ func dumpResolutions(ctx *context, stdout, stderr io.Writer, files ...string) (*
// targetOut calculates the string to output for `target` adding `sep`-separated conditions as needed.
targetOut := func(target *compliance.TargetNode, sep string) string {
tOut := strings.TrimPrefix(target.Name(), ctx.stripPrefix)
tOut := ctx.strip(target.Name())
if ctx.labelConditions {
conditions := target.LicenseConditions().Names()
if len(conditions) > 0 {

View file

@ -65,7 +65,7 @@ func Test_plaintext(t *testing.T) {
condition: "firstparty",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/firstparty/"},
ctx: context{stripPrefix: []string{"testdata/firstparty/"}},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
"bin/bin1.meta_lic lib/liba.so.meta_lic notice",
@ -87,7 +87,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
@ -110,7 +110,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []string{},
},
@ -120,7 +120,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []string{},
},
@ -130,7 +130,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: append(compliance.ImpliesPrivate.AsList(), compliance.ImpliesShared.AsList()...),
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []string{},
},
@ -138,7 +138,7 @@ func Test_plaintext(t *testing.T) {
condition: "firstparty",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/firstparty/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/firstparty/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice",
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:notice notice",
@ -223,7 +223,7 @@ func Test_plaintext(t *testing.T) {
condition: "notice",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/notice/"},
ctx: context{stripPrefix: []string{"testdata/notice/"}},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
"bin/bin1.meta_lic lib/liba.so.meta_lic notice",
@ -245,7 +245,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
@ -268,7 +268,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []string{},
},
@ -278,7 +278,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []string{},
},
@ -288,7 +288,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: append(compliance.ImpliesShared.AsList(), compliance.ImpliesPrivate.AsList()...),
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []string{},
},
@ -296,7 +296,7 @@ func Test_plaintext(t *testing.T) {
condition: "notice",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/notice/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/notice/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice",
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:notice notice",
@ -381,7 +381,7 @@ func Test_plaintext(t *testing.T) {
condition: "reciprocal",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/reciprocal/"},
ctx: context{stripPrefix: []string{"testdata/reciprocal/"}},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
"bin/bin1.meta_lic lib/liba.so.meta_lic reciprocal",
@ -403,7 +403,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
@ -421,7 +421,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic reciprocal",
@ -437,7 +437,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []string{},
},
@ -447,7 +447,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: append(compliance.ImpliesShared.AsList(), compliance.ImpliesPrivate.AsList()...),
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic reciprocal",
@ -461,7 +461,7 @@ func Test_plaintext(t *testing.T) {
condition: "reciprocal",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/reciprocal/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/reciprocal/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice",
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:reciprocal reciprocal",
@ -547,7 +547,7 @@ func Test_plaintext(t *testing.T) {
condition: "restricted",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/restricted/"},
ctx: context{stripPrefix: []string{"testdata/restricted/"}},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice:restricted_allows_dynamic_linking",
"bin/bin1.meta_lic lib/liba.so.meta_lic restricted_allows_dynamic_linking",
@ -570,7 +570,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
@ -586,7 +586,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic restricted_allows_dynamic_linking",
@ -610,7 +610,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []string{},
},
@ -620,7 +620,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: append(compliance.ImpliesShared.AsList(), compliance.ImpliesPrivate.AsList()...),
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic restricted_allows_dynamic_linking",
@ -642,7 +642,7 @@ func Test_plaintext(t *testing.T) {
condition: "restricted",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/restricted/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/restricted/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice:restricted_allows_dynamic_linking",
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:restricted_allows_dynamic_linking restricted_allows_dynamic_linking",
@ -730,7 +730,7 @@ func Test_plaintext(t *testing.T) {
condition: "proprietary",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/proprietary/"},
ctx: context{stripPrefix: []string{"testdata/proprietary/"}},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
"bin/bin1.meta_lic lib/liba.so.meta_lic proprietary:by_exception_only",
@ -753,7 +753,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []string{
"bin/bin1.meta_lic bin/bin1.meta_lic notice",
@ -767,7 +767,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []string{
"bin/bin2.meta_lic bin/bin2.meta_lic restricted",
@ -784,7 +784,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic proprietary",
@ -802,7 +802,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: append(compliance.ImpliesShared.AsList(), compliance.ImpliesPrivate.AsList()...),
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []string{
"bin/bin1.meta_lic lib/liba.so.meta_lic proprietary",
@ -822,7 +822,7 @@ func Test_plaintext(t *testing.T) {
condition: "proprietary",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/proprietary/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/proprietary/"}, labelConditions: true},
expectedOut: []string{
"bin/bin1.meta_lic:notice bin/bin1.meta_lic:notice notice",
"bin/bin1.meta_lic:notice lib/liba.so.meta_lic:proprietary:by_exception_only proprietary:by_exception_only",
@ -1080,7 +1080,7 @@ func Test_graphviz(t *testing.T) {
condition: "firstparty",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/firstparty/"},
ctx: context{stripPrefix: []string{"testdata/firstparty/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("lib/liba.so.meta_lic"),
@ -1144,7 +1144,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -1209,7 +1209,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []getMatcher{},
},
@ -1219,7 +1219,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []getMatcher{},
},
@ -1229,7 +1229,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(),
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []getMatcher{},
},
@ -1237,7 +1237,7 @@ func Test_graphviz(t *testing.T) {
condition: "firstparty",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/firstparty/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/firstparty/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("lib/liba.so.meta_lic", "notice"),
@ -1472,7 +1472,7 @@ func Test_graphviz(t *testing.T) {
condition: "notice",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/notice/"},
ctx: context{stripPrefix: []string{"testdata/notice/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("lib/liba.so.meta_lic"),
@ -1536,7 +1536,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -1601,7 +1601,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []getMatcher{},
},
@ -1611,7 +1611,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []getMatcher{},
},
@ -1621,7 +1621,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(),
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []getMatcher{},
},
@ -1629,7 +1629,7 @@ func Test_graphviz(t *testing.T) {
condition: "notice",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/notice/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/notice/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("lib/liba.so.meta_lic", "notice"),
@ -1864,7 +1864,7 @@ func Test_graphviz(t *testing.T) {
condition: "reciprocal",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/reciprocal/"},
ctx: context{stripPrefix: []string{"testdata/reciprocal/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("lib/liba.so.meta_lic"),
@ -1928,7 +1928,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -1971,7 +1971,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -2006,7 +2006,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []getMatcher{},
},
@ -2016,7 +2016,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(),
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -2049,7 +2049,7 @@ func Test_graphviz(t *testing.T) {
condition: "reciprocal",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/reciprocal/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/reciprocal/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("lib/liba.so.meta_lic", "reciprocal"),
@ -2296,7 +2296,7 @@ func Test_graphviz(t *testing.T) {
condition: "restricted",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/restricted/"},
ctx: context{stripPrefix: []string{"testdata/restricted/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("lib/liba.so.meta_lic"),
@ -2372,7 +2372,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -2406,7 +2406,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -2478,7 +2478,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []getMatcher{},
},
@ -2488,7 +2488,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(),
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -2558,7 +2558,7 @@ func Test_graphviz(t *testing.T) {
condition: "restricted",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/restricted/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/restricted/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("lib/liba.so.meta_lic", "restricted_allows_dynamic_linking"),
@ -2836,7 +2836,7 @@ func Test_graphviz(t *testing.T) {
condition: "proprietary",
name: "apex_trimmed",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/proprietary/"},
ctx: context{stripPrefix: []string{"testdata/proprietary/"}},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
matchTarget("lib/liba.so.meta_lic"),
@ -2914,7 +2914,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: []compliance.LicenseCondition{compliance.NoticeCondition},
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -2939,7 +2939,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.AsList(),
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin2.meta_lic"),
@ -2977,7 +2977,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesPrivate.AsList(),
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -3021,7 +3021,7 @@ func Test_graphviz(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
conditions: compliance.ImpliesShared.Union(compliance.ImpliesPrivate).AsList(),
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic"),
@ -3082,7 +3082,7 @@ func Test_graphviz(t *testing.T) {
condition: "proprietary",
name: "apex_trimmed_labelled",
roots: []string{"highest.apex.meta_lic"},
ctx: context{stripPrefix: "testdata/proprietary/", labelConditions: true},
ctx: context{stripPrefix: []string{"testdata/proprietary/"}, labelConditions: true},
expectedOut: []getMatcher{
matchTarget("bin/bin1.meta_lic", "notice"),
matchTarget("lib/liba.so.meta_lic", "by_exception_only", "proprietary"),

View file

@ -35,7 +35,7 @@ var (
outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
depsFile = flag.String("d", "", "Where to write the deps file")
includeTOC = flag.Bool("toc", true, "Whether to include a table of contents.")
stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
title = flag.String("title", "", "The title of the notice file.")
failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
@ -47,11 +47,27 @@ type context struct {
stderr io.Writer
rootFS fs.FS
includeTOC bool
stripPrefix string
stripPrefix []string
title string
deps *[]string
}
func (ctx context) strip(installPath string) string {
for _, prefix := range ctx.stripPrefix {
if strings.HasPrefix(installPath, prefix) {
p := strings.TrimPrefix(installPath, prefix)
if 0 == len(p) {
p = ctx.title
}
if 0 == len(p) {
continue
}
return p
}
}
return installPath
}
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
@ -65,6 +81,19 @@ Options:
}
}
// newMultiString creates a flag that allows multiple values in an array.
func newMultiString(name, usage string) *multiString {
var f multiString
flag.Var(&f, name, usage)
return &f
}
// multiString implements the flag `Value` interface for multiple strings.
type multiString []string
func (ms *multiString) String() string { return strings.Join(*ms, ", ") }
func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
func main() {
flag.Parse()
@ -190,20 +219,7 @@ func htmlNotice(ctx *context, files ...string) error {
id := fmt.Sprintf("id%d", i)
i++
ids[installPath] = id
var p string
if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
p = installPath[len(ctx.stripPrefix):]
if 0 == len(p) {
if 0 < len(ctx.title) {
p = ctx.title
} else {
p = "root"
}
}
} else {
p = installPath
}
fmt.Fprintf(ctx.stdout, " <li id=\"%s\"><strong>%s</strong>\n <ul>\n", id, html.EscapeString(p))
fmt.Fprintf(ctx.stdout, " <li id=\"%s\"><strong>%s</strong>\n <ul>\n", id, html.EscapeString(ctx.strip(installPath)))
for _, h := range ni.InstallHashes(installPath) {
libs := ni.InstallHashLibs(installPath, h)
fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", h.String(), html.EscapeString(strings.Join(libs, ", ")))
@ -218,17 +234,9 @@ func htmlNotice(ctx *context, files ...string) error {
fmt.Fprintf(ctx.stdout, " <strong>%s</strong> used by:\n <ul class=\"file-list\">\n", html.EscapeString(libName))
for _, installPath := range ni.HashLibInstalls(h, libName) {
if id, ok := ids[installPath]; ok {
if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(installPath[len(ctx.stripPrefix):]))
} else {
fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(installPath))
}
fmt.Fprintf(ctx.stdout, " <li><a href=\"#%s\">%s</a>\n", id, html.EscapeString(ctx.strip(installPath)))
} else {
if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(installPath[len(ctx.stripPrefix):]))
} else {
fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(installPath))
}
fmt.Fprintf(ctx.stdout, " <li>%s\n", html.EscapeString(ctx.strip(installPath)))
}
}
fmt.Fprintf(ctx.stdout, " </ul>\n")

View file

@ -651,7 +651,7 @@ func Test(t *testing.T) {
var deps []string
ctx := context{stdout, stderr, os.DirFS("."), tt.includeTOC, tt.stripPrefix, tt.title, &deps}
ctx := context{stdout, stderr, os.DirFS("."), tt.includeTOC, []string{tt.stripPrefix}, tt.title, &deps}
err := htmlNotice(&ctx, rootFiles...)
if err != nil {

View file

@ -28,7 +28,7 @@ import (
var (
sources = newMultiString("rtrace", "Projects or metadata files to trace back from. (required; multiple allowed)")
stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
failNoSources = fmt.Errorf("\nNo projects or metadata files to trace back from")
@ -37,7 +37,20 @@ var (
type context struct {
sources []string
stripPrefix string
stripPrefix []string
}
func (ctx context) strip(installPath string) string {
for _, prefix := range ctx.stripPrefix {
if strings.HasPrefix(installPath, prefix) {
p := strings.TrimPrefix(installPath, prefix)
if 0 == len(p) {
continue
}
return p
}
}
return installPath
}
func init() {
@ -143,7 +156,7 @@ func traceRestricted(ctx *context, stdout, stderr io.Writer, files ...string) (*
// targetOut calculates the string to output for `target` adding `sep`-separated conditions as needed.
targetOut := func(target *compliance.TargetNode, sep string) string {
tOut := strings.TrimPrefix(target.Name(), ctx.stripPrefix)
tOut := ctx.strip(target.Name())
return tOut
}

View file

@ -52,7 +52,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
sources: []string{"testdata/firstparty/bin/bin1.meta_lic"},
stripPrefix: "testdata/firstparty/",
stripPrefix: []string{"testdata/firstparty/"},
},
expectedOut: []string{},
},
@ -92,7 +92,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
sources: []string{"testdata/notice/bin/bin1.meta_lic"},
stripPrefix: "testdata/notice/",
stripPrefix: []string{"testdata/notice/"},
},
expectedOut: []string{},
},
@ -132,7 +132,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
sources: []string{"testdata/reciprocal/bin/bin1.meta_lic"},
stripPrefix: "testdata/reciprocal/",
stripPrefix: []string{"testdata/reciprocal/"},
},
expectedOut: []string{},
},
@ -175,7 +175,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
sources: []string{"testdata/restricted/bin/bin1.meta_lic"},
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []string{"lib/liba.so.meta_lic restricted_allows_dynamic_linking"},
},
@ -185,7 +185,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
sources: []string{"testdata/restricted/bin/bin2.meta_lic"},
stripPrefix: "testdata/restricted/",
stripPrefix: []string{"testdata/restricted/"},
},
expectedOut: []string{"lib/libb.so.meta_lic restricted"},
},
@ -228,7 +228,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
sources: []string{"testdata/proprietary/bin/bin1.meta_lic"},
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []string{},
},
@ -238,7 +238,7 @@ func Test_plaintext(t *testing.T) {
roots: []string{"highest.apex.meta_lic"},
ctx: context{
sources: []string{"testdata/proprietary/bin/bin2.meta_lic"},
stripPrefix: "testdata/proprietary/",
stripPrefix: []string{"testdata/proprietary/"},
},
expectedOut: []string{"lib/libb.so.meta_lic restricted"},
},

View file

@ -16,6 +16,7 @@ package main
import (
"bytes"
"compress/gzip"
"flag"
"fmt"
"io"
@ -32,7 +33,8 @@ import (
var (
outputFile = flag.String("o", "-", "Where to write the NOTICE text file. (default stdout)")
depsFile = flag.String("d", "", "Where to write the deps file")
stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
title = flag.String("title", "", "The title of the notice file.")
failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
failNoLicenses = fmt.Errorf("No licenses found")
@ -42,10 +44,27 @@ type context struct {
stdout io.Writer
stderr io.Writer
rootFS fs.FS
stripPrefix string
stripPrefix []string
title string
deps *[]string
}
func (ctx context) strip(installPath string) string {
for _, prefix := range ctx.stripPrefix {
if strings.HasPrefix(installPath, prefix) {
p := strings.TrimPrefix(installPath, prefix)
if 0 == len(p) {
p = ctx.title
}
if 0 == len(p) {
continue
}
return p
}
}
return installPath
}
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
@ -58,6 +77,19 @@ Options:
}
}
// newMultiString creates a flag that allows multiple values in an array.
func newMultiString(name, usage string) *multiString {
var f multiString
flag.Var(&f, name, usage)
return &f
}
// multiString implements the flag `Value` interface for multiple strings.
type multiString []string
func (ms *multiString) String() string { return strings.Join(*ms, ", ") }
func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
func main() {
flag.Parse()
@ -89,14 +121,21 @@ 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
ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, &deps}
ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, *title, &deps}
err := textNotice(ctx, flag.Args()...)
if err != nil {
@ -106,8 +145,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)
@ -152,11 +195,7 @@ func textNotice(ctx *context, files ...string) error {
for _, libName := range ni.HashLibs(h) {
fmt.Fprintf(ctx.stdout, "%s used by:\n", libName)
for _, installPath := range ni.HashLibInstalls(h, libName) {
if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
fmt.Fprintf(ctx.stdout, " %s\n", installPath[len(ctx.stripPrefix):])
} else {
fmt.Fprintf(ctx.stdout, " %s\n", installPath)
}
fmt.Fprintf(ctx.stdout, " %s\n", ctx.strip(installPath))
}
fmt.Fprintln(ctx.stdout)
}

View file

@ -564,7 +564,7 @@ func Test(t *testing.T) {
var deps []string
ctx := context{stdout, stderr, os.DirFS("."), tt.stripPrefix, &deps}
ctx := context{stdout, stderr, os.DirFS("."), []string{tt.stripPrefix}, "", &deps}
err := textNotice(&ctx, rootFiles...)
if err != nil {

View file

@ -34,7 +34,8 @@ import (
var (
outputFile = flag.String("o", "-", "Where to write the NOTICE xml or xml.gz file. (default stdout)")
depsFile = flag.String("d", "", "Where to write the deps file")
stripPrefix = flag.String("strip_prefix", "", "Prefix to remove from paths. i.e. path to root")
stripPrefix = newMultiString("strip_prefix", "Prefix to remove from paths. i.e. path to root (multiple allowed)")
title = flag.String("title", "", "The title of the notice file.")
failNoneRequested = fmt.Errorf("\nNo license metadata files requested")
failNoLicenses = fmt.Errorf("No licenses found")
@ -44,10 +45,27 @@ type context struct {
stdout io.Writer
stderr io.Writer
rootFS fs.FS
stripPrefix string
stripPrefix []string
title string
deps *[]string
}
func (ctx context) strip(installPath string) string {
for _, prefix := range ctx.stripPrefix {
if strings.HasPrefix(installPath, prefix) {
p := strings.TrimPrefix(installPath, prefix)
if 0 == len(p) {
p = ctx.title
}
if 0 == len(p) {
continue
}
return p
}
}
return installPath
}
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage: %s {options} file.meta_lic {file.meta_lic...}
@ -61,6 +79,19 @@ Options:
}
}
// newMultiString creates a flag that allows multiple values in an array.
func newMultiString(name, usage string) *multiString {
var f multiString
flag.Var(&f, name, usage)
return &f
}
// multiString implements the flag `Value` interface for multiple strings.
type multiString []string
func (ms *multiString) String() string { return strings.Join(*ms, ", ") }
func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
func main() {
flag.Parse()
@ -106,7 +137,7 @@ func main() {
var deps []string
ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, &deps}
ctx := &context{ofile, os.Stderr, os.DirFS("."), *stripPrefix, *title, &deps}
err := xmlNotice(ctx, flag.Args()...)
if err != nil {
@ -165,15 +196,7 @@ func xmlNotice(ctx *context, files ...string) error {
fmt.Fprintln(ctx.stdout, "<licenses>")
for installPath := range ni.InstallPaths() {
var p string
if 0 < len(ctx.stripPrefix) && strings.HasPrefix(installPath, ctx.stripPrefix) {
p = installPath[len(ctx.stripPrefix):]
if 0 == len(p) {
p = "root"
}
} else {
p = installPath
}
p := ctx.strip(installPath)
for _, h := range ni.InstallHashes(installPath) {
for _, lib := range ni.InstallHashLibs(installPath, h) {
fmt.Fprintf(ctx.stdout, "<file-name contentId=\"%s\" lib=\"", h.String())

View file

@ -459,7 +459,7 @@ func Test(t *testing.T) {
var deps []string
ctx := context{stdout, stderr, os.DirFS("."), tt.stripPrefix, &deps}
ctx := context{stdout, stderr, os.DirFS("."), []string{tt.stripPrefix}, "", &deps}
err := xmlNotice(&ctx, rootFiles...)
if err != nil {