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