From b3297a45ffd09db0d8d567e96671653b02055892 Mon Sep 17 00:00:00 2001 From: Jialei <3217223+jialeicui@users.noreply.github.com> Date: Mon, 10 Oct 2022 17:42:35 +0800 Subject: [PATCH] chore(client): use sys.ver when python version not specified in runtime.yaml (#1338) --- client/starwhale/core/runtime/model.py | 6 +-- client/starwhale/utils/venv.py | 3 +- client/tests/core/test_runtime.py | 57 +++++++++++++++++++++++++- example/runtime/pytorch/runtime.yaml | 1 - 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/client/starwhale/core/runtime/model.py b/client/starwhale/core/runtime/model.py index ac43a6f29b..02832f3829 100644 --- a/client/starwhale/core/runtime/model.py +++ b/client/starwhale/core/runtime/model.py @@ -42,7 +42,6 @@ SW_DEV_DUMMY_VERSION, DEFAULT_CONDA_CHANNEL, DEFAULT_MANIFEST_NAME, - DEFAULT_PYTHON_VERSION, ) from starwhale.version import STARWHALE_VERSION from starwhale.base.tag import StandaloneTag @@ -114,7 +113,7 @@ def __init__( self, arch: _t_mixed_str_list = "", os: str = SupportOS.UBUNTU, - python: str = DEFAULT_PYTHON_VERSION, + python: str = "", cuda: str = "", cudnn: str = "", **kw: t.Any, @@ -122,7 +121,8 @@ def __init__( self.arch = _list(arch) self.os = os.lower() - # TODO: use user's swcli python version as the python argument version + if not python: + python = get_python_version() self.python = trunc_python_version(str(python)) self.cuda = str(cuda).strip() self.cudnn = str(cudnn).strip() diff --git a/client/starwhale/utils/venv.py b/client/starwhale/utils/venv.py index 1927aa44bf..92db61f793 100644 --- a/client/starwhale/utils/venv.py +++ b/client/starwhale/utils/venv.py @@ -21,7 +21,6 @@ SW_DEV_DUMMY_VERSION, WHEEL_FILE_EXTENSION, DEFAULT_CONDA_CHANNEL, - DEFAULT_PYTHON_VERSION, ) from starwhale.version import STARWHALE_VERSION from starwhale.utils.fs import empty_dir, ensure_dir, ensure_file @@ -593,7 +592,7 @@ def create_python_env( mode: str, name: str, workdir: Path, - python_version: str = DEFAULT_PYTHON_VERSION, + python_version: str, force: bool = False, ) -> str: if mode == PythonRunEnv.VENV: diff --git a/client/tests/core/test_runtime.py b/client/tests/core/test_runtime.py index 28e12b1c3a..7a4504e0d3 100644 --- a/client/tests/core/test_runtime.py +++ b/client/tests/core/test_runtime.py @@ -1,7 +1,7 @@ import os import typing as t from pathlib import Path -from unittest.mock import patch, MagicMock +from unittest.mock import call, patch, MagicMock import yaml from pyfakefs.fake_filesystem_unittest import TestCase @@ -21,7 +21,11 @@ from starwhale.utils.fs import empty_dir, ensure_dir, ensure_file from starwhale.base.type import URIType, BundleType, RuntimeLockFileType from starwhale.utils.venv import EnvTarType, get_python_version -from starwhale.utils.error import NoSupportError, UnExpectedConfigFieldError +from starwhale.utils.error import ( + NoSupportError, + ConfigFormatError, + UnExpectedConfigFieldError, +) from starwhale.utils.config import SWCliConfigMixed from starwhale.core.runtime.view import ( get_term_view, @@ -465,6 +469,55 @@ def test_build_conda( sr.info() sr.history() + @patch("starwhale.core.runtime.model.get_python_version") + @patch("starwhale.utils.venv.get_user_runtime_python_bin") + @patch("starwhale.utils.venv.is_venv") + @patch("starwhale.utils.venv.is_conda") + @patch("starwhale.utils.venv.subprocess.check_output") + def test_build_without_python_version( + self, + m_call: MagicMock, + m_conda: MagicMock, + m_venv: MagicMock, + m_py_bin: MagicMock, + m_py_ver: MagicMock, + ) -> None: + m_py_bin.return_value = "/home/starwhale/anaconda3/envs/starwhale/bin/python3" + m_venv.return_value = False + m_conda.return_value = True + m_call.return_value = b"3.7.13" + m_py_ver.return_value = "fake.ver" + + name = "demo_runtime" + workdir = "/home/starwhale/myproject" + self.fs.create_file( + os.path.join(workdir, DefaultYAMLName.RUNTIME), + contents=yaml.safe_dump({"name": name, "mode": "venv"}), + ) + + uri = URI(name, expected_type=URIType.RUNTIME) + sr = StandaloneRuntime(uri) + with self.assertRaises(ConfigFormatError): + sr.build(Path(workdir)) + m_py_ver.assert_called_once() + + m_py_ver.return_value = "3.10" + sr.build(Path(workdir)) + m_py_ver.assert_has_calls([call(), call()]) + + sw = SWCliConfigMixed() + runtime_workdir = os.path.join( + sw.rootdir, + "self", + "workdir", + "runtime", + name, + sr._version[:VERSION_PREFIX_CNT], + sr._version, + ) + _manifest = load_yaml(os.path.join(runtime_workdir, DEFAULT_MANIFEST_NAME)) + assert _manifest["environment"]["python"] == m_py_ver.return_value + def get_runtime_config(self) -> t.Dict[str, t.Any]: return { "name": "rttest", diff --git a/example/runtime/pytorch/runtime.yaml b/example/runtime/pytorch/runtime.yaml index 4780ee1430..ab08c45b87 100644 --- a/example/runtime/pytorch/runtime.yaml +++ b/example/runtime/pytorch/runtime.yaml @@ -35,6 +35,5 @@ environment: arch: noarch os: ubuntu:20.04 cuda: 11.4 - python: 3.8 mode: venv name: pytorch