ó
¢áSc           @` 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 „  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 rje  Z% n* e j$ d# k r‚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($   s¥  
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

Succesfully 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(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt   temp_file_name=   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(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt   get_pythonexeB   s    	(c         C` s)   d d  l  } | j d t ƒ t j |  ƒ S(   Ni    s+   splitcmdline is deprecated; use shlex.split(   t   warningst   warnt   DeprecationWarningt   shlexR   (   t   lineR    (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt   splitcmdlineK   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(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/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   infoR!   (   t   exeR   t   _cachet   keyt   orig_exeR3   t   suffixesR   t   extt   extra_suffixest   pathst   pt   st   f_ext(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/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   R0   R1   (   t   namest   envR   (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/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   R0   (   RJ   R   t   value(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/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   hasattrRO   t   Truet   IOErrort   False(   t   streamt   r(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/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.

    command is a concatenated string of executable and arguments.
    The output contains both stdout and stderr messages.
    The following special keyword arguments can be used:
      use_shell - execute `sh -c command`
      use_tee   - pipe the output of command through tee
      execute_in - before run command `cd execute_in` and after `cd -`.

    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   RL   R/   R   R   R   R9   t   getcwdt   __name__t   dirnamet   __file__R   R   t   argvt   chdirRK   t   listt   keysRN   RV   t   stdoutRO   t   _exec_command_pythont   _exec_command_posixt   _exec_command(   t   commandt
   execute_inRZ   R[   t   _with_pythonRJ   t   kvt   oldcwdt   exec_dirt   oldenvt   st(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/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)RZ   RU   iÿÿÿÿs   
(   R   R.   R   R   Rb   R   R/   R   t   systemR!   Rg   R	   t   readt   intR
   t   remove(   Rh   RZ   R[   RJ   t   command_strt   tmpfilet   stsfilet   filtert   command_posixt   statust   ft   status_textt   text(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyRf   è   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 failedRU   (   R   R.   R   R   t   opent   writeR   R0   R
   Rq   t   RuntimeErrorRt   R	   Rs   Rr   (   Rh   RY   RJ   t
   python_exet   cmdfileRw   t   outfileR{   t   cmdRz   R}   (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyRe     s>    				


c         C` s(   |  d d k r$ d |  k r$ d |  S|  S(   Ni    R*   Rp   s   "%s"(    (   t   arg(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt	   quote_argE  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é t j } n„ t j } t | d ƒ p	| d | d <t j j | d ƒ smt  j d	 | d ƒ t j d k rmt j d d g | } d } qm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 ƒ } | r*t ƒ  } t | d ƒ } n  t  j d | j t j | d | f ƒ | d } | stt | ƒ | d <n  |
 ƒ  | ƒ  | r¡t j | j ƒ  | ƒ n  |	 rß| rÆt j | j ƒ  | ƒ qßt j | j ƒ  | ƒ n  y | t j | | t j ƒ } WnD t  k
 rDt! t" ƒ  ƒ } d } t j j# d | | d f ƒ n X|
 ƒ  | ƒ  | rlt j | | ƒ n  |	 r…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 rZ| d  } n  | d  k rod } n  | r‚t( | ƒ n  | | f S(   Ns   _exec_command(...)R'   i    t   SHELLs   /bin/shs   -cRp   t   spawnvpes   Executable %s does not existR   R   t   COMSPECs   /Ci   R~   s   Running %s(%s,%r,%r,os.environ)iç  s   %s: %sRU   s   
iÿÿÿÿ(   s   nts   dos()   R   R.   R/   R   R   R0   R1   R   R   Rb   R#   R   RP   R‰   t   spawnveR   R   R   R!   RV   R   Rd   t   stderrt   flushRO   t   dupR   R   R]   t   P_WAITR‡   t   dup2t   OSErrort   strR   R€   R
   R	   Rr   Rt   t   print(   Rh   RZ   R[   RJ   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   argv0Rz   t   errmessR}   (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyRg   J  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   R!   R   R   R   R0   R“   (   t   kwsR   R¤   t   using_cygwin_echoRG   t   o(    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/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   R0   R“   (   R®   RG   R°   (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt
   test_posix  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(	   NR~   R²   s:   %s -c "print 'Ignore the following IOError:',open(%r,'r')"R+   s!   %s -c "print open(%r,'r').read()"Ri   i    R­   (   R   R   R   R   t   basenameR^   R   R€   R
   R   R   Rt   R“   (   R®   R   Rv   R   t   tmpdirR{   RG   R°   (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt   test_execute_in=  s    		
$*c          K` sA   t  d d g |  � \ } } | s3 t | | f ƒ ‚ t d ƒ d  S(   Nt   svnRz   s   svn ok(   R   R   R“   (   R®   RG   R°   (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt   test_svnO  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®   RG   R°   (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt   test_clT  s    R'   R   R   s   exec_command tests for t   __main__R[   (   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/   R   RK   RN   RV   R   Rf   Re   R‡   Rg   R±   R´   R·   R¹   R»   R   t   testt   NotImplementedErrorR]   (    (    (    sV   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/distutils/exec_command.pyt   <module>.   sP   				3				J4'	w	L	0					