Skip to content

Commit

Permalink
feat: allow extending bentoml with extra commands (bentoml#5064)
Browse files Browse the repository at this point in the history
* feat: allow extending bentoml with extra commands

Signed-off-by: Frost Ming <me@frostming.com>

* fix: ensure newline at EOF

Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming authored Nov 6, 2024
1 parent 12483dc commit 0088c75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bentoml/_internal/cloud/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,7 @@ def _build_requirements_txt(bento_dir: str) -> bytes:
content = b""
if filename and os.path.exists(fullpath := os.path.join(bento_dir, filename)):
with open(fullpath, "rb") as f:
content = f.read()
content = f.read().rstrip(b"\n") + b"\n"
for package in config.python.packages or []:
content += f"{package}\n".encode()
bentoml_requirement = get_bentoml_requirement()
Expand Down
4 changes: 4 additions & 0 deletions src/bentoml_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def create_bentoml_cli() -> click.Command:
from bentoml_cli.serve import serve_command
from bentoml_cli.start import start_command
from bentoml_cli.utils import BentoMLCommandGroup
from bentoml_cli.utils import get_entry_points

server_context.service_type = "cli"

Expand Down Expand Up @@ -49,6 +50,9 @@ def bentoml_cli():
bentoml_cli.add_command(develop_command)
bentoml_cli.add_command(deployment_command)
bentoml_cli.add_command(secret_command)
# Load commands from extensions
for ep in get_entry_points("bentoml.commands"):
bentoml_cli.add_command(ep.load())

if psutil.WINDOWS:
import sys
Expand Down
12 changes: 12 additions & 0 deletions src/bentoml_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import re
import time
import typing as t
from importlib.metadata import entry_points

import click
import click_option_group as cog
from click import ClickException
from click.exceptions import UsageError

if t.TYPE_CHECKING:
from importlib.metadata import EntryPoint

from click import Command
from click import Context
from click import HelpFormatter
Expand Down Expand Up @@ -474,3 +477,12 @@ def is_valid_bento_tag(value: str) -> bool:

def is_valid_bento_name(value: str) -> bool:
return re.match(r"^[A-Za-z_0-9]*$", value) is not None


def get_entry_points(group: str) -> t.Iterable[EntryPoint]:
"""A compatible version of importlib.metadata.entry_points for Python < 3.10"""
try:
return entry_points(group=group)
except TypeError:
# For Python < 3.10, entry_points() does not accept group argument
return entry_points().get(group, [])

0 comments on commit 0088c75

Please sign in to comment.