##// END OF EJS Templates
Use environment variable to identify conda / mamba...
Shaun Walbridge -
Show More
@@ -1,151 +1,164
1 1 """Implementation of packaging-related magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2018 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 import functools
12 import os
12 13 import re
13 14 import shlex
14 15 import sys
15 16 from pathlib import Path
16 17
17 18 from IPython.core.magic import Magics, magics_class, line_magic
18 19
19 20
20 21 def is_conda_environment(func):
21 22 @functools.wraps(func)
22 23 def wrapper(*args, **kwargs):
23 24 """Return True if the current Python executable is in a conda env"""
24 25 # TODO: does this need to change on windows?
25 26 if not Path(sys.prefix, "conda-meta", "history").exists():
26 27 raise ValueError(
27 28 "The python kernel does not appear to be a conda environment. "
28 29 "Please use ``%pip install`` instead."
29 30 )
30 31 return func(*args, **kwargs)
31 32
32 33 return wrapper
33 34
34 35
35 36 def _get_conda_like_executable(command):
36 37 """Find the path to the given executable
37 38
38 39 Parameters
39 40 ----------
40 41
41 42 executable: string
42 43 Value should be: conda, mamba or micromamba
43 44 """
45 # Check for a environment variable bound to the base executable, both conda and mamba
46 # set these when activating an environment.
47 base_executable = "CONDA_EXE"
48 if 'mamba' in command.lower():
49 base_executable = "MAMBA_EXE"
50 if base_executable in os.environ:
51 executable = Path(os.environ[base_executable])
52 if executable.is_file():
53 return str(executable.resolve())
54
44 55 # Check if there is a conda executable in the same directory as the Python executable.
45 56 # This is the case within conda's root environment.
46 57 executable = Path(sys.executable).parent / command
47 58 if executable.is_file():
48 59 return str(executable)
49 60
50 61 # Otherwise, attempt to extract the executable from conda history.
51 # This applies in any conda environment.
62 # This applies in any conda environment. Parsing this way is error prone because
63 # different versions of conda and mamba include differing cmd values such as
64 # `conda`, `conda-script.py`, or `path/to/conda`, here use the raw command provided.
52 65 history = Path(sys.prefix, "conda-meta", "history").read_text(encoding="utf-8")
53 66 match = re.search(
54 rf"^#\s*cmd:\s*(?P<command>.*{executable})\s[create|install]",
67 rf"^#\s*cmd:\s*(?P<command>.*{command})\s[create|install]",
55 68 history,
56 69 flags=re.MULTILINE,
57 70 )
58 71 if match:
59 72 return match.groupdict()["command"]
60 73
61 74 # Fallback: assume the executable is available on the system path.
62 75 return command
63 76
64 77
65 78 CONDA_COMMANDS_REQUIRING_PREFIX = {
66 79 'install', 'list', 'remove', 'uninstall', 'update', 'upgrade',
67 80 }
68 81 CONDA_COMMANDS_REQUIRING_YES = {
69 82 'install', 'remove', 'uninstall', 'update', 'upgrade',
70 83 }
71 84 CONDA_ENV_FLAGS = {'-p', '--prefix', '-n', '--name'}
72 85 CONDA_YES_FLAGS = {'-y', '--y'}
73 86
74 87
75 88 @magics_class
76 89 class PackagingMagics(Magics):
77 90 """Magics related to packaging & installation"""
78 91
79 92 @line_magic
80 93 def pip(self, line):
81 94 """Run the pip package manager within the current kernel.
82 95
83 96 Usage:
84 97 %pip install [pkgs]
85 98 """
86 99 python = sys.executable
87 100 if sys.platform == "win32":
88 101 python = '"' + python + '"'
89 102 else:
90 103 python = shlex.quote(python)
91 104
92 105 self.shell.system(" ".join([python, "-m", "pip", line]))
93 106
94 107 print("Note: you may need to restart the kernel to use updated packages.")
95 108
96 109 def _run_command(self, cmd, line):
97 110 args = shlex.split(line)
98 111 command = args[0] if len(args) > 0 else ""
99 112 args = args[1:] if len(args) > 1 else [""]
100 113
101 114 extra_args = []
102 115
103 116 # When the subprocess does not allow us to respond "yes" during the installation,
104 117 # we need to insert --yes in the argument list for some commands
105 118 stdin_disabled = getattr(self.shell, 'kernel', None) is not None
106 119 needs_yes = command in CONDA_COMMANDS_REQUIRING_YES
107 120 has_yes = set(args).intersection(CONDA_YES_FLAGS)
108 121 if stdin_disabled and needs_yes and not has_yes:
109 122 extra_args.append("--yes")
110 123
111 124 # Add --prefix to point conda installation to the current environment
112 125 needs_prefix = command in CONDA_COMMANDS_REQUIRING_PREFIX
113 126 has_prefix = set(args).intersection(CONDA_ENV_FLAGS)
114 127 if needs_prefix and not has_prefix:
115 128 extra_args.extend(["--prefix", sys.prefix])
116 129
117 130 self.shell.system(" ".join([cmd, command] + extra_args + args))
118 131 print("\nNote: you may need to restart the kernel to use updated packages.")
119 132
120 133 @line_magic
121 134 @is_conda_environment
122 135 def conda(self, line):
123 136 """Run the conda package manager within the current kernel.
124 137
125 138 Usage:
126 139 %conda install [pkgs]
127 140 """
128 141 conda = _get_conda_like_executable("conda")
129 142 self._run_command(conda, line)
130 143
131 144 @line_magic
132 145 @is_conda_environment
133 146 def mamba(self, line):
134 147 """Run the mamba package manager within the current kernel.
135 148
136 149 Usage:
137 150 %mamba install [pkgs]
138 151 """
139 152 mamba = _get_conda_like_executable("mamba")
140 153 self._run_command(mamba, line)
141 154
142 155 @line_magic
143 156 @is_conda_environment
144 157 def micromamba(self, line):
145 158 """Run the conda package manager within the current kernel.
146 159
147 160 Usage:
148 161 %micromamba install [pkgs]
149 162 """
150 163 micromamba = _get_conda_like_executable("micromamba")
151 164 self._run_command(micromamba, line)
General Comments 0
You need to be logged in to leave comments. Login now