Skip to content

Commit

Permalink
WIP: support runtime deactivate command
Browse files Browse the repository at this point in the history
  • Loading branch information
tianweidut committed Dec 15, 2023
1 parent 0273514 commit 4fcb032
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
15 changes: 10 additions & 5 deletions client/starwhale/core/runtime/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,7 @@ def _tag(
)


@runtime_cmd.command(
"activate",
aliases=["actv"],
help="",
)
@runtime_cmd.command("activate", aliases=["actv"])
@click.argument("uri")
@click.option(
"-f",
Expand All @@ -682,6 +678,15 @@ def _activate(uri: str, force_restore: bool) -> None:
RuntimeTermView.activate(_uri, force_restore)


@runtime_cmd.command(
"deactivate",
aliases=["deactv"],
help="Deactivate the current activate runtime environment, enter the normal shell environment",
)
def _deactivate() -> None:
RuntimeTermView.deactivate()


@runtime_cmd.command("lock")
@click.argument("target_dir", default=".")
@click.option(
Expand Down
5 changes: 5 additions & 0 deletions client/starwhale/core/runtime/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
pip_freeze_by_pybin,
guess_current_py_env,
trunc_python_version,
deactivate_python_env,
get_conda_prefix_path,
check_valid_venv_prefix,
get_user_python_version,
Expand Down Expand Up @@ -779,6 +780,10 @@ def copy(
def activate(cls, uri: Resource, force_restore: bool = False) -> None:
StandaloneRuntime.activate(uri, force_restore)

@classmethod
def deactivate(cls) -> None:
deactivate_python_env()

@classmethod
def lock(
cls,
Expand Down
5 changes: 5 additions & 0 deletions client/starwhale/core/runtime/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ def info(
def activate(cls, uri: Resource, force_restore: bool = False) -> None:
Runtime.activate(uri, force_restore)

@classmethod
@BaseTermView._only_standalone
def deactivate(cls) -> None:
Runtime.deactivate()

@BaseTermView._only_standalone
def dockerize(
self,
Expand Down
40 changes: 31 additions & 9 deletions client/starwhale/utils/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,36 @@ def package_python_env(
return True


def deactivate_python_env() -> None:
pyenv = guess_current_py_env()
if pyenv not in (PythonRunEnv.VENV, PythonRunEnv.CONDA):
console.print(f":tea: current python env is {pyenv}, no need to deactivate.")
return
else:
_name, _bin = get_shell_info()
console.print(
f":cake: deactivate {pyenv} environment, enter a new shell({_name})..."
)
envs = os.environ.copy()

for e in ("PATH", "VIRTUAL_ENV"):
envs.pop(e, None)
os.execl(_bin, _bin, "-i")


def get_shell_info() -> t.Tuple:
import shellingham

try:
_name, _bin = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
_name, _bin = "", ""

if not _bin.startswith("/") or _name == _bin:
_bin = shutil.which(_name) or _bin
return _name, _bin


def activate_python_env(mode: str, identity: str, interactive: bool) -> None:
if mode == PythonRunEnv.VENV:
cmd = f"source {identity}/bin/activate"
Expand All @@ -675,15 +705,7 @@ def activate_python_env(mode: str, identity: str, interactive: bool) -> None:
raise NoSupportError(mode)

if interactive:
import shellingham

try:
_name, _bin = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
_name, _bin = "", ""

if not _bin.startswith("/") or _name == _bin:
_bin = shutil.which(_name) or _bin
_name, _bin = get_shell_info()

if _name == "zsh":
# https://zsh.sourceforge.io/Intro/intro_3.html
Expand Down

0 comments on commit 4fcb032

Please sign in to comment.