Skip to content

utils

Class info

🛈 DocStrings

Utility functions for dependency management.

check_requirements

check_requirements(requirements: list[str]) -> list[str]

Check which requirements need to be installed.

Parameters:

Name Type Description Default
requirements list[str]

List of requirements to check

required

Returns:

Type Description
list[str]

List of requirements that are not yet installed

Source code in src/llmling/depmanager/utils.py
def check_requirements(requirements: list[str]) -> list[str]:
    """Check which requirements need to be installed.

    Args:
        requirements: List of requirements to check

    Returns:
        List of requirements that are not yet installed
    """
    missing = []
    for req in requirements:
        try:
            # Split requirement into name and version specifier
            name = req.split(">=")[0].split("==")[0].split("<")[0].strip()
            importlib.metadata.distribution(name)
        except importlib.metadata.PackageNotFoundError:
            missing.append(req)
        except Exception as exc:  # noqa: BLE001
            logger.warning("Error checking requirement %s: %s", req, exc)
            missing.append(req)
    return missing

collect_file_dependencies

collect_file_dependencies(path: str | PathLike[str] | UPath) -> set[str]

Collect dependencies from a Python file.

Source code in src/llmling/depmanager/utils.py
def collect_file_dependencies(path: str | PathLike[str] | upath.UPath) -> set[str]:
    """Collect dependencies from a Python file."""
    from upathtools import to_upath

    try:
        content = to_upath(path).read_text(encoding="utf-8", errors="ignore")
        return set(parse_pep723_deps(content))
    except Exception as exc:  # noqa: BLE001
        logger.debug("Failed to parse dependencies from %s: %s", path, exc)
        return set()

collect_file_dependencies_async async

collect_file_dependencies_async(path: str | PathLike[str]) -> set[str]

Collect dependencies from a Python file asynchronously.

Source code in src/llmling/depmanager/utils.py
async def collect_file_dependencies_async(path: str | PathLike[str]) -> set[str]:
    """Collect dependencies from a Python file asynchronously."""
    try:
        from upathtools import read_path

        content = await read_path(path, encoding="utf-8")
        return set(parse_pep723_deps(content))
    except Exception as exc:  # noqa: BLE001
        logger.debug("Failed to parse dependencies from %s: %s", path, exc)
        return set()

detect_uv

detect_uv() -> bool

Detect if we're running in a uv environment.

Source code in src/llmling/depmanager/utils.py
def detect_uv() -> bool:
    """Detect if we're running in a uv environment."""
    try:
        return "UV_VIRTUAL_ENV" in os.environ or bool(shutil.which("uv"))
    except Exception:  # noqa: BLE001
        return False

ensure_importable

ensure_importable(import_path: str) -> None

Ensure a module can be imported.

Source code in src/llmling/depmanager/utils.py
def ensure_importable(import_path: str) -> None:
    """Ensure a module can be imported."""
    try:
        module_name = import_path.split(".", maxsplit=1)[0]
        importlib.import_module(module_name)
    except ImportError as exc:
        installed = {dist.name for dist in importlib.metadata.distributions()}
        msg = (
            f"Module {module_name!r} not found. "
            f"Make sure it's included in requirements "
            f"or the module path is in extra_paths. "
            f"Currently installed packages: {', '.join(sorted(installed))}"
        )
        raise DependencyError(msg) from exc

get_pip_command

get_pip_command(*, prefer_uv: bool = False, is_uv: bool = False) -> Command

Get the appropriate pip command based on environment and settings.

Source code in src/llmling/depmanager/utils.py
def get_pip_command(*, prefer_uv: bool = False, is_uv: bool = False) -> Command:
    """Get the appropriate pip command based on environment and settings."""
    if prefer_uv or is_uv:
        # Check for uv in PATH - will find uv.exe on Windows
        if uv_path := shutil.which("uv"):
            return [str(uv_path), "pip"]
        if prefer_uv:
            logger.warning("uv requested but not found, falling back to pip")

    # Use sys.executable for cross-platform compatibility
    return [sys.executable, "-m", "pip"]

get_venv_info

get_venv_info() -> dict[str, str | None]

Get information about the current virtual environment.

Source code in src/llmling/depmanager/utils.py
def get_venv_info() -> dict[str, str | None]:
    """Get information about the current virtual environment."""
    return {
        "is_venv": str(in_virtualenv()),
        "venv_path": sys.prefix,
        "base_path": getattr(sys, "base_prefix", sys.prefix),
        "conda_prefix": os.environ.get("CONDA_PREFIX"),
        "uv_venv": os.environ.get("UV_VIRTUAL_ENV"),
    }

in_virtualenv

in_virtualenv() -> bool

Check if we're running inside a virtual environment or safe Jupyter kernel.

Returns:

Type Description
bool

True if running in a venv, conda env, or Jupyter kernel within a venv

Source code in src/llmling/depmanager/utils.py
def in_virtualenv() -> bool:
    """Check if we're running inside a virtual environment or safe Jupyter kernel.

    Returns:
        True if running in a venv, conda env, or Jupyter kernel within a venv
    """
    # Basic virtualenv checks
    is_venv = (
        # Standard venv/virtualenv
        hasattr(sys, "real_prefix")
        # Python 3's venv
        or (hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix)
        # Conda
        or bool(os.environ.get("CONDA_PREFIX"))
        # UV
        or bool(os.environ.get("UV_VIRTUAL_ENV"))
    )

    # If we're already in a venv, no need to check Jupyter
    if is_venv:
        return True

    # Only allow Jupyter if it's running in a virtual environment
    parts = Path(sys.argv[0]).parts
    in_jupyter = (
        hasattr(sys, "ps1")
        or "IPython" in sys.modules
        or os.environ.get("JUPYTER_RUNTIME_DIR") is not None
        or any(name.startswith(("jupyter-", "ipython")) for name in parts)
    )

    if in_jupyter:
        # Check if Jupyter is running in a venv
        jupyter_python = sys.executable
        jupyter_prefix = Path(jupyter_python).parent.parent
        system_prefix = Path(getattr(sys, "base_prefix", sys.prefix))
        return jupyter_prefix != system_prefix

    return False

install_requirements

install_requirements(
    requirements: list[str], *, pip_command: Command | None = None, pip_index_url: str | None = None
) -> set[str]

Install missing requirements.

Parameters:

Name Type Description Default
requirements list[str]

Requirements to install

required
pip_command Command | None

Base pip command to use (default: pip)

None
pip_index_url str | None

Optional custom PyPI index URL

None

Returns:

Type Description
set[str]

Set of installed requirements

Raises:

Type Description
DependencyError

If installation fails

Source code in src/llmling/depmanager/utils.py
def install_requirements(
    requirements: list[str],
    *,
    pip_command: Command | None = None,
    pip_index_url: str | None = None,
) -> set[str]:
    """Install missing requirements.

    Args:
        requirements: Requirements to install
        pip_command: Base pip command to use (default: pip)
        pip_index_url: Optional custom PyPI index URL

    Returns:
        Set of installed requirements

    Raises:
        DependencyError: If installation fails
    """
    pip_command = pip_command or ["pip"]
    if not requirements:
        return set()

    cmd = [*pip_command, "install"]

    if pip_index_url:
        cmd.extend(["--index-url", pip_index_url])

    cmd.extend(requirements)

    try:
        result = subprocess.run(cmd, check=True, capture_output=True, text=True)
        logger.debug("Package install output:\n%s", result.stdout)
        return set(requirements)

    except subprocess.CalledProcessError as exc:
        msg = f"Failed to install requirements: {exc}\nOutput: {exc.stderr}"
        raise DependencyError(msg) from exc
    except Exception as exc:
        msg = f"Unexpected error installing requirements: {exc}"
        raise DependencyError(msg) from exc

scan_directory_deps

scan_directory_deps(directory: str | PathLike[str]) -> set[str]

Recursively scan directory for PEP 723 dependencies.

Source code in src/llmling/depmanager/utils.py
def scan_directory_deps(directory: str | PathLike[str]) -> set[str]:
    """Recursively scan directory for PEP 723 dependencies."""
    all_deps: set[str] = set()
    dir_path = Path(directory)

    # Don't scan site-packages or other system directories
    if "site-packages" in str(dir_path):
        return all_deps

    try:
        for path in dir_path.rglob("*.py"):
            all_deps.update(collect_file_dependencies(path))
    except Exception as exc:  # noqa: BLE001
        logger.debug("Failed to scan %s for dependencies: %s", directory, exc)
    return all_deps

scan_directory_deps_async async

scan_directory_deps_async(directory: str | PathLike[str]) -> set[str]

Recursively scan directory for PEP 723 dependencies asynchronously.

Source code in src/llmling/depmanager/utils.py
async def scan_directory_deps_async(directory: str | PathLike[str]) -> set[str]:
    """Recursively scan directory for PEP 723 dependencies asynchronously."""
    all_deps: set[str] = set()
    dir_path = Path(directory)

    # Don't scan site-packages or other system directories
    if "site-packages" in str(dir_path):
        return all_deps

    try:
        from upathtools import read_folder

        files = await read_folder(
            directory,
            pattern="*.py",
            recursive=True,
            exclude=["__pycache__/*", "*.pyc"],
        )

        for content in files.values():
            all_deps.update(parse_pep723_deps(content))

    except Exception as exc:  # noqa: BLE001
        logger.debug("Failed to scan %s for dependencies: %s", directory, exc)

    return all_deps

validate_script

validate_script(content: str, script_path: str) -> None

Verify script content is valid Python code.

Parameters:

Name Type Description Default
content str

The script content

required
script_path str

Path to script (for error messages)

required

Raises:

Type Description
DependencyError

If script content has invalid syntax

Source code in src/llmling/depmanager/utils.py
def validate_script(content: str, script_path: str) -> None:
    """Verify script content is valid Python code.

    Args:
        content: The script content
        script_path: Path to script (for error messages)

    Raises:
        DependencyError: If script content has invalid syntax
    """
    try:
        ast.parse(content)
    except SyntaxError as exc:
        msg = f"Invalid Python syntax in script {script_path}: {exc}"
        raise DependencyError(msg) from exc

verify_paths

verify_paths(paths: Sequence[str | PathLike[str]]) -> None

Verify that paths exist and are accessible.

Source code in src/llmling/depmanager/utils.py
def verify_paths(paths: Sequence[str | PathLike[str]]) -> None:
    """Verify that paths exist and are accessible."""
    for path in paths:
        try:
            path_obj = Path(path)
            if not path_obj.exists():
                msg = f"Path does not exist: {path}"
                raise DependencyError(msg)  # noqa: TRY301
            if not path_obj.is_dir():
                msg = f"Path is not a directory: {path}"
                raise DependencyError(msg)  # noqa: TRY301
        except Exception as exc:
            if isinstance(exc, DependencyError):
                raise
            msg = f"Invalid path {path}: {exc}"
            raise DependencyError(msg) from exc