##// END OF EJS Templates
Artur Svistunov -
Show More
@@ -1,106 +1,107 b''
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 re
12 12 import shlex
13 13 import sys
14 14 from pathlib import Path
15 from multiprocessing import list2cmdline
15
16 16 from IPython.core.magic import Magics, magics_class, line_magic
17 17
18 18
19 19 def _is_conda_environment():
20 20 """Return True if the current Python executable is in a conda env"""
21 21 # TODO: does this need to change on windows?
22 22 return Path(sys.prefix, "conda-meta", "history").exists()
23 23
24 24
25 25 def _get_conda_executable():
26 26 """Find the path to the conda executable"""
27 27 # Check if there is a conda executable in the same directory as the Python executable.
28 28 # This is the case within conda's root environment.
29 29 conda = Path(sys.executable).parent / "conda"
30 30 if conda.is_file():
31 31 return str(conda)
32 32
33 33 # Otherwise, attempt to extract the executable from conda history.
34 34 # This applies in any conda environment.
35 35 history = Path(sys.prefix, "conda-meta", "history").read_text()
36 36 match = re.search(
37 37 r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]",
38 38 history,
39 39 flags=re.MULTILINE,
40 40 )
41 41 if match:
42 42 return match.groupdict()["command"]
43 43
44 44 # Fallback: assume conda is available on the system path.
45 45 return "conda"
46 46
47 47
48 48 CONDA_COMMANDS_REQUIRING_PREFIX = {
49 49 'install', 'list', 'remove', 'uninstall', 'update', 'upgrade',
50 50 }
51 51 CONDA_COMMANDS_REQUIRING_YES = {
52 52 'install', 'remove', 'uninstall', 'update', 'upgrade',
53 53 }
54 54 CONDA_ENV_FLAGS = {'-p', '--prefix', '-n', '--name'}
55 55 CONDA_YES_FLAGS = {'-y', '--y'}
56 56
57 57
58 58 @magics_class
59 59 class PackagingMagics(Magics):
60 60 """Magics related to packaging & installation"""
61 61
62 62 @line_magic
63 63 def pip(self, line):
64 64 """Run the pip package manager within the current kernel.
65 65
66 66 Usage:
67 67 %pip install [pkgs]
68 68 """
69 self.shell.system(list2cmdline([sys.executable, "-m", "pip"]) + " " + line)
69 python = shlex.quote(sys.executable)
70 self.shell.system(" ".join([python, "-m", "pip", line]))
70 71
71 72 print("Note: you may need to restart the kernel to use updated packages.")
72 73
73 74 @line_magic
74 75 def conda(self, line):
75 76 """Run the conda package manager within the current kernel.
76 77
77 78 Usage:
78 79 %conda install [pkgs]
79 80 """
80 81 if not _is_conda_environment():
81 82 raise ValueError("The python kernel does not appear to be a conda environment. "
82 83 "Please use ``%pip install`` instead.")
83 84
84 85 conda = _get_conda_executable()
85 86 args = shlex.split(line)
86 87 command = args[0] if len(args) > 0 else ""
87 88 args = args[1:] if len(args) > 1 else [""]
88 89
89 90 extra_args = []
90 91
91 92 # When the subprocess does not allow us to respond "yes" during the installation,
92 93 # we need to insert --yes in the argument list for some commands
93 94 stdin_disabled = getattr(self.shell, 'kernel', None) is not None
94 95 needs_yes = command in CONDA_COMMANDS_REQUIRING_YES
95 96 has_yes = set(args).intersection(CONDA_YES_FLAGS)
96 97 if stdin_disabled and needs_yes and not has_yes:
97 98 extra_args.append("--yes")
98 99
99 100 # Add --prefix to point conda installation to the current environment
100 101 needs_prefix = command in CONDA_COMMANDS_REQUIRING_PREFIX
101 102 has_prefix = set(args).intersection(CONDA_ENV_FLAGS)
102 103 if needs_prefix and not has_prefix:
103 104 extra_args.extend(["--prefix", sys.prefix])
104 105
105 106 self.shell.system(' '.join([conda, command] + extra_args + args))
106 107 print("\nNote: you may need to restart the kernel to use updated packages.")
General Comments 0
You need to be logged in to leave comments. Login now