ó
ã™xTc           @   s:  d  d l  m Z d  d l Z e j d d k r> d  d l Z nX y d  d l m Z WnA e k
 r• y d  d l Z Wq– e k
 r‘ d  d l Z q– Xn Xd  d l Z d  d l Z d „  Z	 d e
 f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ e Z d d „ Z d „  Z d d „ Z e d k r6e ƒ  n  d S(   iÿÿÿÿ(   t   divisionNi   i   (   t   subprocess_with_fixesc         C   s;   | d  k r t j } n  x |  D] } | | | IJq Wd  S(   N(   t   Nonet   syst   stdout(   t   linest   outt   prefixt   line(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   _show_lines   s     t   fully_buffered_basec           B   sV   e  Z d  „  Z e d „ Z d e d „ Z e d „ Z d d d „ Z d d d „ Z	 RS(	   c         C   s~   |  j  s t ‚ t |  j ƒ d k rz d g } | j d t |  j ƒ ƒ x" |  j D] } | j d | ƒ qR Wd j | ƒ Sd  S(   Ni    s   child process stderr output:s     command: s     s   
(	   t   join_stdout_stderrt   AssertionErrort   lent   stderr_linest   appendt   reprt   commandt   joinR   (   t   selft   msgR   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   format_errors_if_any   s    	c         C   s;   |  j  s t ‚ |  j ƒ  } | d  k	 r7 | | ƒ ‚ n  |  S(   N(   R   R   R   R   (   R   t   ErrorR   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   raise_if_errors!   s
    i
   c            sS  ‡  f d †  } ˆ  j  d  k	 rq t ˆ  j  ƒ d k rO| ƒ  } | j d t ˆ  j  ƒ ƒ | d j | ƒ ƒ ‚ qOnÞ t ˆ  j ƒ d k rO| ƒ  } x& ˆ  j |  D] } | j d | ƒ q� Wt ˆ  j ƒ } | | k r7| | d k rxN ˆ  j | | !D] } | j d | ƒ qô Wq7| j d ƒ | j d | | ƒ n  | d j | ƒ ƒ ‚ n  ˆ  S(	   Nc             s'   d g }  |  j  d t ˆ  j ƒ ƒ |  S(   Ns    unexpected child process output:s     command: (   R   R   R   (   t   result(   R   (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt	   start_msg)   s    	i    s     length of output: %d bytess   
s     i   s     ...s     remaining %d lines omitted.(   t   stdout_bufferR   R   R   R   t   stdout_lines(   R   t   show_output_thresholdR   R   R   R   t   n(    (   R   s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   raise_if_output(   s(    			c         C   s$   |  j  d | ƒ |  j d | ƒ |  S(   NR   (   R   R   (   R   R   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   raise_if_errors_or_outputB   s    t    c         C   s    t  d |  j d | d | ƒ d  S(   NR   R   R   (   R	   R   (   R   R   R   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   show_stderrG   s    c         C   s5   |  j  d  k	 s t ‚ t d |  j  d | d | ƒ d  S(   NR   R   R   (   R   R   R   R	   (   R   R   R   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   show_stdoutJ   s    N(
   t   __name__t
   __module__R   t   RuntimeErrorR   R   R   R   R!   R"   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyR
      s   	
t   fully_buffered_simplec           B   s#   e  Z d  Z d e e d d „ Z RS(   s[  Executes command, sends stdin_lines (str or sequence), then reads
stdout_lines first, stderr_lines second (if join_stdout_stderr
is False).

The constructor may deadlock if the I/O buffers are too small to allow
the blocking write and reads in the given sequence. Specifically,
stdin_lines may be too big, or there may be too many stderr_lines,
but there can be any number of stdout_lines. The tests below are
known to work under Mac OS X, Windows XP, IRIX, and Tru64 Unix with
stdin_lines up to 1000000, stderr_lines up to 500. I.e. this simple
implementation should cover most practical situations.
  iÿÿÿÿc   	      C   sb  | |  _  | |  _ | r< t j | d | ƒ \ } } d  } n t j | d | ƒ \ } } } | d  k	 r¼ t | t ƒ s¬ t j j	 | ƒ } t
 | ƒ d k r¬ | t j 7} q¬ n  | j | ƒ n  | j ƒ  | rí d  |  _ | j ƒ  j ƒ  |  _ n | j ƒ  |  _ d  |  _ | d  k	 r)| j ƒ  j ƒ  |  _ n	 g  |  _ | j ƒ  | d  k	 rU| j ƒ  n  d  |  _ d  S(   Nt   ti    (   R   R   t   ost   popen4R   t   popen3t
   isinstancet   strt   linesepR   R   t   writet   closeR   t   readt
   splitlinesR   R   t   return_code(	   R   R   t   stdin_linesR   t   stdout_splitlinest   bufsizet   child_stdint   child_stdoutt   child_stderr(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   __init__]   s2    			
			
N(   R#   R$   t   __doc__R   t   Falset   TrueR9   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyR&   N   s
   t   fully_buffered_subprocessc           B   s#   e  Z d  Z d e e d d „ Z RS(   s/   This implementation is supposed to never block.iÿÿÿÿc   
      C   se  | |  _  | |  _ t | t ƒ s3 t j | ƒ } n  | d  k	 rˆ t | t ƒ sˆ t j j	 | ƒ } t
 | ƒ d k r… | t j 7} q… qˆ n  | rš t j } n	 t j } t j d | d t d | d t j d t j d | d t d	 t j ƒ } | j d
 | ƒ \ } }	 | r"d  |  _ | j ƒ  |  _ n | |  _ d  |  _ | rFg  |  _ n |	 j ƒ  |  _ | j |  _ d  S(   Ni    t   argst   shellR5   t   stdinR   t   stderrt   universal_newlinest	   close_fdst   input(   R   R   R+   R,   t
   subprocesst   list2cmdlineR   R(   R-   R   R   t   STDOUTt   PIPEt   PopenR<   t	   mswindowst   communicateR   R1   R   R   t
   returncodeR2   (
   R   R   R3   R   R4   R5   RA   t   pt   ot   e(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyR9   ƒ   s>    									N(   R#   R$   R:   R   R;   R<   R9   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyR=   €   s
   c         C   s   t  d |  d | d t ƒ S(   NR   R3   R   (   t   fully_bufferedR<   (   R   R3   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   go®   s    c         C   s[   xB t  j t  j g D]. } t | d d ƒ } | d k	 r | ƒ  q q Wt j d |  d t ƒ S(   s  
  Wraps subprocess.call to run a command.

  Parameters
  ----------
  command : str

  Returns
  -------
  int
      Exit code of subprocess.

  Examples
  --------
  >>> from libtbx.easy_run import call
  >>> ret = call("echo 1")
  1
  >>> print ret
  0
  t   flushR>   R?   N(   R   R   RA   t   getattrR   RE   t   callR<   (   R   t   sRR   (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyRT   ´   s
     c      
   C   sF  d d l  m } d d  l } |  d  k r8 | j d }  n  d |  k } d |  k rY t } n t } x~ d dT g D]p } xg | d
 | ƒ j ƒ  | d
 | d t ƒ t	 d
 | ƒ g D]/ } | rÀ | j
 GHn  | j
 d g k s© t ‚ q© Wql Wt j j d ƒ rGxR d dU g D]A } | d
 | ƒ j ƒ  } | r+| j
 GHn  d | j
 k sÿ t ‚ qÿ Wn  t j j d ƒ rêxŽd dV g D]}} | d
 | ƒ j ƒ  } | r’| j
 GHn  g  | j
 D] } | j ƒ  ^ qœd g k sÃt ‚ | d
 | d d g ƒ j ƒ  } | rò| j
 GHn  g  | j
 D] } | j ƒ  ^ qüd g k s#t ‚ | d
 | d d d	 g ƒ j ƒ  } | rU| j
 GHn  g  | j
 D] } | j ƒ  ^ q_d g k s†t ‚ | d
 | d d ƒ j ƒ  } | r²| j
 GHn  g  | j
 D] } | j ƒ  ^ q¼d g k sft ‚ qfWn  t j d k r»| d
 d ƒ j ƒ  } | r| j
 GHn  t | j
 ƒ d k s:t ‚ t j j d d  ƒ } | d  k	 r»| j d ƒ d k  r»| d
 d | ƒ j ƒ  } | rš| j
 GHn  t | j
 ƒ d k s¸t ‚ q»n  | j }	 |	 j d  ƒ d k sßt ‚ d  |	 d  }	 t j d k r	d! |	 }	 n  t j j d" ƒ rGt t d# ƒ s:d$ t j d" <qGt j d" =n  t j d k rn| d
 d% ƒ j ƒ  } n0 t j j d& ƒ r˜| d
 d& ƒ j ƒ  } n d  } | d  k	 rô| r»| j
 GHn  x6 | j
 D]( }
 |
 j d" ƒ sÅ|
 d' k sÅt ‚ qÅWn  xÊ t t g D]¼ } | d
 d( |	 d) | ƒ j ƒ  } | r7| j GHn  | j d* | j j ƒ  d g k s`t ‚ | r“| j d  k s{t ‚ | j
 g  k s½t ‚ q| j d$ k s¨t ‚ | j
 d  k st ‚ qWt	 d
 d( |	 ƒ } | rå| j
 GHn  | j
 d* | j j ƒ  d g k st ‚ | d
 d+ |	 ƒ j ƒ  } | r8| j
 GHn  | j
 d, g k sPt ‚ |	 d- } } | d
 | ƒ j ƒ  } | r„| j
 GHn  | j
 d g k sœt ‚ | d
 | d d g ƒ j ƒ  } | rË| j
 GHn  | j
 d g k sãt ‚ | d
 | d d d	 g ƒ j ƒ  } | r| j
 GHn  | j
 d g k s-t ‚ | d
 | d d ƒ j ƒ  } | rY| j
 GHn  | j
 d g k sqt ‚ d. |  k r†d/ } n d0 } | t k r¡d1 } n d/ } | d
 | d g  t  | ƒ D] } t! | ƒ ^ qÀƒ j ƒ  } | rò| j
 GHn  | j
 t! | ƒ g k st ‚ |	 d2 } | d
 | d d3 ƒ j ƒ  } | ƒ  } | j" d4 | d5 d6 ƒ | rs| j# j$ | j% ƒ  ƒ n  | j% ƒ  d7 k s‹t ‚ |	 d8 } } | d
 | d d ƒ j ƒ  } | ƒ  } | j& d4 | d5 d9 ƒ | rò| j# j$ | j% ƒ  ƒ n  | j% ƒ  d: k s
	t ‚ | d
 | d g  t  | ƒ D] } t! | ƒ ^ q#	ƒ j ƒ  } | rd	| j
 d;  G| j
 d< GHn  t | j
 ƒ | k s	t ‚ | j
 d;  d d d d d= g k s§	t ‚ | j
 d< g  t  | d; | ƒ D] } t! | ƒ ^ qÅ	k sæ	t ‚ |	 d2 } | d
 | d g  t  | d d ƒ D] } t! | ƒ ^ q
ƒ } t | j
 ƒ d k sE
t ‚ | re
| j d;  G| j d< GHn  t | j ƒ | k s€
t ‚ | j d;  g  t  | | d; d ƒ D] } t! | ƒ ^ q¡
k sÂ
t ‚ | j d< d> d= d d d g k sê
t ‚ |	 d? j' d@ | t( dA ƒ f j) ƒ  ƒ } | d
 | d g  t  | ƒ D] } t! | ƒ ^ q,ƒ } | rg| j
 d;  G| j
 d< GHn  | r‡| j d;  G| j d< GHn  t | j
 ƒ | k s¢t ‚ | j
 d;  d d d d d= g k sÊt ‚ | j
 d< g  t  | d; | ƒ D] } t! | ƒ ^ qèk s	t ‚ t | j ƒ | k s$t ‚ | j d;  g  t  | d | dB d ƒ D] } t! | ƒ ^ qIk sjt ‚ | j d< d= d d d d g k s’t ‚ t	 d
 | d g  t  | ƒ D] } t! | ƒ ^ q«ƒ } | ræ| j
 d;  G| j
 d< GHn  t | j
 ƒ | | k st ‚ | j
 d;  d d d d d= g k s-t ‚ | j
 d< d= d d d d g k sUt ‚ y | d
 dC ƒ j ƒ  Wn< t* k
 rª} | rŒ| GHn  t! | ƒ j dD ƒ s±t ‚ n Xt+ ‚ x5t t g D]'} xdW dX dY dZ g D]
\ } } yB | d
 | d g  t  | ƒ D] } t! | ƒ ^ qÿd) | ƒ j ƒ  Wn³ t* k
 rÚ} | rE| GHn  t! | ƒ j dM ƒ s`t ‚ | r¸| dK k ršt! | ƒ j, t! | d ƒ ƒ sµt ‚ q×t! | ƒ j, dN ƒ s×t ‚ qát! | ƒ j, dO | ƒ sát ‚ q×Xt+ ‚ q×Wq¾W| d
 | ƒ j- ƒ  | d
 dP dQ g ƒ } | r*| j
 GH| j GHn  x dR |  k r<q-WdS GHd  S([   Niÿÿÿÿ(   t   StringIOi   s	   --verboses   --simples   echo hello worldt   echot   hellot   worldR   R   s   hello worlds   /bin/lss   /bin/ls /bins   /bint   lss   /usr/bin/wcs   /usr/bin/wc -ls   -lt   0R3   t   1t   2s   hello
world
bye
t   3t   nts   dir /?i    t   windirt    s   dir t   "s   call t
   PYTHONPATHt   unsetenvR    t   sets   /usr/bin/printenvs   PYTHONPATH=s   %s -VR4   s   Python s   %s -c "print 3+4"t   7s:    -c "import sys; print len(sys.stdin.read().splitlines())"s   --quicki'  i@B iô  s4    -c "import sys; sys.stderr.write(sys.stdin.read())"s   Hello
World
Bye
R   R   s   %(s   %(Hello
%(World
%(Bye
s4    -c "import sys; sys.stdout.write(sys.stdin.read())"s   >:s   >:hello
>:world
>:bye
i   iûÿÿÿt   4t   5s   ; sÌ    -c "import sys, os
lines = sys.stdin.read()
sys.stdout.write(lines)
sys.stdout.flush()
lines = lines.splitlines()[:%d]
lines.reverse()
nl = chr(%d)
sys.stderr.write(nl.join(lines)+nl)
sys.stderr.flush()"s   
i   t   C68649356116218352s   child process stderr output:
i
   i   i   i   i   i   i   i   s!   unexpected child process output:
s     remaining 3 lines omitted.s     length of output: %d bytest   nslookupt	   localhosts	   --forevert   OK(   RW   RX   RY   (   s   /bin/lss   /bin(   s   /usr/bin/wcs   -l(   i
   i   (   i   i   (   i   i   (   i   i   (.   t	   cStringIORV   R   R   t   argvR&   RP   R   R<   RQ   R   R   R(   t   patht   isfilet   stript   nameR   t   environt   gett   findt
   executablet   countt   has_keyt   hasattrt
   startswithR;   R   R   t   versiont   splitR   t   xrangeR,   R!   R   R.   t   getvalueR"   R   t   ordR1   R%   t   Exception_expectedt   endswithR   (   R>   RV   R   t   verboset   fbR   R   RU   R`   t   pyexeR   R4   t	   n_lines_ot	   n_lines_et   it   cat_commandRO   R   t   b(    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   exerciseÎ   s¦    	    1 1! 1 8 ! !	 )
 ) )   !  		4 	 		 	4 (24 5(		 .  (29(. ((  % (# t   __main__(   t
   __future__R    R   t   version_infoRE   t   libtbxR   t   ImportErrorR(   R	   t   objectR
   R&   R=   RP   R   RQ   RT   RŠ   R#   (    (    (    s=   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/easy_run.pyt   <module>   s*   	92,	å