Skip to content

Commit

Permalink
fix: parsing path for env
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Dec 5, 2022
1 parent c91f83f commit b717193
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/bentoml/_internal/bento/build_config.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit b717193

Please sign in to comment.