;ò
ÎÑAHc           @   sÙ   d  Z  d k Z d k Z d k Z d k Z d Z d „  Z e ƒ  d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d	 „  ƒ  YZ
 d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d Z d „  Z e d j o e ƒ  n d S(   sð  Module for interactively running scripts.

This module implements classes for interactively running scripts written for
any system with a prompt which can be matched by a regexp suitable for
pexpect.  It can be used to run as if they had been typed up interactively, an
arbitrary series of commands for the target system.

The module includes classes ready for IPython (with the default prompts),
plain Python and SAGE, but making a new one is trivial.  To see how to use it,
simply run the module as a script:

./irunner.py --help


This is an extension of Ken Schutte <kschutte-AT-csail.mit.edu>'s script
contributed on the ipython-user list:

http://scipy.net/pipermail/ipython-user/2006-May/001705.html


NOTES:

 - This module requires pexpect, available in most linux distros, or which can
 be downloaded from

    http://pexpect.sourceforge.net

 - Because pexpect only works under Unix or Windows-Cygwin, this has the same
 limitations.  This means that it will NOT work under native windows Python.
Ns@   
Interactive script runner, type: %s

runner [opts] script_name
c          C   s5   t  i d  d j o d Sn d „  }  |  t  i _ d S(   so  Patch pexpect to prevent unhandled exceptions at VM teardown.

    Calling this function will monkeypatch the pexpect.spawn class and modify
    its __del__ method to make it more robust in the face of failures that can
    occur if it is called when the Python VM is shutting down.

    Since Python may fire __del__ methods arbitrarily late, it's possible for
    them to execute during the teardown of the Python VM itself.  At this
    point, various builtin modules have been reset to None.  Thus, the call to
    self.close() will trigger an exception because it tries to call os.close(),
    and os is now None.
    i   s   2.2Nc         C   s7   |  i o( y |  i ƒ  Wq3 t j
 o q3 Xn d S(   sT  This makes sure that no system resources are left open.
        Python only garbage collects Python objects. OS file descriptors
        are not Python objects, so they must be handled explicitly.
        If the child file descriptor was opened outside of this class
        (passed to the constructor) then this does not close it.
        N(   s   selfs   closeds   closes   AttributeError(   s   self(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   __del__B   s     (   s   pexpects   __version__s   __del__s   spawn(   s   __del__(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   pexpect_monkeypatch0   s
     	s   InteractiveRunnerc           B   sV   t  Z d  Z e e i e d „ Z d „  Z e	 e	 d „ Z
 e	 e	 d „ Z e d „ Z RS(   sC   Class to run a sequence of commands through an interactive program.c         C   s’   | |  _  | |  _ | t j o
 g  } n | |  _ | |  _ | |  _ d |  _ t i	 |  i  |  i d t ƒ} |  _ |  i | _ | i d d ƒ d S(   sd  Construct a runner.

        Inputs:

          - program: command to execute the given program.

          - prompts: a list of patterns to match as valid prompts, in the
          format used by pexpect.  This basically means that it can be either
          a string (to be compiled as a regular expression) or a list of such
          (it must be a true list, as pexpect does type checks).

          If more than one prompt is given, the first is treated as the main
          program prompt and the others as 'continuation' prompts, like
          python's.  This means that blank lines in the input source are
          ommitted when the first prompt is matched, but are NOT ommitted when
          the continuation one matches, since this is how python signals the
          end of multiline input interactively.

        Optional inputs:

          - args(None): optional list of strings to pass as arguments to the
          child program.

          - out(sys.stdout): if given, an output stream to be used when writing
          output.  The only requirement is that it must have a .write() method.

        Public members not parameterized in the constructor:

          - delaybeforesend(0): Newer versions of pexpect have a delay before
          sending each new input.  For our purposes here, it's typically best
          to just set this to zero, but if you encounter reliability problems
          or want an interactive run to pause briefly at each prompt, just
          increase this value (it is measured in seconds).  Note that this
          variable is not honored at all by older versions of pexpect.
        i    s   timeoutic   iÈ   N(   s   programs   selfs   promptss   argss   Nones   outs   echos   delaybeforesends   pexpects   spawns   cs   childs
   setwinsize(   s   selfs   programs   promptss   argss   outs   echos   c(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   __init__W   s    # 		 
				%c         C   s   |  i i ƒ  d S(   s   close child processN(   s   selfs   childs   close(   s   self(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   close’   s     c         C   sI   t  | d ƒ } z |  i | | | ƒ } Wd | i ƒ  X| o | Sn d S(   s»   Run the given file interactively.

        Inputs:

          -fname: name of the file to execute.

        See the run_source docstring for the meaning of the optional
        arguments.s   rN(	   s   opens   fnames   fobjs   selfs
   run_sources   interacts
   get_outputs   outs   close(   s   selfs   fnames   interacts
   get_outputs   fobjs   out(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   run_file—   s      c            s>  t  | t ƒ o | i t ƒ } n |  i o( t i ‰  |  i	 i
 ‰ ‡ ‡  d †  } n
 d „  } |  i } | i |  i ƒ } | i | ƒ } t }	 | o g  } | i } n xÜ | D]Ô }
 | d j o  |
 i ƒ  p |
 i ƒ  i d ƒ o | |
 ƒ q° n | | i ƒ | i |
 ƒ y | i | ƒ } Wn* t i j
 o | | i ƒ t  }	 Pn X| | i ƒ | o | | i t! |
 d ƒ ƒ q° q° W|  i	 i" ƒ  |	 o[ | oP | i d ƒ d Gy | i# ƒ  Wqót$ j
 o | d ƒ |  i	 i" ƒ  qóXqn | o d } t& i' | IJn | i d ƒ | o d	 i( | ƒ Sn d
 S(   sK  Run the given source code interactively.

        Inputs:

          - source: a string of code to be executed, or an open file object we
          can iterate over.

        Optional inputs:

          - interact(False): if true, start to interact with the running
          program at the end of the script.  Otherwise, just exit.

          - get_output(False): if true, capture the output of the child process
          (filtering the input commands out) and return it as a string.

        Returns:
          A string containing the process output, but only if requested.
          c            s   ˆ  |  i d ˆ ƒ ƒ S(   Ns   
(   s   stdwrites   ss   replaces   linesep(   s   s(   s   stdwrites   linesep(    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   <lambda>Ç   s    c         C   s   t  S(   N(   s   None(   s   s(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   <lambda>Ê   s    i    s   #s   
s   << Starting interactive mode >>s    
s;   Further interaction is not possible: child process is dead.s    N()   s
   isinstances   sources   files
   splitliness   Trues   selfs   echos   oss   lineseps   outs   writes   stdwrites   childs   cs   compile_pattern_lists   promptss   expect_lists
   prompt_idxs
   end_normals
   get_outputs   outputs   appends   store_outputs   cmds   isspaces   lstrips
   startswiths   afters   sends   pexpects   EOFs   befores   Falses   lens   flushs   interacts   OSErrors   es   syss   stderrs   join(   s   selfs   sources   interacts
   get_outputs   lineseps   writes   stdwrites   es   cs
   end_normals   cmds   promptss
   prompt_idxs   store_outputs   output(    (   s   lineseps   stdwrites@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys
   run_source©   s^     
			 0
#
c      	   C   s¥   t  i d t |  i i ƒ } | i } | d d d d d t	 d d ƒ| i
 | ƒ \ } } t | ƒ d	 j o t i d
 IJt i d	 ƒ n |  i | d | i ƒ d S(   s   Run as a command-line script.s   usages   -is
   --interacts   actions
   store_trues   defaults   helps2   Interact with the program after the script is run.i   s(   You must supply exactly one file to run.i    N(   s   optparses   OptionParsers   USAGEs   selfs	   __class__s   __name__s   parsers
   add_options   newopts   Falses
   parse_argss   argvs   optss   argss   lens   syss   stderrs   exits   run_files   interact(   s   selfs   argvs   newopts   argss   parsers   opts(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   main  s     	
(   s   __name__s
   __module__s   __doc__s   Nones   syss   stdouts   Trues   __init__s   closes   Falses   run_files
   run_sources   main(    (    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   InteractiveRunnerT   s    ;	gs   IPythonRunnerc           B   s&   t  Z d  Z d e e i e d „ Z RS(   sÐ  Interactive IPython runner.

    This initalizes IPython in 'nocolor' mode for simplicity.  This lets us
    avoid having to write a regexp that matches ANSI sequences, though pexpect
    does support them.  If anyone contributes patches for ANSI color support,
    they will be welcome.

    It also sets the prompts manually, since the prompt regexps for
    pexpect need to be matched to the actual prompts, so user-customized
    prompts would break this.
    s   ipythonc         C   sk   d d d d d d d d g } | t j o
 | } n | | } d	 d
 g } t i |  | | | | | ƒ d S(   s:   New runner, optionally passing the ipython command to use.s   -colorss   NoColors   -pi1s	   In [\#]: s   -pi2s	      .\D.: s   -noterm_titles   -noautoindents   In \[\d+\]: s      \.*: N(
   s   args0s   argss   Nones   promptss   InteractiveRunners   __init__s   selfs   programs   outs   echo(   s   selfs   programs   argss   outs   echos   promptss   args0(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   __init__/  s      

(   s   __name__s
   __module__s   __doc__s   Nones   syss   stdouts   Trues   __init__(    (    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   IPythonRunner"  s    s   PythonRunnerc           B   s&   t  Z d  Z d e e i e d „ Z RS(   s   Interactive Python runner.s   pythonc         C   s,   d d g } t i |  | | | | | ƒ d S(   s9   New runner, optionally passing the python command to use.s   >>> s   \.\.\. N(   s   promptss   InteractiveRunners   __init__s   selfs   programs   argss   outs   echo(   s   selfs   programs   argss   outs   echos   prompts(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   __init__@  s     (   s   __name__s
   __module__s   __doc__s   Nones   syss   stdouts   Trues   __init__(    (    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   PythonRunner=  s    s
   SAGERunnerc           B   s&   t  Z d  Z d e e i e d „ Z RS(   sö   Interactive SAGE runner.
    
    WARNING: this runner only works if you manually configure your SAGE copy
    to use 'colors NoColor' in the ipythonrc config file, since currently the
    prompt matching regexp does not identify color sequences.s   sagec         C   s,   d d g } t i |  | | | | | ƒ d S(   s7   New runner, optionally passing the sage command to use.s   sage: s
   \s*\.\.\. N(   s   promptss   InteractiveRunners   __init__s   selfs   programs   argss   outs   echo(   s   selfs   programs   argss   outs   echos   prompts(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   __init__N  s     (   s   __name__s
   __module__s   __doc__s   Nones   syss   stdouts   Trues   __init__(    (    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys
   SAGERunnerG  s    s   RunnerFactoryc           B   s/   t  Z d  Z e i d „ Z d „  Z d „  Z RS(   s   Code runner factory.

    This class provides an IPython code runner, but enforces that only one
    runner is ever instantiated.  The runner is created based on the extension
    of the first file to run, and it raises an exception if a runner is later
    requested for a different extension type.

    This ensures that we don't generate example files for doctest with a mix of
    python and ipython syntax.
    c         C   s   | |  _  t |  _ t |  _ d S(   s   Instantiate a code runner.N(   s   outs   selfs   Nones   runners   runnerClass(   s   selfs   out(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   __init__a  s     		c         C   s)   | |  _  | d |  i ƒ |  _ |  i Sd  S(   Ns   out(   s   runnerClasss   selfs   outs   runner(   s   selfs   runnerClass(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   _makeRunnerh  s    	c         C   s£   | i d ƒ o
 t } n+ | i d ƒ o
 t } n t d | ƒ ‚ |  i t j o |  i	 | ƒ Sn; | |  i j o |  i Sn  d |  i | f } t | ƒ ‚ d S(   s'   Return a runner for the given filename.s   .pys   .ipys    Unknown file type for Runner: %rs'   A runner of type %r can not run file %rN(   s   fnames   endswiths   PythonRunners   runnerClasss   IPythonRunners
   ValueErrors   selfs   runners   Nones   _makeRunners   e(   s   selfs   fnames   es   runnerClass(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   __call__m  s     

(   s   __name__s
   __module__s   __doc__s   syss   stdouts   __init__s   _makeRunners   __call__(    (    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   RunnerFactoryU  s   
 	s„  
%prog [options] file_to_run

This is an interface to the various interactive runners available in this
module.  If you want to pass specific options to one of the runners, you need
to first terminate the main options with a '--', and then provide the runner's
options.  For example:

irunner.py --python -- --help

will pass --help to the python runner.  Similarly,

irunner.py --ipython -- --interact script.ipy

will run the script.ipy file under the IPython runner, and then will start to
interact with IPython at the end of the script (instead of exiting).

The already implemented runners are listed below; adding one for a new program
is a trivial task, see the source for examples.

WARNING: the SAGE runner only works if you manually configure your SAGE copy
to use 'colors NoColor' in the ipythonrc config file, since currently the
prompt matching regexp does not identify color sequences.
c       
   C   sF  t  i d t ƒ } | i } | i d d ƒ | d d d d d d d d	 d
 ƒ| d d d d d d d d	 d ƒ| d d d d d d d d	 d ƒ| i ƒ  \ } }  t
 d t d t d t ƒ } y t i i |  d ƒ d } Wn t j
 o d } n Xh  d d <d d <d d <} | i | | i ƒ } | | ƒ  i |  ƒ d S(   s   Run as a command-line script.s   usages   modes   ipythons	   --ipythons   actions   store_consts   dests   consts   helps%   IPython interactive runner (default).s   --pythons   pythons   Python interactive runner.s   --sages   sages   SAGE interactive runner.i    iÿÿÿÿs    s   .ipys   .pys   .sageN(   s   optparses   OptionParsers
   MAIN_USAGEs   parsers
   add_options   newopts   set_defaultss
   parse_argss   optss   argss   dicts   IPythonRunners   PythonRunners
   SAGERunners   runnerss   oss   paths   splitexts   exts
   IndexErrors   modess   gets   modes   main(   s   argss   modess   parsers   modes   exts   runnerss   optss   newopt(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   mainœ  s*     	


	!s   __main__(   s   __doc__s   optparses   oss   syss   pexpects   USAGEs   pexpect_monkeypatchs   objects   InteractiveRunners   IPythonRunners   PythonRunners
   SAGERunners   RunnerFactorys
   MAIN_USAGEs   mains   __name__(   s   PythonRunners
   MAIN_USAGEs   InteractiveRunners   pexpects   optparses
   SAGERunners   IPythonRunners   syss   USAGEs   RunnerFactorys   pexpect_monkeypatchs   mains   os(    (    s@   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/irunner.pys   ?   s    					!Î
.	