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