ó
æ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 m Z d d l m Z d „  Z d	 e f d
 „  ƒ  YZ e ƒ  j Z d „  Z d S(   s}   Posix-specific implementation of process utilities.

This file is only meant to be imported by process.py, not by end-users.
iÿÿÿÿ(   t   print_functionNi   (   t	   getoutputt	   arg_split(   t	   py3compat(   t   DEFAULT_ENCODINGc         C  sA   t  j d d |  g d t  j d t  j ƒj ƒ  d } t j | ƒ S(   s,   Find the full path to a command using which.s   /usr/bin/envt   whicht   stdoutt   stderri    (   t   spt   Popent   PIPEt   communicateR   t   bytes_to_str(   t   cmdt   path(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyt	   _find_cmd#   s    t   ProcessHandlerc           B  sb   e  Z d  Z d Z d Z d Z d Z e d „  ƒ Z	 d d d d „ Z
 d „  Z d „  Z d „  Z RS(	   s7   Execute subprocesses under the control of pexpect.
    gš™™™™™©?gš™™™™™É?c         C  sI   |  j  d  k rB t j d ƒ |  _  |  j  d  k rB t d ƒ ‚ qB n  |  j  S(   Nt   shs   "sh" shell not found(   t   _sht   Nonet   pexpectR   t   OSError(   t   self(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyR   =   s
    c         C  s^   | d k r t j n | |  _ | d k r3 t j n | |  _ | d k rQ t j n | |  _ d S(   s%   Arguments are used for pexpect calls.N(   R   R   t   read_timeoutt   terminate_timeoutt   sysR   t   logfile(   R   R   R   R   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyt   __init__F   s
    	c         C  s]   y, t  j |  j d d | g ƒj d d ƒ SWn* t k
 rX t d d t j d d ƒn Xd	 S(
   s  Run a command and return its stdout/stderr as a string.

        Parameters
        ----------
        cmd : str
          A command to be executed in the system shell.

        Returns
        -------
        output : str
          A string containing the combination of stdout and stderr from the
        subprocess, in whatever order the subprocess originally wrote to its
        file descriptors (so the order of the information in this string is the
        correct order as would be seen if running the command in a terminal).
        t   argss   -cs   
s   
s   ^Ct   filet   endt    N(   R   t   runR   t   replacet   KeyboardInterruptt   printR   R   (   R   R   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyR   O   s    ,c         C  s]   y, t  j |  j d d | g ƒj d d ƒ SWn* t k
 rX t d d t j d d ƒn Xd	 S(
   s  Run a command and return its stdout/stderr as a string.

        Parameters
        ----------
        cmd : str
          A command to be executed in the system shell.

        Returns
        -------
        output : str
          A string containing the combination of stdout and stderr from the
        subprocess, in whatever order the subprocess originally wrote to its
        file descriptors (so the order of the information in this string is the
        correct order as would be seen if running the command in a terminal).
        R   s   -cs   
s   
s   ^CR   R   R   N(   R   R    R   R!   R"   R#   R   R   (   R   R   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyt   getoutput_pexpectd   s    ,c   	      C  sø  t  } t j t j g } | j t j ƒ } d } yÉ t t d ƒ rc t j |  j d d | g ƒ} n t j |  j d d | g ƒ} t	 j
 j } xh t r÷ | j | |  j ƒ } t | j | j | d ƒ d d ƒ| ƒ  | | k rå Pn  t | j ƒ } q WWn¦ t k
 r¡| j t d ƒ ƒ zn yV t | j ƒ } | j | |  j ƒ t | j | j | d ƒ d d ƒt	 j
 j ƒ  Wn t k
 rˆn XWd	 | j d
 t ƒ Xn X| j ƒ  | j d	 k rÖ| j d	 k rÎd S| j S| j d k rñ| j d S| j S(   sÜ   Execute a command in a subshell.

        Parameters
        ----------
        cmd : str
          A command to be executed in the system shell.

        Returns
        -------
        int : child's exitstatus
        i    t   spawnbR   s   -cR!   R   R   i   Nt   forcei€   (   R   R   t   TIMEOUTt   EOFt   indext   hasattrR%   R   t   spawnR   R   t   flusht   Truet   expect_listR   R#   t   beforet   decodet   lenR"   t   sendlinet   chrR   t	   terminatet   isalivet
   exitstatusR   t   signalstatus(	   R   R   t   enct   patternst	   EOF_indext   out_sizet   childR,   t   res_idx(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyt   systemy   sF    !	# #
N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   t   propertyR   R   R   R$   R>   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyR   +   s   				c         C  sa   y t  j |  d ƒ WnB t k
 rX } | j t j k r< t S| j t j k rR t S‚  n Xt Sd  S(   Ni    (   t   ost   killR   t   errnot   ESRCHt   Falset   EPERMR-   (   t   pidt   err(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyt	   check_pidÖ   s    (   RA   t
   __future__R    RE   RC   t
   subprocessR   R   R   t   _process_commonR   R   t   IPython.utilsR   t   IPython.utils.encodingR   R   t   objectR   R>   RK   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_posix.pyt   <module>   s   	©