d
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
"""CLI entrypoint module compatible with console scripts.
|
||||
"""Packaged CLI entrypoint used by installers and console scripts.
|
||||
|
||||
This wraps the existing `medeia_entry.py` runner so installers can set
|
||||
entry points to `medeia_macina.cli_entry:main`.
|
||||
This module provides the `main` entrypoint for `mm`/`medeia` and supports
|
||||
running from a development checkout (by importing the top-level
|
||||
`CLI.MedeiaCLI`) or when running tests that inject a legacy
|
||||
`medeia_entry` shim into `sys.modules`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -128,86 +130,44 @@ def _parse_mode_and_strip_args(args: List[str]) -> Tuple[Optional[str], List[str
|
||||
return mode, out
|
||||
|
||||
|
||||
def _import_medeia_entry_module():
|
||||
"""Import and return the top-level 'medeia_entry' module.
|
||||
|
||||
This attempts a regular import first. If that fails with ImportError it will
|
||||
try a few fallbacks useful for editable installs and running directly from
|
||||
the repository (searching for .egg-link, walking parents, or checking CWD).
|
||||
"""
|
||||
try:
|
||||
_ensure_repo_root_on_sys_path()
|
||||
return importlib.import_module("medeia_entry")
|
||||
except ImportError:
|
||||
# Try to find the project root next to this installed package
|
||||
pkg_dir = Path(__file__).resolve().parent
|
||||
|
||||
# 1) Look for an .egg-link that points to the project root
|
||||
try:
|
||||
for egg in pkg_dir.glob("*.egg-link"):
|
||||
try:
|
||||
project_root = egg.read_text().splitlines()[0].strip()
|
||||
if project_root:
|
||||
candidate = Path(project_root) / "medeia_entry.py"
|
||||
if candidate.exists():
|
||||
if str(Path(project_root)) not in sys.path:
|
||||
sys.path.insert(0, str(Path(project_root)))
|
||||
return importlib.import_module("medeia_entry")
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 2) Walk upwards looking for a top-level 'medeia_entry.py'
|
||||
for parent in pkg_dir.parents:
|
||||
candidate = parent / "medeia_entry.py"
|
||||
if candidate.exists():
|
||||
if str(parent) not in sys.path:
|
||||
sys.path.insert(0, str(parent))
|
||||
return importlib.import_module("medeia_entry")
|
||||
|
||||
# 3) Check current working directory
|
||||
candidate = Path.cwd() / "medeia_entry.py"
|
||||
if candidate.exists():
|
||||
if str(Path.cwd()) not in sys.path:
|
||||
sys.path.insert(0, str(Path.cwd()))
|
||||
return importlib.import_module("medeia_entry")
|
||||
|
||||
raise ImportError(
|
||||
"Could not import 'medeia_entry'. This often means the package is not installed into the active virtualenv or is an outdated install.\n"
|
||||
"Remedy: activate your venv and run: pip install -e . (or re-run the bootstrap script).\n"
|
||||
"If problems persist, recreate the venv and reinstall the project."
|
||||
)
|
||||
|
||||
|
||||
def _run_cli(clean_args: List[str]) -> int:
|
||||
"""Run the CLI runner (MedeiaCLI) with cleaned argv list."""
|
||||
"""Run the CLI runner (MedeiaCLI) with cleaned argv list.
|
||||
|
||||
The function supports three modes (in order):
|
||||
1) If a `medeia_entry` module is present in sys.modules (for tests/legacy
|
||||
scenarios), use it as the source of `MedeiaCLI`.
|
||||
2) If running from a development checkout, import the top-level `CLI` and
|
||||
use `MedeiaCLI` from there.
|
||||
3) Otherwise fail with a helpful message suggesting an editable install.
|
||||
"""
|
||||
try:
|
||||
sys.argv[1:] = list(clean_args)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
mod = _import_medeia_entry_module()
|
||||
|
||||
# Backwards compatibility: the imported module may not expose `MedeiaCLI` as
|
||||
# an attribute (for example, the installed `medeia_entry` delegates to the
|
||||
# packaged entrypoint instead of importing the top-level `CLI` module at
|
||||
# import-time). Try a few strategies to obtain or invoke the CLI:
|
||||
# 1) Check for an in-memory 'medeia_entry' module (tests/legacy installs)
|
||||
MedeiaCLI = None
|
||||
if hasattr(mod, "MedeiaCLI"):
|
||||
MedeiaCLI = getattr(mod, "MedeiaCLI")
|
||||
else:
|
||||
# Try importing the top-level `CLI` module directly (editable/repo mode).
|
||||
if "medeia_entry" in sys.modules:
|
||||
mod = sys.modules.get("medeia_entry")
|
||||
if hasattr(mod, "MedeiaCLI"):
|
||||
MedeiaCLI = getattr(mod, "MedeiaCLI")
|
||||
else:
|
||||
# Preserve the existing error message used by tests that inject a
|
||||
# dummy 'medeia_entry' without a `MedeiaCLI` attribute.
|
||||
raise ImportError(
|
||||
"Imported module 'medeia_entry' does not define 'MedeiaCLI' and direct import of top-level 'CLI' failed.\n"
|
||||
"Remedy: ensure the top-level 'medeia_entry' module exports 'MedeiaCLI' or run from the project root/debug the checkout."
|
||||
)
|
||||
|
||||
# 2) If no in-memory module provided the class, try importing the repo-root CLI
|
||||
if MedeiaCLI is None:
|
||||
try:
|
||||
_ensure_repo_root_on_sys_path()
|
||||
from CLI import MedeiaCLI as _M # type: ignore
|
||||
|
||||
MedeiaCLI = _M
|
||||
except Exception:
|
||||
raise ImportError(
|
||||
"Imported module 'medeia_entry' does not define 'MedeiaCLI' and direct import of top-level 'CLI' failed.\n"
|
||||
"Remedy: activate your venv and run: pip install -e . (or re-run the bootstrap script).\n"
|
||||
"If problems persist, recreate the venv and reinstall the project."
|
||||
"Could not import 'MedeiaCLI'. This often means the project is not available on sys.path (run 'pip install -e .' or re-run the bootstrap script)."
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user