diff mbox series

[v3,03/10] bitbake-setup: Add checks for version information

Message ID 20260616-add-pypi-v7-v3-3-fe224e3ba878@windriver.com
State New
Headers show
Series bitbake-setup PyPI Packaging | expand

Commit Message

Rob Woolley June 16, 2026, 9:31 p.m. UTC
The __version__ has been present since bbmake to ensure that the
Python script uses the appropriate bb module.  Other bitbake-*
scripts do not check the version.

When executed in place, the bitbake-* scripts manipulate sys.path
to find the bb module in ../lib. This path is inserted to the
beginning of sys.path and it cannot be overridden by any other
modules named bb.

This commit adds a check for ../lib/bb/__init__.py before making
changes to sys.path. It falls back to importing the bitbake_setup
module using a custom bitbake_setup package.

Signed-off-by: Rob Woolley <rob.woolley@windriver.com>
---
 bin/bitbake-setup | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

Comments

Richard Purdie June 18, 2026, 1:31 p.m. UTC | #1
Hi Rob,

On Tue, 2026-06-16 at 14:31 -0700, Rob Woolley via lists.openembedded.org wrote:
> The __version__ has been present since bbmake to ensure that the
> Python script uses the appropriate bb module.  Other bitbake-*
> scripts do not check the version.
> 
> When executed in place, the bitbake-* scripts manipulate sys.path
> to find the bb module in ../lib. This path is inserted to the
> beginning of sys.path and it cannot be overridden by any other
> modules named bb.
> 
> This commit adds a check for ../lib/bb/__init__.py before making
> changes to sys.path. It falls back to importing the bitbake_setup
> module using a custom bitbake_setup package.
> 
> Signed-off-by: Rob Woolley <rob.woolley@windriver.com>
> ---
>  bin/bitbake-setup | 29 ++++++++++++++++++++++++++---
>  1 file changed, 26 insertions(+), 3 deletions(-)
> 
> diff --git a/bin/bitbake-setup b/bin/bitbake-setup
> index 664bffee..ed9b0947 100755
> --- a/bin/bitbake-setup
> +++ b/bin/bitbake-setup
> @@ -23,10 +23,33 @@ import time
>  
>  
>  bindir = os.path.abspath(os.path.dirname(__file__))
> -sys.path[0:0] = [os.path.join(os.path.dirname(bindir), 'lib')]
> +libdir = os.path.join(os.path.dirname(bindir), 'lib')
> +bbfile = os.path.join(libdir, 'bb', '__init__.py')
>  
> -import bb.msg  # noqa: E402
> -import bb.process  # noqa: E402
> +# Obtain __version__ and import bb
> +if os.path.exists(bbfile):
> +    # Execute bitbake-setup in git repository
> +    try:
> +        sys.path[0:0] = [libdir]
> +        import bb.msg  # noqa: E402
> +        import bb.process  # noqa: E402
> +        from bb import __version__  # noqa: E402
> +    except ImportError as e:
> +        print(f"Could not import bb from lib: {e}")
> +else:
> +    # Execute bitbake-setup installed with packages
> +    try:
> +        from importlib.metadata import version, PackageNotFoundError  # noqa:E402
> +        try:
> +            import bb.msg  # noqa: E402
> +            import bb.process  # noqa: E402
> +            __version__ = version("bitbake_setup")
> +        except ImportError as e:
> +            print(f"Could not import bb: {e}")
> +        except PackageNotFoundError as e:
> +            print(f"Could not find package bb: {e}")
> +    except ImportError as e:
> +        print(f"Could not import bb from package: {e}")
>  
>  logger = bb.msg.logger_create('bitbake-setup', sys.stdout)


I don't think this patch is quite there yet. To solve some of the
issue, I've sent a bitbake __version__ related patch which adds the
checks to all the bin/* scripts. This is part of the guards which I
felt were missing if "bb" (or parts of it) are going to become
available through pypi.

With the above code, I can see why you need to change the way we're
importing bb. I don't really like having the noqa: E402 scattered in
the code, or the duplicate import blocks.

I think after my change, we should just be able to inspect __version__
from the imported bb, which should allow the duplication to be removed.

Could you have another try at this patch on top of mine with the above
in mind please?

FWIW I have merged patches 1, 2 and 10 from this series which should
allow us to make progress as those were ready.

Thanks,

Richard
diff mbox series

Patch

diff --git a/bin/bitbake-setup b/bin/bitbake-setup
index 664bffee..ed9b0947 100755
--- a/bin/bitbake-setup
+++ b/bin/bitbake-setup
@@ -23,10 +23,33 @@  import time
 
 
 bindir = os.path.abspath(os.path.dirname(__file__))
-sys.path[0:0] = [os.path.join(os.path.dirname(bindir), 'lib')]
+libdir = os.path.join(os.path.dirname(bindir), 'lib')
+bbfile = os.path.join(libdir, 'bb', '__init__.py')
 
-import bb.msg  # noqa: E402
-import bb.process  # noqa: E402
+# Obtain __version__ and import bb
+if os.path.exists(bbfile):
+    # Execute bitbake-setup in git repository
+    try:
+        sys.path[0:0] = [libdir]
+        import bb.msg  # noqa: E402
+        import bb.process  # noqa: E402
+        from bb import __version__  # noqa: E402
+    except ImportError as e:
+        print(f"Could not import bb from lib: {e}")
+else:
+    # Execute bitbake-setup installed with packages
+    try:
+        from importlib.metadata import version, PackageNotFoundError  # noqa:E402
+        try:
+            import bb.msg  # noqa: E402
+            import bb.process  # noqa: E402
+            __version__ = version("bitbake_setup")
+        except ImportError as e:
+            print(f"Could not import bb: {e}")
+        except PackageNotFoundError as e:
+            print(f"Could not find package bb: {e}")
+    except ImportError as e:
+        print(f"Could not import bb from package: {e}")
 
 logger = bb.msg.logger_create('bitbake-setup', sys.stdout)