Show More
@@ -0,0 +1,17 | |||||
|
1 | # coding: utf-8 | |||
|
2 | """Test suite for our sysinfo utilities.""" | |||
|
3 | ||||
|
4 | # Copyright (c) IPython Development Team. | |||
|
5 | # Distributed under the terms of the Modified BSD License. | |||
|
6 | ||||
|
7 | import json | |||
|
8 | import nose.tools as nt | |||
|
9 | ||||
|
10 | from IPython.utils import sysinfo | |||
|
11 | ||||
|
12 | ||||
|
13 | def test_json_getsysinfo(): | |||
|
14 | """ | |||
|
15 | test that it is easily jsonable and don't return bytes somewhere. | |||
|
16 | """ | |||
|
17 | json.dumps(sysinfo.get_sys_info()) |
@@ -1,656 +1,653 | |||||
1 | """Implementation of basic magic functions. |
|
1 | """Implementation of basic magic functions. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (c) 2012 The IPython Development Team. |
|
4 | # Copyright (c) 2012 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 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | from __future__ import print_function |
|
14 | from __future__ import print_function | |
15 |
|
15 | |||
16 | # Stdlib |
|
16 | # Stdlib | |
17 | import io |
|
17 | import io | |
18 | import json |
|
18 | import json | |
19 | import sys |
|
19 | import sys | |
20 | from pprint import pformat |
|
20 | from pprint import pformat | |
21 |
|
21 | |||
22 | # Our own packages |
|
22 | # Our own packages | |
23 | from IPython.core import magic_arguments, page |
|
23 | from IPython.core import magic_arguments, page | |
24 | from IPython.core.error import UsageError |
|
24 | from IPython.core.error import UsageError | |
25 | from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes |
|
25 | from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes | |
26 | from IPython.utils.text import format_screen, dedent, indent |
|
26 | from IPython.utils.text import format_screen, dedent, indent | |
27 | from IPython.testing.skipdoctest import skip_doctest |
|
27 | from IPython.testing.skipdoctest import skip_doctest | |
28 | from IPython.utils.ipstruct import Struct |
|
28 | from IPython.utils.ipstruct import Struct | |
29 | from IPython.utils.path import unquote_filename |
|
29 | from IPython.utils.path import unquote_filename | |
30 | from IPython.utils.py3compat import unicode_type |
|
30 | from IPython.utils.py3compat import unicode_type | |
31 | from IPython.utils.warn import warn, error |
|
31 | from IPython.utils.warn import warn, error | |
32 |
|
32 | |||
33 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
34 | # Magics class implementation |
|
34 | # Magics class implementation | |
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 |
|
36 | |||
37 | class MagicsDisplay(object): |
|
37 | class MagicsDisplay(object): | |
38 | def __init__(self, magics_manager): |
|
38 | def __init__(self, magics_manager): | |
39 | self.magics_manager = magics_manager |
|
39 | self.magics_manager = magics_manager | |
40 |
|
40 | |||
41 | def _lsmagic(self): |
|
41 | def _lsmagic(self): | |
42 | """The main implementation of the %lsmagic""" |
|
42 | """The main implementation of the %lsmagic""" | |
43 | mesc = magic_escapes['line'] |
|
43 | mesc = magic_escapes['line'] | |
44 | cesc = magic_escapes['cell'] |
|
44 | cesc = magic_escapes['cell'] | |
45 | mman = self.magics_manager |
|
45 | mman = self.magics_manager | |
46 | magics = mman.lsmagic() |
|
46 | magics = mman.lsmagic() | |
47 | out = ['Available line magics:', |
|
47 | out = ['Available line magics:', | |
48 | mesc + (' '+mesc).join(sorted(magics['line'])), |
|
48 | mesc + (' '+mesc).join(sorted(magics['line'])), | |
49 | '', |
|
49 | '', | |
50 | 'Available cell magics:', |
|
50 | 'Available cell magics:', | |
51 | cesc + (' '+cesc).join(sorted(magics['cell'])), |
|
51 | cesc + (' '+cesc).join(sorted(magics['cell'])), | |
52 | '', |
|
52 | '', | |
53 | mman.auto_status()] |
|
53 | mman.auto_status()] | |
54 | return '\n'.join(out) |
|
54 | return '\n'.join(out) | |
55 |
|
55 | |||
56 | def _repr_pretty_(self, p, cycle): |
|
56 | def _repr_pretty_(self, p, cycle): | |
57 | p.text(self._lsmagic()) |
|
57 | p.text(self._lsmagic()) | |
58 |
|
58 | |||
59 | def __str__(self): |
|
59 | def __str__(self): | |
60 | return self._lsmagic() |
|
60 | return self._lsmagic() | |
61 |
|
61 | |||
62 | def _jsonable(self): |
|
62 | def _jsonable(self): | |
63 | """turn magics dict into jsonable dict of the same structure |
|
63 | """turn magics dict into jsonable dict of the same structure | |
64 |
|
64 | |||
65 | replaces object instances with their class names as strings |
|
65 | replaces object instances with their class names as strings | |
66 | """ |
|
66 | """ | |
67 | magic_dict = {} |
|
67 | magic_dict = {} | |
68 | mman = self.magics_manager |
|
68 | mman = self.magics_manager | |
69 | magics = mman.lsmagic() |
|
69 | magics = mman.lsmagic() | |
70 | for key, subdict in magics.items(): |
|
70 | for key, subdict in magics.items(): | |
71 | d = {} |
|
71 | d = {} | |
72 | magic_dict[key] = d |
|
72 | magic_dict[key] = d | |
73 | for name, obj in subdict.items(): |
|
73 | for name, obj in subdict.items(): | |
74 | try: |
|
74 | try: | |
75 | classname = obj.__self__.__class__.__name__ |
|
75 | classname = obj.__self__.__class__.__name__ | |
76 | except AttributeError: |
|
76 | except AttributeError: | |
77 | classname = 'Other' |
|
77 | classname = 'Other' | |
78 |
|
78 | |||
79 | d[name] = classname |
|
79 | d[name] = classname | |
80 | return magic_dict |
|
80 | return magic_dict | |
81 |
|
81 | |||
82 | def _repr_json_(self): |
|
82 | def _repr_json_(self): | |
83 | return json.dumps(self._jsonable()) |
|
83 | return json.dumps(self._jsonable()) | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | @magics_class |
|
86 | @magics_class | |
87 | class BasicMagics(Magics): |
|
87 | class BasicMagics(Magics): | |
88 | """Magics that provide central IPython functionality. |
|
88 | """Magics that provide central IPython functionality. | |
89 |
|
89 | |||
90 | These are various magics that don't fit into specific categories but that |
|
90 | These are various magics that don't fit into specific categories but that | |
91 | are all part of the base 'IPython experience'.""" |
|
91 | are all part of the base 'IPython experience'.""" | |
92 |
|
92 | |||
93 | @magic_arguments.magic_arguments() |
|
93 | @magic_arguments.magic_arguments() | |
94 | @magic_arguments.argument( |
|
94 | @magic_arguments.argument( | |
95 | '-l', '--line', action='store_true', |
|
95 | '-l', '--line', action='store_true', | |
96 | help="""Create a line magic alias.""" |
|
96 | help="""Create a line magic alias.""" | |
97 | ) |
|
97 | ) | |
98 | @magic_arguments.argument( |
|
98 | @magic_arguments.argument( | |
99 | '-c', '--cell', action='store_true', |
|
99 | '-c', '--cell', action='store_true', | |
100 | help="""Create a cell magic alias.""" |
|
100 | help="""Create a cell magic alias.""" | |
101 | ) |
|
101 | ) | |
102 | @magic_arguments.argument( |
|
102 | @magic_arguments.argument( | |
103 | 'name', |
|
103 | 'name', | |
104 | help="""Name of the magic to be created.""" |
|
104 | help="""Name of the magic to be created.""" | |
105 | ) |
|
105 | ) | |
106 | @magic_arguments.argument( |
|
106 | @magic_arguments.argument( | |
107 | 'target', |
|
107 | 'target', | |
108 | help="""Name of the existing line or cell magic.""" |
|
108 | help="""Name of the existing line or cell magic.""" | |
109 | ) |
|
109 | ) | |
110 | @line_magic |
|
110 | @line_magic | |
111 | def alias_magic(self, line=''): |
|
111 | def alias_magic(self, line=''): | |
112 | """Create an alias for an existing line or cell magic. |
|
112 | """Create an alias for an existing line or cell magic. | |
113 |
|
113 | |||
114 | Examples |
|
114 | Examples | |
115 | -------- |
|
115 | -------- | |
116 | :: |
|
116 | :: | |
117 |
|
117 | |||
118 | In [1]: %alias_magic t timeit |
|
118 | In [1]: %alias_magic t timeit | |
119 | Created `%t` as an alias for `%timeit`. |
|
119 | Created `%t` as an alias for `%timeit`. | |
120 | Created `%%t` as an alias for `%%timeit`. |
|
120 | Created `%%t` as an alias for `%%timeit`. | |
121 |
|
121 | |||
122 | In [2]: %t -n1 pass |
|
122 | In [2]: %t -n1 pass | |
123 | 1 loops, best of 3: 954 ns per loop |
|
123 | 1 loops, best of 3: 954 ns per loop | |
124 |
|
124 | |||
125 | In [3]: %%t -n1 |
|
125 | In [3]: %%t -n1 | |
126 | ...: pass |
|
126 | ...: pass | |
127 | ...: |
|
127 | ...: | |
128 | 1 loops, best of 3: 954 ns per loop |
|
128 | 1 loops, best of 3: 954 ns per loop | |
129 |
|
129 | |||
130 | In [4]: %alias_magic --cell whereami pwd |
|
130 | In [4]: %alias_magic --cell whereami pwd | |
131 | UsageError: Cell magic function `%%pwd` not found. |
|
131 | UsageError: Cell magic function `%%pwd` not found. | |
132 | In [5]: %alias_magic --line whereami pwd |
|
132 | In [5]: %alias_magic --line whereami pwd | |
133 | Created `%whereami` as an alias for `%pwd`. |
|
133 | Created `%whereami` as an alias for `%pwd`. | |
134 |
|
134 | |||
135 | In [6]: %whereami |
|
135 | In [6]: %whereami | |
136 | Out[6]: u'/home/testuser' |
|
136 | Out[6]: u'/home/testuser' | |
137 | """ |
|
137 | """ | |
138 | args = magic_arguments.parse_argstring(self.alias_magic, line) |
|
138 | args = magic_arguments.parse_argstring(self.alias_magic, line) | |
139 | shell = self.shell |
|
139 | shell = self.shell | |
140 | mman = self.shell.magics_manager |
|
140 | mman = self.shell.magics_manager | |
141 | escs = ''.join(magic_escapes.values()) |
|
141 | escs = ''.join(magic_escapes.values()) | |
142 |
|
142 | |||
143 | target = args.target.lstrip(escs) |
|
143 | target = args.target.lstrip(escs) | |
144 | name = args.name.lstrip(escs) |
|
144 | name = args.name.lstrip(escs) | |
145 |
|
145 | |||
146 | # Find the requested magics. |
|
146 | # Find the requested magics. | |
147 | m_line = shell.find_magic(target, 'line') |
|
147 | m_line = shell.find_magic(target, 'line') | |
148 | m_cell = shell.find_magic(target, 'cell') |
|
148 | m_cell = shell.find_magic(target, 'cell') | |
149 | if args.line and m_line is None: |
|
149 | if args.line and m_line is None: | |
150 | raise UsageError('Line magic function `%s%s` not found.' % |
|
150 | raise UsageError('Line magic function `%s%s` not found.' % | |
151 | (magic_escapes['line'], target)) |
|
151 | (magic_escapes['line'], target)) | |
152 | if args.cell and m_cell is None: |
|
152 | if args.cell and m_cell is None: | |
153 | raise UsageError('Cell magic function `%s%s` not found.' % |
|
153 | raise UsageError('Cell magic function `%s%s` not found.' % | |
154 | (magic_escapes['cell'], target)) |
|
154 | (magic_escapes['cell'], target)) | |
155 |
|
155 | |||
156 | # If --line and --cell are not specified, default to the ones |
|
156 | # If --line and --cell are not specified, default to the ones | |
157 | # that are available. |
|
157 | # that are available. | |
158 | if not args.line and not args.cell: |
|
158 | if not args.line and not args.cell: | |
159 | if not m_line and not m_cell: |
|
159 | if not m_line and not m_cell: | |
160 | raise UsageError( |
|
160 | raise UsageError( | |
161 | 'No line or cell magic with name `%s` found.' % target |
|
161 | 'No line or cell magic with name `%s` found.' % target | |
162 | ) |
|
162 | ) | |
163 | args.line = bool(m_line) |
|
163 | args.line = bool(m_line) | |
164 | args.cell = bool(m_cell) |
|
164 | args.cell = bool(m_cell) | |
165 |
|
165 | |||
166 | if args.line: |
|
166 | if args.line: | |
167 | mman.register_alias(name, target, 'line') |
|
167 | mman.register_alias(name, target, 'line') | |
168 | print('Created `%s%s` as an alias for `%s%s`.' % ( |
|
168 | print('Created `%s%s` as an alias for `%s%s`.' % ( | |
169 | magic_escapes['line'], name, |
|
169 | magic_escapes['line'], name, | |
170 | magic_escapes['line'], target)) |
|
170 | magic_escapes['line'], target)) | |
171 |
|
171 | |||
172 | if args.cell: |
|
172 | if args.cell: | |
173 | mman.register_alias(name, target, 'cell') |
|
173 | mman.register_alias(name, target, 'cell') | |
174 | print('Created `%s%s` as an alias for `%s%s`.' % ( |
|
174 | print('Created `%s%s` as an alias for `%s%s`.' % ( | |
175 | magic_escapes['cell'], name, |
|
175 | magic_escapes['cell'], name, | |
176 | magic_escapes['cell'], target)) |
|
176 | magic_escapes['cell'], target)) | |
177 |
|
177 | |||
178 | @line_magic |
|
178 | @line_magic | |
179 | def lsmagic(self, parameter_s=''): |
|
179 | def lsmagic(self, parameter_s=''): | |
180 | """List currently available magic functions.""" |
|
180 | """List currently available magic functions.""" | |
181 | return MagicsDisplay(self.shell.magics_manager) |
|
181 | return MagicsDisplay(self.shell.magics_manager) | |
182 |
|
182 | |||
183 | def _magic_docs(self, brief=False, rest=False): |
|
183 | def _magic_docs(self, brief=False, rest=False): | |
184 | """Return docstrings from magic functions.""" |
|
184 | """Return docstrings from magic functions.""" | |
185 | mman = self.shell.magics_manager |
|
185 | mman = self.shell.magics_manager | |
186 | docs = mman.lsmagic_docs(brief, missing='No documentation') |
|
186 | docs = mman.lsmagic_docs(brief, missing='No documentation') | |
187 |
|
187 | |||
188 | if rest: |
|
188 | if rest: | |
189 | format_string = '**%s%s**::\n\n%s\n\n' |
|
189 | format_string = '**%s%s**::\n\n%s\n\n' | |
190 | else: |
|
190 | else: | |
191 | format_string = '%s%s:\n%s\n' |
|
191 | format_string = '%s%s:\n%s\n' | |
192 |
|
192 | |||
193 | return ''.join( |
|
193 | return ''.join( | |
194 | [format_string % (magic_escapes['line'], fname, |
|
194 | [format_string % (magic_escapes['line'], fname, | |
195 | indent(dedent(fndoc))) |
|
195 | indent(dedent(fndoc))) | |
196 | for fname, fndoc in sorted(docs['line'].items())] |
|
196 | for fname, fndoc in sorted(docs['line'].items())] | |
197 | + |
|
197 | + | |
198 | [format_string % (magic_escapes['cell'], fname, |
|
198 | [format_string % (magic_escapes['cell'], fname, | |
199 | indent(dedent(fndoc))) |
|
199 | indent(dedent(fndoc))) | |
200 | for fname, fndoc in sorted(docs['cell'].items())] |
|
200 | for fname, fndoc in sorted(docs['cell'].items())] | |
201 | ) |
|
201 | ) | |
202 |
|
202 | |||
203 | @line_magic |
|
203 | @line_magic | |
204 | def magic(self, parameter_s=''): |
|
204 | def magic(self, parameter_s=''): | |
205 | """Print information about the magic function system. |
|
205 | """Print information about the magic function system. | |
206 |
|
206 | |||
207 | Supported formats: -latex, -brief, -rest |
|
207 | Supported formats: -latex, -brief, -rest | |
208 | """ |
|
208 | """ | |
209 |
|
209 | |||
210 | mode = '' |
|
210 | mode = '' | |
211 | try: |
|
211 | try: | |
212 | mode = parameter_s.split()[0][1:] |
|
212 | mode = parameter_s.split()[0][1:] | |
213 | if mode == 'rest': |
|
213 | if mode == 'rest': | |
214 | rest_docs = [] |
|
214 | rest_docs = [] | |
215 | except IndexError: |
|
215 | except IndexError: | |
216 | pass |
|
216 | pass | |
217 |
|
217 | |||
218 | brief = (mode == 'brief') |
|
218 | brief = (mode == 'brief') | |
219 | rest = (mode == 'rest') |
|
219 | rest = (mode == 'rest') | |
220 | magic_docs = self._magic_docs(brief, rest) |
|
220 | magic_docs = self._magic_docs(brief, rest) | |
221 |
|
221 | |||
222 | if mode == 'latex': |
|
222 | if mode == 'latex': | |
223 | print(self.format_latex(magic_docs)) |
|
223 | print(self.format_latex(magic_docs)) | |
224 | return |
|
224 | return | |
225 | else: |
|
225 | else: | |
226 | magic_docs = format_screen(magic_docs) |
|
226 | magic_docs = format_screen(magic_docs) | |
227 |
|
227 | |||
228 | out = [""" |
|
228 | out = [""" | |
229 | IPython's 'magic' functions |
|
229 | IPython's 'magic' functions | |
230 | =========================== |
|
230 | =========================== | |
231 |
|
231 | |||
232 | The magic function system provides a series of functions which allow you to |
|
232 | The magic function system provides a series of functions which allow you to | |
233 | control the behavior of IPython itself, plus a lot of system-type |
|
233 | control the behavior of IPython itself, plus a lot of system-type | |
234 | features. There are two kinds of magics, line-oriented and cell-oriented. |
|
234 | features. There are two kinds of magics, line-oriented and cell-oriented. | |
235 |
|
235 | |||
236 | Line magics are prefixed with the % character and work much like OS |
|
236 | Line magics are prefixed with the % character and work much like OS | |
237 | command-line calls: they get as an argument the rest of the line, where |
|
237 | command-line calls: they get as an argument the rest of the line, where | |
238 | arguments are passed without parentheses or quotes. For example, this will |
|
238 | arguments are passed without parentheses or quotes. For example, this will | |
239 | time the given statement:: |
|
239 | time the given statement:: | |
240 |
|
240 | |||
241 | %timeit range(1000) |
|
241 | %timeit range(1000) | |
242 |
|
242 | |||
243 | Cell magics are prefixed with a double %%, and they are functions that get as |
|
243 | Cell magics are prefixed with a double %%, and they are functions that get as | |
244 | an argument not only the rest of the line, but also the lines below it in a |
|
244 | an argument not only the rest of the line, but also the lines below it in a | |
245 | separate argument. These magics are called with two arguments: the rest of the |
|
245 | separate argument. These magics are called with two arguments: the rest of the | |
246 | call line and the body of the cell, consisting of the lines below the first. |
|
246 | call line and the body of the cell, consisting of the lines below the first. | |
247 | For example:: |
|
247 | For example:: | |
248 |
|
248 | |||
249 | %%timeit x = numpy.random.randn((100, 100)) |
|
249 | %%timeit x = numpy.random.randn((100, 100)) | |
250 | numpy.linalg.svd(x) |
|
250 | numpy.linalg.svd(x) | |
251 |
|
251 | |||
252 | will time the execution of the numpy svd routine, running the assignment of x |
|
252 | will time the execution of the numpy svd routine, running the assignment of x | |
253 | as part of the setup phase, which is not timed. |
|
253 | as part of the setup phase, which is not timed. | |
254 |
|
254 | |||
255 | In a line-oriented client (the terminal or Qt console IPython), starting a new |
|
255 | In a line-oriented client (the terminal or Qt console IPython), starting a new | |
256 | input with %% will automatically enter cell mode, and IPython will continue |
|
256 | input with %% will automatically enter cell mode, and IPython will continue | |
257 | reading input until a blank line is given. In the notebook, simply type the |
|
257 | reading input until a blank line is given. In the notebook, simply type the | |
258 | whole cell as one entity, but keep in mind that the %% escape can only be at |
|
258 | whole cell as one entity, but keep in mind that the %% escape can only be at | |
259 | the very start of the cell. |
|
259 | the very start of the cell. | |
260 |
|
260 | |||
261 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
261 | NOTE: If you have 'automagic' enabled (via the command line option or with the | |
262 | %automagic function), you don't need to type in the % explicitly for line |
|
262 | %automagic function), you don't need to type in the % explicitly for line | |
263 | magics; cell magics always require an explicit '%%' escape. By default, |
|
263 | magics; cell magics always require an explicit '%%' escape. By default, | |
264 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
264 | IPython ships with automagic on, so you should only rarely need the % escape. | |
265 |
|
265 | |||
266 | Example: typing '%cd mydir' (without the quotes) changes you working directory |
|
266 | Example: typing '%cd mydir' (without the quotes) changes you working directory | |
267 | to 'mydir', if it exists. |
|
267 | to 'mydir', if it exists. | |
268 |
|
268 | |||
269 | For a list of the available magic functions, use %lsmagic. For a description |
|
269 | For a list of the available magic functions, use %lsmagic. For a description | |
270 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
270 | of any of them, type %magic_name?, e.g. '%cd?'. | |
271 |
|
271 | |||
272 | Currently the magic system has the following functions:""", |
|
272 | Currently the magic system has the following functions:""", | |
273 | magic_docs, |
|
273 | magic_docs, | |
274 | "Summary of magic functions (from %slsmagic):" % magic_escapes['line'], |
|
274 | "Summary of magic functions (from %slsmagic):" % magic_escapes['line'], | |
275 | str(self.lsmagic()), |
|
275 | str(self.lsmagic()), | |
276 | ] |
|
276 | ] | |
277 | page.page('\n'.join(out)) |
|
277 | page.page('\n'.join(out)) | |
278 |
|
278 | |||
279 |
|
279 | |||
280 | @line_magic |
|
280 | @line_magic | |
281 | def page(self, parameter_s=''): |
|
281 | def page(self, parameter_s=''): | |
282 | """Pretty print the object and display it through a pager. |
|
282 | """Pretty print the object and display it through a pager. | |
283 |
|
283 | |||
284 | %page [options] OBJECT |
|
284 | %page [options] OBJECT | |
285 |
|
285 | |||
286 | If no object is given, use _ (last output). |
|
286 | If no object is given, use _ (last output). | |
287 |
|
287 | |||
288 | Options: |
|
288 | Options: | |
289 |
|
289 | |||
290 | -r: page str(object), don't pretty-print it.""" |
|
290 | -r: page str(object), don't pretty-print it.""" | |
291 |
|
291 | |||
292 | # After a function contributed by Olivier Aubert, slightly modified. |
|
292 | # After a function contributed by Olivier Aubert, slightly modified. | |
293 |
|
293 | |||
294 | # Process options/args |
|
294 | # Process options/args | |
295 | opts, args = self.parse_options(parameter_s, 'r') |
|
295 | opts, args = self.parse_options(parameter_s, 'r') | |
296 | raw = 'r' in opts |
|
296 | raw = 'r' in opts | |
297 |
|
297 | |||
298 | oname = args and args or '_' |
|
298 | oname = args and args or '_' | |
299 | info = self.shell._ofind(oname) |
|
299 | info = self.shell._ofind(oname) | |
300 | if info['found']: |
|
300 | if info['found']: | |
301 | txt = (raw and str or pformat)( info['obj'] ) |
|
301 | txt = (raw and str or pformat)( info['obj'] ) | |
302 | page.page(txt) |
|
302 | page.page(txt) | |
303 | else: |
|
303 | else: | |
304 | print('Object `%s` not found' % oname) |
|
304 | print('Object `%s` not found' % oname) | |
305 |
|
305 | |||
306 | @line_magic |
|
306 | @line_magic | |
307 | def profile(self, parameter_s=''): |
|
307 | def profile(self, parameter_s=''): | |
308 | """Print your currently active IPython profile. |
|
308 | """Print your currently active IPython profile. | |
309 |
|
309 | |||
310 | See Also |
|
310 | See Also | |
311 | -------- |
|
311 | -------- | |
312 | prun : run code using the Python profiler |
|
312 | prun : run code using the Python profiler | |
313 | (:meth:`~IPython.core.magics.execution.ExecutionMagics.prun`) |
|
313 | (:meth:`~IPython.core.magics.execution.ExecutionMagics.prun`) | |
314 | """ |
|
314 | """ | |
315 | warn("%profile is now deprecated. Please use get_ipython().profile instead.") |
|
315 | warn("%profile is now deprecated. Please use get_ipython().profile instead.") | |
316 | from IPython.core.application import BaseIPythonApplication |
|
316 | from IPython.core.application import BaseIPythonApplication | |
317 | if BaseIPythonApplication.initialized(): |
|
317 | if BaseIPythonApplication.initialized(): | |
318 | print(BaseIPythonApplication.instance().profile) |
|
318 | print(BaseIPythonApplication.instance().profile) | |
319 | else: |
|
319 | else: | |
320 | error("profile is an application-level value, but you don't appear to be in an IPython application") |
|
320 | error("profile is an application-level value, but you don't appear to be in an IPython application") | |
321 |
|
321 | |||
322 | @line_magic |
|
322 | @line_magic | |
323 | def pprint(self, parameter_s=''): |
|
323 | def pprint(self, parameter_s=''): | |
324 | """Toggle pretty printing on/off.""" |
|
324 | """Toggle pretty printing on/off.""" | |
325 | ptformatter = self.shell.display_formatter.formatters['text/plain'] |
|
325 | ptformatter = self.shell.display_formatter.formatters['text/plain'] | |
326 | ptformatter.pprint = bool(1 - ptformatter.pprint) |
|
326 | ptformatter.pprint = bool(1 - ptformatter.pprint) | |
327 | print('Pretty printing has been turned', |
|
327 | print('Pretty printing has been turned', | |
328 | ['OFF','ON'][ptformatter.pprint]) |
|
328 | ['OFF','ON'][ptformatter.pprint]) | |
329 |
|
329 | |||
330 | @line_magic |
|
330 | @line_magic | |
331 | def colors(self, parameter_s=''): |
|
331 | def colors(self, parameter_s=''): | |
332 | """Switch color scheme for prompts, info system and exception handlers. |
|
332 | """Switch color scheme for prompts, info system and exception handlers. | |
333 |
|
333 | |||
334 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
334 | Currently implemented schemes: NoColor, Linux, LightBG. | |
335 |
|
335 | |||
336 | Color scheme names are not case-sensitive. |
|
336 | Color scheme names are not case-sensitive. | |
337 |
|
337 | |||
338 | Examples |
|
338 | Examples | |
339 | -------- |
|
339 | -------- | |
340 | To get a plain black and white terminal:: |
|
340 | To get a plain black and white terminal:: | |
341 |
|
341 | |||
342 | %colors nocolor |
|
342 | %colors nocolor | |
343 | """ |
|
343 | """ | |
344 | def color_switch_err(name): |
|
344 | def color_switch_err(name): | |
345 | warn('Error changing %s color schemes.\n%s' % |
|
345 | warn('Error changing %s color schemes.\n%s' % | |
346 | (name, sys.exc_info()[1])) |
|
346 | (name, sys.exc_info()[1])) | |
347 |
|
347 | |||
348 |
|
348 | |||
349 | new_scheme = parameter_s.strip() |
|
349 | new_scheme = parameter_s.strip() | |
350 | if not new_scheme: |
|
350 | if not new_scheme: | |
351 | raise UsageError( |
|
351 | raise UsageError( | |
352 | "%colors: you must specify a color scheme. See '%colors?'") |
|
352 | "%colors: you must specify a color scheme. See '%colors?'") | |
353 | # local shortcut |
|
353 | # local shortcut | |
354 | shell = self.shell |
|
354 | shell = self.shell | |
355 |
|
355 | |||
356 | import IPython.utils.rlineimpl as readline |
|
356 | import IPython.utils.rlineimpl as readline | |
357 |
|
357 | |||
358 | if not shell.colors_force and \ |
|
358 | if not shell.colors_force and \ | |
359 | not readline.have_readline and \ |
|
359 | not readline.have_readline and \ | |
360 | (sys.platform == "win32" or sys.platform == "cli"): |
|
360 | (sys.platform == "win32" or sys.platform == "cli"): | |
361 | msg = """\ |
|
361 | msg = """\ | |
362 | Proper color support under MS Windows requires the pyreadline library. |
|
362 | Proper color support under MS Windows requires the pyreadline library. | |
363 | You can find it at: |
|
363 | You can find it at: | |
364 | http://ipython.org/pyreadline.html |
|
364 | http://ipython.org/pyreadline.html | |
365 | Gary's readline needs the ctypes module, from: |
|
|||
366 | http://starship.python.net/crew/theller/ctypes |
|
|||
367 | (Note that ctypes is already part of Python versions 2.5 and newer). |
|
|||
368 |
|
365 | |||
369 | Defaulting color scheme to 'NoColor'""" |
|
366 | Defaulting color scheme to 'NoColor'""" | |
370 | new_scheme = 'NoColor' |
|
367 | new_scheme = 'NoColor' | |
371 | warn(msg) |
|
368 | warn(msg) | |
372 |
|
369 | |||
373 | # readline option is 0 |
|
370 | # readline option is 0 | |
374 | if not shell.colors_force and not shell.has_readline: |
|
371 | if not shell.colors_force and not shell.has_readline: | |
375 | new_scheme = 'NoColor' |
|
372 | new_scheme = 'NoColor' | |
376 |
|
373 | |||
377 | # Set prompt colors |
|
374 | # Set prompt colors | |
378 | try: |
|
375 | try: | |
379 | shell.prompt_manager.color_scheme = new_scheme |
|
376 | shell.prompt_manager.color_scheme = new_scheme | |
380 | except: |
|
377 | except: | |
381 | color_switch_err('prompt') |
|
378 | color_switch_err('prompt') | |
382 | else: |
|
379 | else: | |
383 | shell.colors = \ |
|
380 | shell.colors = \ | |
384 | shell.prompt_manager.color_scheme_table.active_scheme_name |
|
381 | shell.prompt_manager.color_scheme_table.active_scheme_name | |
385 | # Set exception colors |
|
382 | # Set exception colors | |
386 | try: |
|
383 | try: | |
387 | shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
384 | shell.InteractiveTB.set_colors(scheme = new_scheme) | |
388 | shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
385 | shell.SyntaxTB.set_colors(scheme = new_scheme) | |
389 | except: |
|
386 | except: | |
390 | color_switch_err('exception') |
|
387 | color_switch_err('exception') | |
391 |
|
388 | |||
392 | # Set info (for 'object?') colors |
|
389 | # Set info (for 'object?') colors | |
393 | if shell.color_info: |
|
390 | if shell.color_info: | |
394 | try: |
|
391 | try: | |
395 | shell.inspector.set_active_scheme(new_scheme) |
|
392 | shell.inspector.set_active_scheme(new_scheme) | |
396 | except: |
|
393 | except: | |
397 | color_switch_err('object inspector') |
|
394 | color_switch_err('object inspector') | |
398 | else: |
|
395 | else: | |
399 | shell.inspector.set_active_scheme('NoColor') |
|
396 | shell.inspector.set_active_scheme('NoColor') | |
400 |
|
397 | |||
401 | @line_magic |
|
398 | @line_magic | |
402 | def xmode(self, parameter_s=''): |
|
399 | def xmode(self, parameter_s=''): | |
403 | """Switch modes for the exception handlers. |
|
400 | """Switch modes for the exception handlers. | |
404 |
|
401 | |||
405 | Valid modes: Plain, Context and Verbose. |
|
402 | Valid modes: Plain, Context and Verbose. | |
406 |
|
403 | |||
407 | If called without arguments, acts as a toggle.""" |
|
404 | If called without arguments, acts as a toggle.""" | |
408 |
|
405 | |||
409 | def xmode_switch_err(name): |
|
406 | def xmode_switch_err(name): | |
410 | warn('Error changing %s exception modes.\n%s' % |
|
407 | warn('Error changing %s exception modes.\n%s' % | |
411 | (name,sys.exc_info()[1])) |
|
408 | (name,sys.exc_info()[1])) | |
412 |
|
409 | |||
413 | shell = self.shell |
|
410 | shell = self.shell | |
414 | new_mode = parameter_s.strip().capitalize() |
|
411 | new_mode = parameter_s.strip().capitalize() | |
415 | try: |
|
412 | try: | |
416 | shell.InteractiveTB.set_mode(mode=new_mode) |
|
413 | shell.InteractiveTB.set_mode(mode=new_mode) | |
417 | print('Exception reporting mode:',shell.InteractiveTB.mode) |
|
414 | print('Exception reporting mode:',shell.InteractiveTB.mode) | |
418 | except: |
|
415 | except: | |
419 | xmode_switch_err('user') |
|
416 | xmode_switch_err('user') | |
420 |
|
417 | |||
421 | @line_magic |
|
418 | @line_magic | |
422 | def quickref(self,arg): |
|
419 | def quickref(self,arg): | |
423 | """ Show a quick reference sheet """ |
|
420 | """ Show a quick reference sheet """ | |
424 | from IPython.core.usage import quick_reference |
|
421 | from IPython.core.usage import quick_reference | |
425 | qr = quick_reference + self._magic_docs(brief=True) |
|
422 | qr = quick_reference + self._magic_docs(brief=True) | |
426 | page.page(qr) |
|
423 | page.page(qr) | |
427 |
|
424 | |||
428 | @line_magic |
|
425 | @line_magic | |
429 | def doctest_mode(self, parameter_s=''): |
|
426 | def doctest_mode(self, parameter_s=''): | |
430 | """Toggle doctest mode on and off. |
|
427 | """Toggle doctest mode on and off. | |
431 |
|
428 | |||
432 | This mode is intended to make IPython behave as much as possible like a |
|
429 | This mode is intended to make IPython behave as much as possible like a | |
433 | plain Python shell, from the perspective of how its prompts, exceptions |
|
430 | plain Python shell, from the perspective of how its prompts, exceptions | |
434 | and output look. This makes it easy to copy and paste parts of a |
|
431 | and output look. This makes it easy to copy and paste parts of a | |
435 | session into doctests. It does so by: |
|
432 | session into doctests. It does so by: | |
436 |
|
433 | |||
437 | - Changing the prompts to the classic ``>>>`` ones. |
|
434 | - Changing the prompts to the classic ``>>>`` ones. | |
438 | - Changing the exception reporting mode to 'Plain'. |
|
435 | - Changing the exception reporting mode to 'Plain'. | |
439 | - Disabling pretty-printing of output. |
|
436 | - Disabling pretty-printing of output. | |
440 |
|
437 | |||
441 | Note that IPython also supports the pasting of code snippets that have |
|
438 | Note that IPython also supports the pasting of code snippets that have | |
442 | leading '>>>' and '...' prompts in them. This means that you can paste |
|
439 | leading '>>>' and '...' prompts in them. This means that you can paste | |
443 | doctests from files or docstrings (even if they have leading |
|
440 | doctests from files or docstrings (even if they have leading | |
444 | whitespace), and the code will execute correctly. You can then use |
|
441 | whitespace), and the code will execute correctly. You can then use | |
445 | '%history -t' to see the translated history; this will give you the |
|
442 | '%history -t' to see the translated history; this will give you the | |
446 | input after removal of all the leading prompts and whitespace, which |
|
443 | input after removal of all the leading prompts and whitespace, which | |
447 | can be pasted back into an editor. |
|
444 | can be pasted back into an editor. | |
448 |
|
445 | |||
449 | With these features, you can switch into this mode easily whenever you |
|
446 | With these features, you can switch into this mode easily whenever you | |
450 | need to do testing and changes to doctests, without having to leave |
|
447 | need to do testing and changes to doctests, without having to leave | |
451 | your existing IPython session. |
|
448 | your existing IPython session. | |
452 | """ |
|
449 | """ | |
453 |
|
450 | |||
454 | # Shorthands |
|
451 | # Shorthands | |
455 | shell = self.shell |
|
452 | shell = self.shell | |
456 | pm = shell.prompt_manager |
|
453 | pm = shell.prompt_manager | |
457 | meta = shell.meta |
|
454 | meta = shell.meta | |
458 | disp_formatter = self.shell.display_formatter |
|
455 | disp_formatter = self.shell.display_formatter | |
459 | ptformatter = disp_formatter.formatters['text/plain'] |
|
456 | ptformatter = disp_formatter.formatters['text/plain'] | |
460 | # dstore is a data store kept in the instance metadata bag to track any |
|
457 | # dstore is a data store kept in the instance metadata bag to track any | |
461 | # changes we make, so we can undo them later. |
|
458 | # changes we make, so we can undo them later. | |
462 | dstore = meta.setdefault('doctest_mode',Struct()) |
|
459 | dstore = meta.setdefault('doctest_mode',Struct()) | |
463 | save_dstore = dstore.setdefault |
|
460 | save_dstore = dstore.setdefault | |
464 |
|
461 | |||
465 | # save a few values we'll need to recover later |
|
462 | # save a few values we'll need to recover later | |
466 | mode = save_dstore('mode',False) |
|
463 | mode = save_dstore('mode',False) | |
467 | save_dstore('rc_pprint',ptformatter.pprint) |
|
464 | save_dstore('rc_pprint',ptformatter.pprint) | |
468 | save_dstore('xmode',shell.InteractiveTB.mode) |
|
465 | save_dstore('xmode',shell.InteractiveTB.mode) | |
469 | save_dstore('rc_separate_out',shell.separate_out) |
|
466 | save_dstore('rc_separate_out',shell.separate_out) | |
470 | save_dstore('rc_separate_out2',shell.separate_out2) |
|
467 | save_dstore('rc_separate_out2',shell.separate_out2) | |
471 | save_dstore('rc_prompts_pad_left',pm.justify) |
|
468 | save_dstore('rc_prompts_pad_left',pm.justify) | |
472 | save_dstore('rc_separate_in',shell.separate_in) |
|
469 | save_dstore('rc_separate_in',shell.separate_in) | |
473 | save_dstore('rc_active_types',disp_formatter.active_types) |
|
470 | save_dstore('rc_active_types',disp_formatter.active_types) | |
474 | save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template)) |
|
471 | save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template)) | |
475 |
|
472 | |||
476 | if mode == False: |
|
473 | if mode == False: | |
477 | # turn on |
|
474 | # turn on | |
478 | pm.in_template = '>>> ' |
|
475 | pm.in_template = '>>> ' | |
479 | pm.in2_template = '... ' |
|
476 | pm.in2_template = '... ' | |
480 | pm.out_template = '' |
|
477 | pm.out_template = '' | |
481 |
|
478 | |||
482 | # Prompt separators like plain python |
|
479 | # Prompt separators like plain python | |
483 | shell.separate_in = '' |
|
480 | shell.separate_in = '' | |
484 | shell.separate_out = '' |
|
481 | shell.separate_out = '' | |
485 | shell.separate_out2 = '' |
|
482 | shell.separate_out2 = '' | |
486 |
|
483 | |||
487 | pm.justify = False |
|
484 | pm.justify = False | |
488 |
|
485 | |||
489 | ptformatter.pprint = False |
|
486 | ptformatter.pprint = False | |
490 | disp_formatter.active_types = ['text/plain'] |
|
487 | disp_formatter.active_types = ['text/plain'] | |
491 |
|
488 | |||
492 | shell.magic('xmode Plain') |
|
489 | shell.magic('xmode Plain') | |
493 | else: |
|
490 | else: | |
494 | # turn off |
|
491 | # turn off | |
495 | pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates |
|
492 | pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates | |
496 |
|
493 | |||
497 | shell.separate_in = dstore.rc_separate_in |
|
494 | shell.separate_in = dstore.rc_separate_in | |
498 |
|
495 | |||
499 | shell.separate_out = dstore.rc_separate_out |
|
496 | shell.separate_out = dstore.rc_separate_out | |
500 | shell.separate_out2 = dstore.rc_separate_out2 |
|
497 | shell.separate_out2 = dstore.rc_separate_out2 | |
501 |
|
498 | |||
502 | pm.justify = dstore.rc_prompts_pad_left |
|
499 | pm.justify = dstore.rc_prompts_pad_left | |
503 |
|
500 | |||
504 | ptformatter.pprint = dstore.rc_pprint |
|
501 | ptformatter.pprint = dstore.rc_pprint | |
505 | disp_formatter.active_types = dstore.rc_active_types |
|
502 | disp_formatter.active_types = dstore.rc_active_types | |
506 |
|
503 | |||
507 | shell.magic('xmode ' + dstore.xmode) |
|
504 | shell.magic('xmode ' + dstore.xmode) | |
508 |
|
505 | |||
509 | # Store new mode and inform |
|
506 | # Store new mode and inform | |
510 | dstore.mode = bool(1-int(mode)) |
|
507 | dstore.mode = bool(1-int(mode)) | |
511 | mode_label = ['OFF','ON'][dstore.mode] |
|
508 | mode_label = ['OFF','ON'][dstore.mode] | |
512 | print('Doctest mode is:', mode_label) |
|
509 | print('Doctest mode is:', mode_label) | |
513 |
|
510 | |||
514 | @line_magic |
|
511 | @line_magic | |
515 | def gui(self, parameter_s=''): |
|
512 | def gui(self, parameter_s=''): | |
516 | """Enable or disable IPython GUI event loop integration. |
|
513 | """Enable or disable IPython GUI event loop integration. | |
517 |
|
514 | |||
518 | %gui [GUINAME] |
|
515 | %gui [GUINAME] | |
519 |
|
516 | |||
520 | This magic replaces IPython's threaded shells that were activated |
|
517 | This magic replaces IPython's threaded shells that were activated | |
521 | using the (pylab/wthread/etc.) command line flags. GUI toolkits |
|
518 | using the (pylab/wthread/etc.) command line flags. GUI toolkits | |
522 | can now be enabled at runtime and keyboard |
|
519 | can now be enabled at runtime and keyboard | |
523 | interrupts should work without any problems. The following toolkits |
|
520 | interrupts should work without any problems. The following toolkits | |
524 | are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX):: |
|
521 | are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX):: | |
525 |
|
522 | |||
526 | %gui wx # enable wxPython event loop integration |
|
523 | %gui wx # enable wxPython event loop integration | |
527 | %gui qt4|qt # enable PyQt4 event loop integration |
|
524 | %gui qt4|qt # enable PyQt4 event loop integration | |
528 | %gui qt5 # enable PyQt5 event loop integration |
|
525 | %gui qt5 # enable PyQt5 event loop integration | |
529 | %gui gtk # enable PyGTK event loop integration |
|
526 | %gui gtk # enable PyGTK event loop integration | |
530 | %gui gtk3 # enable Gtk3 event loop integration |
|
527 | %gui gtk3 # enable Gtk3 event loop integration | |
531 | %gui tk # enable Tk event loop integration |
|
528 | %gui tk # enable Tk event loop integration | |
532 | %gui osx # enable Cocoa event loop integration |
|
529 | %gui osx # enable Cocoa event loop integration | |
533 | # (requires %matplotlib 1.1) |
|
530 | # (requires %matplotlib 1.1) | |
534 | %gui # disable all event loop integration |
|
531 | %gui # disable all event loop integration | |
535 |
|
532 | |||
536 | WARNING: after any of these has been called you can simply create |
|
533 | WARNING: after any of these has been called you can simply create | |
537 | an application object, but DO NOT start the event loop yourself, as |
|
534 | an application object, but DO NOT start the event loop yourself, as | |
538 | we have already handled that. |
|
535 | we have already handled that. | |
539 | """ |
|
536 | """ | |
540 | opts, arg = self.parse_options(parameter_s, '') |
|
537 | opts, arg = self.parse_options(parameter_s, '') | |
541 | if arg=='': arg = None |
|
538 | if arg=='': arg = None | |
542 | try: |
|
539 | try: | |
543 | return self.shell.enable_gui(arg) |
|
540 | return self.shell.enable_gui(arg) | |
544 | except Exception as e: |
|
541 | except Exception as e: | |
545 | # print simple error message, rather than traceback if we can't |
|
542 | # print simple error message, rather than traceback if we can't | |
546 | # hook up the GUI |
|
543 | # hook up the GUI | |
547 | error(str(e)) |
|
544 | error(str(e)) | |
548 |
|
545 | |||
549 | @skip_doctest |
|
546 | @skip_doctest | |
550 | @line_magic |
|
547 | @line_magic | |
551 | def precision(self, s=''): |
|
548 | def precision(self, s=''): | |
552 | """Set floating point precision for pretty printing. |
|
549 | """Set floating point precision for pretty printing. | |
553 |
|
550 | |||
554 | Can set either integer precision or a format string. |
|
551 | Can set either integer precision or a format string. | |
555 |
|
552 | |||
556 | If numpy has been imported and precision is an int, |
|
553 | If numpy has been imported and precision is an int, | |
557 | numpy display precision will also be set, via ``numpy.set_printoptions``. |
|
554 | numpy display precision will also be set, via ``numpy.set_printoptions``. | |
558 |
|
555 | |||
559 | If no argument is given, defaults will be restored. |
|
556 | If no argument is given, defaults will be restored. | |
560 |
|
557 | |||
561 | Examples |
|
558 | Examples | |
562 | -------- |
|
559 | -------- | |
563 | :: |
|
560 | :: | |
564 |
|
561 | |||
565 | In [1]: from math import pi |
|
562 | In [1]: from math import pi | |
566 |
|
563 | |||
567 | In [2]: %precision 3 |
|
564 | In [2]: %precision 3 | |
568 | Out[2]: u'%.3f' |
|
565 | Out[2]: u'%.3f' | |
569 |
|
566 | |||
570 | In [3]: pi |
|
567 | In [3]: pi | |
571 | Out[3]: 3.142 |
|
568 | Out[3]: 3.142 | |
572 |
|
569 | |||
573 | In [4]: %precision %i |
|
570 | In [4]: %precision %i | |
574 | Out[4]: u'%i' |
|
571 | Out[4]: u'%i' | |
575 |
|
572 | |||
576 | In [5]: pi |
|
573 | In [5]: pi | |
577 | Out[5]: 3 |
|
574 | Out[5]: 3 | |
578 |
|
575 | |||
579 | In [6]: %precision %e |
|
576 | In [6]: %precision %e | |
580 | Out[6]: u'%e' |
|
577 | Out[6]: u'%e' | |
581 |
|
578 | |||
582 | In [7]: pi**10 |
|
579 | In [7]: pi**10 | |
583 | Out[7]: 9.364805e+04 |
|
580 | Out[7]: 9.364805e+04 | |
584 |
|
581 | |||
585 | In [8]: %precision |
|
582 | In [8]: %precision | |
586 | Out[8]: u'%r' |
|
583 | Out[8]: u'%r' | |
587 |
|
584 | |||
588 | In [9]: pi**10 |
|
585 | In [9]: pi**10 | |
589 | Out[9]: 93648.047476082982 |
|
586 | Out[9]: 93648.047476082982 | |
590 | """ |
|
587 | """ | |
591 | ptformatter = self.shell.display_formatter.formatters['text/plain'] |
|
588 | ptformatter = self.shell.display_formatter.formatters['text/plain'] | |
592 | ptformatter.float_precision = s |
|
589 | ptformatter.float_precision = s | |
593 | return ptformatter.float_format |
|
590 | return ptformatter.float_format | |
594 |
|
591 | |||
595 | @magic_arguments.magic_arguments() |
|
592 | @magic_arguments.magic_arguments() | |
596 | @magic_arguments.argument( |
|
593 | @magic_arguments.argument( | |
597 | '-e', '--export', action='store_true', default=False, |
|
594 | '-e', '--export', action='store_true', default=False, | |
598 | help='Export IPython history as a notebook. The filename argument ' |
|
595 | help='Export IPython history as a notebook. The filename argument ' | |
599 | 'is used to specify the notebook name and format. For example ' |
|
596 | 'is used to specify the notebook name and format. For example ' | |
600 | 'a filename of notebook.ipynb will result in a notebook name ' |
|
597 | 'a filename of notebook.ipynb will result in a notebook name ' | |
601 | 'of "notebook" and a format of "json". Likewise using a ".py" ' |
|
598 | 'of "notebook" and a format of "json". Likewise using a ".py" ' | |
602 | 'file extension will write the notebook as a Python script' |
|
599 | 'file extension will write the notebook as a Python script' | |
603 | ) |
|
600 | ) | |
604 | @magic_arguments.argument( |
|
601 | @magic_arguments.argument( | |
605 | '-f', '--format', |
|
602 | '-f', '--format', | |
606 | help='Convert an existing IPython notebook to a new format. This option ' |
|
603 | help='Convert an existing IPython notebook to a new format. This option ' | |
607 | 'specifies the new format and can have the values: json, py. ' |
|
604 | 'specifies the new format and can have the values: json, py. ' | |
608 | 'The target filename is chosen automatically based on the new ' |
|
605 | 'The target filename is chosen automatically based on the new ' | |
609 | 'format. The filename argument gives the name of the source file.' |
|
606 | 'format. The filename argument gives the name of the source file.' | |
610 | ) |
|
607 | ) | |
611 | @magic_arguments.argument( |
|
608 | @magic_arguments.argument( | |
612 | 'filename', type=unicode_type, |
|
609 | 'filename', type=unicode_type, | |
613 | help='Notebook name or filename' |
|
610 | help='Notebook name or filename' | |
614 | ) |
|
611 | ) | |
615 | @line_magic |
|
612 | @line_magic | |
616 | def notebook(self, s): |
|
613 | def notebook(self, s): | |
617 | """Export and convert IPython notebooks. |
|
614 | """Export and convert IPython notebooks. | |
618 |
|
615 | |||
619 | This function can export the current IPython history to a notebook file |
|
616 | This function can export the current IPython history to a notebook file | |
620 | or can convert an existing notebook file into a different format. For |
|
617 | or can convert an existing notebook file into a different format. For | |
621 | example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". |
|
618 | example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". | |
622 | To export the history to "foo.py" do "%notebook -e foo.py". To convert |
|
619 | To export the history to "foo.py" do "%notebook -e foo.py". To convert | |
623 | "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible |
|
620 | "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible | |
624 | formats include (json/ipynb, py). |
|
621 | formats include (json/ipynb, py). | |
625 | """ |
|
622 | """ | |
626 | args = magic_arguments.parse_argstring(self.notebook, s) |
|
623 | args = magic_arguments.parse_argstring(self.notebook, s) | |
627 |
|
624 | |||
628 | from IPython.nbformat import current |
|
625 | from IPython.nbformat import current | |
629 | args.filename = unquote_filename(args.filename) |
|
626 | args.filename = unquote_filename(args.filename) | |
630 | if args.export: |
|
627 | if args.export: | |
631 | fname, name, format = current.parse_filename(args.filename) |
|
628 | fname, name, format = current.parse_filename(args.filename) | |
632 | cells = [] |
|
629 | cells = [] | |
633 | hist = list(self.shell.history_manager.get_range()) |
|
630 | hist = list(self.shell.history_manager.get_range()) | |
634 | for session, prompt_number, input in hist[:-1]: |
|
631 | for session, prompt_number, input in hist[:-1]: | |
635 | cells.append(current.new_code_cell(prompt_number=prompt_number, |
|
632 | cells.append(current.new_code_cell(prompt_number=prompt_number, | |
636 | input=input)) |
|
633 | input=input)) | |
637 | worksheet = current.new_worksheet(cells=cells) |
|
634 | worksheet = current.new_worksheet(cells=cells) | |
638 | nb = current.new_notebook(name=name,worksheets=[worksheet]) |
|
635 | nb = current.new_notebook(name=name,worksheets=[worksheet]) | |
639 | with io.open(fname, 'w', encoding='utf-8') as f: |
|
636 | with io.open(fname, 'w', encoding='utf-8') as f: | |
640 | current.write(nb, f, format); |
|
637 | current.write(nb, f, format); | |
641 | elif args.format is not None: |
|
638 | elif args.format is not None: | |
642 | old_fname, old_name, old_format = current.parse_filename(args.filename) |
|
639 | old_fname, old_name, old_format = current.parse_filename(args.filename) | |
643 | new_format = args.format |
|
640 | new_format = args.format | |
644 | if new_format == u'xml': |
|
641 | if new_format == u'xml': | |
645 | raise ValueError('Notebooks cannot be written as xml.') |
|
642 | raise ValueError('Notebooks cannot be written as xml.') | |
646 | elif new_format == u'ipynb' or new_format == u'json': |
|
643 | elif new_format == u'ipynb' or new_format == u'json': | |
647 | new_fname = old_name + u'.ipynb' |
|
644 | new_fname = old_name + u'.ipynb' | |
648 | new_format = u'json' |
|
645 | new_format = u'json' | |
649 | elif new_format == u'py': |
|
646 | elif new_format == u'py': | |
650 | new_fname = old_name + u'.py' |
|
647 | new_fname = old_name + u'.py' | |
651 | else: |
|
648 | else: | |
652 | raise ValueError('Invalid notebook format: %s' % new_format) |
|
649 | raise ValueError('Invalid notebook format: %s' % new_format) | |
653 | with io.open(old_fname, 'r', encoding='utf-8') as f: |
|
650 | with io.open(old_fname, 'r', encoding='utf-8') as f: | |
654 | nb = current.read(f, old_format) |
|
651 | nb = current.read(f, old_format) | |
655 | with io.open(new_fname, 'w', encoding='utf-8') as f: |
|
652 | with io.open(new_fname, 'w', encoding='utf-8') as f: | |
656 | current.write(nb, f, new_format) |
|
653 | current.write(nb, f, new_format) |
@@ -1,170 +1,167 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for getting information about IPython and the system it's running in. |
|
3 | Utilities for getting information about IPython and the system it's running in. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | import os |
|
17 | import os | |
18 | import platform |
|
18 | import platform | |
19 | import pprint |
|
19 | import pprint | |
20 | import sys |
|
20 | import sys | |
21 | import subprocess |
|
21 | import subprocess | |
22 |
|
22 | |||
23 | from IPython.core import release |
|
23 | from IPython.core import release | |
24 | from IPython.utils import py3compat, _sysinfo, encoding |
|
24 | from IPython.utils import py3compat, _sysinfo, encoding | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Code |
|
27 | # Code | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | def pkg_commit_hash(pkg_path): |
|
30 | def pkg_commit_hash(pkg_path): | |
31 | """Get short form of commit hash given directory `pkg_path` |
|
31 | """Get short form of commit hash given directory `pkg_path` | |
32 |
|
32 | |||
33 | We get the commit hash from (in order of preference): |
|
33 | We get the commit hash from (in order of preference): | |
34 |
|
34 | |||
35 | * IPython.utils._sysinfo.commit |
|
35 | * IPython.utils._sysinfo.commit | |
36 | * git output, if we are in a git repository |
|
36 | * git output, if we are in a git repository | |
37 |
|
37 | |||
38 | If these fail, we return a not-found placeholder tuple |
|
38 | If these fail, we return a not-found placeholder tuple | |
39 |
|
39 | |||
40 | Parameters |
|
40 | Parameters | |
41 | ---------- |
|
41 | ---------- | |
42 | pkg_path : str |
|
42 | pkg_path : str | |
43 | directory containing package |
|
43 | directory containing package | |
44 | only used for getting commit from active repo |
|
44 | only used for getting commit from active repo | |
45 |
|
45 | |||
46 | Returns |
|
46 | Returns | |
47 | ------- |
|
47 | ------- | |
48 | hash_from : str |
|
48 | hash_from : str | |
49 | Where we got the hash from - description |
|
49 | Where we got the hash from - description | |
50 | hash_str : str |
|
50 | hash_str : str | |
51 | short form of hash |
|
51 | short form of hash | |
52 | """ |
|
52 | """ | |
53 | # Try and get commit from written commit text file |
|
53 | # Try and get commit from written commit text file | |
54 | if _sysinfo.commit: |
|
54 | if _sysinfo.commit: | |
55 | return "installation", _sysinfo.commit |
|
55 | return "installation", _sysinfo.commit | |
56 |
|
56 | |||
57 | # maybe we are in a repository |
|
57 | # maybe we are in a repository | |
58 | proc = subprocess.Popen('git rev-parse --short HEAD', |
|
58 | proc = subprocess.Popen('git rev-parse --short HEAD', | |
59 | stdout=subprocess.PIPE, |
|
59 | stdout=subprocess.PIPE, | |
60 | stderr=subprocess.PIPE, |
|
60 | stderr=subprocess.PIPE, | |
61 | cwd=pkg_path, shell=True) |
|
61 | cwd=pkg_path, shell=True) | |
62 | repo_commit, _ = proc.communicate() |
|
62 | repo_commit, _ = proc.communicate() | |
63 | if repo_commit: |
|
63 | if repo_commit: | |
64 | return 'repository', repo_commit.strip() |
|
64 | return 'repository', repo_commit.strip().decode('ascii') | |
65 | return '(none found)', '<not found>' |
|
65 | return '(none found)', u'<not found>' | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | def pkg_info(pkg_path): |
|
68 | def pkg_info(pkg_path): | |
69 | """Return dict describing the context of this package |
|
69 | """Return dict describing the context of this package | |
70 |
|
70 | |||
71 | Parameters |
|
71 | Parameters | |
72 | ---------- |
|
72 | ---------- | |
73 | pkg_path : str |
|
73 | pkg_path : str | |
74 | path containing __init__.py for package |
|
74 | path containing __init__.py for package | |
75 |
|
75 | |||
76 | Returns |
|
76 | Returns | |
77 | ------- |
|
77 | ------- | |
78 | context : dict |
|
78 | context : dict | |
79 | with named parameters of interest |
|
79 | with named parameters of interest | |
80 | """ |
|
80 | """ | |
81 | src, hsh = pkg_commit_hash(pkg_path) |
|
81 | src, hsh = pkg_commit_hash(pkg_path) | |
82 | return dict( |
|
82 | return dict( | |
83 | ipython_version=release.version, |
|
83 | ipython_version=release.version, | |
84 | ipython_path=pkg_path, |
|
84 | ipython_path=pkg_path, | |
85 | commit_source=src, |
|
85 | commit_source=src, | |
86 | commit_hash=hsh, |
|
86 | commit_hash=hsh, | |
87 | sys_version=sys.version, |
|
87 | sys_version=sys.version, | |
88 | sys_executable=sys.executable, |
|
88 | sys_executable=sys.executable, | |
89 | sys_platform=sys.platform, |
|
89 | sys_platform=sys.platform, | |
90 | platform=platform.platform(), |
|
90 | platform=platform.platform(), | |
91 | os_name=os.name, |
|
91 | os_name=os.name, | |
92 | default_encoding=encoding.DEFAULT_ENCODING, |
|
92 | default_encoding=encoding.DEFAULT_ENCODING, | |
93 | ) |
|
93 | ) | |
94 |
|
94 | |||
95 | def get_sys_info(): |
|
95 | def get_sys_info(): | |
96 | """Return useful information about IPython and the system, as a dict.""" |
|
96 | """Return useful information about IPython and the system, as a dict.""" | |
97 | p = os.path |
|
97 | p = os.path | |
98 | path = p.realpath(p.dirname(p.abspath(p.join(__file__, '..')))) |
|
98 | path = p.realpath(p.dirname(p.abspath(p.join(__file__, '..')))) | |
99 | return pkg_info(path) |
|
99 | return pkg_info(path) | |
100 |
|
100 | |||
101 | @py3compat.doctest_refactor_print |
|
101 | @py3compat.doctest_refactor_print | |
102 | def sys_info(): |
|
102 | def sys_info(): | |
103 | """Return useful information about IPython and the system, as a string. |
|
103 | """Return useful information about IPython and the system, as a string. | |
104 |
|
104 | |||
105 | Examples |
|
105 | Examples | |
106 | -------- |
|
106 | -------- | |
107 | :: |
|
107 | :: | |
108 |
|
108 | |||
109 | In [2]: print sys_info() |
|
109 | In [2]: print sys_info() | |
110 | {'commit_hash': '144fdae', # random |
|
110 | {'commit_hash': '144fdae', # random | |
111 | 'commit_source': 'repository', |
|
111 | 'commit_source': 'repository', | |
112 | 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython', |
|
112 | 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython', | |
113 | 'ipython_version': '0.11.dev', |
|
113 | 'ipython_version': '0.11.dev', | |
114 | 'os_name': 'posix', |
|
114 | 'os_name': 'posix', | |
115 | 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick', |
|
115 | 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick', | |
116 | 'sys_executable': '/usr/bin/python', |
|
116 | 'sys_executable': '/usr/bin/python', | |
117 | 'sys_platform': 'linux2', |
|
117 | 'sys_platform': 'linux2', | |
118 | 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'} |
|
118 | 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \\n[GCC 4.4.5]'} | |
119 | """ |
|
119 | """ | |
120 | return pprint.pformat(get_sys_info()) |
|
120 | return pprint.pformat(get_sys_info()) | |
121 |
|
121 | |||
122 | def _num_cpus_unix(): |
|
122 | def _num_cpus_unix(): | |
123 | """Return the number of active CPUs on a Unix system.""" |
|
123 | """Return the number of active CPUs on a Unix system.""" | |
124 | return os.sysconf("SC_NPROCESSORS_ONLN") |
|
124 | return os.sysconf("SC_NPROCESSORS_ONLN") | |
125 |
|
125 | |||
126 |
|
126 | |||
127 | def _num_cpus_darwin(): |
|
127 | def _num_cpus_darwin(): | |
128 | """Return the number of active CPUs on a Darwin system.""" |
|
128 | """Return the number of active CPUs on a Darwin system.""" | |
129 | p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE) |
|
129 | p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE) | |
130 | return p.stdout.read() |
|
130 | return p.stdout.read() | |
131 |
|
131 | |||
132 |
|
132 | |||
133 | def _num_cpus_windows(): |
|
133 | def _num_cpus_windows(): | |
134 | """Return the number of active CPUs on a Windows system.""" |
|
134 | """Return the number of active CPUs on a Windows system.""" | |
135 | return os.environ.get("NUMBER_OF_PROCESSORS") |
|
135 | return os.environ.get("NUMBER_OF_PROCESSORS") | |
136 |
|
136 | |||
137 |
|
137 | |||
138 | def num_cpus(): |
|
138 | def num_cpus(): | |
139 | """Return the effective number of CPUs in the system as an integer. |
|
139 | """Return the effective number of CPUs in the system as an integer. | |
140 |
|
140 | |||
141 | This cross-platform function makes an attempt at finding the total number of |
|
141 | This cross-platform function makes an attempt at finding the total number of | |
142 | available CPUs in the system, as returned by various underlying system and |
|
142 | available CPUs in the system, as returned by various underlying system and | |
143 | python calls. |
|
143 | python calls. | |
144 |
|
144 | |||
145 | If it can't find a sensible answer, it returns 1 (though an error *may* make |
|
145 | If it can't find a sensible answer, it returns 1 (though an error *may* make | |
146 | it return a large positive number that's actually incorrect). |
|
146 | it return a large positive number that's actually incorrect). | |
147 | """ |
|
147 | """ | |
148 |
|
148 | |||
149 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) |
|
149 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) | |
150 | # for the names of the keys we needed to look up for this function. This |
|
150 | # for the names of the keys we needed to look up for this function. This | |
151 | # code was inspired by their equivalent function. |
|
151 | # code was inspired by their equivalent function. | |
152 |
|
152 | |||
153 | ncpufuncs = {'Linux':_num_cpus_unix, |
|
153 | ncpufuncs = {'Linux':_num_cpus_unix, | |
154 | 'Darwin':_num_cpus_darwin, |
|
154 | 'Darwin':_num_cpus_darwin, | |
155 |
'Windows':_num_cpus_windows |
|
155 | 'Windows':_num_cpus_windows | |
156 | # On Vista, python < 2.5.2 has a bug and returns 'Microsoft' |
|
|||
157 | # See http://bugs.python.org/issue1082 for details. |
|
|||
158 | 'Microsoft':_num_cpus_windows, |
|
|||
159 | } |
|
156 | } | |
160 |
|
157 | |||
161 | ncpufunc = ncpufuncs.get(platform.system(), |
|
158 | ncpufunc = ncpufuncs.get(platform.system(), | |
162 | # default to unix version (Solaris, AIX, etc) |
|
159 | # default to unix version (Solaris, AIX, etc) | |
163 | _num_cpus_unix) |
|
160 | _num_cpus_unix) | |
164 |
|
161 | |||
165 | try: |
|
162 | try: | |
166 | ncpus = max(1,int(ncpufunc())) |
|
163 | ncpus = max(1,int(ncpufunc())) | |
167 | except: |
|
164 | except: | |
168 | ncpus = 1 |
|
165 | ncpus = 1 | |
169 | return ncpus |
|
166 | return ncpus | |
170 |
|
167 |
@@ -1,730 +1,730 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | This module defines the things that are used in setup.py for building IPython |
|
3 | This module defines the things that are used in setup.py for building IPython | |
4 |
|
4 | |||
5 | This includes: |
|
5 | This includes: | |
6 |
|
6 | |||
7 | * The basic arguments to setup |
|
7 | * The basic arguments to setup | |
8 | * Functions for finding things like packages, package data, etc. |
|
8 | * Functions for finding things like packages, package data, etc. | |
9 | * A function for checking dependencies. |
|
9 | * A function for checking dependencies. | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | # Copyright (c) IPython Development Team. |
|
12 | # Copyright (c) IPython Development Team. | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 |
|
14 | |||
15 | from __future__ import print_function |
|
15 | from __future__ import print_function | |
16 |
|
16 | |||
17 | import errno |
|
17 | import errno | |
18 | import os |
|
18 | import os | |
19 | import sys |
|
19 | import sys | |
20 |
|
20 | |||
21 | from distutils import log |
|
21 | from distutils import log | |
22 | from distutils.command.build_py import build_py |
|
22 | from distutils.command.build_py import build_py | |
23 | from distutils.command.build_scripts import build_scripts |
|
23 | from distutils.command.build_scripts import build_scripts | |
24 | from distutils.command.install import install |
|
24 | from distutils.command.install import install | |
25 | from distutils.command.install_scripts import install_scripts |
|
25 | from distutils.command.install_scripts import install_scripts | |
26 | from distutils.cmd import Command |
|
26 | from distutils.cmd import Command | |
27 | from fnmatch import fnmatch |
|
27 | from fnmatch import fnmatch | |
28 | from glob import glob |
|
28 | from glob import glob | |
29 | from subprocess import check_call |
|
29 | from subprocess import check_call | |
30 |
|
30 | |||
31 | from setupext import install_data_ext |
|
31 | from setupext import install_data_ext | |
32 |
|
32 | |||
33 | #------------------------------------------------------------------------------- |
|
33 | #------------------------------------------------------------------------------- | |
34 | # Useful globals and utility functions |
|
34 | # Useful globals and utility functions | |
35 | #------------------------------------------------------------------------------- |
|
35 | #------------------------------------------------------------------------------- | |
36 |
|
36 | |||
37 | # A few handy globals |
|
37 | # A few handy globals | |
38 | isfile = os.path.isfile |
|
38 | isfile = os.path.isfile | |
39 | pjoin = os.path.join |
|
39 | pjoin = os.path.join | |
40 | repo_root = os.path.dirname(os.path.abspath(__file__)) |
|
40 | repo_root = os.path.dirname(os.path.abspath(__file__)) | |
41 |
|
41 | |||
42 | def oscmd(s): |
|
42 | def oscmd(s): | |
43 | print(">", s) |
|
43 | print(">", s) | |
44 | os.system(s) |
|
44 | os.system(s) | |
45 |
|
45 | |||
46 | # Py3 compatibility hacks, without assuming IPython itself is installed with |
|
46 | # Py3 compatibility hacks, without assuming IPython itself is installed with | |
47 | # the full py3compat machinery. |
|
47 | # the full py3compat machinery. | |
48 |
|
48 | |||
49 | try: |
|
49 | try: | |
50 | execfile |
|
50 | execfile | |
51 | except NameError: |
|
51 | except NameError: | |
52 | def execfile(fname, globs, locs=None): |
|
52 | def execfile(fname, globs, locs=None): | |
53 | locs = locs or globs |
|
53 | locs = locs or globs | |
54 | exec(compile(open(fname).read(), fname, "exec"), globs, locs) |
|
54 | exec(compile(open(fname).read(), fname, "exec"), globs, locs) | |
55 |
|
55 | |||
56 | # A little utility we'll need below, since glob() does NOT allow you to do |
|
56 | # A little utility we'll need below, since glob() does NOT allow you to do | |
57 | # exclusion on multiple endings! |
|
57 | # exclusion on multiple endings! | |
58 | def file_doesnt_endwith(test,endings): |
|
58 | def file_doesnt_endwith(test,endings): | |
59 | """Return true if test is a file and its name does NOT end with any |
|
59 | """Return true if test is a file and its name does NOT end with any | |
60 | of the strings listed in endings.""" |
|
60 | of the strings listed in endings.""" | |
61 | if not isfile(test): |
|
61 | if not isfile(test): | |
62 | return False |
|
62 | return False | |
63 | for e in endings: |
|
63 | for e in endings: | |
64 | if test.endswith(e): |
|
64 | if test.endswith(e): | |
65 | return False |
|
65 | return False | |
66 | return True |
|
66 | return True | |
67 |
|
67 | |||
68 | #--------------------------------------------------------------------------- |
|
68 | #--------------------------------------------------------------------------- | |
69 | # Basic project information |
|
69 | # Basic project information | |
70 | #--------------------------------------------------------------------------- |
|
70 | #--------------------------------------------------------------------------- | |
71 |
|
71 | |||
72 | # release.py contains version, authors, license, url, keywords, etc. |
|
72 | # release.py contains version, authors, license, url, keywords, etc. | |
73 | execfile(pjoin(repo_root, 'IPython','core','release.py'), globals()) |
|
73 | execfile(pjoin(repo_root, 'IPython','core','release.py'), globals()) | |
74 |
|
74 | |||
75 | # Create a dict with the basic information |
|
75 | # Create a dict with the basic information | |
76 | # This dict is eventually passed to setup after additional keys are added. |
|
76 | # This dict is eventually passed to setup after additional keys are added. | |
77 | setup_args = dict( |
|
77 | setup_args = dict( | |
78 | name = name, |
|
78 | name = name, | |
79 | version = version, |
|
79 | version = version, | |
80 | description = description, |
|
80 | description = description, | |
81 | long_description = long_description, |
|
81 | long_description = long_description, | |
82 | author = author, |
|
82 | author = author, | |
83 | author_email = author_email, |
|
83 | author_email = author_email, | |
84 | url = url, |
|
84 | url = url, | |
85 | download_url = download_url, |
|
85 | download_url = download_url, | |
86 | license = license, |
|
86 | license = license, | |
87 | platforms = platforms, |
|
87 | platforms = platforms, | |
88 | keywords = keywords, |
|
88 | keywords = keywords, | |
89 | classifiers = classifiers, |
|
89 | classifiers = classifiers, | |
90 | cmdclass = {'install_data': install_data_ext}, |
|
90 | cmdclass = {'install_data': install_data_ext}, | |
91 | ) |
|
91 | ) | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | #--------------------------------------------------------------------------- |
|
94 | #--------------------------------------------------------------------------- | |
95 | # Find packages |
|
95 | # Find packages | |
96 | #--------------------------------------------------------------------------- |
|
96 | #--------------------------------------------------------------------------- | |
97 |
|
97 | |||
98 | def find_packages(): |
|
98 | def find_packages(): | |
99 | """ |
|
99 | """ | |
100 | Find all of IPython's packages. |
|
100 | Find all of IPython's packages. | |
101 | """ |
|
101 | """ | |
102 | excludes = ['deathrow', 'quarantine'] |
|
102 | excludes = ['deathrow', 'quarantine'] | |
103 | packages = [] |
|
103 | packages = [] | |
104 | for dir,subdirs,files in os.walk('IPython'): |
|
104 | for dir,subdirs,files in os.walk('IPython'): | |
105 | package = dir.replace(os.path.sep, '.') |
|
105 | package = dir.replace(os.path.sep, '.') | |
106 | if any(package.startswith('IPython.'+exc) for exc in excludes): |
|
106 | if any(package.startswith('IPython.'+exc) for exc in excludes): | |
107 | # package is to be excluded (e.g. deathrow) |
|
107 | # package is to be excluded (e.g. deathrow) | |
108 | continue |
|
108 | continue | |
109 | if '__init__.py' not in files: |
|
109 | if '__init__.py' not in files: | |
110 | # not a package |
|
110 | # not a package | |
111 | continue |
|
111 | continue | |
112 | packages.append(package) |
|
112 | packages.append(package) | |
113 | return packages |
|
113 | return packages | |
114 |
|
114 | |||
115 | #--------------------------------------------------------------------------- |
|
115 | #--------------------------------------------------------------------------- | |
116 | # Find package data |
|
116 | # Find package data | |
117 | #--------------------------------------------------------------------------- |
|
117 | #--------------------------------------------------------------------------- | |
118 |
|
118 | |||
119 | def find_package_data(): |
|
119 | def find_package_data(): | |
120 | """ |
|
120 | """ | |
121 | Find IPython's package_data. |
|
121 | Find IPython's package_data. | |
122 | """ |
|
122 | """ | |
123 | # This is not enough for these things to appear in an sdist. |
|
123 | # This is not enough for these things to appear in an sdist. | |
124 | # We need to muck with the MANIFEST to get this to work |
|
124 | # We need to muck with the MANIFEST to get this to work | |
125 |
|
125 | |||
126 | # exclude components and less from the walk; |
|
126 | # exclude components and less from the walk; | |
127 | # we will build the components separately |
|
127 | # we will build the components separately | |
128 | excludes = [ |
|
128 | excludes = [ | |
129 | pjoin('static', 'components'), |
|
129 | pjoin('static', 'components'), | |
130 | pjoin('static', '*', 'less'), |
|
130 | pjoin('static', '*', 'less'), | |
131 | ] |
|
131 | ] | |
132 |
|
132 | |||
133 | # walk notebook resources: |
|
133 | # walk notebook resources: | |
134 | cwd = os.getcwd() |
|
134 | cwd = os.getcwd() | |
135 | os.chdir(os.path.join('IPython', 'html')) |
|
135 | os.chdir(os.path.join('IPython', 'html')) | |
136 | static_data = [] |
|
136 | static_data = [] | |
137 | for parent, dirs, files in os.walk('static'): |
|
137 | for parent, dirs, files in os.walk('static'): | |
138 | if any(fnmatch(parent, pat) for pat in excludes): |
|
138 | if any(fnmatch(parent, pat) for pat in excludes): | |
139 | # prevent descending into subdirs |
|
139 | # prevent descending into subdirs | |
140 | dirs[:] = [] |
|
140 | dirs[:] = [] | |
141 | continue |
|
141 | continue | |
142 | for f in files: |
|
142 | for f in files: | |
143 | static_data.append(pjoin(parent, f)) |
|
143 | static_data.append(pjoin(parent, f)) | |
144 |
|
144 | |||
145 | components = pjoin("static", "components") |
|
145 | components = pjoin("static", "components") | |
146 | # select the components we actually need to install |
|
146 | # select the components we actually need to install | |
147 | # (there are lots of resources we bundle for sdist-reasons that we don't actually use) |
|
147 | # (there are lots of resources we bundle for sdist-reasons that we don't actually use) | |
148 | static_data.extend([ |
|
148 | static_data.extend([ | |
149 | pjoin(components, "backbone", "backbone-min.js"), |
|
149 | pjoin(components, "backbone", "backbone-min.js"), | |
150 | pjoin(components, "bootstrap", "js", "bootstrap.min.js"), |
|
150 | pjoin(components, "bootstrap", "js", "bootstrap.min.js"), | |
151 | pjoin(components, "bootstrap-tour", "build", "css", "bootstrap-tour.min.css"), |
|
151 | pjoin(components, "bootstrap-tour", "build", "css", "bootstrap-tour.min.css"), | |
152 | pjoin(components, "bootstrap-tour", "build", "js", "bootstrap-tour.min.js"), |
|
152 | pjoin(components, "bootstrap-tour", "build", "js", "bootstrap-tour.min.js"), | |
153 | pjoin(components, "font-awesome", "fonts", "*.*"), |
|
153 | pjoin(components, "font-awesome", "fonts", "*.*"), | |
154 | pjoin(components, "google-caja", "html-css-sanitizer-minified.js"), |
|
154 | pjoin(components, "google-caja", "html-css-sanitizer-minified.js"), | |
155 | pjoin(components, "highlight.js", "build", "highlight.pack.js"), |
|
155 | pjoin(components, "highlight.js", "build", "highlight.pack.js"), | |
156 | pjoin(components, "jquery", "jquery.min.js"), |
|
156 | pjoin(components, "jquery", "jquery.min.js"), | |
157 | pjoin(components, "jquery-ui", "ui", "minified", "jquery-ui.min.js"), |
|
157 | pjoin(components, "jquery-ui", "ui", "minified", "jquery-ui.min.js"), | |
158 | pjoin(components, "jquery-ui", "themes", "smoothness", "jquery-ui.min.css"), |
|
158 | pjoin(components, "jquery-ui", "themes", "smoothness", "jquery-ui.min.css"), | |
159 | pjoin(components, "jquery-ui", "themes", "smoothness", "images", "*"), |
|
159 | pjoin(components, "jquery-ui", "themes", "smoothness", "images", "*"), | |
160 | pjoin(components, "marked", "lib", "marked.js"), |
|
160 | pjoin(components, "marked", "lib", "marked.js"), | |
161 | pjoin(components, "requirejs", "require.js"), |
|
161 | pjoin(components, "requirejs", "require.js"), | |
162 | pjoin(components, "underscore", "underscore-min.js"), |
|
162 | pjoin(components, "underscore", "underscore-min.js"), | |
163 | pjoin(components, "moment", "moment.js"), |
|
163 | pjoin(components, "moment", "moment.js"), | |
164 | pjoin(components, "moment", "min","moment.min.js"), |
|
164 | pjoin(components, "moment", "min","moment.min.js"), | |
165 | ]) |
|
165 | ]) | |
166 |
|
166 | |||
167 | # Ship all of Codemirror's CSS and JS |
|
167 | # Ship all of Codemirror's CSS and JS | |
168 | for parent, dirs, files in os.walk(pjoin(components, 'codemirror')): |
|
168 | for parent, dirs, files in os.walk(pjoin(components, 'codemirror')): | |
169 | for f in files: |
|
169 | for f in files: | |
170 | if f.endswith(('.js', '.css')): |
|
170 | if f.endswith(('.js', '.css')): | |
171 | static_data.append(pjoin(parent, f)) |
|
171 | static_data.append(pjoin(parent, f)) | |
172 |
|
172 | |||
173 | os.chdir(os.path.join('tests',)) |
|
173 | os.chdir(os.path.join('tests',)) | |
174 | js_tests = glob('*.js') + glob('*/*.js') |
|
174 | js_tests = glob('*.js') + glob('*/*.js') | |
175 |
|
175 | |||
176 | os.chdir(os.path.join(cwd, 'IPython', 'nbconvert')) |
|
176 | os.chdir(os.path.join(cwd, 'IPython', 'nbconvert')) | |
177 | nbconvert_templates = [os.path.join(dirpath, '*.*') |
|
177 | nbconvert_templates = [os.path.join(dirpath, '*.*') | |
178 | for dirpath, _, _ in os.walk('templates')] |
|
178 | for dirpath, _, _ in os.walk('templates')] | |
179 |
|
179 | |||
180 | os.chdir(cwd) |
|
180 | os.chdir(cwd) | |
181 |
|
181 | |||
182 | package_data = { |
|
182 | package_data = { | |
183 | 'IPython.config.profile' : ['README*', '*/*.py'], |
|
183 | 'IPython.config.profile' : ['README*', '*/*.py'], | |
184 | 'IPython.core.tests' : ['*.png', '*.jpg'], |
|
184 | 'IPython.core.tests' : ['*.png', '*.jpg'], | |
185 | 'IPython.lib.tests' : ['*.wav'], |
|
185 | 'IPython.lib.tests' : ['*.wav'], | |
186 | 'IPython.testing.plugin' : ['*.txt'], |
|
186 | 'IPython.testing.plugin' : ['*.txt'], | |
187 | 'IPython.html' : ['templates/*'] + static_data, |
|
187 | 'IPython.html' : ['templates/*'] + static_data, | |
188 | 'IPython.html.tests' : js_tests, |
|
188 | 'IPython.html.tests' : js_tests, | |
189 | 'IPython.qt.console' : ['resources/icon/*.svg'], |
|
189 | 'IPython.qt.console' : ['resources/icon/*.svg'], | |
190 | 'IPython.nbconvert' : nbconvert_templates + |
|
190 | 'IPython.nbconvert' : nbconvert_templates + | |
191 | [ |
|
191 | [ | |
192 | 'tests/files/*.*', |
|
192 | 'tests/files/*.*', | |
193 | 'exporters/tests/files/*.*', |
|
193 | 'exporters/tests/files/*.*', | |
194 | 'preprocessors/tests/files/*.*', |
|
194 | 'preprocessors/tests/files/*.*', | |
195 | ], |
|
195 | ], | |
196 | 'IPython.nbconvert.filters' : ['marked.js'], |
|
196 | 'IPython.nbconvert.filters' : ['marked.js'], | |
197 | 'IPython.nbformat' : ['tests/*.ipynb','v3/v3.withref.json'] |
|
197 | 'IPython.nbformat' : ['tests/*.ipynb','v3/v3.withref.json'] | |
198 | } |
|
198 | } | |
199 |
|
199 | |||
200 | return package_data |
|
200 | return package_data | |
201 |
|
201 | |||
202 |
|
202 | |||
203 | def check_package_data(package_data): |
|
203 | def check_package_data(package_data): | |
204 | """verify that package_data globs make sense""" |
|
204 | """verify that package_data globs make sense""" | |
205 | print("checking package data") |
|
205 | print("checking package data") | |
206 | for pkg, data in package_data.items(): |
|
206 | for pkg, data in package_data.items(): | |
207 | pkg_root = pjoin(*pkg.split('.')) |
|
207 | pkg_root = pjoin(*pkg.split('.')) | |
208 | for d in data: |
|
208 | for d in data: | |
209 | path = pjoin(pkg_root, d) |
|
209 | path = pjoin(pkg_root, d) | |
210 | if '*' in path: |
|
210 | if '*' in path: | |
211 | assert len(glob(path)) > 0, "No files match pattern %s" % path |
|
211 | assert len(glob(path)) > 0, "No files match pattern %s" % path | |
212 | else: |
|
212 | else: | |
213 | assert os.path.exists(path), "Missing package data: %s" % path |
|
213 | assert os.path.exists(path), "Missing package data: %s" % path | |
214 |
|
214 | |||
215 |
|
215 | |||
216 | def check_package_data_first(command): |
|
216 | def check_package_data_first(command): | |
217 | """decorator for checking package_data before running a given command |
|
217 | """decorator for checking package_data before running a given command | |
218 |
|
218 | |||
219 | Probably only needs to wrap build_py |
|
219 | Probably only needs to wrap build_py | |
220 | """ |
|
220 | """ | |
221 | class DecoratedCommand(command): |
|
221 | class DecoratedCommand(command): | |
222 | def run(self): |
|
222 | def run(self): | |
223 | check_package_data(self.package_data) |
|
223 | check_package_data(self.package_data) | |
224 | command.run(self) |
|
224 | command.run(self) | |
225 | return DecoratedCommand |
|
225 | return DecoratedCommand | |
226 |
|
226 | |||
227 |
|
227 | |||
228 | #--------------------------------------------------------------------------- |
|
228 | #--------------------------------------------------------------------------- | |
229 | # Find data files |
|
229 | # Find data files | |
230 | #--------------------------------------------------------------------------- |
|
230 | #--------------------------------------------------------------------------- | |
231 |
|
231 | |||
232 | def make_dir_struct(tag,base,out_base): |
|
232 | def make_dir_struct(tag,base,out_base): | |
233 | """Make the directory structure of all files below a starting dir. |
|
233 | """Make the directory structure of all files below a starting dir. | |
234 |
|
234 | |||
235 | This is just a convenience routine to help build a nested directory |
|
235 | This is just a convenience routine to help build a nested directory | |
236 | hierarchy because distutils is too stupid to do this by itself. |
|
236 | hierarchy because distutils is too stupid to do this by itself. | |
237 |
|
237 | |||
238 | XXX - this needs a proper docstring! |
|
238 | XXX - this needs a proper docstring! | |
239 | """ |
|
239 | """ | |
240 |
|
240 | |||
241 | # we'll use these a lot below |
|
241 | # we'll use these a lot below | |
242 | lbase = len(base) |
|
242 | lbase = len(base) | |
243 | pathsep = os.path.sep |
|
243 | pathsep = os.path.sep | |
244 | lpathsep = len(pathsep) |
|
244 | lpathsep = len(pathsep) | |
245 |
|
245 | |||
246 | out = [] |
|
246 | out = [] | |
247 | for (dirpath,dirnames,filenames) in os.walk(base): |
|
247 | for (dirpath,dirnames,filenames) in os.walk(base): | |
248 | # we need to strip out the dirpath from the base to map it to the |
|
248 | # we need to strip out the dirpath from the base to map it to the | |
249 | # output (installation) path. This requires possibly stripping the |
|
249 | # output (installation) path. This requires possibly stripping the | |
250 | # path separator, because otherwise pjoin will not work correctly |
|
250 | # path separator, because otherwise pjoin will not work correctly | |
251 | # (pjoin('foo/','/bar') returns '/bar'). |
|
251 | # (pjoin('foo/','/bar') returns '/bar'). | |
252 |
|
252 | |||
253 | dp_eff = dirpath[lbase:] |
|
253 | dp_eff = dirpath[lbase:] | |
254 | if dp_eff.startswith(pathsep): |
|
254 | if dp_eff.startswith(pathsep): | |
255 | dp_eff = dp_eff[lpathsep:] |
|
255 | dp_eff = dp_eff[lpathsep:] | |
256 | # The output path must be anchored at the out_base marker |
|
256 | # The output path must be anchored at the out_base marker | |
257 | out_path = pjoin(out_base,dp_eff) |
|
257 | out_path = pjoin(out_base,dp_eff) | |
258 | # Now we can generate the final filenames. Since os.walk only produces |
|
258 | # Now we can generate the final filenames. Since os.walk only produces | |
259 | # filenames, we must join back with the dirpath to get full valid file |
|
259 | # filenames, we must join back with the dirpath to get full valid file | |
260 | # paths: |
|
260 | # paths: | |
261 | pfiles = [pjoin(dirpath,f) for f in filenames] |
|
261 | pfiles = [pjoin(dirpath,f) for f in filenames] | |
262 | # Finally, generate the entry we need, which is a pari of (output |
|
262 | # Finally, generate the entry we need, which is a pari of (output | |
263 | # path, files) for use as a data_files parameter in install_data. |
|
263 | # path, files) for use as a data_files parameter in install_data. | |
264 | out.append((out_path, pfiles)) |
|
264 | out.append((out_path, pfiles)) | |
265 |
|
265 | |||
266 | return out |
|
266 | return out | |
267 |
|
267 | |||
268 |
|
268 | |||
269 | def find_data_files(): |
|
269 | def find_data_files(): | |
270 | """ |
|
270 | """ | |
271 | Find IPython's data_files. |
|
271 | Find IPython's data_files. | |
272 |
|
272 | |||
273 | Just man pages at this point. |
|
273 | Just man pages at this point. | |
274 | """ |
|
274 | """ | |
275 |
|
275 | |||
276 | manpagebase = pjoin('share', 'man', 'man1') |
|
276 | manpagebase = pjoin('share', 'man', 'man1') | |
277 |
|
277 | |||
278 | # Simple file lists can be made by hand |
|
278 | # Simple file lists can be made by hand | |
279 | manpages = [f for f in glob(pjoin('docs','man','*.1.gz')) if isfile(f)] |
|
279 | manpages = [f for f in glob(pjoin('docs','man','*.1.gz')) if isfile(f)] | |
280 | if not manpages: |
|
280 | if not manpages: | |
281 | # When running from a source tree, the manpages aren't gzipped |
|
281 | # When running from a source tree, the manpages aren't gzipped | |
282 | manpages = [f for f in glob(pjoin('docs','man','*.1')) if isfile(f)] |
|
282 | manpages = [f for f in glob(pjoin('docs','man','*.1')) if isfile(f)] | |
283 |
|
283 | |||
284 | # And assemble the entire output list |
|
284 | # And assemble the entire output list | |
285 | data_files = [ (manpagebase, manpages) ] |
|
285 | data_files = [ (manpagebase, manpages) ] | |
286 |
|
286 | |||
287 | return data_files |
|
287 | return data_files | |
288 |
|
288 | |||
289 |
|
289 | |||
290 | def make_man_update_target(manpage): |
|
290 | def make_man_update_target(manpage): | |
291 | """Return a target_update-compliant tuple for the given manpage. |
|
291 | """Return a target_update-compliant tuple for the given manpage. | |
292 |
|
292 | |||
293 | Parameters |
|
293 | Parameters | |
294 | ---------- |
|
294 | ---------- | |
295 | manpage : string |
|
295 | manpage : string | |
296 | Name of the manpage, must include the section number (trailing number). |
|
296 | Name of the manpage, must include the section number (trailing number). | |
297 |
|
297 | |||
298 | Example |
|
298 | Example | |
299 | ------- |
|
299 | ------- | |
300 |
|
300 | |||
301 | >>> make_man_update_target('ipython.1') #doctest: +NORMALIZE_WHITESPACE |
|
301 | >>> make_man_update_target('ipython.1') #doctest: +NORMALIZE_WHITESPACE | |
302 | ('docs/man/ipython.1.gz', |
|
302 | ('docs/man/ipython.1.gz', | |
303 | ['docs/man/ipython.1'], |
|
303 | ['docs/man/ipython.1'], | |
304 | 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz') |
|
304 | 'cd docs/man && gzip -9c ipython.1 > ipython.1.gz') | |
305 | """ |
|
305 | """ | |
306 | man_dir = pjoin('docs', 'man') |
|
306 | man_dir = pjoin('docs', 'man') | |
307 | manpage_gz = manpage + '.gz' |
|
307 | manpage_gz = manpage + '.gz' | |
308 | manpath = pjoin(man_dir, manpage) |
|
308 | manpath = pjoin(man_dir, manpage) | |
309 | manpath_gz = pjoin(man_dir, manpage_gz) |
|
309 | manpath_gz = pjoin(man_dir, manpage_gz) | |
310 | gz_cmd = ( "cd %(man_dir)s && gzip -9c %(manpage)s > %(manpage_gz)s" % |
|
310 | gz_cmd = ( "cd %(man_dir)s && gzip -9c %(manpage)s > %(manpage_gz)s" % | |
311 | locals() ) |
|
311 | locals() ) | |
312 | return (manpath_gz, [manpath], gz_cmd) |
|
312 | return (manpath_gz, [manpath], gz_cmd) | |
313 |
|
313 | |||
314 | # The two functions below are copied from IPython.utils.path, so we don't need |
|
314 | # The two functions below are copied from IPython.utils.path, so we don't need | |
315 | # to import IPython during setup, which fails on Python 3. |
|
315 | # to import IPython during setup, which fails on Python 3. | |
316 |
|
316 | |||
317 | def target_outdated(target,deps): |
|
317 | def target_outdated(target,deps): | |
318 | """Determine whether a target is out of date. |
|
318 | """Determine whether a target is out of date. | |
319 |
|
319 | |||
320 | target_outdated(target,deps) -> 1/0 |
|
320 | target_outdated(target,deps) -> 1/0 | |
321 |
|
321 | |||
322 | deps: list of filenames which MUST exist. |
|
322 | deps: list of filenames which MUST exist. | |
323 | target: single filename which may or may not exist. |
|
323 | target: single filename which may or may not exist. | |
324 |
|
324 | |||
325 | If target doesn't exist or is older than any file listed in deps, return |
|
325 | If target doesn't exist or is older than any file listed in deps, return | |
326 | true, otherwise return false. |
|
326 | true, otherwise return false. | |
327 | """ |
|
327 | """ | |
328 | try: |
|
328 | try: | |
329 | target_time = os.path.getmtime(target) |
|
329 | target_time = os.path.getmtime(target) | |
330 | except os.error: |
|
330 | except os.error: | |
331 | return 1 |
|
331 | return 1 | |
332 | for dep in deps: |
|
332 | for dep in deps: | |
333 | dep_time = os.path.getmtime(dep) |
|
333 | dep_time = os.path.getmtime(dep) | |
334 | if dep_time > target_time: |
|
334 | if dep_time > target_time: | |
335 | #print "For target",target,"Dep failed:",dep # dbg |
|
335 | #print "For target",target,"Dep failed:",dep # dbg | |
336 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
336 | #print "times (dep,tar):",dep_time,target_time # dbg | |
337 | return 1 |
|
337 | return 1 | |
338 | return 0 |
|
338 | return 0 | |
339 |
|
339 | |||
340 |
|
340 | |||
341 | def target_update(target,deps,cmd): |
|
341 | def target_update(target,deps,cmd): | |
342 | """Update a target with a given command given a list of dependencies. |
|
342 | """Update a target with a given command given a list of dependencies. | |
343 |
|
343 | |||
344 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
344 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
345 |
|
345 | |||
346 | This is just a wrapper around target_outdated() which calls the given |
|
346 | This is just a wrapper around target_outdated() which calls the given | |
347 | command if target is outdated.""" |
|
347 | command if target is outdated.""" | |
348 |
|
348 | |||
349 | if target_outdated(target,deps): |
|
349 | if target_outdated(target,deps): | |
350 | os.system(cmd) |
|
350 | os.system(cmd) | |
351 |
|
351 | |||
352 | #--------------------------------------------------------------------------- |
|
352 | #--------------------------------------------------------------------------- | |
353 | # Find scripts |
|
353 | # Find scripts | |
354 | #--------------------------------------------------------------------------- |
|
354 | #--------------------------------------------------------------------------- | |
355 |
|
355 | |||
356 | def find_entry_points(): |
|
356 | def find_entry_points(): | |
357 | """Find IPython's scripts. |
|
357 | """Find IPython's scripts. | |
358 |
|
358 | |||
359 | if entry_points is True: |
|
359 | if entry_points is True: | |
360 | return setuptools entry_point-style definitions |
|
360 | return setuptools entry_point-style definitions | |
361 | else: |
|
361 | else: | |
362 | return file paths of plain scripts [default] |
|
362 | return file paths of plain scripts [default] | |
363 |
|
363 | |||
364 | suffix is appended to script names if entry_points is True, so that the |
|
364 | suffix is appended to script names if entry_points is True, so that the | |
365 | Python 3 scripts get named "ipython3" etc. |
|
365 | Python 3 scripts get named "ipython3" etc. | |
366 | """ |
|
366 | """ | |
367 | ep = [ |
|
367 | ep = [ | |
368 | 'ipython%s = IPython:start_ipython', |
|
368 | 'ipython%s = IPython:start_ipython', | |
369 | 'ipcontroller%s = IPython.parallel.apps.ipcontrollerapp:launch_new_instance', |
|
369 | 'ipcontroller%s = IPython.parallel.apps.ipcontrollerapp:launch_new_instance', | |
370 | 'ipengine%s = IPython.parallel.apps.ipengineapp:launch_new_instance', |
|
370 | 'ipengine%s = IPython.parallel.apps.ipengineapp:launch_new_instance', | |
371 | 'ipcluster%s = IPython.parallel.apps.ipclusterapp:launch_new_instance', |
|
371 | 'ipcluster%s = IPython.parallel.apps.ipclusterapp:launch_new_instance', | |
372 | 'iptest%s = IPython.testing.iptestcontroller:main', |
|
372 | 'iptest%s = IPython.testing.iptestcontroller:main', | |
373 | ] |
|
373 | ] | |
374 | suffix = str(sys.version_info[0]) |
|
374 | suffix = str(sys.version_info[0]) | |
375 | return [e % '' for e in ep] + [e % suffix for e in ep] |
|
375 | return [e % '' for e in ep] + [e % suffix for e in ep] | |
376 |
|
376 | |||
377 | script_src = """#!{executable} |
|
377 | script_src = """#!{executable} | |
378 | # This script was automatically generated by setup.py |
|
378 | # This script was automatically generated by setup.py | |
379 | if __name__ == '__main__': |
|
379 | if __name__ == '__main__': | |
380 | from {mod} import {func} |
|
380 | from {mod} import {func} | |
381 | {func}() |
|
381 | {func}() | |
382 | """ |
|
382 | """ | |
383 |
|
383 | |||
384 | class build_scripts_entrypt(build_scripts): |
|
384 | class build_scripts_entrypt(build_scripts): | |
385 | def run(self): |
|
385 | def run(self): | |
386 | self.mkpath(self.build_dir) |
|
386 | self.mkpath(self.build_dir) | |
387 | outfiles = [] |
|
387 | outfiles = [] | |
388 | for script in find_entry_points(): |
|
388 | for script in find_entry_points(): | |
389 | name, entrypt = script.split('=') |
|
389 | name, entrypt = script.split('=') | |
390 | name = name.strip() |
|
390 | name = name.strip() | |
391 | entrypt = entrypt.strip() |
|
391 | entrypt = entrypt.strip() | |
392 | outfile = os.path.join(self.build_dir, name) |
|
392 | outfile = os.path.join(self.build_dir, name) | |
393 | outfiles.append(outfile) |
|
393 | outfiles.append(outfile) | |
394 | print('Writing script to', outfile) |
|
394 | print('Writing script to', outfile) | |
395 |
|
395 | |||
396 | mod, func = entrypt.split(':') |
|
396 | mod, func = entrypt.split(':') | |
397 | with open(outfile, 'w') as f: |
|
397 | with open(outfile, 'w') as f: | |
398 | f.write(script_src.format(executable=sys.executable, |
|
398 | f.write(script_src.format(executable=sys.executable, | |
399 | mod=mod, func=func)) |
|
399 | mod=mod, func=func)) | |
400 |
|
400 | |||
401 | return outfiles, outfiles |
|
401 | return outfiles, outfiles | |
402 |
|
402 | |||
403 | class install_lib_symlink(Command): |
|
403 | class install_lib_symlink(Command): | |
404 | user_options = [ |
|
404 | user_options = [ | |
405 | ('install-dir=', 'd', "directory to install to"), |
|
405 | ('install-dir=', 'd', "directory to install to"), | |
406 | ] |
|
406 | ] | |
407 |
|
407 | |||
408 | def initialize_options(self): |
|
408 | def initialize_options(self): | |
409 | self.install_dir = None |
|
409 | self.install_dir = None | |
410 |
|
410 | |||
411 | def finalize_options(self): |
|
411 | def finalize_options(self): | |
412 | self.set_undefined_options('symlink', |
|
412 | self.set_undefined_options('symlink', | |
413 | ('install_lib', 'install_dir'), |
|
413 | ('install_lib', 'install_dir'), | |
414 | ) |
|
414 | ) | |
415 |
|
415 | |||
416 | def run(self): |
|
416 | def run(self): | |
417 | if sys.platform == 'win32': |
|
417 | if sys.platform == 'win32': | |
418 | raise Exception("This doesn't work on Windows.") |
|
418 | raise Exception("This doesn't work on Windows.") | |
419 | pkg = os.path.join(os.getcwd(), 'IPython') |
|
419 | pkg = os.path.join(os.getcwd(), 'IPython') | |
420 | dest = os.path.join(self.install_dir, 'IPython') |
|
420 | dest = os.path.join(self.install_dir, 'IPython') | |
421 | if os.path.islink(dest): |
|
421 | if os.path.islink(dest): | |
422 | print('removing existing symlink at %s' % dest) |
|
422 | print('removing existing symlink at %s' % dest) | |
423 | os.unlink(dest) |
|
423 | os.unlink(dest) | |
424 | print('symlinking %s -> %s' % (pkg, dest)) |
|
424 | print('symlinking %s -> %s' % (pkg, dest)) | |
425 | os.symlink(pkg, dest) |
|
425 | os.symlink(pkg, dest) | |
426 |
|
426 | |||
427 | class unsymlink(install): |
|
427 | class unsymlink(install): | |
428 | def run(self): |
|
428 | def run(self): | |
429 | dest = os.path.join(self.install_lib, 'IPython') |
|
429 | dest = os.path.join(self.install_lib, 'IPython') | |
430 | if os.path.islink(dest): |
|
430 | if os.path.islink(dest): | |
431 | print('removing symlink at %s' % dest) |
|
431 | print('removing symlink at %s' % dest) | |
432 | os.unlink(dest) |
|
432 | os.unlink(dest) | |
433 | else: |
|
433 | else: | |
434 | print('No symlink exists at %s' % dest) |
|
434 | print('No symlink exists at %s' % dest) | |
435 |
|
435 | |||
436 | class install_symlinked(install): |
|
436 | class install_symlinked(install): | |
437 | def run(self): |
|
437 | def run(self): | |
438 | if sys.platform == 'win32': |
|
438 | if sys.platform == 'win32': | |
439 | raise Exception("This doesn't work on Windows.") |
|
439 | raise Exception("This doesn't work on Windows.") | |
440 |
|
440 | |||
441 | # Run all sub-commands (at least those that need to be run) |
|
441 | # Run all sub-commands (at least those that need to be run) | |
442 | for cmd_name in self.get_sub_commands(): |
|
442 | for cmd_name in self.get_sub_commands(): | |
443 | self.run_command(cmd_name) |
|
443 | self.run_command(cmd_name) | |
444 |
|
444 | |||
445 | # 'sub_commands': a list of commands this command might have to run to |
|
445 | # 'sub_commands': a list of commands this command might have to run to | |
446 | # get its work done. See cmd.py for more info. |
|
446 | # get its work done. See cmd.py for more info. | |
447 | sub_commands = [('install_lib_symlink', lambda self:True), |
|
447 | sub_commands = [('install_lib_symlink', lambda self:True), | |
448 | ('install_scripts_sym', lambda self:True), |
|
448 | ('install_scripts_sym', lambda self:True), | |
449 | ] |
|
449 | ] | |
450 |
|
450 | |||
451 | class install_scripts_for_symlink(install_scripts): |
|
451 | class install_scripts_for_symlink(install_scripts): | |
452 | """Redefined to get options from 'symlink' instead of 'install'. |
|
452 | """Redefined to get options from 'symlink' instead of 'install'. | |
453 |
|
453 | |||
454 | I love distutils almost as much as I love setuptools. |
|
454 | I love distutils almost as much as I love setuptools. | |
455 | """ |
|
455 | """ | |
456 | def finalize_options(self): |
|
456 | def finalize_options(self): | |
457 | self.set_undefined_options('build', ('build_scripts', 'build_dir')) |
|
457 | self.set_undefined_options('build', ('build_scripts', 'build_dir')) | |
458 | self.set_undefined_options('symlink', |
|
458 | self.set_undefined_options('symlink', | |
459 | ('install_scripts', 'install_dir'), |
|
459 | ('install_scripts', 'install_dir'), | |
460 | ('force', 'force'), |
|
460 | ('force', 'force'), | |
461 | ('skip_build', 'skip_build'), |
|
461 | ('skip_build', 'skip_build'), | |
462 | ) |
|
462 | ) | |
463 |
|
463 | |||
464 | #--------------------------------------------------------------------------- |
|
464 | #--------------------------------------------------------------------------- | |
465 | # Verify all dependencies |
|
465 | # Verify all dependencies | |
466 | #--------------------------------------------------------------------------- |
|
466 | #--------------------------------------------------------------------------- | |
467 |
|
467 | |||
468 | def check_for_dependencies(): |
|
468 | def check_for_dependencies(): | |
469 | """Check for IPython's dependencies. |
|
469 | """Check for IPython's dependencies. | |
470 |
|
470 | |||
471 | This function should NOT be called if running under setuptools! |
|
471 | This function should NOT be called if running under setuptools! | |
472 | """ |
|
472 | """ | |
473 | from setupext.setupext import ( |
|
473 | from setupext.setupext import ( | |
474 | print_line, print_raw, print_status, |
|
474 | print_line, print_raw, print_status, | |
475 | check_for_sphinx, check_for_pygments, |
|
475 | check_for_sphinx, check_for_pygments, | |
476 | check_for_nose, check_for_pexpect, |
|
476 | check_for_nose, check_for_pexpect, | |
477 | check_for_pyzmq, check_for_readline, |
|
477 | check_for_pyzmq, check_for_readline, | |
478 | check_for_jinja2, check_for_tornado |
|
478 | check_for_jinja2, check_for_tornado | |
479 | ) |
|
479 | ) | |
480 | print_line() |
|
480 | print_line() | |
481 | print_raw("BUILDING IPYTHON") |
|
481 | print_raw("BUILDING IPYTHON") | |
482 | print_status('python', sys.version) |
|
482 | print_status('python', sys.version) | |
483 | print_status('platform', sys.platform) |
|
483 | print_status('platform', sys.platform) | |
484 | if sys.platform == 'win32': |
|
484 | if sys.platform == 'win32': | |
485 | print_status('Windows version', sys.getwindowsversion()) |
|
485 | print_status('Windows version', sys.getwindowsversion()) | |
486 |
|
486 | |||
487 | print_raw("") |
|
487 | print_raw("") | |
488 | print_raw("OPTIONAL DEPENDENCIES") |
|
488 | print_raw("OPTIONAL DEPENDENCIES") | |
489 |
|
489 | |||
490 | check_for_sphinx() |
|
490 | check_for_sphinx() | |
491 | check_for_pygments() |
|
491 | check_for_pygments() | |
492 | check_for_nose() |
|
492 | check_for_nose() | |
493 | if os.name == 'posix': |
|
493 | if os.name == 'posix': | |
494 | check_for_pexpect() |
|
494 | check_for_pexpect() | |
495 | check_for_pyzmq() |
|
495 | check_for_pyzmq() | |
496 | check_for_tornado() |
|
496 | check_for_tornado() | |
497 | check_for_readline() |
|
497 | check_for_readline() | |
498 | check_for_jinja2() |
|
498 | check_for_jinja2() | |
499 |
|
499 | |||
500 | #--------------------------------------------------------------------------- |
|
500 | #--------------------------------------------------------------------------- | |
501 | # VCS related |
|
501 | # VCS related | |
502 | #--------------------------------------------------------------------------- |
|
502 | #--------------------------------------------------------------------------- | |
503 |
|
503 | |||
504 | # utils.submodule has checks for submodule status |
|
504 | # utils.submodule has checks for submodule status | |
505 | execfile(pjoin('IPython','utils','submodule.py'), globals()) |
|
505 | execfile(pjoin('IPython','utils','submodule.py'), globals()) | |
506 |
|
506 | |||
507 | class UpdateSubmodules(Command): |
|
507 | class UpdateSubmodules(Command): | |
508 | """Update git submodules |
|
508 | """Update git submodules | |
509 |
|
509 | |||
510 | IPython's external javascript dependencies live in a separate repo. |
|
510 | IPython's external javascript dependencies live in a separate repo. | |
511 | """ |
|
511 | """ | |
512 | description = "Update git submodules" |
|
512 | description = "Update git submodules" | |
513 | user_options = [] |
|
513 | user_options = [] | |
514 |
|
514 | |||
515 | def initialize_options(self): |
|
515 | def initialize_options(self): | |
516 | pass |
|
516 | pass | |
517 |
|
517 | |||
518 | def finalize_options(self): |
|
518 | def finalize_options(self): | |
519 | pass |
|
519 | pass | |
520 |
|
520 | |||
521 | def run(self): |
|
521 | def run(self): | |
522 | failure = False |
|
522 | failure = False | |
523 | try: |
|
523 | try: | |
524 | self.spawn('git submodule init'.split()) |
|
524 | self.spawn('git submodule init'.split()) | |
525 | self.spawn('git submodule update --recursive'.split()) |
|
525 | self.spawn('git submodule update --recursive'.split()) | |
526 | except Exception as e: |
|
526 | except Exception as e: | |
527 | failure = e |
|
527 | failure = e | |
528 | print(e) |
|
528 | print(e) | |
529 |
|
529 | |||
530 | if not check_submodule_status(repo_root) == 'clean': |
|
530 | if not check_submodule_status(repo_root) == 'clean': | |
531 | print("submodules could not be checked out") |
|
531 | print("submodules could not be checked out") | |
532 | sys.exit(1) |
|
532 | sys.exit(1) | |
533 |
|
533 | |||
534 |
|
534 | |||
535 | def git_prebuild(pkg_dir, build_cmd=build_py): |
|
535 | def git_prebuild(pkg_dir, build_cmd=build_py): | |
536 | """Return extended build or sdist command class for recording commit |
|
536 | """Return extended build or sdist command class for recording commit | |
537 |
|
537 | |||
538 | records git commit in IPython.utils._sysinfo.commit |
|
538 | records git commit in IPython.utils._sysinfo.commit | |
539 |
|
539 | |||
540 | for use in IPython.utils.sysinfo.sys_info() calls after installation. |
|
540 | for use in IPython.utils.sysinfo.sys_info() calls after installation. | |
541 |
|
541 | |||
542 | Also ensures that submodules exist prior to running |
|
542 | Also ensures that submodules exist prior to running | |
543 | """ |
|
543 | """ | |
544 |
|
544 | |||
545 | class MyBuildPy(build_cmd): |
|
545 | class MyBuildPy(build_cmd): | |
546 | ''' Subclass to write commit data into installation tree ''' |
|
546 | ''' Subclass to write commit data into installation tree ''' | |
547 | def run(self): |
|
547 | def run(self): | |
548 | build_cmd.run(self) |
|
548 | build_cmd.run(self) | |
549 | # this one will only fire for build commands |
|
549 | # this one will only fire for build commands | |
550 | if hasattr(self, 'build_lib'): |
|
550 | if hasattr(self, 'build_lib'): | |
551 | self._record_commit(self.build_lib) |
|
551 | self._record_commit(self.build_lib) | |
552 |
|
552 | |||
553 | def make_release_tree(self, base_dir, files): |
|
553 | def make_release_tree(self, base_dir, files): | |
554 | # this one will fire for sdist |
|
554 | # this one will fire for sdist | |
555 | build_cmd.make_release_tree(self, base_dir, files) |
|
555 | build_cmd.make_release_tree(self, base_dir, files) | |
556 | self._record_commit(base_dir) |
|
556 | self._record_commit(base_dir) | |
557 |
|
557 | |||
558 | def _record_commit(self, base_dir): |
|
558 | def _record_commit(self, base_dir): | |
559 | import subprocess |
|
559 | import subprocess | |
560 | proc = subprocess.Popen('git rev-parse --short HEAD', |
|
560 | proc = subprocess.Popen('git rev-parse --short HEAD', | |
561 | stdout=subprocess.PIPE, |
|
561 | stdout=subprocess.PIPE, | |
562 | stderr=subprocess.PIPE, |
|
562 | stderr=subprocess.PIPE, | |
563 | shell=True) |
|
563 | shell=True) | |
564 | repo_commit, _ = proc.communicate() |
|
564 | repo_commit, _ = proc.communicate() | |
565 | repo_commit = repo_commit.strip().decode("ascii") |
|
565 | repo_commit = repo_commit.strip().decode("ascii") | |
566 |
|
566 | |||
567 | out_pth = pjoin(base_dir, pkg_dir, 'utils', '_sysinfo.py') |
|
567 | out_pth = pjoin(base_dir, pkg_dir, 'utils', '_sysinfo.py') | |
568 | if os.path.isfile(out_pth) and not repo_commit: |
|
568 | if os.path.isfile(out_pth) and not repo_commit: | |
569 | # nothing to write, don't clobber |
|
569 | # nothing to write, don't clobber | |
570 | return |
|
570 | return | |
571 |
|
571 | |||
572 | print("writing git commit '%s' to %s" % (repo_commit, out_pth)) |
|
572 | print("writing git commit '%s' to %s" % (repo_commit, out_pth)) | |
573 |
|
573 | |||
574 | # remove to avoid overwriting original via hard link |
|
574 | # remove to avoid overwriting original via hard link | |
575 | try: |
|
575 | try: | |
576 | os.remove(out_pth) |
|
576 | os.remove(out_pth) | |
577 | except (IOError, OSError): |
|
577 | except (IOError, OSError): | |
578 | pass |
|
578 | pass | |
579 | with open(out_pth, 'w') as out_file: |
|
579 | with open(out_pth, 'w') as out_file: | |
580 | out_file.writelines([ |
|
580 | out_file.writelines([ | |
581 | '# GENERATED BY setup.py\n', |
|
581 | '# GENERATED BY setup.py\n', | |
582 | 'commit = "%s"\n' % repo_commit, |
|
582 | 'commit = u"%s"\n' % repo_commit, | |
583 | ]) |
|
583 | ]) | |
584 | return require_submodules(MyBuildPy) |
|
584 | return require_submodules(MyBuildPy) | |
585 |
|
585 | |||
586 |
|
586 | |||
587 | def require_submodules(command): |
|
587 | def require_submodules(command): | |
588 | """decorator for instructing a command to check for submodules before running""" |
|
588 | """decorator for instructing a command to check for submodules before running""" | |
589 | class DecoratedCommand(command): |
|
589 | class DecoratedCommand(command): | |
590 | def run(self): |
|
590 | def run(self): | |
591 | if not check_submodule_status(repo_root) == 'clean': |
|
591 | if not check_submodule_status(repo_root) == 'clean': | |
592 | print("submodules missing! Run `setup.py submodule` and try again") |
|
592 | print("submodules missing! Run `setup.py submodule` and try again") | |
593 | sys.exit(1) |
|
593 | sys.exit(1) | |
594 | command.run(self) |
|
594 | command.run(self) | |
595 | return DecoratedCommand |
|
595 | return DecoratedCommand | |
596 |
|
596 | |||
597 | #--------------------------------------------------------------------------- |
|
597 | #--------------------------------------------------------------------------- | |
598 | # bdist related |
|
598 | # bdist related | |
599 | #--------------------------------------------------------------------------- |
|
599 | #--------------------------------------------------------------------------- | |
600 |
|
600 | |||
601 | def get_bdist_wheel(): |
|
601 | def get_bdist_wheel(): | |
602 | """Construct bdist_wheel command for building wheels |
|
602 | """Construct bdist_wheel command for building wheels | |
603 |
|
603 | |||
604 | Constructs py2-none-any tag, instead of py2.7-none-any |
|
604 | Constructs py2-none-any tag, instead of py2.7-none-any | |
605 | """ |
|
605 | """ | |
606 | class RequiresWheel(Command): |
|
606 | class RequiresWheel(Command): | |
607 | description = "Dummy command for missing bdist_wheel" |
|
607 | description = "Dummy command for missing bdist_wheel" | |
608 | user_options = [] |
|
608 | user_options = [] | |
609 |
|
609 | |||
610 | def initialize_options(self): |
|
610 | def initialize_options(self): | |
611 | pass |
|
611 | pass | |
612 |
|
612 | |||
613 | def finalize_options(self): |
|
613 | def finalize_options(self): | |
614 | pass |
|
614 | pass | |
615 |
|
615 | |||
616 | def run(self): |
|
616 | def run(self): | |
617 | print("bdist_wheel requires the wheel package") |
|
617 | print("bdist_wheel requires the wheel package") | |
618 | sys.exit(1) |
|
618 | sys.exit(1) | |
619 |
|
619 | |||
620 | if 'setuptools' not in sys.modules: |
|
620 | if 'setuptools' not in sys.modules: | |
621 | return RequiresWheel |
|
621 | return RequiresWheel | |
622 | else: |
|
622 | else: | |
623 | try: |
|
623 | try: | |
624 | from wheel.bdist_wheel import bdist_wheel, read_pkg_info, write_pkg_info |
|
624 | from wheel.bdist_wheel import bdist_wheel, read_pkg_info, write_pkg_info | |
625 | except ImportError: |
|
625 | except ImportError: | |
626 | return RequiresWheel |
|
626 | return RequiresWheel | |
627 |
|
627 | |||
628 | class bdist_wheel_tag(bdist_wheel): |
|
628 | class bdist_wheel_tag(bdist_wheel): | |
629 |
|
629 | |||
630 | def add_requirements(self, metadata_path): |
|
630 | def add_requirements(self, metadata_path): | |
631 | """transform platform-dependent requirements""" |
|
631 | """transform platform-dependent requirements""" | |
632 | pkg_info = read_pkg_info(metadata_path) |
|
632 | pkg_info = read_pkg_info(metadata_path) | |
633 | # pkg_info is an email.Message object (?!) |
|
633 | # pkg_info is an email.Message object (?!) | |
634 | # we have to remove the unconditional 'readline' and/or 'pyreadline' entries |
|
634 | # we have to remove the unconditional 'readline' and/or 'pyreadline' entries | |
635 | # and transform them to conditionals |
|
635 | # and transform them to conditionals | |
636 | requires = pkg_info.get_all('Requires-Dist') |
|
636 | requires = pkg_info.get_all('Requires-Dist') | |
637 | del pkg_info['Requires-Dist'] |
|
637 | del pkg_info['Requires-Dist'] | |
638 | def _remove_startswith(lis, prefix): |
|
638 | def _remove_startswith(lis, prefix): | |
639 | """like list.remove, but with startswith instead of ==""" |
|
639 | """like list.remove, but with startswith instead of ==""" | |
640 | found = False |
|
640 | found = False | |
641 | for idx, item in enumerate(lis): |
|
641 | for idx, item in enumerate(lis): | |
642 | if item.startswith(prefix): |
|
642 | if item.startswith(prefix): | |
643 | found = True |
|
643 | found = True | |
644 | break |
|
644 | break | |
645 | if found: |
|
645 | if found: | |
646 | lis.pop(idx) |
|
646 | lis.pop(idx) | |
647 |
|
647 | |||
648 | for pkg in ("gnureadline", "pyreadline", "mock"): |
|
648 | for pkg in ("gnureadline", "pyreadline", "mock"): | |
649 | _remove_startswith(requires, pkg) |
|
649 | _remove_startswith(requires, pkg) | |
650 | requires.append("gnureadline; sys.platform == 'darwin' and platform.python_implementation == 'CPython'") |
|
650 | requires.append("gnureadline; sys.platform == 'darwin' and platform.python_implementation == 'CPython'") | |
651 | requires.append("pyreadline (>=2.0); extra == 'terminal' and sys.platform == 'win32' and platform.python_implementation == 'CPython'") |
|
651 | requires.append("pyreadline (>=2.0); extra == 'terminal' and sys.platform == 'win32' and platform.python_implementation == 'CPython'") | |
652 | requires.append("pyreadline (>=2.0); extra == 'all' and sys.platform == 'win32' and platform.python_implementation == 'CPython'") |
|
652 | requires.append("pyreadline (>=2.0); extra == 'all' and sys.platform == 'win32' and platform.python_implementation == 'CPython'") | |
653 | requires.append("mock; extra == 'test' and python_version < '3.3'") |
|
653 | requires.append("mock; extra == 'test' and python_version < '3.3'") | |
654 | for r in requires: |
|
654 | for r in requires: | |
655 | pkg_info['Requires-Dist'] = r |
|
655 | pkg_info['Requires-Dist'] = r | |
656 | write_pkg_info(metadata_path, pkg_info) |
|
656 | write_pkg_info(metadata_path, pkg_info) | |
657 |
|
657 | |||
658 | return bdist_wheel_tag |
|
658 | return bdist_wheel_tag | |
659 |
|
659 | |||
660 | #--------------------------------------------------------------------------- |
|
660 | #--------------------------------------------------------------------------- | |
661 | # Notebook related |
|
661 | # Notebook related | |
662 | #--------------------------------------------------------------------------- |
|
662 | #--------------------------------------------------------------------------- | |
663 |
|
663 | |||
664 | class CompileCSS(Command): |
|
664 | class CompileCSS(Command): | |
665 | """Recompile Notebook CSS |
|
665 | """Recompile Notebook CSS | |
666 |
|
666 | |||
667 | Regenerate the compiled CSS from LESS sources. |
|
667 | Regenerate the compiled CSS from LESS sources. | |
668 |
|
668 | |||
669 | Requires various dev dependencies, such as fabric and lessc. |
|
669 | Requires various dev dependencies, such as fabric and lessc. | |
670 | """ |
|
670 | """ | |
671 | description = "Recompile Notebook CSS" |
|
671 | description = "Recompile Notebook CSS" | |
672 | user_options = [ |
|
672 | user_options = [ | |
673 | ('minify', 'x', "minify CSS"), |
|
673 | ('minify', 'x', "minify CSS"), | |
674 | ('force', 'f', "force recompilation of CSS"), |
|
674 | ('force', 'f', "force recompilation of CSS"), | |
675 | ] |
|
675 | ] | |
676 |
|
676 | |||
677 | def initialize_options(self): |
|
677 | def initialize_options(self): | |
678 | self.minify = False |
|
678 | self.minify = False | |
679 | self.force = False |
|
679 | self.force = False | |
680 |
|
680 | |||
681 | def finalize_options(self): |
|
681 | def finalize_options(self): | |
682 | self.minify = bool(self.minify) |
|
682 | self.minify = bool(self.minify) | |
683 | self.force = bool(self.force) |
|
683 | self.force = bool(self.force) | |
684 |
|
684 | |||
685 | def run(self): |
|
685 | def run(self): | |
686 | check_call([ |
|
686 | check_call([ | |
687 | "fab", |
|
687 | "fab", | |
688 | "css:minify=%s,force=%s" % (self.minify, self.force), |
|
688 | "css:minify=%s,force=%s" % (self.minify, self.force), | |
689 | ], cwd=pjoin(repo_root, "IPython", "html"), |
|
689 | ], cwd=pjoin(repo_root, "IPython", "html"), | |
690 | ) |
|
690 | ) | |
691 |
|
691 | |||
692 |
|
692 | |||
693 | class JavascriptVersion(Command): |
|
693 | class JavascriptVersion(Command): | |
694 | """write the javascript version to notebook javascript""" |
|
694 | """write the javascript version to notebook javascript""" | |
695 | description = "Write IPython version to javascript" |
|
695 | description = "Write IPython version to javascript" | |
696 | user_options = [] |
|
696 | user_options = [] | |
697 |
|
697 | |||
698 | def initialize_options(self): |
|
698 | def initialize_options(self): | |
699 | pass |
|
699 | pass | |
700 |
|
700 | |||
701 | def finalize_options(self): |
|
701 | def finalize_options(self): | |
702 | pass |
|
702 | pass | |
703 |
|
703 | |||
704 | def run(self): |
|
704 | def run(self): | |
705 | nsfile = pjoin(repo_root, "IPython", "html", "static", "base", "js", "namespace.js") |
|
705 | nsfile = pjoin(repo_root, "IPython", "html", "static", "base", "js", "namespace.js") | |
706 | with open(nsfile) as f: |
|
706 | with open(nsfile) as f: | |
707 | lines = f.readlines() |
|
707 | lines = f.readlines() | |
708 | with open(nsfile, 'w') as f: |
|
708 | with open(nsfile, 'w') as f: | |
709 | for line in lines: |
|
709 | for line in lines: | |
710 | if line.startswith("IPython.version"): |
|
710 | if line.startswith("IPython.version"): | |
711 | line = 'IPython.version = "{0}";\n'.format(version) |
|
711 | line = 'IPython.version = "{0}";\n'.format(version) | |
712 | f.write(line) |
|
712 | f.write(line) | |
713 |
|
713 | |||
714 |
|
714 | |||
715 | def css_js_prerelease(command, strict=True): |
|
715 | def css_js_prerelease(command, strict=True): | |
716 | """decorator for building js/minified css prior to a release""" |
|
716 | """decorator for building js/minified css prior to a release""" | |
717 | class DecoratedCommand(command): |
|
717 | class DecoratedCommand(command): | |
718 | def run(self): |
|
718 | def run(self): | |
719 | self.distribution.run_command('jsversion') |
|
719 | self.distribution.run_command('jsversion') | |
720 | css = self.distribution.get_command_obj('css') |
|
720 | css = self.distribution.get_command_obj('css') | |
721 | css.minify = True |
|
721 | css.minify = True | |
722 | try: |
|
722 | try: | |
723 | self.distribution.run_command('css') |
|
723 | self.distribution.run_command('css') | |
724 | except Exception as e: |
|
724 | except Exception as e: | |
725 | if strict: |
|
725 | if strict: | |
726 | raise |
|
726 | raise | |
727 | else: |
|
727 | else: | |
728 | log.warn("Failed to build css sourcemaps: %s" % e) |
|
728 | log.warn("Failed to build css sourcemaps: %s" % e) | |
729 | command.run(self) |
|
729 | command.run(self) | |
730 | return DecoratedCommand |
|
730 | return DecoratedCommand |
General Comments 0
You need to be logged in to leave comments.
Login now