From b717193714347c68272b702f5f405f54ce2555f9 Mon Sep 17 00:00:00 2001 From: Aaron Pham <29749331+aarnphm@users.noreply.github.com> Date: Mon, 5 Dec 2022 09:13:24 -0800 Subject: [PATCH] fix: parsing path for env Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com> --- src/bentoml/_internal/bento/build_config.py | 25 ++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/bentoml/_internal/bento/build_config.py b/src/bentoml/_internal/bento/build_config.py index 6b372d0b95f..3536d93f6fa 100644 --- a/src/bentoml/_internal/bento/build_config.py +++ b/src/bentoml/_internal/bento/build_config.py @@ -26,6 +26,7 @@ from ...exceptions import BentoMLException from ..utils.dotenv import parse_dotenv from ..configuration import CLEAN_BENTOML_VERSION +from ..container.generate import BENTO_PATH from .build_dev_bentoml_whl import build_bentoml_editable_wheel from ..container.frontend.dockerfile import DistroSpec from ..container.frontend.dockerfile import get_supported_spec @@ -94,6 +95,13 @@ def _convert_cuda_version( ) +# Possible envvar: +# - ENV=VALUE +# - ENV=/path/to/string +# TODO: support DOS path +_ENVVAR_REGEX = re.compile(r"^(\w+)=(?:[\.\w\-,\/]+)$", re.I) + + def _convert_env( env: str | list[str] | dict[str, str] | None ) -> dict[str, str] | dict[str, str | None] | None: @@ -111,12 +119,23 @@ def _convert_env( if isinstance(env, list): env_dict: dict[str, str | None] = {} for envvar in env: - match = re.match(r"^(\w+)=([\w\-,]+)$", envvar) - if not match: + if not _ENVVAR_REGEX.match(envvar): raise BentoMLException( "All value in `env` list must follow format ENV=VALUE" ) - env_key, env_value = match.groups() + env_key, _, env_value = envvar.partition("=") + if os.path.isfile(env_value): + bento_env_path = BENTO_PATH + os.path.abspath( + os.path.expanduser(os.path.expandvars(env_value)) + ) + logger.info( + "'%s' sets to '%s', which is a file. Make sure to mount this file as a persistent volume to the container when using 'run' command: 'docker run -v %s:%s ...'", + env_key, + env_value, + env_value, + bento_env_path, + ) + env_value = bento_env_path env_dict[env_key] = env_value return env_dict