Show More
@@ -1,104 +1,105 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 | from pathlib import Path | |
|
14 | 15 | |
|
15 | 16 | from pathlib import Path |
|
16 | 17 | from IPython.core.magic import Magics, magics_class, line_magic |
|
17 | 18 | |
|
18 | 19 | |
|
19 | 20 | def _is_conda_environment(): |
|
20 | 21 | """Return True if the current Python executable is in a conda env""" |
|
21 | 22 | # TODO: does this need to change on windows? |
|
22 | 23 | return Path(sys.prefix, "conda-meta", "history").exists() |
|
23 | 24 | |
|
24 | 25 | |
|
25 | 26 | def _get_conda_executable(): |
|
26 | 27 | """Find the path to the conda executable""" |
|
27 | 28 | # Check if there is a conda executable in the same directory as the Python executable. |
|
28 | 29 | # This is the case within conda's root environment. |
|
29 | 30 | conda = Path(sys.executable).parent / "conda" |
|
30 | 31 | if conda.isfile(): |
|
31 | 32 | return str(conda) |
|
32 | 33 | |
|
33 | 34 | # Otherwise, attempt to extract the executable from conda history. |
|
34 | 35 | # This applies in any conda environment. |
|
35 | 36 | history = Path(sys.prefix, "conda-meta", "history").read_text() |
|
36 | 37 | match = re.search( |
|
37 | 38 | r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]", |
|
38 | 39 | history, |
|
39 | 40 | flags=re.MULTILINE, |
|
40 | 41 | ) |
|
41 | 42 | if match: |
|
42 | 43 | return match.groupdict()["command"] |
|
43 | 44 | |
|
44 | 45 | # Fallback: assume conda is available on the system path. |
|
45 | 46 | return "conda" |
|
46 | 47 | |
|
47 | 48 | |
|
48 | 49 | CONDA_COMMANDS_REQUIRING_PREFIX = { |
|
49 | 50 | 'install', 'list', 'remove', 'uninstall', 'update', 'upgrade', |
|
50 | 51 | } |
|
51 | 52 | CONDA_COMMANDS_REQUIRING_YES = { |
|
52 | 53 | 'install', 'remove', 'uninstall', 'update', 'upgrade', |
|
53 | 54 | } |
|
54 | 55 | CONDA_ENV_FLAGS = {'-p', '--prefix', '-n', '--name'} |
|
55 | 56 | CONDA_YES_FLAGS = {'-y', '--y'} |
|
56 | 57 | |
|
57 | 58 | |
|
58 | 59 | @magics_class |
|
59 | 60 | class PackagingMagics(Magics): |
|
60 | 61 | """Magics related to packaging & installation""" |
|
61 | 62 | |
|
62 | 63 | @line_magic |
|
63 | 64 | def pip(self, line): |
|
64 | 65 | """Run the pip package manager within the current kernel. |
|
65 | 66 | |
|
66 | 67 | Usage: |
|
67 | 68 | %pip install [pkgs] |
|
68 | 69 | """ |
|
69 | 70 | self.shell.system(' '.join([sys.executable, '-m', 'pip', line])) |
|
70 | 71 | print("Note: you may need to restart the kernel to use updated packages.") |
|
71 | 72 | |
|
72 | 73 | @line_magic |
|
73 | 74 | def conda(self, line): |
|
74 | 75 | """Run the conda package manager within the current kernel. |
|
75 | 76 | |
|
76 | 77 | Usage: |
|
77 | 78 | %conda install [pkgs] |
|
78 | 79 | """ |
|
79 | 80 | if not _is_conda_environment(): |
|
80 | 81 | raise ValueError("The python kernel does not appear to be a conda environment. " |
|
81 | 82 | "Please use ``%pip install`` instead.") |
|
82 | 83 | |
|
83 | 84 | conda = _get_conda_executable() |
|
84 | 85 | args = shlex.split(line) |
|
85 | 86 | command = args[0] |
|
86 | 87 | args = args[1:] |
|
87 | 88 | extra_args = [] |
|
88 | 89 | |
|
89 | 90 | # When the subprocess does not allow us to respond "yes" during the installation, |
|
90 | 91 | # we need to insert --yes in the argument list for some commands |
|
91 | 92 | stdin_disabled = getattr(self.shell, 'kernel', None) is not None |
|
92 | 93 | needs_yes = command in CONDA_COMMANDS_REQUIRING_YES |
|
93 | 94 | has_yes = set(args).intersection(CONDA_YES_FLAGS) |
|
94 | 95 | if stdin_disabled and needs_yes and not has_yes: |
|
95 | 96 | extra_args.append("--yes") |
|
96 | 97 | |
|
97 | 98 | # Add --prefix to point conda installation to the current environment |
|
98 | 99 | needs_prefix = command in CONDA_COMMANDS_REQUIRING_PREFIX |
|
99 | 100 | has_prefix = set(args).intersection(CONDA_ENV_FLAGS) |
|
100 | 101 | if needs_prefix and not has_prefix: |
|
101 | 102 | extra_args.extend(["--prefix", sys.prefix]) |
|
102 | 103 | |
|
103 | 104 | self.shell.system(' '.join([conda, command] + extra_args + args)) |
|
104 | 105 | print("\nNote: you may need to restart the kernel to use updated packages.") |
@@ -1,88 +1,87 b'' | |||
|
1 | 1 | # coding: utf-8 |
|
2 | 2 | |
|
3 | 3 | # This script autogenerates `IPython.core.latex_symbols.py`, which contains a |
|
4 | 4 | # single dict , named `latex_symbols`. The keys in this dict are latex symbols, |
|
5 | 5 | # such as `\\alpha` and the values in the dict are the unicode equivalents for |
|
6 | 6 | # those. Most importantly, only unicode symbols that are valid identifiers in |
|
7 | 7 | # Python 3 are included. |
|
8 | 8 | |
|
9 | 9 | # |
|
10 | 10 | # The original mapping of latex symbols to unicode comes from the `latex_symbols.jl` files from Julia. |
|
11 | 11 | |
|
12 | import os, sys | |
|
12 | from pathlib import Path | |
|
13 | 13 | |
|
14 | 14 | # Import the Julia LaTeX symbols |
|
15 | 15 | print('Importing latex_symbols.js from Julia...') |
|
16 | 16 | import requests |
|
17 | 17 | url = 'https://raw.githubusercontent.com/JuliaLang/julia/master/stdlib/REPL/src/latex_symbols.jl' |
|
18 | 18 | r = requests.get(url) |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | # Build a list of key, value pairs |
|
22 | 22 | print('Building a list of (latex, unicode) key-value pairs...') |
|
23 | 23 | lines = r.text.splitlines() |
|
24 | 24 | |
|
25 | 25 | prefixes_line = lines.index('# "font" prefixes') |
|
26 | 26 | symbols_line = lines.index('# manual additions:') |
|
27 | 27 | |
|
28 | 28 | prefix_dict = {} |
|
29 | 29 | for l in lines[prefixes_line + 1: symbols_line]: |
|
30 | 30 | p = l.split() |
|
31 | 31 | if not p or p[1] == 'latex_symbols': continue |
|
32 | 32 | prefix_dict[p[1]] = p[3] |
|
33 | 33 | |
|
34 | 34 | idents = [] |
|
35 | 35 | for l in lines[symbols_line:]: |
|
36 | 36 | if not '=>' in l: continue # if it's not a def, skip |
|
37 | 37 | if '#' in l: l = l[:l.index('#')] # get rid of eol comments |
|
38 | 38 | x, y = l.strip().split('=>') |
|
39 | 39 | if '*' in x: # if a prefix is present substitute it with its value |
|
40 | 40 | p, x = x.split('*') |
|
41 | 41 | x = prefix_dict[p][:-1] + x[1:] |
|
42 | 42 | x, y = x.split('"')[1], y.split('"')[1] # get the values in quotes |
|
43 | 43 | idents.append((x, y)) |
|
44 | 44 | |
|
45 | 45 | # Filter out non-valid identifiers |
|
46 | 46 | print('Filtering out characters that are not valid Python 3 identifiers') |
|
47 | 47 | |
|
48 | 48 | def test_ident(i): |
|
49 | 49 | """Is the unicode string valid in a Python 3 identifier.""" |
|
50 | 50 | # Some characters are not valid at the start of a name, but we still want to |
|
51 | 51 | # include them. So prefix with 'a', which is valid at the start. |
|
52 | 52 | return ('a' + i).isidentifier() |
|
53 | 53 | |
|
54 | 54 | assert test_ident("α") |
|
55 | 55 | assert not test_ident('‴') |
|
56 | 56 | |
|
57 | 57 | valid_idents = [line for line in idents if test_ident(line[1])] |
|
58 | 58 | |
|
59 | 59 | # Write the `latex_symbols.py` module in the cwd |
|
60 | 60 | |
|
61 | 61 | s = """# encoding: utf-8 |
|
62 | 62 | |
|
63 | 63 | # DO NOT EDIT THIS FILE BY HAND. |
|
64 | 64 | |
|
65 | 65 | # To update this file, run the script /tools/gen_latex_symbols.py using Python 3 |
|
66 | 66 | |
|
67 | 67 | # This file is autogenerated from the file: |
|
68 | 68 | # https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl |
|
69 | 69 | # This original list is filtered to remove any unicode characters that are not valid |
|
70 | 70 | # Python identifiers. |
|
71 | 71 | |
|
72 | 72 | latex_symbols = {\n |
|
73 | 73 | """ |
|
74 | 74 | for line in valid_idents: |
|
75 | 75 | s += ' "%s" : "%s",\n' % (line[0], line[1]) |
|
76 | 76 | s += "}\n" |
|
77 | 77 | |
|
78 | 78 | s += """ |
|
79 | 79 | |
|
80 | 80 | reverse_latex_symbol = { v:k for k,v in latex_symbols.items()} |
|
81 | 81 | """ |
|
82 | 82 | |
|
83 |
fn = |
|
|
84 | print("Writing the file: %s" % fn) | |
|
85 | with open(fn, 'w', encoding='utf-8') as f: | |
|
86 | f.write(s) | |
|
83 | fn = Path("..", "IPython", "core", "latex_symbols.py") | |
|
84 | print("Writing the file: %s" % str(fn)) | |
|
85 | fn.write_text(s, encoding="utf-8") | |
|
87 | 86 | |
|
88 | 87 |
General Comments 0
You need to be logged in to leave comments.
Login now