annotations: do not try to auto-detect JSON format main
authorAndrea Righi <andrea.righi@canonical.com>
Fri, 16 Feb 2024 10:54:34 +0000 (11:54 +0100)
committerAndrea Righi <andrea.righi@canonical.com>
Fri, 16 Feb 2024 11:03:37 +0000 (12:03 +0100)
The script tries to automatically detect the format of the annotations
file (legacy or pure JSON).

However, if the legacy file contains a syntax error, the script will try
to parse the file as JSON (incorrectly) triggering another obscure
error, complaining that the JSON format is invalid.

This hides the capability to detect syntax errors properly in the
annotations file.

To prevent this, introduce a new option --json. In this way the caller
has complete control to decide which format needs to be used to parse
the annotations file.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
kconfig/annotations.py
kconfig/run.py

index 69cf5015f771b4b7e409146cc8e069cbf102ff31..b7812fa832b661c99dfb15c6b66d6c5e72077171 100644 (file)
@@ -65,6 +65,10 @@ class Annotation(Config):
     Parse body of annotations file
     """
 
+    def __init__(self, fname, do_include=True, do_json=False):
+        self.do_json = do_json
+        super().__init__(fname, do_include=True)
+
     def _parse_body(self, data: str, parent=True):
         for line in data.splitlines():
             # Replace tabs with spaces, squeeze multiple into singles and
@@ -229,12 +233,10 @@ class Annotation(Config):
                 self._json_parse(data, is_included=True)
 
     def _parse(self, data: str):
-        # Try to parse the legacy format first, otherwise use the new JSON
-        # format.
-        try:
-            self._legacy_parse(data)
-        except SyntaxError:
+        if self.do_json:
             self._json_parse(data, is_included=False)
+        else:
+            self._legacy_parse(data)
 
     def _remove_entry(self, config: str):
         if self.config[config]:
index e857d5620a723064a874b8aa970e11062cac0c6e..e9a51da916dfdc9a68f96fe3c73af008a61249c5 100644 (file)
@@ -90,6 +90,11 @@ def make_parser():
         action="store_true",
         help="Do not process included annotations (stop at the main file)",
     )
+    parser.add_argument(
+        "--json",
+        action="store_true",
+        help="Try to parse annotations file in pure JSON format",
+    )
 
     ga = parser.add_argument_group(title="Action").add_mutually_exclusive_group(
         required=False
@@ -178,7 +183,7 @@ def print_result(config, data):
 def do_query(args):
     if args.arch is None and args.flavour is not None:
         arg_fail(_ARGPARSER, "error: --flavour requires --arch")
-    a = Annotation(args.file, do_include=(not args.no_include))
+    a = Annotation(args.file, do_include=(not args.no_include), do_json=args.json)
     res = a.search_config(config=args.config, arch=args.arch, flavour=args.flavour)
     # If no arguments are specified dump the whole annotations structure
     if args.config is None and args.arch is None and args.flavour is None: