Skip to content

Commit

Permalink
detect shell conda bin path at first
Browse files Browse the repository at this point in the history
  • Loading branch information
tianweidut committed May 11, 2023
1 parent c9eef92 commit ac04d99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions client/starwhale/utils/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import platform
import subprocess
from pathlib import Path, PurePath, PosixPath
from functools import lru_cache

import yaml
import conda_pack
Expand Down Expand Up @@ -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"
Expand Down
18 changes: 15 additions & 3 deletions client/tests/core/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -131,13 +138,18 @@ 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"
runtime_path = os.path.join(workdir, DefaultYAMLName.RUNTIME)
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,
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit ac04d99

Please sign in to comment.