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