Skip to content

Commit

Permalink
Adapt mamba name in Win shell wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Mar 12, 2024
1 parent 10f35fa commit e5426e1
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libmamba/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ set(
SHELL_SCRIPTS
mamba.sh
mamba.csh
micromamba.bat
mamba.bat
activate.bat
_mamba_activate.bat
mamba_hook.bat
Expand Down
2 changes: 1 addition & 1 deletion libmamba/data/_mamba_activate.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@REM This is the standard user case. This script is run in root\condabin.
@REM FOR %%A IN ("%~dp0.") DO @SET _sysp=%%~dpA
@REM IF NOT EXIST "!_sysp!\Scripts\micromamba.exe" @SET "_sysp=!_sysp!..\"
@REM IF NOT EXIST "!_sysp!\Scripts\mamba.exe" @SET "_sysp=!_sysp!..\"

@FOR %%A in ("%TMP%") do @SET TMP=%%~sA
@REM It seems that it is not possible to have "CONDA_EXE=Something With Spaces"
Expand Down
13 changes: 12 additions & 1 deletion libmamba/data/activate.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
@REM Copyright (C) 2021 QuantStack
@REM SPDX-License-Identifier: BSD-3-Clause

@CALL "%~dp0..\condabin\mamba_hook.bat"
micromamba activate %*

@REM Replaced by mamba executable with the MAMBA_EXE variable pointing to the correct location.
__MAMBA_INSERT_MAMBA_EXE__

@REM We need to know the name of the executable, either mamba or micromamba
@REM Get the base filename of MAMBA_EXE
@FOR %%A in ("%MAMBA_EXE%") do set "__mamba_filename=%%~nxA"
@REM Remove .exe extension from the filename
@SET "__mamba_name=!__mamba_filename:%~x1=!"

!__mamba_name! activate %*
2 changes: 2 additions & 0 deletions libmamba/data/micromamba.bat → libmamba/data/mamba.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@REM Copyright (C) 2012 Anaconda, Inc
@REM SPDX-License-Identifier: BSD-3-Clause

@REM Replaced by mamba executable with the MAMBA_EXE and MAMBA_ROOT_PREFIX variable pointing
@REM to the correct locations.
__MAMBA_INSERT_MAMBA_EXE__
__MAMBA_INSERT_ROOT_PREFIX__

Expand Down
11 changes: 9 additions & 2 deletions libmamba/data/mamba_hook.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
@FOR %%F in ("%~dp0") do @SET "__mambabin_dir=%%~dpF"
@SET "__mambabin_dir=%__mambabin_dir:~0,-1%"
@SET "PATH=%__mambabin_dir%;%PATH%"
@SET "MAMBA_BAT=%__mambabin_dir%\micromamba.bat"
@SET "MAMBA_BAT=%__mambabin_dir%\mamba.bat"
@FOR %%F in ("%__mambabin_dir%") do @SET "__mamba_root=%%~dpF"
__MAMBA_INSERT_MAMBA_EXE__
@SET __mambabin_dir=
@SET __mamba_root=

@DOSKEY micromamba="%MAMBA_BAT%" $*
@REM We need to define an alias with the same name as the executable to be called by the user.
@REM Get the base filename of MAMBA_EXE
@FOR %%A in ("%MAMBA_EXE%") do set "__mamba_filename=%%~nxA"
@REM Remove .exe extension from the filename
@SET "__mamba_name=!__mamba_filename:%~x1=!"
@REM Define correct alias depending on the executable name
@set "__mamba_cmd=call ""%MAMBA_BAT%"" $*"
@DOSKEY !__mamba_name!=!__mamba_cmd!

@SET CONDA_SHLVL=0
2 changes: 1 addition & 1 deletion libmamba/include/mamba/core/shell_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

extern const char data_mamba_sh[];
extern const char data_mamba_csh[];
extern const char data_micromamba_bat[];
extern const char data_mamba_bat[];
extern const char data_activate_bat[];
extern const char data__mamba_activate_bat[];
extern const char data_mamba_hook_bat[];
Expand Down
8 changes: 4 additions & 4 deletions libmamba/src/core/shell_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ namespace mamba
// Maybe the prefix isn't writable. No big deal, just keep going.
}

std::ofstream mamba_bat_f = open_ofstream(root_prefix / "condabin" / "micromamba.bat");
std::string mamba_bat_contents(data_micromamba_bat);
std::ofstream mamba_bat_f = open_ofstream(root_prefix / "condabin" / "mamba.bat");
std::string mamba_bat_contents(data_mamba_bat);
util::replace_all(
mamba_bat_contents,
std::string("__MAMBA_INSERT_ROOT_PREFIX__"),
Expand Down Expand Up @@ -759,13 +759,13 @@ namespace mamba
return;
}

auto micromamba_bat = root_prefix / "condabin" / "micromamba.bat";
auto mamba_bat = root_prefix / "condabin" / "mamba.bat";
auto _mamba_activate_bat = root_prefix / "condabin" / "_mamba_activate.bat";
auto condabin_activate_bat = root_prefix / "condabin" / "activate.bat";
auto scripts_activate_bat = root_prefix / "Scripts" / "activate.bat";
auto mamba_hook_bat = root_prefix / "condabin" / "mamba_hook.bat";

for (auto& f : { micromamba_bat,
for (auto& f : { mamba_bat,
_mamba_activate_bat,
condabin_activate_bat,
scripts_activate_bat,
Expand Down
10 changes: 6 additions & 4 deletions micromamba/tests/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ def call_interpreter(s, tmp_path, interpreter, interactive=False, env=None):

if interpreter == "cmd.exe":
mods = ["@chcp 65001>nul"]
umamba = helpers.get_umamba()
mamba_name = Path(umamba).stem
for x in s:
if x.startswith("micromamba activate") or x.startswith("micromamba deactivate"):
if x.startswith(f"{mamba_name} activate") or x.startswith(f"{mamba_name} deactivate"):
mods.append("call " + x)
else:
mods.append(x)
Expand Down Expand Up @@ -413,7 +415,7 @@ def test_shell_init_deinit_root_prefix_files(
elif interpreter == "cmd.exe":
files = [
tmp_root_prefix / "condabin" / "mamba_hook.bat",
tmp_root_prefix / "condabin" / "micromamba.bat",
tmp_root_prefix / "condabin" / "mamba.bat",
tmp_root_prefix / "condabin" / "_mamba_activate.bat",
tmp_root_prefix / "condabin" / "activate.bat",
]
Expand All @@ -429,7 +431,7 @@ def test_shell_init_deinit_root_prefix_files(
elif interpreter in ["csh", "tcsh"]:
files = [tmp_root_prefix / "etc" / "profile.d" / "mamba.csh"]
elif interpreter == "nu":
files = [] # moved to ~/.config/nushell.nu controlled by micromamba activation
files = [] # moved to ~/.config/nushell.nu controlled by mamba activation
else:
raise ValueError(f"Unknown shell {interpreter}")

Expand Down Expand Up @@ -798,7 +800,7 @@ def call(s):
s1 = [f"{mamba_name} create -n {u1} xtensor -y -c conda-forge"]
s2 = [f"{mamba_name} create -n {u2} xtensor -y -c conda-forge"]
if interpreter == "cmd.exe":
s3 = [f'micromamba create -n "{u3}" xtensor -y -c conda-forge']
s3 = [f'{mamba_name} create -n "{u3}" xtensor -y -c conda-forge']
else:
s3 = [f"{mamba_name} create -n '{u3}' xtensor -y -c conda-forge"]
call(s1)
Expand Down

0 comments on commit e5426e1

Please sign in to comment.