[Concept,06/12] buildman: Extract defconfig loading from scan() into _load_defconfig()

Message ID 20260103203243.3727493-7-sjg@u-boot.org
State New
Headers
Series buildman: Fix pylint warnings in board.py and boards.py |

Commit Message

Simon Glass Jan. 3, 2026, 8:32 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Move the logic that handles #include preprocessing and config loading
into a separate method to reduce complexity of scan()

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 tools/buildman/boards.py | 53 ++++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 21 deletions(-)
  

Patch

diff --git a/tools/buildman/boards.py b/tools/buildman/boards.py
index 1983e4a50d6..e7844f0e5e7 100644
--- a/tools/buildman/boards.py
+++ b/tools/buildman/boards.py
@@ -222,6 +222,37 @@  class KconfigScanner:
         if self._tmpfile:
             try_remove(self._tmpfile)
 
+    def _load_defconfig(self, defconfig):
+        """Load a defconfig file, preprocessing if needed
+
+        If the defconfig contains #include directives, run the C
+        preprocessor to expand them before loading.
+
+        Args:
+            defconfig (str): Path to the defconfig file
+        """
+        temp = None
+        if b'#include' in tools.read_file(defconfig):
+            cpp = os.getenv('CPP', 'cpp').split()
+            cmd = cpp + [
+                '-nostdinc', '-P',
+                '-I', self._srctree,
+                '-undef',
+                '-x', 'assembler-with-cpp',
+                defconfig]
+            stdout = command.output(*cmd, capture_stderr=True)
+            temp = tempfile.NamedTemporaryFile(prefix='buildman-')
+            tools.write_file(temp.name, stdout, False)
+            fname = temp.name
+            tout.info(f'Processing #include to produce {defconfig}')
+        else:
+            fname = defconfig
+
+        self._conf.load_config(fname)
+        if temp:
+            del temp
+        self._tmpfile = None
+
     def scan(self, defconfig, warn_targets):
         """Load a defconfig file to obtain board parameters.
 
@@ -247,27 +278,7 @@  class KconfigScanner:
         expect_target, match, rear = leaf.partition('_defconfig')
         assert match and not rear, f'{leaf} : invalid defconfig'
 
-        temp = None
-        if b'#include' in tools.read_file(defconfig):
-            cpp = os.getenv('CPP', 'cpp').split()
-            cmd = cpp + [
-                '-nostdinc', '-P',
-                '-I', self._srctree,
-                '-undef',
-                '-x', 'assembler-with-cpp',
-                defconfig]
-            stdout = command.output(*cmd, capture_stderr=True)
-            temp = tempfile.NamedTemporaryFile(prefix='buildman-')
-            tools.write_file(temp.name, stdout, False)
-            fname = temp.name
-            tout.info(f'Processing #include to produce {defconfig}')
-        else:
-            fname = defconfig
-
-        self._conf.load_config(fname)
-        if temp:
-            del temp
-        self._tmpfile = None
+        self._load_defconfig(defconfig)
 
         params = {}
         warnings = []