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

chore(client): use sys.ver when python version not specified in runtime.yaml #1338

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions client/starwhale/core/runtime/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,15 +113,16 @@ 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,
) -> None:
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()
Expand Down
3 changes: 1 addition & 2 deletions client/starwhale/utils/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
57 changes: 55 additions & 2 deletions client/tests/core/test_runtime.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion example/runtime/pytorch/runtime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ environment:
arch: noarch
os: ubuntu:20.04
cuda: 11.4
python: 3.8
mode: venv
name: pytorch