diff mbox series

[1/6] bitbake: Add checks for importing bb module

Message ID 20260331145319.3125456-2-rob.woolley@windriver.com
State New
Headers show
Series [1/6] bitbake: Add checks for importing bb module | expand

Commit Message

Rob Woolley March 31, 2026, 2:53 p.m. UTC
The __version__ has been present since bbmake to ensure that the
Python script uses the appropriate bb module. This was necessary
when bbmake had functional code that had to match the bb module.
The code has since been moved into bb/main.py.  The skeleton
code left in bin/bitbake rarely changes save for bumping the
version.

When executed in place, the bitbake script manipulates sys.path
to find the bb module in ../lib. This path is inserted to the
beginning of sys.path and it cannot be overridden. This path is
inserted regardless of whether the bb module is present in the
lib directory.

This adds a check for the existence of the lib/bb/__init__.py file
before inserting the directory to the beginning of sys.path. This
supports bitbake to be executed from inside the git repository.

It deliberately fails in the situation where lib/bb/__init__.py
is missing, but the bb package exists inside a site-package
directory. This situation was previously possible.

Signed-off-by: Rob Woolley <rob.woolley@windriver.com>
---
 bin/bitbake | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/bin/bitbake b/bin/bitbake
index a995bd66..76ec80d9 100755
--- a/bin/bitbake
+++ b/bin/bitbake
@@ -15,23 +15,27 @@  import sys
 import warnings
 warnings.simplefilter("default")
 
-sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)),
-                                'lib'))
-try:
-    import bb
-except RuntimeError as exc:
-    sys.exit(str(exc))
+bindir = os.path.abspath(os.path.dirname(__file__))
+libdir = os.path.join(os.path.dirname(bindir), 'lib')
+bbfile = os.path.join(libdir, 'bb', '__init__.py')
+
+if os.path.exists(bbfile):
+    # Execute bitbake in git repository
+    try:
+        sys.path[0:0] = [libdir]
+        import bb
+    except ImportError as e:
+        print(f"Could not import bb from lib: {e}", file=sys.stderr)
+else:
+    print(f"Could not find bb module", file=sys.stderr)
+    sys.exit(1)
 
 from bb import cookerdata
 from bb.main import bitbake_main, BitBakeConfigParameters, BBMainException
 
 bb.utils.check_system_locale()
 
-__version__ = "2.16.0"
-
 if __name__ == "__main__":
-    if __version__ != bb.__version__:
-        sys.exit("Bitbake core version and program version mismatch!")
     try:
         sys.exit(bitbake_main(BitBakeConfigParameters(sys.argv),
                               cookerdata.CookerConfiguration()))