Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 00e438dd authored by Bob Badour's avatar Bob Badour Committed by Automerger Merge Worker
Browse files

Merge "Regularize command-line flags." am: 339edb60

Original change: https://android-review.googlesource.com/c/platform/build/+/1969760

Change-Id: Idfddaa7a9fe398734278e0d889108becb63f4e59
parents 075084d9 339edb60
Loading
Loading
Loading
Loading
+29 −7
Original line number Diff line number Diff line
@@ -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
}
+1 −1
Original line number Diff line number Diff line
@@ -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 {
+29 −3
Original line number Diff line number Diff line
@@ -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)
+20 −20
Original line number Diff line number Diff line
@@ -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"),
+16 −3
Original line number Diff line number Diff line
@@ -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 {
Loading