Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[App] Add utility to get install command for package extras #15809

Merged
merged 11 commits into from
Nov 24, 2022
Merged
24 changes: 24 additions & 0 deletions src/lightning_app/utilities/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@
from typing import List, Union

from lightning_utilities.core.imports import module_available
from packaging.requirements import Marker, Requirement

try:
from importlib import metadata
except ImportError:
# Python < 3.8
import importlib_metadata as metadata # type: ignore


def _get_extras_install_command(extras: str) -> str:
"""Get the pip install command for the given extras.

Used by the platform to install cloud extras in the cloud.
"""
from lightning_app import __package_name__

requirements = {r: Requirement(r) for r in metadata.requires(__package_name__)}
marker = Marker(f'extra == "{extras}"')
requirements = [r for r, req in requirements.items() if str(req.marker) == str(marker)]

if requirements:
requirements = [f"'{r.split(';')[0].strip()}'" for r in requirements]
return f"pip install {' '.join(requirements)}"
return ""


def requires(module_paths: Union[str, List]):
Expand Down
10 changes: 9 additions & 1 deletion tests/tests_app/utilities/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@

import pytest

from lightning_app.utilities.imports import requires
from lightning_app import __package_name__
from lightning_app.utilities.imports import _get_extras_install_command, requires


def test_get_extras_install_command():
extras = "app-cloud" if __package_name__ == "lightning" else "cloud"
assert "'docker (>=5.0.0)'" in _get_extras_install_command(extras)

assert _get_extras_install_command("fake-extras") == ""


@mock.patch.dict(os.environ, {"LIGHTING_TESTING": "0"})
Expand Down