ó
æNXc           @  s%  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m	 Z	 m
 Z
 d d l Z d d l m Z d d l m Z d d l m Z m Z m Z m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z m Z d „  Z e d e f d „  ƒ  Yƒ Z d S(   s5   Magic functions for running cells in various scripts.iÿÿÿÿ(   t   print_functionN(   t   Popent   PIPE(   t   Configurable(   t   magic_arguments(   t   Magicst   magics_classt
   line_magict
   cell_magic(   t   BackgroundJobManager(   t	   py3compat(   t	   arg_split(   t   Listt   Dictc      	   C  s‡   t  j d d t d d ƒt  j d d t d d ƒt  j d d d	 d d
 ƒt  j d d t d d ƒg } x | D] } | |  ƒ }  qm W|  S(   s'   single decorator for adding script argss   --outt   typet   helps½   The variable in which to store stdout from the script.
            If the script is backgrounded, this will be the stdout *pipe*,
            instead of the stderr text itself.
            s   --errs½   The variable in which to store stderr from the script.
            If the script is backgrounded, this will be the stderr *pipe*,
            instead of the stderr text itself.
            s   --bgt   actiont
   store_truesœ   Whether to run the script in the background.
            If given, the only way to see the output of the command is
            with --out/err.
            s   --procst   The variable in which to store Popen instance.
            This is used only when --bg option is given.
            (   R   t   argumentt   str(   t   ft   argst   arg(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyt   script_args'   s    t   ScriptMagicsc           B  sÂ   e  Z d  Z e d e d d ƒ Z d „  Z e d e d d ƒ Z d d „ Z
 d „  Z d „  Z d	 „  Z e j ƒ  e e d
 ƒ d „  ƒ ƒ ƒ Z d „  Z e d ƒ d d „ ƒ Z d „  Z d „  Z RS(   sâ   Magics for talking to scripts
    
    This defines a base `%%script` cell magic for running a cell
    with a program in a subprocess, and registers a few top-level
    magics that call %%script with common interpreters.
    t   configR   sê   Extra script cell magics to define
        
        This generates simple wrappers of `%%script foo` as `%%foo`.
        
        If you want to add script magics that aren't on your path,
        specify them in script_paths
        c         C  sD   d d d d d d d d g } t  j d	 k r@ | j d
 g ƒ n  | S(   s$   default to a common list of programst   sht   basht   perlt   rubyt   pythont   python2t   python3t   pypyt   ntt   cmd(   t   ost   namet   extend(   t   selft   defaults(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyt   _script_magics_default[   s    	sÕ   Dict mapping short 'ruby' names to full paths, such as '/opt/secret/bin/ruby'
        
        Only necessary for items in script_magics where the default path will not
        find the right interpreter.
        c         C  sL   t  t |  ƒ j d | ƒ |  j ƒ  t ƒ  |  _ g  |  _ t j |  j	 ƒ d  S(   Nt   shell(
   t   superR   t   __init__t   _generate_script_magicsR	   t   job_managert   bg_processest   atexitt   registert   kill_bg_processes(   R'   R*   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyR,   w   s
    
	c         C  s   |  j  ƒ  d  S(   N(   R2   (   R'   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyt   __del__~   s    c         C  s8   |  j  d } x$ |  j D] } |  j | ƒ | | <q Wd  S(   Nt   cell(   t   magicst   script_magicst   _make_script_magic(   R'   t   cell_magicsR%   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyR-      s    c           sR   ˆ j  j | | ƒ ‰  t j ƒ  t ‡ ‡  f d †  ƒ ƒ } d j t ƒ    | _ | S(   sA   make a named magic, that calls %%script with a particular programc           s/   |  r d ˆ |  f }  n ˆ }  ˆ  j  |  | ƒ S(   Ns   %s %s(   t   shebang(   t   lineR4   (   R'   t   script(    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyt   named_script_magic‹   s    s”   %%{name} script magic
        
        Run cells with {script} in a subprocess.
        
        This is a shortcut for `%%script {script}`
        (   t   script_pathst   getR   R   t   formatt   localst   __doc__(   R'   R%   R<   (    (   R;   R'   sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyR7   †   s
    	R;   c   
      C  s  t  | d t j j d ƒ ƒ} |  j j j | ƒ \ } } y" t | d t d t d t ƒ} Wn> t	 k
 rœ } | j
 t
 j k r– t d | d ƒ d S‚  n X| j d	 ƒ s¹ | d	 7} n  | j d
 d ƒ } | j rt|  j j | ƒ |  j ƒ  | j r| j |  j j | j <n  | j r2| j |  j j | j <n  |  j j |  j | | d t ƒ| j rp| |  j j | j <n  d Sy | j | ƒ \ } }	 WnÛ t k
 rjyŒ | j  t! j" ƒ t# j$ d ƒ | j% ƒ  d k	 rÝt d ƒ d S| j' ƒ  t# j$ d ƒ | j% ƒ  d k	 rt d ƒ d S| j( ƒ  t d ƒ Wn: t	 k
 r<n* t) k
 re} t d | j* | f ƒ n Xd SXt+ j, | ƒ } t+ j, |	 ƒ }	 | j r¨| |  j j | j <n t j j- | ƒ t j j. ƒ  | j rä|	 |  j j | j <n t j j- |	 ƒ t j j. ƒ  d S(   sÛ  Run a cell via a shell command
        
        The `%%script` line is like the #! line of script,
        specifying a program (bash, perl, ruby, etc.) with which to run.
        
        The rest of the cell is run by that program.
        
        Examples
        --------
        ::
        
            In [1]: %%script bash
               ...: for i in 1 2 3; do
               ...:   echo $i
               ...: done
            1
            2
            3
        t   posixt   wint   stdoutt   stderrt   stdins   Couldn't find program: %ri    Ns   
t   utf8t   replacet   daemongš™™™™™¹?s   Process is interrupted.s   Process is terminated.s   Process is killed.s/   Error while terminating subprocess (pid=%i): %s(/   R   t   syst   platformt
   startswithR9   t   parsert   parse_known_argsR   R   t   OSErrort   errnot   ENOENTt   printt   endswitht   encodet   bgR/   t   appendt   _gc_bg_processest   outRD   R*   t   user_nst   errRE   R.   t   newt   _run_scriptt   Truet   proct   communicatet   KeyboardInterruptt   send_signalt   signalt   SIGINTt   timet   sleept   pollt   Nonet	   terminatet   killt	   Exceptiont   pidR
   t   bytes_to_strt   writet   flush(
   R'   R:   R4   t   argvR   R#   t   pt   eRX   RZ   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyR9       sn    "	
			



		c         C  s+   | j  j | ƒ | j  j ƒ  | j ƒ  d S(   s1   callback for running the script in the backgroundN(   RF   Rm   t   closet   wait(   R'   Rp   R4   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyR\   õ   s    t   killbgscriptst    c         C  s   |  j  ƒ  t d ƒ d S(   s9   Kill all BG processes started by %%script and its family.s%   All background processes were killed.N(   R2   RR   (   R'   t   _nouse_(    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyRt   û   s    
c         C  sñ   xD |  j  D]9 } | j ƒ  d k r
 y | j t j ƒ WqC qC Xq
 q
 Wt j d ƒ x> |  j  D]3 } | j ƒ  d k r^ y | j ƒ  Wq‘ q‘ Xq^ q^ Wt j d ƒ x> |  j  D]3 } | j ƒ  d k r¬ y | j	 ƒ  Wqß qß Xq¬ q¬ W|  j
 ƒ  d S(   s.   Kill all BG processes which are still running.gš™™™™™¹?N(   R/   Rf   Rg   Ra   Rb   Rc   Rd   Re   Rh   Ri   RW   (   R'   Rp   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyR2     s*    c         C  s5   g  |  j  D] } | j ƒ  d  k r
 | ^ q
 |  _  d  S(   N(   R/   Rf   Rg   (   R'   Rp   (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyRW     s    N(   t   __name__t
   __module__RA   R   R]   R6   R)   R   R=   Rg   R,   R3   R-   R7   R   R   R   R9   R\   R   Rt   R2   RW   (    (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyR   J   s$   							S			(    RA   t
   __future__R    RP   R$   RJ   Rb   Rd   t
   subprocessR   R   R0   t   traitlets.config.configurableR   t   IPython.coreR   t   IPython.core.magicR   R   R   R   t   IPython.lib.backgroundjobsR	   t   IPython.utilsR
   t   IPython.utils.processR   t	   traitletsR   R   R   R   (    (    (    sR   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/magics/script.pyt   <module>   s$   "	#