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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements/app/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ croniter>=1.3.0, <1.4.0 # strict; TODO: for now until we find something more ro
traitlets>=5.3.0, <=5.4.0
arrow>=1.2.0, <1.2.4
lightning-utilities>=0.3.*, !=0.4.0, <0.5.0
importlib-metadata>=4.0.0; python_version < '3.8'
beautifulsoup4>=4.8.0, <4.11.2
inquirer>=2.10.0
psutil<5.9.4
Expand Down
17 changes: 17 additions & 0 deletions src/lightning_app/utilities/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
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(extras: str = "cloud") -> List[str]:
"""Get the list of installable packages in the given extras."""
from lightning_app import __package_name__

extras = f"app-{extras}" if __package_name__ == "lightning" else extras
requirements = {r: Requirement(r) for r in metadata.requires(__package_name__)}
marker = Marker(f'extra == "{extras}"')
return [r.split(";")[0].strip() for r, req in requirements.items() if str(req.marker) == str(marker)]


def requires(module_paths: Union[str, List]):
Expand Down
9 changes: 8 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,14 @@

import pytest

from lightning_app.utilities.imports import requires
from lightning_app.utilities.imports import _get_extras, requires


@mock.patch("lightning_app.utilities.imports.metadata.requires")
def test_get_extras(mock_requires):
mock_requires.return_value = ["docker (>=5.0.0) ; extra == 'test'"]

assert _get_extras("test") == ["docker (>=5.0.0)"]


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