ó
ØÆ÷Xc           @` sì  d  Z  d d l m Z m Z m Z d d g 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 d	 l m Z d
 „  Z d „  Z d i  d „ Z d „  Z d „  Z d „  Z d d d d d „ Z d d d „ Z d d „ Z d „  Z d d d „ Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" e j# d k rae Z$ n* e j# d" k rye Z$ n e% d e j# ƒ ‚ e& d  k rèe$ d! d ƒ e$ d! d ƒ e  d! d ƒ e  d! d ƒ e! d! d ƒ e" d! d ƒ n  d S(#   s0  
exec_command

Implements exec_command function that is (almost) equivalent to
commands.getstatusoutput function but on NT, DOS systems the
returned status is actually correct (though, the returned status
values may be different by a factor). In addition, exec_command
takes keyword arguments for (re-)defining environment variables.

Provides functions:

  exec_command  --- execute command in a specified directory and
                    in the modified environment.
  find_executable --- locate a command using info from environment
                    variable PATH. Equivalent to posix `which`
                    command.

Author: Pearu Peterson <pearu@cens.ioc.ee>
Created: 11 January 2003

Requires: Python 2.x

Successfully tested on:

========  ============  =================================================
os.name   sys.platform  comments
========  ============  =================================================
posix     linux2        Debian (sid) Linux, Python 2.1.3+, 2.2.3+, 2.3.3
                        PyCrust 0.9.3, Idle 1.0.2
posix     linux2        Red Hat 9 Linux, Python 2.1.3, 2.2.2, 2.3.2
posix     sunos5        SunOS 5.9, Python 2.2, 2.3.2
posix     darwin        Darwin 7.2.0, Python 2.3
nt        win32         Windows Me
                        Python 2.3(EE), Idle 1.0, PyCrust 0.7.2
                        Python 2.1.1 Idle 0.8
nt        win32         Windows 98, Python 2.1.1. Idle 0.8
nt        win32         Cygwin 98-4.10, Python 2.1.1(MSC) - echo tests
                        fail i.e. redefining environment variables may
                        not work. FIXED: don't use cygwin echo!
                        Comment: also `cmd /c echo` will not work
                        but redefining environment variables do work.
posix     cygwin        Cygwin 98-4.10, Python 2.3.3(cygming special)
nt        win32         Windows XP, Python 2.3.3
========  ============  =================================================

Known bugs:

* Tests, that send messages to stderr, fail when executed from MSYS prompt
  because the messages are lost at some point.

i    (   t   divisiont   absolute_importt   print_functiont   exec_commandt   find_executableN(   t   is_sequencet   make_temp_file(   t   log(   t   get_exception(   t   open_latin1c          C` s   t  ƒ  \ }  } |  j ƒ  | S(   N(   R   t   close(   t   fot   name(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   temp_file_nameC   s    
c          C` s‰   t  j }  t j d k r… t j j |  ƒ \ } } | j ƒ  j d d ƒ } t j j | | ƒ }  t j j	 |  ƒ s… t
 d |  f ƒ ‚ n  |  S(   Nt   ntt   dost   PYTHONWt   PYTHONs   %r is not a file(   s   nts   dos(   t   syst
   executablet   osR   t   patht   splitt   uppert   replacet   joint   isfilet   AssertionError(   t	   pythonexet   fdirt   fn(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   get_pythonexeH   s    	(c         C` s  |  | f } y | | SWn t  k
 r+ n Xt j d |  ƒ |  } | d k rj t j j d t j ƒ } n  t j d k rˆ t j	 j
 } n	 d „  } |  j d ƒ r° |  d d !}  n  d g } t j d k rt j	 j |  ƒ \ } } d d d g }	 | j ƒ  |	 k r|	 } qn  t j	 j |  ƒ r+d g }
 n1 g  | j t j ƒ D] } t j	 j | ƒ ^ q>}
 x¥ |
 D] } t j	 j | |  ƒ } x | D]w } | | } t j	 j | ƒ s¶| | ƒ } n  t j	 j | ƒ r…t j | t j ƒ r…t j d | ƒ | | | <| Sq…WqcWt j d | ƒ d S(   sT   Return full path of a executable or None.

    Symbolic links are not followed.
    s   find_executable(%r)t   PATHt   posixc         S` s   |  S(   N(    (   t   a(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   <lambda>c   s    t   "i   iÿÿÿÿt    R   R   t   os2s   .exes   .coms   .bats   Found executable %ss   Could not locate executable %sN(   s   nts   doss   os2(   t   KeyErrorR   t   debugt   NoneR   t   environt   gett   defpathR   R   t   realpatht
   startswitht   splitextt   lowert   isabsR   t   pathsept   abspathR   t   islinkR   t   accesst   X_OKt   infot   warn(   t   exeR   t   _cachet   keyt   orig_exeR-   t   suffixesR   t   extt   extra_suffixest   pathst   pt   st   f_ext(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyR   Q   sF    		1
'
c         C` sB   t  j d |  ƒ i  } x$ |  D] } t j j | ƒ | | <q W| S(   Ns   _preserve_environment(%r)(   R   R(   R   R*   R+   (   t   namest   envR   (    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   _preserve_environment„   s
    c          K` sA   t  j d ƒ x- |  j ƒ  D] \ } } | p/ d t j | <q Wd  S(   Ns   _update_environment(...)R%   (   R   R(   t   itemsR   R*   (   RE   R   t   value(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   _update_environment‹   s    c         C` sC   t  |  d ƒ r; y |  j ƒ  } t SWq? t k
 r7 t SXn t Sd S(   sT   
    Returns True if 'stream' supports the file descriptor and allows fileno().
    t   filenoN(   t   hasattrRJ   t   Truet   IOErrort   False(   t   streamt   r(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   _supports_fileno   s    R%   i   c         K` sh  t  j d |  d j g  | j ƒ  D] } d | ^ q ƒ f ƒ | d k r[ t j d k } n  | d k ry t j d k } n  t j j | ƒ } t j j t j	 ƒ  ƒ } t
 d d k rÔ t j j t j j t ƒ ƒ } ng t j j d ƒ rû t j j d ƒ } n@ t j j t j d	 ƒ } t j j | ƒ r;t j j | ƒ } n  | | k rht j | ƒ t  j d
 | ƒ n t  j d | ƒ t t | j ƒ  ƒ ƒ }	 t |   zŽ | ràt t j ƒ ràt j j ƒ  d k ràt |  d | | }
 nH t j d k rt |  d | d | | }
 n t |  d | d | | }
 Wd | | k rYt j | ƒ t  j d | ƒ n  t |	   X|
 S(   s°  
    Return (status,output) of executed command.

    Parameters
    ----------
    command : str
        A concatenated string of executable and arguments.
    execute_in : str
        Before running command ``cd execute_in`` and after ``cd -``.
    use_shell : {bool, None}, optional
        If True, execute ``sh -c command``. Default None (True)
    use_tee : {bool, None}, optional
        If True use tee. Default None (True)


    Returns
    -------
    res : str
        Both stdout and stderr messages.

    Notes
    -----
    On NT, DOS systems the returned status is correct for external commands.
    Wild cards will not work for non-posix systems or when use_shell=0.

    s   exec_command(%r,%s)t   ,s   %s=%rR!   iôÿÿÿR   s   exec_command.pyt   .i    s   New cwd: %ss   Retaining cwd: %siÿÿÿÿt   exec_command_dirt	   use_shellt   use_teeNs   Restored cwd to %s(   R   R(   R   RG   R)   R   R   R   R3   t   getcwdt   __name__t   dirnamet   __file__R   R   t   argvt   chdirRF   t   listt   keysRI   RQ   t   stdoutRJ   t   _exec_command_pythont   _exec_command_posixt   _exec_command(   t   commandt
   execute_inRU   RV   t   _with_pythonRE   t   kvt   oldcwdt   exec_dirt   oldenvt   st(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyR      sN    1!

		c         K` sž  t  j d ƒ t |  ƒ r1 d j t |  ƒ ƒ } n |  } t ƒ  } d  } | r‰ t ƒ  } d } | d k rp d } n  d | | | | f } n t ƒ  } d | | | f } t  j d | ƒ t j | ƒ }	 | rø |	 rø t  j	 d	 |	 ƒ t
 |  d
 | | Sn  | d  k	 rEt | d ƒ }
 |
 j ƒ  } t | ƒ }	 |
 j ƒ  t j | ƒ n  t | d ƒ }
 |
 j ƒ  } |
 j ƒ  t j | ƒ | d d k r”| d  } n  |	 | f S(   Ns   _exec_command_posix(...)t    R%   i   s!   | tr -cd "\n" | tr "\n" "."; echos&   ( %s ; echo $? > %s ) 2>&1 | tee %s %ss   ( %s ; echo $? > %s ) > %s 2>&1s   Running os.system(%r)s&   _exec_command_posix failed (status=%s)RU   RP   iÿÿÿÿs   
(   R   R(   R   R   R]   R   R)   R   t   systemR8   Rb   R	   t   readt   intR
   t   remove(   Rc   RU   RV   RE   t   command_strt   tmpfilet   stsfilet   filtert   command_posixt   statust   ft   status_textt   text(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyRa   ô   sF    				

c         K` s£  t  j d ƒ t ƒ  } t ƒ  } t ƒ  } t ƒ  } t | d ƒ } | j d ƒ | j d ƒ | j d | ƒ | j d ƒ | j d ƒ | j d |  ƒ | j d	 t j ƒ | j d
 | ƒ | j d | ƒ | j d | ƒ | j ƒ  d | | f } t j	 | ƒ }	 |	 r"t
 d | f ƒ ‚ n  t j | ƒ t | d ƒ } t | j ƒ  ƒ }	 | j ƒ  t j | ƒ t | d ƒ } | j ƒ  }
 | j ƒ  t j | ƒ |	 |
 f S(   Ns   _exec_command_python(...)t   ws
   import os
s   import sys
s   sys.path.insert(0,%r)
s&   from exec_command import exec_command
s   del sys.path[0]
s	   cmd = %r
s   os.environ = %r
s.   s,o = exec_command(cmd, _with_python=0, **%r)
s)   f=open(%r,"w")
f.write(str(s))
f.close()
s$   f=open(%r,"w")
f.write(o)
f.close()
s   %s %ss	   %r failedRP   (   R   R(   R   R   t   opent   writeR   R*   R
   Rl   t   RuntimeErrorRo   R	   Rn   Rm   (   Rc   RT   RE   t
   python_exet   cmdfileRr   t   outfileRv   t   cmdRu   Rx   (    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyR`   )  s>    				


c         C` s(   |  d d k r$ d |  k r$ d |  S|  S(   Ni    R$   Rk   s   "%s"(    (   t   arg(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt	   quote_argQ  s    c         K` s‰  t  j d ƒ | d  k r+ t j d k } n  | d  k rI t j d k } n  d } | r© t j j d d ƒ } t |  ƒ r— | d d j t	 |  ƒ ƒ g } qÎ | d |  g } n% t |  ƒ r¿ |  } n t
 j |  ƒ } t t d ƒ rø | rì t j n t j } n“ | rt j n t j } t | d ƒ p'| d | d <t j j | d ƒ s‹t  j d	 | d ƒ t j d k r‹t j d d g | } d } q‹n  t t j ƒ } t t j ƒ }	 t j j }
 t j j } | rèt j j ƒ  } t j | ƒ } n  |	 rt j j ƒ  } t j | ƒ } n  t ƒ  } t | d ƒ } | rHt ƒ  } t | d ƒ } n  t  j d | j t j | d | f ƒ | rt j  d d k rt j d
 k ri  } xy t j j! ƒ  D]\ \ } } y, | j" t j# ƒ  ƒ | | j" t j# ƒ  ƒ <Wq­t$ k
 rt  j d | ƒ q­Xq­Wn	 t j } | d } | s<t% | ƒ | d <n  |
 ƒ  | ƒ  | rit j& | j ƒ  | ƒ n  |	 r§| rŽt j& | j ƒ  | ƒ q§t j& | j ƒ  | ƒ n  y: | rË| t j | | | ƒ } n | t j | | ƒ } WnD t' k
 r't( t) ƒ  ƒ } d } t j j* d | | d f ƒ n X|
 ƒ  | ƒ  | r\t j& | | ƒ t j+ | ƒ n  |	 r‚t j& | | ƒ t j+ | ƒ n  | j+ ƒ  t, | d ƒ } | j- ƒ  } | j+ ƒ  t j. | ƒ | r:| j+ ƒ  t, | d ƒ } | j- ƒ  } | j+ ƒ  t j. | ƒ | r:| r:| r | d } n  | | } t/ | ƒ q:n  | d d k rW| d  } n  | d  k rld } n  | rt/ | ƒ n  | | f S(   Ns   _exec_command(...)R!   i    t   SHELLs   /bin/shs   -cRk   t   spawnvpes   Executable %s does not existR   R   t   COMSPECs   /Ci   Ry   s   Running %s(%s,%r,%r,os.environ)i   s"   ignoring un-encodable env entry %siç  s   %s: %sRP   s   
iÿÿÿÿ(   s   nts   dos(0   R   R(   R)   R   R   R*   R+   R   R   R]   t   shlexR   RK   R„   t   spawnvpt   spawnvet   spawnvR   R   R   R8   RQ   R   R_   t   stderrt   flushRJ   t   dupR   Rz   RX   t   P_WAITt   version_infoRG   t   encodet   getfilesystemencodingt   UnicodeEncodeErrorR‚   t   dup2t	   Exceptiont   strR   R{   R
   R	   Rm   Ro   t   print(   Rc   RU   RV   RE   t   using_commandt   shR[   t   spawn_commandt   _so_has_filenot   _se_has_filenot   so_flusht   se_flusht	   so_filenot   so_dupt	   se_filenot   se_dupR   t   foutt   errfilet   ferrt   encoded_environt   kt   vt   argv0Ru   t   errmessRx   (    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyRb   V  sÆ    !
			(	&	
"




	c          K` s  t  ƒ  } t d ƒ } | d k } | rt j d ƒ t | d ƒ \ } } | d k rb | d k st t | | f ƒ ‚ t | d d d ƒ\ } } | d k r¨ | d k sº t | | f ƒ ‚ d	 t j d
 <t | d ƒ \ } } | d k rõ | d	 k st | | f ƒ ‚ t | d d
 d ƒ\ } } | d k r;| d k sMt | | f ƒ ‚ t | d ƒ \ } } | d k r{| d	 k st | | f ƒ ‚ n  t d ƒ \ } } | d k rº| d k sÌt | | f ƒ ‚ t d | ƒ \ } } | d k rú| d k st | | f ƒ ‚ t d | ƒ \ } } | d k r4| sFt | | f ƒ ‚ t d | ƒ \ } } | d k rt| d k s†t | | f ƒ ‚ t d | ƒ \ } } | d k r´| d k sÆt | | f ƒ ‚ t d | ƒ \ } } | d k rô| d k st | | f ƒ ‚ t d ƒ d  S(   Nt   echos7   Using cygwin echo in win32 environment is not supporteds.    -c "import os;print os.environ.get('AAA','')"i    R%   s+    -c "import os;print os.environ.get('AAA')"t   AAAt   Teret   Hit   BBBs.    -c "import os;print os.environ.get('BBB','')"t   Heys   echo path=%path%s1   %s -c "import sys;sys.stderr.write(sys.platform)"t   win32s   %s -c "raise 'Ignore me.'"i   sT   %s -c "import sys;sys.stderr.write('0');sys.stderr.write('1');sys.stderr.write('2')"t   012s   %s -c "import sys;sys.exit(15)"i   s   %s -c "print 'Heipa'"t   Heipat   ok(	   R   R   R   R8   R   R   R   R*   R•   (   t   kwsR   R©   t   using_cygwin_echoRB   t   o(    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   test_ntê  sL    	****-**$***c          K` s“  t  d |   \ } } | d k r- | d k s? t | | f ƒ ‚ t  d |   \ } } | d k rl | d k s~ t | | f ƒ ‚ t  d d d |  \ } } | d k r± | d k sÃ t | | f ƒ ‚ t  d |   \ } } | d k rð | d k st | | f ƒ ‚ d	 t j d
 <t  d |   \ } } | d k r<| d	 k sNt | | f ƒ ‚ t  d d
 d |  \ } } | d k r| d k s“t | | f ƒ ‚ t  d |   \ } } | d k rÀ| d	 k sÒt | | f ƒ ‚ t  d |   \ } } | d k rÿ| d k st | | f ƒ ‚ t  d |   \ } } | d k r>| d k sPt | | f ƒ ‚ t  d |   \ } } | d k r}| d k st | | f ƒ ‚ t  d |   \ } } | d k r¶| sÈt | | f ƒ ‚ t  d |   \ } } | d k rõ| d k st | | f ƒ ‚ t  d |   \ } } | d k r4| d k sFt | | f ƒ ‚ t  d |   \ } } | d k rs| d k s…t | | f ƒ ‚ t d ƒ d  S(   Ns
   echo Helloi    t   Hellos	   echo $AAAR%   s   echo "$AAA"Rª   R«   R¬   R­   s   echo "$BBB"R®   t   this_is_not_a_commands   echo path=$PATHs3   python -c "import sys,os;sys.stderr.write(os.name)"R!   s   python -c "raise 'Ignore me.'"i   sX   python -c "import sys;sys.stderr.write('0');sys.stderr.write('1');sys.stderr.write('2')"R°   s#   python -c "import sys;sys.exit(15)"i   s   python -c "print 'Heipa'"R±   R²   (   R   R   R   R*   R•   (   R³   RB   Rµ   (    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt
   test_posix6  s<    **********$***c          K` s	  t  ƒ  } t ƒ  } t j j | ƒ } t j j | ƒ } t | d ƒ } | j d ƒ | j ƒ  t	 d | | f |   \ } } | r | d k sŸ t
 | | f ƒ ‚ t	 d | | f d | |  \ } } | d k rÜ | d k sî t
 | | f ƒ ‚ t j | ƒ t d ƒ d  S(	   NRy   R·   s:   %s -c "print 'Ignore the following IOError:',open(%r,'r')"R%   s!   %s -c "print open(%r,'r').read()"Rd   i    R²   (   R   R   R   R   t   basenameRY   Rz   R{   R
   R   R   Ro   R•   (   R³   R   Rq   R   t   tmpdirRv   RB   Rµ   (    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   test_execute_inf  s    		
$*c          K` sA   t  d d g |   \ } } | s3 t | | f ƒ ‚ t d ƒ d  S(   Nt   svnRu   s   svn ok(   R   R   R•   (   R³   RB   Rµ   (    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   test_svnx  s    c          K` sS   t  j d k rO t d d g |   \ } } | sB t | | f ƒ ‚ t d ƒ n  d  S(   NR   t   cls   /Vs   cl ok(   R   R   R   R   R•   (   R³   RB   Rµ   (    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   test_cl}  s    R!   R   R   s   exec_command tests for t   __main__RV   (   s   nts   dos('   t   __doc__t
   __future__R    R   R   t   __all__R   R   R†   t   numpy.distutils.misc_utilR   R   t   numpy.distutilsR   t   numpy.distutils.compatR   t   numpy.compatR	   R   R   R)   R   RF   RI   RQ   R   Ra   R`   R‚   Rb   R¶   R¹   R¼   R¾   RÀ   R   t   testt   NotImplementedErrorRX   (    (    (    s;   /tmp/pip-build-X4mzal/numpy/numpy/distutils/exec_command.pyt   <module>4   sN   			3				W4'	”	L	0					