Show More
@@ -1,103 +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 os |
|
11 | import os | |
12 | import re |
|
12 | import re | |
13 | import shlex |
|
13 | import shlex | |
14 | import sys |
|
14 | import sys | |
15 |
|
15 | |||
16 | from IPython.core.magic import Magics, magics_class, line_magic |
|
16 | from IPython.core.magic import Magics, magics_class, line_magic | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | def _is_conda_environment(): |
|
19 | def _is_conda_environment(): | |
20 | """Return True if the current Python executable is in a conda env""" |
|
20 | """Return True if the current Python executable is in a conda env""" | |
21 | # TODO: does this need to change on windows? |
|
21 | # TODO: does this need to change on windows? | |
22 | conda_history = os.path.join(sys.prefix, 'conda-meta', 'history') |
|
22 | conda_history = os.path.join(sys.prefix, 'conda-meta', 'history') | |
23 | return os.path.exists(conda_history) |
|
23 | return os.path.exists(conda_history) | |
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 = os.path.join(os.path.dirname(sys.executable), 'conda') |
|
30 | conda = os.path.join(os.path.dirname(sys.executable), 'conda') | |
31 | if os.path.isfile(conda): |
|
31 | if os.path.isfile(conda): | |
32 | return conda |
|
32 | return 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 | R = re.compile(r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]") |
|
36 | R = re.compile(r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]") | |
37 | with open(os.path.join(sys.prefix, 'conda-meta', 'history')) as f: |
|
37 | with open(os.path.join(sys.prefix, 'conda-meta', 'history')) as f: | |
38 | for line in f: |
|
38 | for line in f: | |
39 | match = R.match(line) |
|
39 | match = R.match(line) | |
40 | if match: |
|
40 | if match: | |
41 | return match.groupdict()['command'] |
|
41 | return match.groupdict()['command'] | |
42 |
|
42 | |||
43 | # Fallback: assume conda is available on the system path. |
|
43 | # Fallback: assume conda is available on the system path. | |
44 | return "conda" |
|
44 | return "conda" | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | CONDA_COMMANDS_REQUIRING_PREFIX = { |
|
47 | CONDA_COMMANDS_REQUIRING_PREFIX = { | |
48 | 'install', 'list', 'remove', 'uninstall', 'update', 'upgrade', |
|
48 | 'install', 'list', 'remove', 'uninstall', 'update', 'upgrade', | |
49 | } |
|
49 | } | |
50 | CONDA_COMMANDS_REQUIRING_YES = { |
|
50 | CONDA_COMMANDS_REQUIRING_YES = { | |
51 | 'install', 'remove', 'uninstall', 'update', 'upgrade', |
|
51 | 'install', 'remove', 'uninstall', 'update', 'upgrade', | |
52 | } |
|
52 | } | |
53 | CONDA_ENV_FLAGS = {'-p', '--prefix', '-n', '--name'} |
|
53 | CONDA_ENV_FLAGS = {'-p', '--prefix', '-n', '--name'} | |
54 | CONDA_YES_FLAGS = {'-y', '--y'} |
|
54 | CONDA_YES_FLAGS = {'-y', '--y'} | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | @magics_class |
|
57 | @magics_class | |
58 | class PackagingMagics(Magics): |
|
58 | class PackagingMagics(Magics): | |
59 | """Magics related to packaging & installation""" |
|
59 | """Magics related to packaging & installation""" | |
60 |
|
60 | |||
61 | @line_magic |
|
61 | @line_magic | |
62 | def pip(self, line): |
|
62 | def pip(self, line): | |
63 | """Run the pip package manager within the current kernel. |
|
63 | """Run the pip package manager within the current kernel. | |
64 |
|
64 | |||
65 | Usage: |
|
65 | Usage: | |
66 | %pip install [pkgs] |
|
66 | %pip install [pkgs] | |
67 | """ |
|
67 | """ | |
68 | self.shell.system(' '.join([sys.executable, '-m', 'pip', line])) |
|
68 | python = shlex.quote(sys.executable) | |
|
69 | self.shell.system(" ".join([python, "-m", "pip", line])) | |||
|
70 | ||||
69 | 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.") | |
70 |
|
72 | |||
71 | @line_magic |
|
73 | @line_magic | |
72 | def conda(self, line): |
|
74 | def conda(self, line): | |
73 | """Run the conda package manager within the current kernel. |
|
75 | """Run the conda package manager within the current kernel. | |
74 |
|
76 | |||
75 | Usage: |
|
77 | Usage: | |
76 | %conda install [pkgs] |
|
78 | %conda install [pkgs] | |
77 | """ |
|
79 | """ | |
78 | if not _is_conda_environment(): |
|
80 | if not _is_conda_environment(): | |
79 | 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. " | |
80 | "Please use ``%pip install`` instead.") |
|
82 | "Please use ``%pip install`` instead.") | |
81 |
|
83 | |||
82 | conda = _get_conda_executable() |
|
84 | conda = _get_conda_executable() | |
83 | args = shlex.split(line) |
|
85 | args = shlex.split(line) | |
84 | command = args[0] |
|
86 | command = args[0] | |
85 | args = args[1:] |
|
87 | args = args[1:] | |
86 | extra_args = [] |
|
88 | extra_args = [] | |
87 |
|
89 | |||
88 | # 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, | |
89 | # 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 | |
90 | stdin_disabled = getattr(self.shell, 'kernel', None) is not None |
|
92 | stdin_disabled = getattr(self.shell, 'kernel', None) is not None | |
91 | needs_yes = command in CONDA_COMMANDS_REQUIRING_YES |
|
93 | needs_yes = command in CONDA_COMMANDS_REQUIRING_YES | |
92 | has_yes = set(args).intersection(CONDA_YES_FLAGS) |
|
94 | has_yes = set(args).intersection(CONDA_YES_FLAGS) | |
93 | if stdin_disabled and needs_yes and not has_yes: |
|
95 | if stdin_disabled and needs_yes and not has_yes: | |
94 | extra_args.append("--yes") |
|
96 | extra_args.append("--yes") | |
95 |
|
97 | |||
96 | # Add --prefix to point conda installation to the current environment |
|
98 | # Add --prefix to point conda installation to the current environment | |
97 | needs_prefix = command in CONDA_COMMANDS_REQUIRING_PREFIX |
|
99 | needs_prefix = command in CONDA_COMMANDS_REQUIRING_PREFIX | |
98 | has_prefix = set(args).intersection(CONDA_ENV_FLAGS) |
|
100 | has_prefix = set(args).intersection(CONDA_ENV_FLAGS) | |
99 | if needs_prefix and not has_prefix: |
|
101 | if needs_prefix and not has_prefix: | |
100 | extra_args.extend(["--prefix", sys.prefix]) |
|
102 | extra_args.extend(["--prefix", sys.prefix]) | |
101 |
|
103 | |||
102 | self.shell.system(' '.join([conda, command] + extra_args + args)) |
|
104 | self.shell.system(' '.join([conda, command] + extra_args + args)) | |
103 | 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