Skip to content

Commit f9be66d

Browse files
authored
add lightning cli (#446)
1 parent d1d2bd9 commit f9be66d

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

setup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ def _prepare_extras(requirements_dir: str = _PATH_REQUIRES, skip_files: tuple =
111111
"Programming Language :: Python :: 3.11",
112112
],
113113
entry_points={
114-
"console_scripts": [
115-
"litserve=litserve.__main__:main",
116-
],
114+
"console_scripts": ["litserve=litserve.__main__:main", "lightning=litserve.cli:main"],
117115
},
118116
)

src/litserve/cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import importlib.util
2+
import subprocess
3+
import sys
4+
5+
6+
def _ensure_lightning_installed():
7+
if not importlib.util.find_spec("lightning_sdk"):
8+
print("Lightning CLI not found. Installing...")
9+
subprocess.check_call([sys.executable, "-m", "pip", "install", "-U", "lightning-sdk"])
10+
11+
12+
def main():
13+
_ensure_lightning_installed()
14+
15+
# Forward CLI arguments to the real lightning command
16+
cli_args = sys.argv[1:]
17+
subprocess.run(["lightning"] + cli_args)

tests/minimal_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
def main():
2323
process = subprocess.Popen(
24-
["python", "tests/simple_server.py"],
24+
["lightning", "serve", "api", "tests/simple_server.py"],
2525
)
2626
print("Waiting for server to start...")
27-
time.sleep(5)
27+
time.sleep(10)
2828
try:
2929
url = "http://127.0.0.1:8000/predict"
3030
data = json.dumps({"input": 4.0}).encode("utf-8")

tests/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import os
2+
import sys
3+
from unittest.mock import patch
24

35
import pytest
46

57
from litserve.__main__ import main
8+
from litserve.cli import _ensure_lightning_installed
69

710

811
def test_dockerize_help(monkeypatch, capsys):
@@ -27,3 +30,11 @@ def test_dockerize_command(monkeypatch, capsys):
2730
os.remove(dummy_server_file)
2831
assert "Dockerfile created successfully" in captured.out, "CLI did not create Dockerfile"
2932
assert os.path.exists("Dockerfile"), "CLI did not create Dockerfile"
33+
34+
35+
@patch("importlib.util.find_spec")
36+
@patch("subprocess.check_call")
37+
def test_ensure_lightning_installed(mock_check_call, mock_find_spec):
38+
mock_find_spec.return_value = False
39+
_ensure_lightning_installed()
40+
mock_check_call.assert_called_once_with([sys.executable, "-m", "pip", "install", "-U", "lightning-sdk"])

0 commit comments

Comments
 (0)