fix: use importlib.util.find_spec instead of import aisbf to avoid __init__.py...

fix: use importlib.util.find_spec instead of import aisbf to avoid __init__.py crash on missing providers.json
parent 67c880ce
...@@ -21,6 +21,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. ...@@ -21,6 +21,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
Why did the programmer quit his job? Because he didn't get arrays! Why did the programmer quit his job? Because he didn't get arrays!
""" """
import importlib.util
import os import os
import sys import sys
import subprocess import subprocess
...@@ -92,13 +93,18 @@ def _pkg_bundle_dir(): ...@@ -92,13 +93,18 @@ def _pkg_bundle_dir():
""" """
Locate the bundle of runtime files shipped inside the aisbf package. Locate the bundle of runtime files shipped inside the aisbf package.
Uses importlib.util.find_spec() to locate the package on disk without
executing __init__.py (which would crash trying to load providers.json).
Two sources, tried in order: Two sources, tried in order:
1. aisbf/_share/ — populated by the build_py hook at wheel-build time 1. aisbf/_share/ — populated by the build_py hook at wheel-build time
2. project root — for editable (pip install -e) installs 2. project root — for editable (pip install -e) installs
""" """
try: try:
import aisbf as _pkg spec = importlib.util.find_spec('aisbf')
pkg_dir = Path(_pkg.__file__).parent if spec is None or spec.origin is None:
return None
pkg_dir = Path(spec.origin).parent
share = pkg_dir / '_share' share = pkg_dir / '_share'
if share.is_dir() and (share / 'main.py').exists(): if share.is_dir() and (share / 'main.py').exists():
...@@ -150,8 +156,9 @@ def _bootstrap_share_dir(): ...@@ -150,8 +156,9 @@ def _bootstrap_share_dir():
# ── package bundle ──────────────────────────────────────────────────────── # ── package bundle ────────────────────────────────────────────────────────
_log("Package bundle location:") _log("Package bundle location:")
try: try:
import aisbf as _pkg spec = importlib.util.find_spec('aisbf')
pkg_dir = Path(_pkg.__file__).parent if spec is not None and spec.origin is not None:
pkg_dir = Path(spec.origin).parent
_log(f" Package dir : {pkg_dir}") _log(f" Package dir : {pkg_dir}")
share = pkg_dir / '_share' share = pkg_dir / '_share'
...@@ -159,6 +166,8 @@ def _bootstrap_share_dir(): ...@@ -159,6 +166,8 @@ def _bootstrap_share_dir():
if share.is_dir(): if share.is_dir():
contents = sorted(p.name for p in share.iterdir()) contents = sorted(p.name for p in share.iterdir())
_log(f" _share contents: {contents}") _log(f" _share contents: {contents}")
else:
_log(" Package 'aisbf' not found by importlib")
bundle = _pkg_bundle_dir() bundle = _pkg_bundle_dir()
_log(f" Bundle source: {bundle}") _log(f" Bundle source: {bundle}")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment