From 80070005451086d43d85777831454d024a4e8b94 Mon Sep 17 00:00:00 2001 From: Aaron Pham <29749331+aarnphm@users.noreply.github.com> Date: Wed, 26 Oct 2022 17:22:33 -0700 Subject: [PATCH 1/4] feat: add bentoml env [skip ci] Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com> --- src/bentoml_cli/env.py | 73 +++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/src/bentoml_cli/env.py b/src/bentoml_cli/env.py index f9460e21c88..edf268d2c96 100644 --- a/src/bentoml_cli/env.py +++ b/src/bentoml_cli/env.py @@ -2,6 +2,7 @@ import os import sys +import shlex import shutil import typing as t import platform @@ -12,6 +13,30 @@ conda_packages_name = "conda_packages" +_ENVVAR = [ + "BENTOML_DEBUG", + "BENTOML_QUIET", + "BENTOML_BUNDLE_LOCAL_BUILD", + "BENTOML_DO_NOT_TRACK", + "BENTOML_CONFIG", + "BENTOML_CONFIG_OPTIONS", + "BENTOML_PORT", + "BENTOML_HOST", + "BENTOML_API_WORKERS", +] +_CONDITIONAL_ENVVAR = [ + # Only print if set + "BENTOML_NUM_THREAD", + "OMP_NUM_THREADS", + "OPENBLAS_NUM_THREADS", + "MKL_NUM_THREADS", + "VECLIB_MAXIMUM_THREADS", + "NUMEXPR_NUM_THREADS", + "RAYON_RS_NUM_CPUS", + "TF_NUM_INTEROP_THREADS", + "TF_NUM_INTRAOP_THREADS", +] + def run_cmd(cmd: list[str]) -> list[str]: return subprocess.check_output(cmd).decode("utf-8").split("\n")[:-1] @@ -32,22 +57,48 @@ def format_dropdown(title: str, content: t.Iterable[str]) -> str: """ -def format_keys(key: str, markdown: bool) -> str: - return f"`{key}`" if markdown else key - - def pretty_format( info_dict: dict[str, str | list[str]], output: t.Literal["md", "plain"] ) -> str: out: list[str] = [] - for key, value in info_dict.items(): - if isinstance(value, list): - if output == "md": + _default_env = [f'{env}={shlex.quote(os.environ.get(env, ""))}' for env in _ENVVAR] + _conditional_env = [ + f'{env}={shlex.quote(os.environ.get(env, ""))}' + for env in _CONDITIONAL_ENVVAR + if os.environ.get(env) is not None + ] + # environment format + if output == "md": + out.append( + """\ +#### Environment variable + +```bash +{env} +``` +""".format( + env="\n".join(_default_env) + ) + ) + if len(_conditional_env) > 0: + out.extend(_conditional_env) + out.append("#### System information\n") + for key, value in info_dict.items(): + if isinstance(value, list): out.append(format_dropdown(key, value)) else: - out.append(f"{key}:\n " + "\n ".join(value)) - else: - out.append(f"{format_keys(key, markdown=output=='md')}: {value}") + out.append(f"`{key}`: {value}") + else: + out.extend(_default_env) + if len(_conditional_env) > 0: + out.extend(_conditional_env) + for key, value in info_dict.items(): + if isinstance(value, list): + out.append( + f"{key}=( " + " ".join(map(lambda s: f'"{s}"', value)) + " )" + ) + else: + out.append(f'{key}="{value}"') return "\n".join(out) @@ -86,7 +137,7 @@ def env(ctx: click.Context, output: t.Literal["md", "plain"]) -> None: # type: is_admin: bool = windll.shell32.IsUserAnAdmin() != 0 info_dict["is_window_admin"] = str(is_admin) else: - info_dict["uid:gid"] = f"{os.geteuid()}:{os.getegid()}" + info_dict["uid_gid"] = f"{os.geteuid()}:{os.getegid()}" if "CONDA_PREFIX" in os.environ: # conda packages From d514149b13c5af10df35b0df5cb8553a6f550382 Mon Sep 17 00:00:00 2001 From: Aaron Pham <29749331+aarnphm@users.noreply.github.com> Date: Fri, 28 Oct 2022 08:01:45 -0700 Subject: [PATCH 2/4] feat: add environment variable for env Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com> --- src/bentoml_cli/env.py | 79 +++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/src/bentoml_cli/env.py b/src/bentoml_cli/env.py index edf268d2c96..6c73e36c0f9 100644 --- a/src/bentoml_cli/env.py +++ b/src/bentoml_cli/env.py @@ -42,7 +42,7 @@ def run_cmd(cmd: list[str]) -> list[str]: return subprocess.check_output(cmd).decode("utf-8").split("\n")[:-1] -def format_dropdown(title: str, content: t.Iterable[str]) -> str: +def _format_dropdown(title: str, content: t.Iterable[str]) -> str: processed = "\n".join(content) return f"""\
{title} @@ -57,49 +57,48 @@ def format_dropdown(title: str, content: t.Iterable[str]) -> str: """ -def pretty_format( - info_dict: dict[str, str | list[str]], output: t.Literal["md", "plain"] -) -> str: +def _format_env(env: list[str]) -> list[str]: + return [f"{e}={shlex.quote(os.environ.get(e, ''))}" for e in env] + + +def format_md(env: list[str], info_dict: dict[str, str | list[str]]) -> list[str]: out: list[str] = [] - _default_env = [f'{env}={shlex.quote(os.environ.get(env, ""))}' for env in _ENVVAR] - _conditional_env = [ - f'{env}={shlex.quote(os.environ.get(env, ""))}' - for env in _CONDITIONAL_ENVVAR - if os.environ.get(env) is not None - ] - # environment format - if output == "md": - out.append( - """\ + out.append( + """\ #### Environment variable ```bash {env} ``` """.format( - env="\n".join(_default_env) - ) + env="\n".join(_format_env(env)) ) - if len(_conditional_env) > 0: - out.extend(_conditional_env) - out.append("#### System information\n") - for key, value in info_dict.items(): - if isinstance(value, list): - out.append(format_dropdown(key, value)) - else: - out.append(f"`{key}`: {value}") - else: - out.extend(_default_env) - if len(_conditional_env) > 0: - out.extend(_conditional_env) - for key, value in info_dict.items(): - if isinstance(value, list): - out.append( - f"{key}=( " + " ".join(map(lambda s: f'"{s}"', value)) + " )" - ) - else: - out.append(f'{key}="{value}"') - return "\n".join(out) + ) + out.append("#### System information\n") + for key, value in info_dict.items(): + if isinstance(value, list): + out.append(_format_dropdown(key, value)) + else: + out.append(f"`{key}`: {value}") + return out + + +def format_bash(env: list[str], info_dict: dict[str, str | list[str]]) -> list[str]: + out: list[str] = [] + out.extend(_format_env(env)) + for key, value in info_dict.items(): + if isinstance(value, list): + out.append(f"{key}=( " + " ".join(map(lambda s: f'"{s}"', value)) + " )") + else: + out.append(f'{key}="{value}"') + return out + + +def pretty_format( + info_dict: dict[str, str | list[str]], output: t.Literal["md", "bash"] +) -> str: + env = _ENVVAR + list(filter(lambda e: e in os.environ, _CONDITIONAL_ENVVAR)) + return "\n".join({"md": format_md, "bash": format_bash}[output](env, info_dict)) def add_env_command(cli: click.Group) -> None: @@ -112,14 +111,14 @@ def add_env_command(cli: click.Group) -> None: @click.option( "-o", "--output", - type=click.Choice(["md", "plain"]), + type=click.Choice(["md", "bash"]), default="md", show_default=True, - help="Output format. '-o plain' to display without format.", + help="Output format. '-o bash' to display without format.", ) @click.pass_context - def env(ctx: click.Context, output: t.Literal["md", "plain"]) -> None: # type: ignore (unused warning) - if output not in ["md", "plain"]: + def env(ctx: click.Context, output: t.Literal["md", "bash"]) -> None: # type: ignore (unused warning) + if output not in ["md", "bash"]: raise CLIException(f"Unknown output format: {output}") is_windows = sys.platform == "win32" From a808612115f5f1e35a769eabebc1449e655d3b32 Mon Sep 17 00:00:00 2001 From: Aaron Pham <29749331+aarnphm@users.noreply.github.com> Date: Mon, 31 Oct 2022 12:54:05 -0700 Subject: [PATCH 3/4] chore: add recent retry runner request to env Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com> --- src/bentoml_cli/env.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bentoml_cli/env.py b/src/bentoml_cli/env.py index 6c73e36c0f9..369e94d9891 100644 --- a/src/bentoml_cli/env.py +++ b/src/bentoml_cli/env.py @@ -26,6 +26,7 @@ ] _CONDITIONAL_ENVVAR = [ # Only print if set + "BENTOML_RETRY_RUNNER_REQUESTS", "BENTOML_NUM_THREAD", "OMP_NUM_THREADS", "OPENBLAS_NUM_THREADS", From 178a64f360348ee46d619edadd6edd15600e0a05 Mon Sep 17 00:00:00 2001 From: Aaron Pham <29749331+aarnphm@users.noreply.github.com> Date: Tue, 1 Nov 2022 00:42:29 -0700 Subject: [PATCH 4/4] chore: update CUDA_VISIBLE_DEVICES env Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com> --- src/bentoml_cli/env.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bentoml_cli/env.py b/src/bentoml_cli/env.py index 369e94d9891..7a41248ebb9 100644 --- a/src/bentoml_cli/env.py +++ b/src/bentoml_cli/env.py @@ -36,6 +36,7 @@ "RAYON_RS_NUM_CPUS", "TF_NUM_INTEROP_THREADS", "TF_NUM_INTRAOP_THREADS", + "CUDA_VISIBLE_DEVICES", ]