Skip to contentSkip to conversation

bitbake-getvar: Add a (suppressable) error for undefined variables

PATCH New Peter Kjellerstedt 1 participant 4 messages Sep 27, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/4 bitbake-getvar: Add a (suppressable) error for undefined variables

If an undefined variable or variable flag is specified, bitbake-getvar will now fail with an error message indicating this.

The error can be supressed with --ignore-undefined, which matches the previous behavior.

This also changes the errors related to specifying --flag or --unexpand without --value so that they are sent to stderr rather than stdout.

---

PATCHv2: New solution based on the discussion after the previous patch.
PATCHv3: Dropped the idea with the --default option and instead went
with errors for undefined variables, that can be supressed. Also split the changes into multiple commits to make it more clear what is happening.

 bitbake/bin/bitbake-getvar | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
bitbake/bin/bitbake-getvar +17 -9
index afd2849846..53ab900693 100755
@@ -26,15 +26,15 @@ if __name__ == "__main__":
     parser.add_argument('-f', '--flag', help='Specify a variable flag to query (with --value)', default=None)
     parser.add_argument('--value', help='Only report the value, no history and no variable name', action="store_true")
     parser.add_argument('-q', '--quiet', help='Silence bitbake server logging', action="store_true")
+    parser.add_argument('--ignore-undefined', help='Suppress any errors related to undefined variables', action="store_true")
     args = parser.parse_args()
 
-    if args.unexpand and not args.value:
-        print("--unexpand only makes sense with --value")
-        sys.exit(1)
+    if not args.value:
+        if args.unexpand:
+            sys.exit("--unexpand only makes sense with --value")
 
-    if args.flag and not args.value:
-        print("--flag only makes sense with --value")
-        sys.exit(1)
+        if args.flag:
+            sys.exit("--flag only makes sense with --value")
 
     quiet = args.quiet or args.value
     with bb.tinfoil.Tinfoil(tracking=True, setup_logging=not quiet) as tinfoil:
@@ -44,9 +44,17 @@ if __name__ == "__main__":
         else:
             tinfoil.prepare(quiet=2, config_only=True)
             d = tinfoil.config_data
+
+        value = None
         if args.flag:
-            print(str(d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))))
-        elif args.value:
-            print(str(d.getVar(args.variable, expand=(not args.unexpand))))
+            value = d.getVarFlag(args.variable, args.flag, expand=not args.unexpand)
+            if value is None and not args.ignore_undefined:
+                sys.exit(f"The flag '{args.flag}' is not defined for variable '{args.variable}'")
+        else:
+            value = d.getVar(args.variable, expand=not args.unexpand)
+            if value is None and not args.ignore_undefined:
+                sys.exit(f"The variable '{args.variable}' is not defined")
+        if args.value:
+            print(str(value))
         else:
             bb.data.emit_var(args.variable, d=d, all=True)
#1 lore raw

PATCH v3 2/4 bitbake-getvar: Treat undefined variables as empty with --value

Rather than outputting the string "None" for undefined variables, output only a linefeed (the same as for variables that are defined to the empty string).

---

PATCHv3: Split from the preceding commit.

 bitbake/bin/bitbake-getvar | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bitbake/bin/bitbake-getvar +1 -1
index 53ab900693..8901f99ae2 100755
@@ -55,6 +55,6 @@ if __name__ == "__main__":
             if value is None and not args.ignore_undefined:
                 sys.exit(f"The variable '{args.variable}' is not defined")
         if args.value:
-            print(str(value))
+            print(str(value if value is not None else ""))
         else:
             bb.data.emit_var(args.variable, d=d, all=True)
#2 lore raw

PATCH v3 3/4 bitbake-getvar: Add -s as short option for --ignore-undefined

In reply to Peter Kjellerstedt

---

PATCHv3: New in this patch set.

 bitbake/bin/bitbake-getvar | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bitbake/bin/bitbake-getvar +1 -1
index 8901f99ae2..1b5bb2e1f5 100755
@@ -26,7 +26,7 @@ if __name__ == "__main__":
     parser.add_argument('-f', '--flag', help='Specify a variable flag to query (with --value)', default=None)
     parser.add_argument('--value', help='Only report the value, no history and no variable name', action="store_true")
     parser.add_argument('-q', '--quiet', help='Silence bitbake server logging', action="store_true")
-    parser.add_argument('--ignore-undefined', help='Suppress any errors related to undefined variables', action="store_true")
+    parser.add_argument('-s', '--ignore-undefined', help='Suppress any errors related to undefined variables', action="store_true")
     args = parser.parse_args()
 
     if not args.value:
#3 lore raw

PATCH v3 4/4 bitbake-getvar: Add -v as short option for --value

In reply to Peter Kjellerstedt

---

PATCHv3: New in this patch set.

 bitbake/bin/bitbake-getvar | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bitbake/bin/bitbake-getvar +1 -1
index 1b5bb2e1f5..22e2cb7d8d 100755
@@ -24,7 +24,7 @@ if __name__ == "__main__":
     parser.add_argument("-r", "--recipe", help="Recipe name to query", default=None, required=False)
     parser.add_argument('-u', '--unexpand', help='Do not expand the value (with --value)', action="store_true")
     parser.add_argument('-f', '--flag', help='Specify a variable flag to query (with --value)', default=None)
-    parser.add_argument('--value', help='Only report the value, no history and no variable name', action="store_true")
+    parser.add_argument('-v', '--value', help='Only report the value, no history and no variable name', action="store_true")
     parser.add_argument('-q', '--quiet', help='Silence bitbake server logging', action="store_true")
     parser.add_argument('-s', '--ignore-undefined', help='Suppress any errors related to undefined variables', action="store_true")
     args = parser.parse_args()