##// END OF EJS Templates
irunner use python -m IPython to launch subprocess with matching interpreter...
Thomas Kluyver -
Show More
@@ -1,441 +1,445 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """Module for interactively running scripts.
2 """Module for interactively running scripts.
3
3
4 This module implements classes for interactively running scripts written for
4 This module implements classes for interactively running scripts written for
5 any system with a prompt which can be matched by a regexp suitable for
5 any system with a prompt which can be matched by a regexp suitable for
6 pexpect. It can be used to run as if they had been typed up interactively, an
6 pexpect. It can be used to run as if they had been typed up interactively, an
7 arbitrary series of commands for the target system.
7 arbitrary series of commands for the target system.
8
8
9 The module includes classes ready for IPython (with the default prompts),
9 The module includes classes ready for IPython (with the default prompts),
10 plain Python and SAGE, but making a new one is trivial. To see how to use it,
10 plain Python and SAGE, but making a new one is trivial. To see how to use it,
11 simply run the module as a script:
11 simply run the module as a script:
12
12
13 ./irunner.py --help
13 ./irunner.py --help
14
14
15
15
16 This is an extension of Ken Schutte <kschutte-AT-csail.mit.edu>'s script
16 This is an extension of Ken Schutte <kschutte-AT-csail.mit.edu>'s script
17 contributed on the ipython-user list:
17 contributed on the ipython-user list:
18
18
19 http://mail.scipy.org/pipermail/ipython-user/2006-May/003539.html
19 http://mail.scipy.org/pipermail/ipython-user/2006-May/003539.html
20
20
21 Notes
21 Notes
22 -----
22 -----
23
23
24 - This module requires pexpect, available in most linux distros, or which can
24 - This module requires pexpect, available in most linux distros, or which can
25 be downloaded from http://pexpect.sourceforge.net
25 be downloaded from http://pexpect.sourceforge.net
26
26
27 - Because pexpect only works under Unix or Windows-Cygwin, this has the same
27 - Because pexpect only works under Unix or Windows-Cygwin, this has the same
28 limitations. This means that it will NOT work under native windows Python.
28 limitations. This means that it will NOT work under native windows Python.
29 """
29 """
30 from __future__ import print_function
30 from __future__ import print_function
31
31
32 # Stdlib imports
32 # Stdlib imports
33 import optparse
33 import optparse
34 import os
34 import os
35 import sys
35 import sys
36
36
37 # Third-party modules: we carry a copy of pexpect to reduce the need for
37 # Third-party modules: we carry a copy of pexpect to reduce the need for
38 # external dependencies, but our import checks for a system version first.
38 # external dependencies, but our import checks for a system version first.
39 from IPython.external import pexpect
39 from IPython.external import pexpect
40 from IPython.utils import py3compat
40 from IPython.utils import py3compat
41
41
42 # Global usage strings, to avoid indentation issues when typing it below.
42 # Global usage strings, to avoid indentation issues when typing it below.
43 USAGE = """
43 USAGE = """
44 Interactive script runner, type: %s
44 Interactive script runner, type: %s
45
45
46 runner [opts] script_name
46 runner [opts] script_name
47 """
47 """
48
48
49 def pexpect_monkeypatch():
49 def pexpect_monkeypatch():
50 """Patch pexpect to prevent unhandled exceptions at VM teardown.
50 """Patch pexpect to prevent unhandled exceptions at VM teardown.
51
51
52 Calling this function will monkeypatch the pexpect.spawn class and modify
52 Calling this function will monkeypatch the pexpect.spawn class and modify
53 its __del__ method to make it more robust in the face of failures that can
53 its __del__ method to make it more robust in the face of failures that can
54 occur if it is called when the Python VM is shutting down.
54 occur if it is called when the Python VM is shutting down.
55
55
56 Since Python may fire __del__ methods arbitrarily late, it's possible for
56 Since Python may fire __del__ methods arbitrarily late, it's possible for
57 them to execute during the teardown of the Python VM itself. At this
57 them to execute during the teardown of the Python VM itself. At this
58 point, various builtin modules have been reset to None. Thus, the call to
58 point, various builtin modules have been reset to None. Thus, the call to
59 self.close() will trigger an exception because it tries to call os.close(),
59 self.close() will trigger an exception because it tries to call os.close(),
60 and os is now None.
60 and os is now None.
61 """
61 """
62
62
63 if pexpect.__version__[:3] >= '2.2':
63 if pexpect.__version__[:3] >= '2.2':
64 # No need to patch, fix is already the upstream version.
64 # No need to patch, fix is already the upstream version.
65 return
65 return
66
66
67 def __del__(self):
67 def __del__(self):
68 """This makes sure that no system resources are left open.
68 """This makes sure that no system resources are left open.
69 Python only garbage collects Python objects. OS file descriptors
69 Python only garbage collects Python objects. OS file descriptors
70 are not Python objects, so they must be handled explicitly.
70 are not Python objects, so they must be handled explicitly.
71 If the child file descriptor was opened outside of this class
71 If the child file descriptor was opened outside of this class
72 (passed to the constructor) then this does not close it.
72 (passed to the constructor) then this does not close it.
73 """
73 """
74 if not self.closed:
74 if not self.closed:
75 try:
75 try:
76 self.close()
76 self.close()
77 except AttributeError:
77 except AttributeError:
78 pass
78 pass
79
79
80 pexpect.spawn.__del__ = __del__
80 pexpect.spawn.__del__ = __del__
81
81
82 pexpect_monkeypatch()
82 pexpect_monkeypatch()
83
83
84 # The generic runner class
84 # The generic runner class
85 class InteractiveRunner(object):
85 class InteractiveRunner(object):
86 """Class to run a sequence of commands through an interactive program."""
86 """Class to run a sequence of commands through an interactive program."""
87
87
88 def __init__(self,program,prompts,args=None,out=sys.stdout,echo=True):
88 def __init__(self,program,prompts,args=None,out=sys.stdout,echo=True):
89 """Construct a runner.
89 """Construct a runner.
90
90
91 Inputs:
91 Inputs:
92
92
93 - program: command to execute the given program.
93 - program: command to execute the given program.
94
94
95 - prompts: a list of patterns to match as valid prompts, in the
95 - prompts: a list of patterns to match as valid prompts, in the
96 format used by pexpect. This basically means that it can be either
96 format used by pexpect. This basically means that it can be either
97 a string (to be compiled as a regular expression) or a list of such
97 a string (to be compiled as a regular expression) or a list of such
98 (it must be a true list, as pexpect does type checks).
98 (it must be a true list, as pexpect does type checks).
99
99
100 If more than one prompt is given, the first is treated as the main
100 If more than one prompt is given, the first is treated as the main
101 program prompt and the others as 'continuation' prompts, like
101 program prompt and the others as 'continuation' prompts, like
102 python's. This means that blank lines in the input source are
102 python's. This means that blank lines in the input source are
103 ommitted when the first prompt is matched, but are NOT ommitted when
103 ommitted when the first prompt is matched, but are NOT ommitted when
104 the continuation one matches, since this is how python signals the
104 the continuation one matches, since this is how python signals the
105 end of multiline input interactively.
105 end of multiline input interactively.
106
106
107 Optional inputs:
107 Optional inputs:
108
108
109 - args(None): optional list of strings to pass as arguments to the
109 - args(None): optional list of strings to pass as arguments to the
110 child program.
110 child program.
111
111
112 - out(sys.stdout): if given, an output stream to be used when writing
112 - out(sys.stdout): if given, an output stream to be used when writing
113 output. The only requirement is that it must have a .write() method.
113 output. The only requirement is that it must have a .write() method.
114
114
115 Public members not parameterized in the constructor:
115 Public members not parameterized in the constructor:
116
116
117 - delaybeforesend(0): Newer versions of pexpect have a delay before
117 - delaybeforesend(0): Newer versions of pexpect have a delay before
118 sending each new input. For our purposes here, it's typically best
118 sending each new input. For our purposes here, it's typically best
119 to just set this to zero, but if you encounter reliability problems
119 to just set this to zero, but if you encounter reliability problems
120 or want an interactive run to pause briefly at each prompt, just
120 or want an interactive run to pause briefly at each prompt, just
121 increase this value (it is measured in seconds). Note that this
121 increase this value (it is measured in seconds). Note that this
122 variable is not honored at all by older versions of pexpect.
122 variable is not honored at all by older versions of pexpect.
123 """
123 """
124
124
125 self.program = program
125 self.program = program
126 self.prompts = prompts
126 self.prompts = prompts
127 if args is None: args = []
127 if args is None: args = []
128 self.args = args
128 self.args = args
129 self.out = out
129 self.out = out
130 self.echo = echo
130 self.echo = echo
131 # Other public members which we don't make as parameters, but which
131 # Other public members which we don't make as parameters, but which
132 # users may occasionally want to tweak
132 # users may occasionally want to tweak
133 self.delaybeforesend = 0
133 self.delaybeforesend = 0
134
134
135 # Create child process and hold on to it so we don't have to re-create
135 # Create child process and hold on to it so we don't have to re-create
136 # for every single execution call
136 # for every single execution call
137 c = self.child = pexpect.spawn(self.program,self.args,timeout=None)
137 c = self.child = pexpect.spawn(self.program,self.args,timeout=None)
138 c.delaybeforesend = self.delaybeforesend
138 c.delaybeforesend = self.delaybeforesend
139 # pexpect hard-codes the terminal size as (24,80) (rows,columns).
139 # pexpect hard-codes the terminal size as (24,80) (rows,columns).
140 # This causes problems because any line longer than 80 characters gets
140 # This causes problems because any line longer than 80 characters gets
141 # completely overwrapped on the printed outptut (even though
141 # completely overwrapped on the printed outptut (even though
142 # internally the code runs fine). We reset this to 99 rows X 200
142 # internally the code runs fine). We reset this to 99 rows X 200
143 # columns (arbitrarily chosen), which should avoid problems in all
143 # columns (arbitrarily chosen), which should avoid problems in all
144 # reasonable cases.
144 # reasonable cases.
145 c.setwinsize(99,200)
145 c.setwinsize(99,200)
146
146
147 def close(self):
147 def close(self):
148 """close child process"""
148 """close child process"""
149
149
150 self.child.close()
150 self.child.close()
151
151
152 def run_file(self,fname,interact=False,get_output=False):
152 def run_file(self,fname,interact=False,get_output=False):
153 """Run the given file interactively.
153 """Run the given file interactively.
154
154
155 Inputs:
155 Inputs:
156
156
157 -fname: name of the file to execute.
157 -fname: name of the file to execute.
158
158
159 See the run_source docstring for the meaning of the optional
159 See the run_source docstring for the meaning of the optional
160 arguments."""
160 arguments."""
161
161
162 fobj = open(fname,'r')
162 fobj = open(fname,'r')
163 try:
163 try:
164 out = self.run_source(fobj,interact,get_output)
164 out = self.run_source(fobj,interact,get_output)
165 finally:
165 finally:
166 fobj.close()
166 fobj.close()
167 if get_output:
167 if get_output:
168 return out
168 return out
169
169
170 def run_source(self,source,interact=False,get_output=False):
170 def run_source(self,source,interact=False,get_output=False):
171 """Run the given source code interactively.
171 """Run the given source code interactively.
172
172
173 Inputs:
173 Inputs:
174
174
175 - source: a string of code to be executed, or an open file object we
175 - source: a string of code to be executed, or an open file object we
176 can iterate over.
176 can iterate over.
177
177
178 Optional inputs:
178 Optional inputs:
179
179
180 - interact(False): if true, start to interact with the running
180 - interact(False): if true, start to interact with the running
181 program at the end of the script. Otherwise, just exit.
181 program at the end of the script. Otherwise, just exit.
182
182
183 - get_output(False): if true, capture the output of the child process
183 - get_output(False): if true, capture the output of the child process
184 (filtering the input commands out) and return it as a string.
184 (filtering the input commands out) and return it as a string.
185
185
186 Returns:
186 Returns:
187 A string containing the process output, but only if requested.
187 A string containing the process output, but only if requested.
188 """
188 """
189
189
190 # if the source is a string, chop it up in lines so we can iterate
190 # if the source is a string, chop it up in lines so we can iterate
191 # over it just as if it were an open file.
191 # over it just as if it were an open file.
192 if isinstance(source, basestring):
192 if isinstance(source, basestring):
193 source = source.splitlines(True)
193 source = source.splitlines(True)
194
194
195 if self.echo:
195 if self.echo:
196 # normalize all strings we write to use the native OS line
196 # normalize all strings we write to use the native OS line
197 # separators.
197 # separators.
198 linesep = os.linesep
198 linesep = os.linesep
199 stdwrite = self.out.write
199 stdwrite = self.out.write
200 write = lambda s: stdwrite(s.replace('\r\n',linesep))
200 write = lambda s: stdwrite(s.replace('\r\n',linesep))
201 else:
201 else:
202 # Quiet mode, all writes are no-ops
202 # Quiet mode, all writes are no-ops
203 write = lambda s: None
203 write = lambda s: None
204
204
205 c = self.child
205 c = self.child
206 prompts = c.compile_pattern_list(self.prompts)
206 prompts = c.compile_pattern_list(self.prompts)
207 prompt_idx = c.expect_list(prompts)
207 prompt_idx = c.expect_list(prompts)
208
208
209 # Flag whether the script ends normally or not, to know whether we can
209 # Flag whether the script ends normally or not, to know whether we can
210 # do anything further with the underlying process.
210 # do anything further with the underlying process.
211 end_normal = True
211 end_normal = True
212
212
213 # If the output was requested, store it in a list for return at the end
213 # If the output was requested, store it in a list for return at the end
214 if get_output:
214 if get_output:
215 output = []
215 output = []
216 store_output = output.append
216 store_output = output.append
217
217
218 for cmd in source:
218 for cmd in source:
219 # skip blank lines for all matches to the 'main' prompt, while the
219 # skip blank lines for all matches to the 'main' prompt, while the
220 # secondary prompts do not
220 # secondary prompts do not
221 if prompt_idx==0 and \
221 if prompt_idx==0 and \
222 (cmd.isspace() or cmd.lstrip().startswith('#')):
222 (cmd.isspace() or cmd.lstrip().startswith('#')):
223 write(cmd)
223 write(cmd)
224 continue
224 continue
225
225
226 # write('AFTER: '+c.after) # dbg
226 # write('AFTER: '+c.after) # dbg
227 write(c.after)
227 write(c.after)
228 c.send(cmd)
228 c.send(cmd)
229 try:
229 try:
230 prompt_idx = c.expect_list(prompts)
230 prompt_idx = c.expect_list(prompts)
231 except pexpect.EOF:
231 except pexpect.EOF:
232 # this will happen if the child dies unexpectedly
232 # this will happen if the child dies unexpectedly
233 write(c.before)
233 write(c.before)
234 end_normal = False
234 end_normal = False
235 break
235 break
236
236
237 write(c.before)
237 write(c.before)
238
238
239 # With an echoing process, the output we get in c.before contains
239 # With an echoing process, the output we get in c.before contains
240 # the command sent, a newline, and then the actual process output
240 # the command sent, a newline, and then the actual process output
241 if get_output:
241 if get_output:
242 store_output(c.before[len(cmd+'\n'):])
242 store_output(c.before[len(cmd+'\n'):])
243 #write('CMD: <<%s>>' % cmd) # dbg
243 #write('CMD: <<%s>>' % cmd) # dbg
244 #write('OUTPUT: <<%s>>' % output[-1]) # dbg
244 #write('OUTPUT: <<%s>>' % output[-1]) # dbg
245
245
246 self.out.flush()
246 self.out.flush()
247 if end_normal:
247 if end_normal:
248 if interact:
248 if interact:
249 c.send('\n')
249 c.send('\n')
250 print('<< Starting interactive mode >>', end=' ')
250 print('<< Starting interactive mode >>', end=' ')
251 try:
251 try:
252 c.interact()
252 c.interact()
253 except OSError:
253 except OSError:
254 # This is what fires when the child stops. Simply print a
254 # This is what fires when the child stops. Simply print a
255 # newline so the system prompt is aligned. The extra
255 # newline so the system prompt is aligned. The extra
256 # space is there to make sure it gets printed, otherwise
256 # space is there to make sure it gets printed, otherwise
257 # OS buffering sometimes just suppresses it.
257 # OS buffering sometimes just suppresses it.
258 write(' \n')
258 write(' \n')
259 self.out.flush()
259 self.out.flush()
260 else:
260 else:
261 if interact:
261 if interact:
262 e="Further interaction is not possible: child process is dead."
262 e="Further interaction is not possible: child process is dead."
263 print(e, file=sys.stderr)
263 print(e, file=sys.stderr)
264
264
265 # Leave the child ready for more input later on, otherwise select just
265 # Leave the child ready for more input later on, otherwise select just
266 # hangs on the second invocation.
266 # hangs on the second invocation.
267 if c.isalive():
267 if c.isalive():
268 c.send('\n')
268 c.send('\n')
269
269
270 # Return any requested output
270 # Return any requested output
271 if get_output:
271 if get_output:
272 return ''.join(output)
272 return ''.join(output)
273
273
274 def main(self,argv=None):
274 def main(self,argv=None):
275 """Run as a command-line script."""
275 """Run as a command-line script."""
276
276
277 parser = optparse.OptionParser(usage=USAGE % self.__class__.__name__)
277 parser = optparse.OptionParser(usage=USAGE % self.__class__.__name__)
278 newopt = parser.add_option
278 newopt = parser.add_option
279 newopt('-i','--interact',action='store_true',default=False,
279 newopt('-i','--interact',action='store_true',default=False,
280 help='Interact with the program after the script is run.')
280 help='Interact with the program after the script is run.')
281
281
282 opts,args = parser.parse_args(argv)
282 opts,args = parser.parse_args(argv)
283
283
284 if len(args) != 1:
284 if len(args) != 1:
285 print("You must supply exactly one file to run.", file=sys.stderr)
285 print("You must supply exactly one file to run.", file=sys.stderr)
286 sys.exit(1)
286 sys.exit(1)
287
287
288 self.run_file(args[0],opts.interact)
288 self.run_file(args[0],opts.interact)
289
289
290 _ipython_cmd = "ipython3" if py3compat.PY3 else "ipython"
291
290
292 # Specific runners for particular programs
291 # Specific runners for particular programs
293 class IPythonRunner(InteractiveRunner):
292 class IPythonRunner(InteractiveRunner):
294 """Interactive IPython runner.
293 """Interactive IPython runner.
295
294
296 This initalizes IPython in 'nocolor' mode for simplicity. This lets us
295 This initalizes IPython in 'nocolor' mode for simplicity. This lets us
297 avoid having to write a regexp that matches ANSI sequences, though pexpect
296 avoid having to write a regexp that matches ANSI sequences, though pexpect
298 does support them. If anyone contributes patches for ANSI color support,
297 does support them. If anyone contributes patches for ANSI color support,
299 they will be welcome.
298 they will be welcome.
300
299
301 It also sets the prompts manually, since the prompt regexps for
300 It also sets the prompts manually, since the prompt regexps for
302 pexpect need to be matched to the actual prompts, so user-customized
301 pexpect need to be matched to the actual prompts, so user-customized
303 prompts would break this.
302 prompts would break this.
304 """
303 """
305
304
306 def __init__(self,program = _ipython_cmd, args=None, out=sys.stdout, echo=True):
305 def __init__(self, program='<ipython>', args=None, out=sys.stdout, echo=True):
307 """New runner, optionally passing the ipython command to use."""
306 """New runner, optionally passing the ipython command to use."""
308 args0 = ['--colors=NoColor',
307 args0 = ['--colors=NoColor',
309 '--no-term-title',
308 '--no-term-title',
310 '--no-autoindent',
309 '--no-autoindent',
311 # '--quick' is important, to prevent loading default config:
310 # '--quick' is important, to prevent loading default config:
312 '--quick']
311 '--quick']
313 if args is None: args = args0
312 args = args0 + (args or [])
314 else: args = args0 + args
313
314 # Special case to launch IPython with current interpreter
315 if program == '<ipython>':
316 program = sys.executable
317 args = ['-m', 'IPython'] + args
318
315 prompts = [r'In \[\d+\]: ',r' \.*: ']
319 prompts = [r'In \[\d+\]: ',r' \.*: ']
316 InteractiveRunner.__init__(self,program,prompts,args,out,echo)
320 InteractiveRunner.__init__(self,program,prompts,args,out,echo)
317
321
318
322
319 class PythonRunner(InteractiveRunner):
323 class PythonRunner(InteractiveRunner):
320 """Interactive Python runner."""
324 """Interactive Python runner."""
321
325
322 def __init__(self,program=sys.executable, args=None, out=sys.stdout, echo=True):
326 def __init__(self,program=sys.executable, args=None, out=sys.stdout, echo=True):
323 """New runner, optionally passing the python command to use."""
327 """New runner, optionally passing the python command to use."""
324
328
325 prompts = [r'>>> ',r'\.\.\. ']
329 prompts = [r'>>> ',r'\.\.\. ']
326 InteractiveRunner.__init__(self,program,prompts,args,out,echo)
330 InteractiveRunner.__init__(self,program,prompts,args,out,echo)
327
331
328
332
329 class SAGERunner(InteractiveRunner):
333 class SAGERunner(InteractiveRunner):
330 """Interactive SAGE runner.
334 """Interactive SAGE runner.
331
335
332 WARNING: this runner only works if you manually adjust your SAGE
336 WARNING: this runner only works if you manually adjust your SAGE
333 configuration so that the 'color' option in the configuration file is set to
337 configuration so that the 'color' option in the configuration file is set to
334 'NoColor', because currently the prompt matching regexp does not identify
338 'NoColor', because currently the prompt matching regexp does not identify
335 color sequences."""
339 color sequences."""
336
340
337 def __init__(self,program='sage',args=None,out=sys.stdout,echo=True):
341 def __init__(self,program='sage',args=None,out=sys.stdout,echo=True):
338 """New runner, optionally passing the sage command to use."""
342 """New runner, optionally passing the sage command to use."""
339
343
340 prompts = ['sage: ',r'\s*\.\.\. ']
344 prompts = ['sage: ',r'\s*\.\.\. ']
341 InteractiveRunner.__init__(self,program,prompts,args,out,echo)
345 InteractiveRunner.__init__(self,program,prompts,args,out,echo)
342
346
343
347
344 class RunnerFactory(object):
348 class RunnerFactory(object):
345 """Code runner factory.
349 """Code runner factory.
346
350
347 This class provides an IPython code runner, but enforces that only one
351 This class provides an IPython code runner, but enforces that only one
348 runner is ever instantiated. The runner is created based on the extension
352 runner is ever instantiated. The runner is created based on the extension
349 of the first file to run, and it raises an exception if a runner is later
353 of the first file to run, and it raises an exception if a runner is later
350 requested for a different extension type.
354 requested for a different extension type.
351
355
352 This ensures that we don't generate example files for doctest with a mix of
356 This ensures that we don't generate example files for doctest with a mix of
353 python and ipython syntax.
357 python and ipython syntax.
354 """
358 """
355
359
356 def __init__(self,out=sys.stdout):
360 def __init__(self,out=sys.stdout):
357 """Instantiate a code runner."""
361 """Instantiate a code runner."""
358
362
359 self.out = out
363 self.out = out
360 self.runner = None
364 self.runner = None
361 self.runnerClass = None
365 self.runnerClass = None
362
366
363 def _makeRunner(self,runnerClass):
367 def _makeRunner(self,runnerClass):
364 self.runnerClass = runnerClass
368 self.runnerClass = runnerClass
365 self.runner = runnerClass(out=self.out)
369 self.runner = runnerClass(out=self.out)
366 return self.runner
370 return self.runner
367
371
368 def __call__(self,fname):
372 def __call__(self,fname):
369 """Return a runner for the given filename."""
373 """Return a runner for the given filename."""
370
374
371 if fname.endswith('.py'):
375 if fname.endswith('.py'):
372 runnerClass = PythonRunner
376 runnerClass = PythonRunner
373 elif fname.endswith('.ipy'):
377 elif fname.endswith('.ipy'):
374 runnerClass = IPythonRunner
378 runnerClass = IPythonRunner
375 else:
379 else:
376 raise ValueError('Unknown file type for Runner: %r' % fname)
380 raise ValueError('Unknown file type for Runner: %r' % fname)
377
381
378 if self.runner is None:
382 if self.runner is None:
379 return self._makeRunner(runnerClass)
383 return self._makeRunner(runnerClass)
380 else:
384 else:
381 if runnerClass==self.runnerClass:
385 if runnerClass==self.runnerClass:
382 return self.runner
386 return self.runner
383 else:
387 else:
384 e='A runner of type %r can not run file %r' % \
388 e='A runner of type %r can not run file %r' % \
385 (self.runnerClass,fname)
389 (self.runnerClass,fname)
386 raise ValueError(e)
390 raise ValueError(e)
387
391
388
392
389 # Global usage string, to avoid indentation issues if typed in a function def.
393 # Global usage string, to avoid indentation issues if typed in a function def.
390 MAIN_USAGE = """
394 MAIN_USAGE = """
391 %prog [options] file_to_run
395 %prog [options] file_to_run
392
396
393 This is an interface to the various interactive runners available in this
397 This is an interface to the various interactive runners available in this
394 module. If you want to pass specific options to one of the runners, you need
398 module. If you want to pass specific options to one of the runners, you need
395 to first terminate the main options with a '--', and then provide the runner's
399 to first terminate the main options with a '--', and then provide the runner's
396 options. For example:
400 options. For example:
397
401
398 irunner.py --python -- --help
402 irunner.py --python -- --help
399
403
400 will pass --help to the python runner. Similarly,
404 will pass --help to the python runner. Similarly,
401
405
402 irunner.py --ipython -- --interact script.ipy
406 irunner.py --ipython -- --interact script.ipy
403
407
404 will run the script.ipy file under the IPython runner, and then will start to
408 will run the script.ipy file under the IPython runner, and then will start to
405 interact with IPython at the end of the script (instead of exiting).
409 interact with IPython at the end of the script (instead of exiting).
406
410
407 The already implemented runners are listed below; adding one for a new program
411 The already implemented runners are listed below; adding one for a new program
408 is a trivial task, see the source for examples.
412 is a trivial task, see the source for examples.
409 """
413 """
410
414
411 def main():
415 def main():
412 """Run as a command-line script."""
416 """Run as a command-line script."""
413
417
414 parser = optparse.OptionParser(usage=MAIN_USAGE)
418 parser = optparse.OptionParser(usage=MAIN_USAGE)
415 newopt = parser.add_option
419 newopt = parser.add_option
416 newopt('--ipython',action='store_const',dest='mode',const='ipython',
420 newopt('--ipython',action='store_const',dest='mode',const='ipython',
417 help='IPython interactive runner (default).')
421 help='IPython interactive runner (default).')
418 newopt('--python',action='store_const',dest='mode',const='python',
422 newopt('--python',action='store_const',dest='mode',const='python',
419 help='Python interactive runner.')
423 help='Python interactive runner.')
420 newopt('--sage',action='store_const',dest='mode',const='sage',
424 newopt('--sage',action='store_const',dest='mode',const='sage',
421 help='SAGE interactive runner.')
425 help='SAGE interactive runner.')
422
426
423 opts,args = parser.parse_args()
427 opts,args = parser.parse_args()
424 runners = dict(ipython=IPythonRunner,
428 runners = dict(ipython=IPythonRunner,
425 python=PythonRunner,
429 python=PythonRunner,
426 sage=SAGERunner)
430 sage=SAGERunner)
427
431
428 try:
432 try:
429 ext = os.path.splitext(args[0])[-1]
433 ext = os.path.splitext(args[0])[-1]
430 except IndexError:
434 except IndexError:
431 ext = ''
435 ext = ''
432 modes = {'.ipy':'ipython',
436 modes = {'.ipy':'ipython',
433 '.py':'python',
437 '.py':'python',
434 '.sage':'sage'}
438 '.sage':'sage'}
435 mode = modes.get(ext,"ipython")
439 mode = modes.get(ext,"ipython")
436 if opts.mode:
440 if opts.mode:
437 mode = opts.mode
441 mode = opts.mode
438 runners[mode]().main(args)
442 runners[mode]().main(args)
439
443
440 if __name__ == '__main__':
444 if __name__ == '__main__':
441 main()
445 main()
General Comments 0
You need to be logged in to leave comments. Login now