diff --git a/client/starwhale/utils/venv.py b/client/starwhale/utils/venv.py index d5b8ed9f62..bbb663c6fa 100644 --- a/client/starwhale/utils/venv.py +++ b/client/starwhale/utils/venv.py @@ -5,6 +5,7 @@ import platform import subprocess from pathlib import Path, PurePath, PosixPath +from functools import lru_cache import yaml import conda_pack @@ -549,15 +550,18 @@ def get_conda_prefix_path(name: str = "") -> str: return output.decode().strip() +@lru_cache() def get_conda_bin() -> str: - # TODO: add process cache for _p in ( + os.environ.get("CONDA_EXE", ""), "/opt/miniconda3/bin/conda", "/opt/anaconda3/bin/conda", + "/usr/local/miniconda3/bin/conda", + "/usr/local/anaconda3/bin/conda", os.path.expanduser("~/miniconda3/bin/conda"), os.path.expanduser("~/anaconda3/bin/conda"), ): - if os.path.exists(_p): + if _p and os.path.exists(_p): return _p else: return "conda" diff --git a/client/tests/core/test_runtime.py b/client/tests/core/test_runtime.py index 2f6597e438..b55822ea57 100644 --- a/client/tests/core/test_runtime.py +++ b/client/tests/core/test_runtime.py @@ -26,7 +26,7 @@ ) from starwhale.utils.fs import empty_dir, ensure_dir, ensure_file from starwhale.base.type import BundleType, DependencyType, RuntimeLockFileType -from starwhale.utils.venv import EnvTarType, get_python_version +from starwhale.utils.venv import EnvTarType, get_conda_bin, get_python_version from starwhale.utils.error import ( FormatError, NotFoundError, @@ -62,7 +62,14 @@ class StandaloneRuntimeTestCase(TestCase): def setUp(self) -> None: self.setUpPyfakefs() + self._clear_cache() + + def tearDown(self) -> None: + self._clear_cache() + + def _clear_cache(self) -> None: sw_config._config = {} + get_conda_bin.cache_clear() @patch("starwhale.utils.venv.check_call") @patch("starwhale.utils.venv.virtualenv.cli_run") @@ -131,6 +138,7 @@ def test_quickstart_from_ishell_venv( assert _rt_config.dependencies._pip_pkgs[0] == "starwhale" assert _rt_config.dependencies._pip_files == [] + @patch("os.environ", {}) @patch("starwhale.utils.venv.check_call") def test_quickstart_from_ishell_conda(self, m_call: MagicMock) -> None: workdir = "/home/starwhale/myproject" @@ -138,6 +146,10 @@ def test_quickstart_from_ishell_conda(self, m_call: MagicMock) -> None: conda_prefix_dir = os.path.join(workdir, SW_AUTO_DIRNAME, "conda") name = "test-conda" + conda_bin_path = "/home/starwhale/miniconda3/bin/conda" + os.environ["CONDA_EXE"] = conda_bin_path + ensure_file(conda_bin_path, "", parents=True) + StandaloneRuntime.quickstart_from_ishell( workdir=workdir, name=name, @@ -148,7 +160,7 @@ def test_quickstart_from_ishell_conda(self, m_call: MagicMock) -> None: _rt_config = load_yaml(runtime_path) assert _rt_config["mode"] == "conda" assert m_call.call_args_list[0][0][0] == [ - "conda", + conda_bin_path, "create", "--yes", "--prefix", @@ -158,7 +170,7 @@ def test_quickstart_from_ishell_conda(self, m_call: MagicMock) -> None: assert " ".join(m_call.call_args_list[1][0][0]).startswith( " ".join( [ - "conda", + conda_bin_path, "run", "--live-stream", "--prefix",