Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abf6641

Browse files
committed
Implement magic for mamba and micromamba
1 parent 6004e9b commit abf6641

2 files changed

Lines changed: 66 additions & 28 deletions

File tree

.pre-commit-config.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
44
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v3.2.0
5+
rev: v4.4.0
66
hooks:
77
- id: trailing-whitespace
88
- id: end-of-file-fixer
99
- id: check-yaml
1010
- id: check-added-large-files
1111

1212
- repo: https://github.com/akaihola/darker
13-
rev: 1.3.1
13+
rev: 1.7.2
1414
hooks:
1515
- id: darker
16-

IPython/core/magics/packaging.py

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# The full license is in the file COPYING.txt, distributed with this software.
99
#-----------------------------------------------------------------------------
1010

11+
import functools
1112
import re
1213
import shlex
1314
import sys
@@ -16,33 +17,49 @@
1617
from IPython.core.magic import Magics, magics_class, line_magic
1718

1819

19-
def _is_conda_environment():
20-
"""Return True if the current Python executable is in a conda env"""
21-
# TODO: does this need to change on windows?
22-
return Path(sys.prefix, "conda-meta", "history").exists()
20+
def is_conda_environment(func):
21+
@functools.wraps(func)
22+
def wrapper(*args, **kwargs):
23+
"""Return True if the current Python executable is in a conda env"""
24+
# TODO: does this need to change on windows?
25+
if not Path(sys.prefix, "conda-meta", "history").exists():
26+
raise ValueError(
27+
"The python kernel does not appear to be a conda environment. "
28+
"Please use ``%pip install`` instead."
29+
)
30+
return func(*args, **kwargs)
2331

32+
return wrapper
2433

25-
def _get_conda_executable():
26-
"""Find the path to the conda executable"""
34+
35+
def _get_conda_like_executable(command):
36+
"""Find the path to the given executable
37+
38+
Parameters
39+
----------
40+
41+
executable: string
42+
Value should be: conda, mamba or micromamba
43+
"""
2744
# Check if there is a conda executable in the same directory as the Python executable.
2845
# This is the case within conda's root environment.
29-
conda = Path(sys.executable).parent / "conda"
30-
if conda.is_file():
31-
return str(conda)
46+
executable = Path(sys.executable).parent / command
47+
if executable.is_file():
48+
return str(executable)
3249

3350
# Otherwise, attempt to extract the executable from conda history.
3451
# This applies in any conda environment.
3552
history = Path(sys.prefix, "conda-meta", "history").read_text(encoding="utf-8")
3653
match = re.search(
37-
r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]",
54+
rf"^#\s*cmd:\s*(?P<command>.*{executable})\s[create|install]",
3855
history,
3956
flags=re.MULTILINE,
4057
)
4158
if match:
4259
return match.groupdict()["command"]
4360

44-
# Fallback: assume conda is available on the system path.
45-
return "conda"
61+
# Fallback: assume the executable is available on the system path.
62+
return command
4663

4764

4865
CONDA_COMMANDS_REQUIRING_PREFIX = {
@@ -76,18 +93,7 @@ def pip(self, line):
7693

7794
print("Note: you may need to restart the kernel to use updated packages.")
7895

79-
@line_magic
80-
def conda(self, line):
81-
"""Run the conda package manager within the current kernel.
82-
83-
Usage:
84-
%conda install [pkgs]
85-
"""
86-
if not _is_conda_environment():
87-
raise ValueError("The python kernel does not appear to be a conda environment. "
88-
"Please use ``%pip install`` instead.")
89-
90-
conda = _get_conda_executable()
96+
def _run_command(self, cmd, line):
9197
args = shlex.split(line)
9298
command = args[0] if len(args) > 0 else ""
9399
args = args[1:] if len(args) > 1 else [""]
@@ -108,5 +114,38 @@ def conda(self, line):
108114
if needs_prefix and not has_prefix:
109115
extra_args.extend(["--prefix", sys.prefix])
110116

111-
self.shell.system(' '.join([conda, command] + extra_args + args))
117+
self.shell.system(" ".join([cmd, command] + extra_args + args))
112118
print("\nNote: you may need to restart the kernel to use updated packages.")
119+
120+
@line_magic
121+
@is_conda_environment
122+
def conda(self, line):
123+
"""Run the conda package manager within the current kernel.
124+
125+
Usage:
126+
%conda install [pkgs]
127+
"""
128+
conda = _get_conda_like_executable("conda")
129+
self._run_command(conda, line)
130+
131+
@line_magic
132+
@is_conda_environment
133+
def mamba(self, line):
134+
"""Run the mamba package manager within the current kernel.
135+
136+
Usage:
137+
%mamba install [pkgs]
138+
"""
139+
mamba = _get_conda_like_executable("mamba")
140+
self._run_command(mamba, line)
141+
142+
@line_magic
143+
@is_conda_environment
144+
def micromamba(self, line):
145+
"""Run the conda package manager within the current kernel.
146+
147+
Usage:
148+
%micromamba install [pkgs]
149+
"""
150+
micromamba = _get_conda_like_executable("micromamba")
151+
self._run_command(micromamba, line)

0 commit comments

Comments
 (0)