� �9Zc@`s�dZddlmZmZmZddgZddlZddlZddlZddl m Z m Z ddl m Z ddlmZdd lmZd �Zd �Zdid �Zd �Zd�Zd�Zddddd�Zddd�Zdd�Zd�Zddd�Zd�Zd�Zd�Z d�Z!d�Z"ej#dkraeZ$n*ej#d"kryeZ$ne%dej#��e&d kr�e$d!d�e$d!d�e d!d�e d!d�e!d!d�e"d!d�ndS(#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(tdivisiontabsolute_importtprint_functiont exec_commandtfind_executableN(t is_sequencetmake_temp_file(tlog(t get_exception(t open_latin1cC`st�\}}|j�|S(N(Rtclose(tfotname((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttemp_file_nameBs cC`s�tj}tjdkr�tjj|�\}}|j�jdd�}tjj||�}tjj |�s�t d|f��n|S(NtnttdostPYTHONWtPYTHONs%r is not a file(RR( tsyst executabletosR tpathtsplittuppertreplacetjointisfiletAssertionError(t pythonexetfdirtfn((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt get_pythonexeGs (cC`s||f}y ||SWntk r+nXtjd|�|}|dkrjtjjdtj�}ntjdkr�tj j }n d�}|j d�r�|dd!}ndg}tjdkr tj j |�\}}d d dg} |j �| kr | }q ntj j|�r+dg} n1g|jtj�D]} tj j| �^q>} x�| D]�}tj j||�}x|D]w} || } tj j| �s�|| �} ntj j| �r�tj| tj�r�tjd| �| ||<| Sq�WqcWtjd|�dS(sTReturn full path of a executable or None. Symbolic links are not followed. sfind_executable(%r)tPATHtposixcS`s|S(N((ta((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt<lambda>bst"ii����tRRtos2s.exes.coms.batsFound executable %ssCould not locate executable %sN(RRR&(tKeyErrorRtdebugtNoneRtenvirontgettdefpathR Rtrealpatht startswithtsplitexttlowertisabsRtpathseptabspathRtislinkRtaccesstX_OKtinfotwarn(texeRt_cachetkeytorig_exeR-tsuffixesRtexttextra_suffixestpathstptstf_ext((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyRPsF        1   '  cC`sBtjd|�i}x$|D]}tjj|�||<qW|S(Ns_preserve_environment(%r)(RR(RR*R+(tnamestenvR ((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt_preserve_environment�s  cK`sAtjd�x-|j�D]\}}|p/dtj|<qWdS(Ns_update_environment(...)R%(RR(titemsRR*(RER tvalue((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt_update_environment�s cC`sCt|d�r;y|j�}tSWq?tk r7tSXntSdS(sT Returns True if 'stream' supports the file descriptor and allows fileno(). tfilenoN(thasattrRJtTruetIOErrortFalse(tstreamtr((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt_supports_fileno�s  R%ic K`shtjd|djg|j�D]}d|^q�f�|dkr[tjdk}n|dkrytjdk}ntjj|�}tjjtj ��}t ddkr�tjj tjjt ��}ngtjj d�r�tjjd�}n@tjjtjd �}tjj |�r;tjj |�}n||krhtj|�tjd |�ntjd |�tt|j���} t|�z�|r�ttj�r�tjj�d kr�t|d ||�} nHtjdkr t|d|d||�} nt|d|d||�} Wd||krYtj|�tjd|�nt| �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. sexec_command(%r,%s)t,s%s=%rR!i�Rsexec_command.pyt.is New cwd: %ssRetaining cwd: %si����texec_command_dirt use_shelltuse_teeNsRestored cwd to %s(RR(RRGR)RR RR3tgetcwdt__name__tdirnamet__file__RRtargvtchdirRFtlisttkeysRIRQtstdoutRJt_exec_command_pythont_exec_command_posixt _exec_command( tcommandt execute_inRURVt _with_pythonREtkvtoldcwdtexec_dirtoldenvtst((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyR�sN 1  !           c K`s�tjd�t|�r1djt|��}n|}t�}d}|r�t�}d}|dkrpd}nd||||f}nt�}d|||f}tjd|�tj|�} |r�| r�tj d | �t |d ||�Sn|dk rEt |d �} | j �} t | �} | j�tj|�nt |d �} | j �} | j�tj|�| d d kr�| d } n| | fS(Ns_exec_command_posix(...)t R%is!| tr -cd "\n" | tr "\n" "."; echos&( %s ; echo $? > %s ) 2>&1 | tee %s %ss( %s ; echo $? > %s ) > %s 2>&1sRunning os.system(%r)s&_exec_command_posix failed (status=%s)RURPi����s (RR(RRR]R R)RtsystemR8RbR treadtintR tremove( RcRURVREt command_strttmpfiletstsfiletfiltert command_posixtstatustft status_textttext((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyRa�sF               c K`s�tjd�t�}t�}t�}t�}t|d�}|jd�|jd�|jd|�|jd�|jd�|jd|�|jd tj�|jd |�|jd |�|jd |�|j�d ||f}tj |�} | r"t d|f��ntj |�t |d�}t |j��} |j�tj |�t |d�}|j�} |j�tj |�| | fS(Ns_exec_command_python(...)tws import os s import sys ssys.path.insert(0,%r) s&from exec_command import exec_command sdel sys.path[0] s cmd = %r sos.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(RR(RR topentwriteRR*R Rlt RuntimeErrorRoR RnRm( RcRTREt python_exetcmdfileRrtoutfileRvtcmdRuRx((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyR`(s>                cC`s(|ddkr$d|kr$d|S|S(NiR$Rks"%s"((targ((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt quote_argPscK`sGtjd�|dkr+tjdk}n|dkrItjdk}nd}|r�tjjdd�}t|�r�|ddjt |��g}q�|d|g}n%t|�r�|}nt j |�}t td�r�tj }n�tj}t|d�p |d|d<tjj|d�smtjd |d�tjdkrmtjd d g|}d}qmnttj�}ttj�} tjj} tjj} |r�tjj�} tj| �} n| r�tjj�}tj|�}nt�}t|d�}|r*t�}t|d�}ntjd|jtj|d|f�tjddkr�tjd kr�i}xytjj�D]\\}}y,|j tj!��||j tj!��<Wq�t"k r�tjd|�q�Xq�Wn tj}|d}|st#|�|d<n| �| �|rEtj$|j�| �n| r�|rjtj$|j�|�q�tj$|j�|�ny|tj|||�}WnDt%k r�t&t'��}d}tjj(d||df�nX| �| �|rtj$| | �tj)| �n| r@tj$||�tj)|�n|j)�t*|d�}|j+�}|j)�tj,|�|r�|j)�t*|d�}|j+�}|j)�tj,|�|r�| r�|r�|d}n||}t-|�q�n|ddkr|d }n|dkr*d}n|r=t-|�n||fS(Ns_exec_command(...)R!itSHELLs/bin/shs-cRktspawnvpesExecutable %s does not existRRtCOMSPECs/CiRysRunning %s(%s,%r,%r,os.environ)is"ignoring un-encodable env entry %si�s%s: %sRPs i����(RR(.RR(R)RR R*R+RRR]tshlexRRKR�tspawnveRRRR8RQRR_tstderrtflushRJtdupR RzRXtP_WAITt version_infoRGtencodetgetfilesystemencodingtUnicodeEncodeErrorR�tdup2t ExceptiontstrRR{R R RmRotprint(RcRURVREt using_commandtshR[t spawn_commandt_so_has_filenot_se_has_filenotso_flushtse_flusht so_filenotso_dupt se_filenotse_dupRtfoutterrfiletferrtencoded_environtktvtargv0RuterrmessRx((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyRbUs�    !          " &    "               cK`st�}td�}|dk}|r�tjd�t|d�\}}|dkrb|dkstt||f��t|ddd�\}}|dkr�|dks�t||f��d tjd <t|d �\}}|dkr�|d kst||f��t|d d d �\}}|dkr;|d ksMt||f��t|d �\}}|dkr{|d ks�t||f��ntd �\}}|dkr�|dks�t||f��td|�\}}|dkr�|dks t||f��td|�\}}|dkr4|sFt||f��td|�\}}|dkrt|dks�t||f��td|�\}}|dkr�|dks�t||f��td|�\}}|dkr�|dkst||f��td�dS(Ntechos7Using cygwin echo in win32 environment is not supporteds. -c "import os;print os.environ.get('AAA','')"iR%s+ -c "import os;print os.environ.get('AAA')"tAAAtTeretHitBBBs. -c "import os;print os.environ.get('BBB','')"tHeysecho path=%path%s1%s -c "import sys;sys.stderr.write(sys.platform)"twin32s%s -c "raise 'Ignore me.'"isT%s -c "import sys;sys.stderr.write('0');sys.stderr.write('1');sys.stderr.write('2')"t012s%s -c "import sys;sys.exit(15)"is%s -c "print 'Heipa'"tHeipatok( RRRR8RRRR*R�(tkwsRR�tusing_cygwin_echoRBto((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttest_nt�sL    ** **-**$***cK`s�td|�\}}|dkr-|dks?t||f��td|�\}}|dkrl|dks~t||f��tddd|�\}}|dkr�|dks�t||f��td|�\}}|dkr�|dkst||f��d tjd <td |�\}}|dkr<|d ksNt||f��td d d |�\}}|dkr�|d ks�t||f��td |�\}}|dkr�|d ks�t||f��td |�\}}|dkr�|dkst||f��td|�\}}|dkr>|dksPt||f��td|�\}}|dkr}|dks�t||f��td|�\}}|dkr�|s�t||f��td|�\}}|dkr�|dkst||f��td|�\}}|dkr4|dksFt||f��td|�\}}|dkrs|dks�t||f��td�dS(Ns echo HelloitHellos echo $AAAR%s echo "$AAA"R�R�R�R�s echo "$BBB"R�tthis_is_not_a_commandsecho path=$PATHs3python -c "import sys,os;sys.stderr.write(os.name)"R!spython -c "raise 'Ignore me.'"isXpython -c "import sys;sys.stderr.write('0');sys.stderr.write('1');sys.stderr.write('2')"R�s#python -c "import sys;sys.exit(15)"ispython -c "print 'Heipa'"R�R�(RRRR*R�(R�RBR�((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt test_posix-s<**** ******$***cK`s t�}t�}tjj|�}tjj|�}t|d�}|jd�|j�t d||f|�\}}|r�|dks�t ||f��t d||fd||�\}}|dkr�|dks�t ||f��tj |�t d�dS( NRyR�s:%s -c "print 'Ignore the following IOError:',open(%r,'r')"R%s!%s -c "print open(%r,'r').read()"RdiR�( RR RRtbasenameRYRzR{R RRRoR�(R�RRqRttmpdirRvRBR�((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttest_execute_in]s    $* cK`sAtddg|�\}}|s3t||f��td�dS(NtsvnRussvn ok(RRR�(R�RBR�((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttest_svnoscK`sStjdkrOtddg|�\}}|sBt||f��td�ndS(NRtcls/Vscl ok(RR RRR�(R�RBR�((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttest_cltsR!RRsexec_command tests for t__main__RV(RR('t__doc__t __future__RRRt__all__RRR�tnumpy.distutils.misc_utilRRtnumpy.distutilsRtnumpy.distutils.compatRt numpy.compatR R RR)RRFRIRQRRaR`R�RbR�R�R�R�R�R ttesttNotImplementedErrorRX(((sO/opt/alt/python27/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt<module>3sN      3    W 4 ' � L 0