diff mbox series

[master-next,1/2] recipetool: don't dump stack traces if a toml parser can't be found

Message ID 20240209153951.2659327-1-ross.burton@arm.com
State Accepted, archived
Commit c472b4ce5990080e258a409cec0cab4cdb1a37f8
Headers show
Series [master-next,1/2] recipetool: don't dump stack traces if a toml parser can't be found | expand

Commit Message

Ross Burton Feb. 9, 2024, 3:39 p.m. UTC
From: Ross Burton <ross.burton@arm.com>

If we can't find tomllib or tomli then we can just tell the user politely
that we can't parse the pyproject.toml file, there's no need to dump
exception stack traces.

Move the parser exception handler to catch the actual parse, as otherwise
it will never be used.

Whilst here, also add some debug statements to make it clear what of the
handlers is being called.

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 .../lib/recipetool/create_buildsys_python.py  | 20 +++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/scripts/lib/recipetool/create_buildsys_python.py b/scripts/lib/recipetool/create_buildsys_python.py
index 60c59034504..a589343cfbf 100644
--- a/scripts/lib/recipetool/create_buildsys_python.py
+++ b/scripts/lib/recipetool/create_buildsys_python.py
@@ -573,12 +573,15 @@  class PythonSetupPyRecipeHandler(PythonRecipeHandler):
         if 'buildsystem' in handled:
             return False
 
+        logger.debug("Trying setup.py parser")
+
         # Check for non-zero size setup.py files
         setupfiles = RecipeHandler.checkfiles(srctree, ['setup.py'])
         for fn in setupfiles:
             if os.path.getsize(fn):
                 break
         else:
+            logger.debug("No setup.py found")
             return False
 
         # setup.py is always parsed to get at certain required information, such as
@@ -799,12 +802,15 @@  class PythonPyprojectTomlRecipeHandler(PythonRecipeHandler):
         if 'buildsystem' in handled:
             return False
 
+        logger.debug("Trying pyproject.toml parser")
+
         # Check for non-zero size setup.py files
         setupfiles = RecipeHandler.checkfiles(srctree, ["pyproject.toml"])
         for fn in setupfiles:
             if os.path.getsize(fn):
                 break
         else:
+            logger.debug("No pyproject.toml found")
             return False
 
         setupscript = os.path.join(srctree, "pyproject.toml")
@@ -816,14 +822,16 @@  class PythonPyprojectTomlRecipeHandler(PythonRecipeHandler):
                 try:
                     import tomli as tomllib
                 except ImportError:
-                    logger.exception("Neither 'tomllib' nor 'tomli' could be imported. Please use python3.11 or above or install tomli module")
-                    return False
-                except Exception:
-                    logger.exception("Failed to parse pyproject.toml")
+                    logger.error("Neither 'tomllib' nor 'tomli' could be imported, cannot scan pyproject.toml.")
                     return False
 
-            with open(setupscript, "rb") as f:
-                config = tomllib.load(f)
+            try:
+                with open(setupscript, "rb") as f:
+                    config = tomllib.load(f)
+            except Exception:
+                logger.exception("Failed to parse pyproject.toml")
+                return False
+
             build_backend = config["build-system"]["build-backend"]
             if build_backend in self.build_backend_map:
                 classes.append(self.build_backend_map[build_backend])