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

feat: allow extending bentoml with extra commands #5064

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Changes from all 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
2 changes: 1 addition & 1 deletion src/bentoml/_internal/cloud/deployment.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions src/bentoml_cli/cli.py
Original file line number Diff line number Diff line change
@@ -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"

@@ -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
12 changes: 12 additions & 0 deletions src/bentoml_cli/utils.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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, [])