Show More
@@ -1,280 +1,281 b'' | |||||
1 | """Magic functions for running cells in various scripts.""" |
|
1 | """Magic functions for running cells in various scripts.""" | |
2 | from __future__ import print_function |
|
2 | from __future__ import print_function | |
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 |
|
14 | |||
15 | # Stdlib |
|
15 | # Stdlib | |
16 | import errno |
|
16 | import errno | |
17 | import os |
|
17 | import os | |
18 | import sys |
|
18 | import sys | |
19 | import signal |
|
19 | import signal | |
20 | import time |
|
20 | import time | |
21 | from subprocess import Popen, PIPE |
|
21 | from subprocess import Popen, PIPE | |
22 | import atexit |
|
22 | import atexit | |
23 |
|
23 | |||
24 | # Our own packages |
|
24 | # Our own packages | |
25 | from IPython.config.configurable import Configurable |
|
25 | from IPython.config.configurable import Configurable | |
26 | from IPython.core import magic_arguments |
|
26 | from IPython.core import magic_arguments | |
27 | from IPython.core.magic import ( |
|
27 | from IPython.core.magic import ( | |
28 | Magics, magics_class, line_magic, cell_magic |
|
28 | Magics, magics_class, line_magic, cell_magic | |
29 | ) |
|
29 | ) | |
30 | from IPython.lib.backgroundjobs import BackgroundJobManager |
|
30 | from IPython.lib.backgroundjobs import BackgroundJobManager | |
31 | from IPython.utils import py3compat |
|
31 | from IPython.utils import py3compat | |
32 | from IPython.utils.process import arg_split |
|
32 | from IPython.utils.process import arg_split | |
33 | from IPython.utils.traitlets import List, Dict |
|
33 | from IPython.utils.traitlets import List, Dict | |
34 |
|
34 | |||
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 | # Magic implementation classes |
|
36 | # Magic implementation classes | |
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
|
38 | |||
39 | def script_args(f): |
|
39 | def script_args(f): | |
40 | """single decorator for adding script args""" |
|
40 | """single decorator for adding script args""" | |
41 | args = [ |
|
41 | args = [ | |
42 | magic_arguments.argument( |
|
42 | magic_arguments.argument( | |
43 | '--out', type=str, |
|
43 | '--out', type=str, | |
44 | help="""The variable in which to store stdout from the script. |
|
44 | help="""The variable in which to store stdout from the script. | |
45 | If the script is backgrounded, this will be the stdout *pipe*, |
|
45 | If the script is backgrounded, this will be the stdout *pipe*, | |
46 | instead of the stderr text itself. |
|
46 | instead of the stderr text itself. | |
47 | """ |
|
47 | """ | |
48 | ), |
|
48 | ), | |
49 | magic_arguments.argument( |
|
49 | magic_arguments.argument( | |
50 | '--err', type=str, |
|
50 | '--err', type=str, | |
51 | help="""The variable in which to store stderr from the script. |
|
51 | help="""The variable in which to store stderr from the script. | |
52 | If the script is backgrounded, this will be the stderr *pipe*, |
|
52 | If the script is backgrounded, this will be the stderr *pipe*, | |
53 | instead of the stderr text itself. |
|
53 | instead of the stderr text itself. | |
54 | """ |
|
54 | """ | |
55 | ), |
|
55 | ), | |
56 | magic_arguments.argument( |
|
56 | magic_arguments.argument( | |
57 | '--bg', action="store_true", |
|
57 | '--bg', action="store_true", | |
58 | help="""Whether to run the script in the background. |
|
58 | help="""Whether to run the script in the background. | |
59 | If given, the only way to see the output of the command is |
|
59 | If given, the only way to see the output of the command is | |
60 | with --out/err. |
|
60 | with --out/err. | |
61 | """ |
|
61 | """ | |
62 | ), |
|
62 | ), | |
63 | magic_arguments.argument( |
|
63 | magic_arguments.argument( | |
64 | '--proc', type=str, |
|
64 | '--proc', type=str, | |
65 | help="""The variable in which to store Popen instance. |
|
65 | help="""The variable in which to store Popen instance. | |
66 | This is used only when --bg option is given. |
|
66 | This is used only when --bg option is given. | |
67 | """ |
|
67 | """ | |
68 | ), |
|
68 | ), | |
69 | ] |
|
69 | ] | |
70 | for arg in args: |
|
70 | for arg in args: | |
71 | f = arg(f) |
|
71 | f = arg(f) | |
72 | return f |
|
72 | return f | |
73 |
|
73 | |||
74 | @magics_class |
|
74 | @magics_class | |
75 | class ScriptMagics(Magics): |
|
75 | class ScriptMagics(Magics): | |
76 | """Magics for talking to scripts |
|
76 | """Magics for talking to scripts | |
77 |
|
77 | |||
78 | This defines a base `%%script` cell magic for running a cell |
|
78 | This defines a base `%%script` cell magic for running a cell | |
79 | with a program in a subprocess, and registers a few top-level |
|
79 | with a program in a subprocess, and registers a few top-level | |
80 | magics that call %%script with common interpreters. |
|
80 | magics that call %%script with common interpreters. | |
81 | """ |
|
81 | """ | |
82 | script_magics = List(config=True, |
|
82 | script_magics = List(config=True, | |
83 | help="""Extra script cell magics to define |
|
83 | help="""Extra script cell magics to define | |
84 |
|
84 | |||
85 | This generates simple wrappers of `%%script foo` as `%%foo`. |
|
85 | This generates simple wrappers of `%%script foo` as `%%foo`. | |
86 |
|
86 | |||
87 | If you want to add script magics that aren't on your path, |
|
87 | If you want to add script magics that aren't on your path, | |
88 | specify them in script_paths |
|
88 | specify them in script_paths | |
89 | """, |
|
89 | """, | |
90 | ) |
|
90 | ) | |
91 | def _script_magics_default(self): |
|
91 | def _script_magics_default(self): | |
92 | """default to a common list of programs""" |
|
92 | """default to a common list of programs""" | |
93 |
|
93 | |||
94 | defaults = [ |
|
94 | defaults = [ | |
95 | 'sh', |
|
95 | 'sh', | |
96 | 'bash', |
|
96 | 'bash', | |
97 | 'perl', |
|
97 | 'perl', | |
98 | 'ruby', |
|
98 | 'ruby', | |
99 | 'python', |
|
99 | 'python', | |
|
100 | 'python2', | |||
100 | 'python3', |
|
101 | 'python3', | |
101 | 'pypy', |
|
102 | 'pypy', | |
102 | ] |
|
103 | ] | |
103 | if os.name == 'nt': |
|
104 | if os.name == 'nt': | |
104 | defaults.extend([ |
|
105 | defaults.extend([ | |
105 | 'cmd', |
|
106 | 'cmd', | |
106 | 'powershell', |
|
107 | 'powershell', | |
107 | ]) |
|
108 | ]) | |
108 |
|
109 | |||
109 | return defaults |
|
110 | return defaults | |
110 |
|
111 | |||
111 | script_paths = Dict(config=True, |
|
112 | script_paths = Dict(config=True, | |
112 | help="""Dict mapping short 'ruby' names to full paths, such as '/opt/secret/bin/ruby' |
|
113 | help="""Dict mapping short 'ruby' names to full paths, such as '/opt/secret/bin/ruby' | |
113 |
|
114 | |||
114 | Only necessary for items in script_magics where the default path will not |
|
115 | Only necessary for items in script_magics where the default path will not | |
115 | find the right interpreter. |
|
116 | find the right interpreter. | |
116 | """ |
|
117 | """ | |
117 | ) |
|
118 | ) | |
118 |
|
119 | |||
119 | def __init__(self, shell=None): |
|
120 | def __init__(self, shell=None): | |
120 | super(ScriptMagics, self).__init__(shell=shell) |
|
121 | super(ScriptMagics, self).__init__(shell=shell) | |
121 | self._generate_script_magics() |
|
122 | self._generate_script_magics() | |
122 | self.job_manager = BackgroundJobManager() |
|
123 | self.job_manager = BackgroundJobManager() | |
123 | self.bg_processes = [] |
|
124 | self.bg_processes = [] | |
124 | atexit.register(self.kill_bg_processes) |
|
125 | atexit.register(self.kill_bg_processes) | |
125 |
|
126 | |||
126 | def __del__(self): |
|
127 | def __del__(self): | |
127 | self.kill_bg_processes() |
|
128 | self.kill_bg_processes() | |
128 |
|
129 | |||
129 | def _generate_script_magics(self): |
|
130 | def _generate_script_magics(self): | |
130 | cell_magics = self.magics['cell'] |
|
131 | cell_magics = self.magics['cell'] | |
131 | for name in self.script_magics: |
|
132 | for name in self.script_magics: | |
132 | cell_magics[name] = self._make_script_magic(name) |
|
133 | cell_magics[name] = self._make_script_magic(name) | |
133 |
|
134 | |||
134 | def _make_script_magic(self, name): |
|
135 | def _make_script_magic(self, name): | |
135 | """make a named magic, that calls %%script with a particular program""" |
|
136 | """make a named magic, that calls %%script with a particular program""" | |
136 | # expand to explicit path if necessary: |
|
137 | # expand to explicit path if necessary: | |
137 | script = self.script_paths.get(name, name) |
|
138 | script = self.script_paths.get(name, name) | |
138 |
|
139 | |||
139 | @magic_arguments.magic_arguments() |
|
140 | @magic_arguments.magic_arguments() | |
140 | @script_args |
|
141 | @script_args | |
141 | def named_script_magic(line, cell): |
|
142 | def named_script_magic(line, cell): | |
142 | # if line, add it as cl-flags |
|
143 | # if line, add it as cl-flags | |
143 | if line: |
|
144 | if line: | |
144 | line = "%s %s" % (script, line) |
|
145 | line = "%s %s" % (script, line) | |
145 | else: |
|
146 | else: | |
146 | line = script |
|
147 | line = script | |
147 | return self.shebang(line, cell) |
|
148 | return self.shebang(line, cell) | |
148 |
|
149 | |||
149 | # write a basic docstring: |
|
150 | # write a basic docstring: | |
150 | named_script_magic.__doc__ = \ |
|
151 | named_script_magic.__doc__ = \ | |
151 | """%%{name} script magic |
|
152 | """%%{name} script magic | |
152 |
|
153 | |||
153 | Run cells with {script} in a subprocess. |
|
154 | Run cells with {script} in a subprocess. | |
154 |
|
155 | |||
155 | This is a shortcut for `%%script {script}` |
|
156 | This is a shortcut for `%%script {script}` | |
156 | """.format(**locals()) |
|
157 | """.format(**locals()) | |
157 |
|
158 | |||
158 | return named_script_magic |
|
159 | return named_script_magic | |
159 |
|
160 | |||
160 | @magic_arguments.magic_arguments() |
|
161 | @magic_arguments.magic_arguments() | |
161 | @script_args |
|
162 | @script_args | |
162 | @cell_magic("script") |
|
163 | @cell_magic("script") | |
163 | def shebang(self, line, cell): |
|
164 | def shebang(self, line, cell): | |
164 | """Run a cell via a shell command |
|
165 | """Run a cell via a shell command | |
165 |
|
166 | |||
166 | The `%%script` line is like the #! line of script, |
|
167 | The `%%script` line is like the #! line of script, | |
167 | specifying a program (bash, perl, ruby, etc.) with which to run. |
|
168 | specifying a program (bash, perl, ruby, etc.) with which to run. | |
168 |
|
169 | |||
169 | The rest of the cell is run by that program. |
|
170 | The rest of the cell is run by that program. | |
170 |
|
171 | |||
171 | Examples |
|
172 | Examples | |
172 | -------- |
|
173 | -------- | |
173 | :: |
|
174 | :: | |
174 |
|
175 | |||
175 | In [1]: %%script bash |
|
176 | In [1]: %%script bash | |
176 | ...: for i in 1 2 3; do |
|
177 | ...: for i in 1 2 3; do | |
177 | ...: echo $i |
|
178 | ...: echo $i | |
178 | ...: done |
|
179 | ...: done | |
179 | 1 |
|
180 | 1 | |
180 | 2 |
|
181 | 2 | |
181 | 3 |
|
182 | 3 | |
182 | """ |
|
183 | """ | |
183 | argv = arg_split(line, posix = not sys.platform.startswith('win')) |
|
184 | argv = arg_split(line, posix = not sys.platform.startswith('win')) | |
184 | args, cmd = self.shebang.parser.parse_known_args(argv) |
|
185 | args, cmd = self.shebang.parser.parse_known_args(argv) | |
185 |
|
186 | |||
186 | try: |
|
187 | try: | |
187 | p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
|
188 | p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |
188 | except OSError as e: |
|
189 | except OSError as e: | |
189 | if e.errno == errno.ENOENT: |
|
190 | if e.errno == errno.ENOENT: | |
190 | print("Couldn't find program: %r" % cmd[0]) |
|
191 | print("Couldn't find program: %r" % cmd[0]) | |
191 | return |
|
192 | return | |
192 | else: |
|
193 | else: | |
193 | raise |
|
194 | raise | |
194 |
|
195 | |||
195 | cell = cell.encode('utf8', 'replace') |
|
196 | cell = cell.encode('utf8', 'replace') | |
196 | if args.bg: |
|
197 | if args.bg: | |
197 | self.bg_processes.append(p) |
|
198 | self.bg_processes.append(p) | |
198 | self._gc_bg_processes() |
|
199 | self._gc_bg_processes() | |
199 | if args.out: |
|
200 | if args.out: | |
200 | self.shell.user_ns[args.out] = p.stdout |
|
201 | self.shell.user_ns[args.out] = p.stdout | |
201 | if args.err: |
|
202 | if args.err: | |
202 | self.shell.user_ns[args.err] = p.stderr |
|
203 | self.shell.user_ns[args.err] = p.stderr | |
203 | self.job_manager.new(self._run_script, p, cell, daemon=True) |
|
204 | self.job_manager.new(self._run_script, p, cell, daemon=True) | |
204 | if args.proc: |
|
205 | if args.proc: | |
205 | self.shell.user_ns[args.proc] = p |
|
206 | self.shell.user_ns[args.proc] = p | |
206 | return |
|
207 | return | |
207 |
|
208 | |||
208 | try: |
|
209 | try: | |
209 | out, err = p.communicate(cell) |
|
210 | out, err = p.communicate(cell) | |
210 | except KeyboardInterrupt: |
|
211 | except KeyboardInterrupt: | |
211 | try: |
|
212 | try: | |
212 | p.send_signal(signal.SIGINT) |
|
213 | p.send_signal(signal.SIGINT) | |
213 | time.sleep(0.1) |
|
214 | time.sleep(0.1) | |
214 | if p.poll() is not None: |
|
215 | if p.poll() is not None: | |
215 | print("Process is interrupted.") |
|
216 | print("Process is interrupted.") | |
216 | return |
|
217 | return | |
217 | p.terminate() |
|
218 | p.terminate() | |
218 | time.sleep(0.1) |
|
219 | time.sleep(0.1) | |
219 | if p.poll() is not None: |
|
220 | if p.poll() is not None: | |
220 | print("Process is terminated.") |
|
221 | print("Process is terminated.") | |
221 | return |
|
222 | return | |
222 | p.kill() |
|
223 | p.kill() | |
223 | print("Process is killed.") |
|
224 | print("Process is killed.") | |
224 | except OSError: |
|
225 | except OSError: | |
225 | pass |
|
226 | pass | |
226 | except Exception as e: |
|
227 | except Exception as e: | |
227 | print("Error while terminating subprocess (pid=%i): %s" \ |
|
228 | print("Error while terminating subprocess (pid=%i): %s" \ | |
228 | % (p.pid, e)) |
|
229 | % (p.pid, e)) | |
229 | return |
|
230 | return | |
230 | out = py3compat.bytes_to_str(out) |
|
231 | out = py3compat.bytes_to_str(out) | |
231 | err = py3compat.bytes_to_str(err) |
|
232 | err = py3compat.bytes_to_str(err) | |
232 | if args.out: |
|
233 | if args.out: | |
233 | self.shell.user_ns[args.out] = out |
|
234 | self.shell.user_ns[args.out] = out | |
234 | else: |
|
235 | else: | |
235 | sys.stdout.write(out) |
|
236 | sys.stdout.write(out) | |
236 | sys.stdout.flush() |
|
237 | sys.stdout.flush() | |
237 | if args.err: |
|
238 | if args.err: | |
238 | self.shell.user_ns[args.err] = err |
|
239 | self.shell.user_ns[args.err] = err | |
239 | else: |
|
240 | else: | |
240 | sys.stderr.write(err) |
|
241 | sys.stderr.write(err) | |
241 | sys.stderr.flush() |
|
242 | sys.stderr.flush() | |
242 |
|
243 | |||
243 | def _run_script(self, p, cell): |
|
244 | def _run_script(self, p, cell): | |
244 | """callback for running the script in the background""" |
|
245 | """callback for running the script in the background""" | |
245 | p.stdin.write(cell) |
|
246 | p.stdin.write(cell) | |
246 | p.stdin.close() |
|
247 | p.stdin.close() | |
247 | p.wait() |
|
248 | p.wait() | |
248 |
|
249 | |||
249 | @line_magic("killbgscripts") |
|
250 | @line_magic("killbgscripts") | |
250 | def killbgscripts(self, _nouse_=''): |
|
251 | def killbgscripts(self, _nouse_=''): | |
251 | """Kill all BG processes started by %%script and its family.""" |
|
252 | """Kill all BG processes started by %%script and its family.""" | |
252 | self.kill_bg_processes() |
|
253 | self.kill_bg_processes() | |
253 | print("All background processes were killed.") |
|
254 | print("All background processes were killed.") | |
254 |
|
255 | |||
255 | def kill_bg_processes(self): |
|
256 | def kill_bg_processes(self): | |
256 | """Kill all BG processes which are still running.""" |
|
257 | """Kill all BG processes which are still running.""" | |
257 | for p in self.bg_processes: |
|
258 | for p in self.bg_processes: | |
258 | if p.poll() is None: |
|
259 | if p.poll() is None: | |
259 | try: |
|
260 | try: | |
260 | p.send_signal(signal.SIGINT) |
|
261 | p.send_signal(signal.SIGINT) | |
261 | except: |
|
262 | except: | |
262 | pass |
|
263 | pass | |
263 | time.sleep(0.1) |
|
264 | time.sleep(0.1) | |
264 | for p in self.bg_processes: |
|
265 | for p in self.bg_processes: | |
265 | if p.poll() is None: |
|
266 | if p.poll() is None: | |
266 | try: |
|
267 | try: | |
267 | p.terminate() |
|
268 | p.terminate() | |
268 | except: |
|
269 | except: | |
269 | pass |
|
270 | pass | |
270 | time.sleep(0.1) |
|
271 | time.sleep(0.1) | |
271 | for p in self.bg_processes: |
|
272 | for p in self.bg_processes: | |
272 | if p.poll() is None: |
|
273 | if p.poll() is None: | |
273 | try: |
|
274 | try: | |
274 | p.kill() |
|
275 | p.kill() | |
275 | except: |
|
276 | except: | |
276 | pass |
|
277 | pass | |
277 | self._gc_bg_processes() |
|
278 | self._gc_bg_processes() | |
278 |
|
279 | |||
279 | def _gc_bg_processes(self): |
|
280 | def _gc_bg_processes(self): | |
280 | self.bg_processes = [p for p in self.bg_processes if p.poll() is None] |
|
281 | self.bg_processes = [p for p in self.bg_processes if p.poll() is None] |
General Comments 0
You need to be logged in to leave comments.
Login now