Current File : //usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyc
�
E�`Qc@s�dZddgZddlZddlZddlZddlmZmZddlm	Z	ddl
mZddlm
Z
d	�Zd
�Zd�Zdid�Zd
�Zd�Zd�Zddddd�Zddd�Zdd�Zd�Zddd�Zd�Zd�Zd�Zd�Zd�Zej dkrNeZ!n*ej d#krfeZ!ne"dej ��e#d kr�e!d!d"�e!d!d�ed!d"�ed!d�ed!d�ed!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.
texec_commandtfind_executablei����N(tis_sequencetmake_temp_file(tlog(t
get_exception(topen_latin1cCst�\}}|j�|S(N(Rtclose(tfotname((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttemp_file_name<s
cCs�tj}tjdkr�tjj|�\}}|j�jdd�}tjj||�}tjj	|�s�t
d|f��n|S(NtnttdostPYTHONWtPYTHONs%r is not a file(sntR(tsyst
executabletosR	tpathtsplittuppertreplacetjointisfiletAssertionError(t	pythonexetfdirtfn((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt
get_pythonexeAs	(cCs)ddl}|jdt�tj|�S(Ni����s+splitcmdline is deprecated; use shlex.split(twarningstwarntDeprecationWarningtshlexR(tlineR((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pytsplitcmdlineJs	cCs||f}y||SWntk
r+nXtjd|�|}|dkrjtjjdtj�}ntjdkr�tj	j
}n	d�}|jd�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)tPATHtposixcSs|S(N((ta((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt<lambda>bst"ii����tRRtos2s.exes.coms.batsFound executable %ssCould not locate executable %sN(sntsdossos2(tKeyErrorRtdebugtNoneRtenvirontgettdefpathR	Rtrealpatht
startswithtsplitexttlowertisabsRtpathseptabspathRtislinkRtaccesstX_OKtgoodR(texeRt_cachetkeytorig_exeR0tsuffixesRtexttextra_suffixestpathstptstf_ext((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyRPsF
		1


'
cCsBtjd|�i}x$|D]}tjj|�||<qW|S(Ns_preserve_environment(%r)(RR+RR-R.(tnamestenvR	((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt_preserve_environment�s

cKsAtjd�x-|j�D]\}}|p/dtj|<qWdS(Ns_update_environment(...)R((RR+titemsRR-(RGR	tvalue((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt_update_environment�s
cCsCt|d�r;y|j�}tSWq?tk
r7tSXntSdS(sT
    Returns True if 'stream' supports the file descriptor and allows fileno().
    tfilenoN(thasattrRLtTruetIOErrortFalse(tstreamtr((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt_supports_fileno�s
R(ic	Ksbtjd|djg|j�D]}d|^q�f�|dkr[tjdk}n|dkrytjdk}ntjj|�}tjjtj	��}t
ddkr�tjjtjjt��}ngtjj
d�r�tjjd�}n@tjjtjd	�}tjj
|�r;tjj|�}n||krhtj|�tjd
|�ntjd|�t|j��}	t|�z�|r�ttj�r�tjj�dkr�t|d
||�}
nHtjdkrt|d|d||�}
nt|d|d||�}
Wd||krStj|�tjd|�nt|	�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.
    sexec_command(%r,%s)t,s%s=%rR$i�Rsexec_command.pyt.isNew cwd: %ssRetaining cwd: %si����texec_command_dirt	use_shelltuse_teeNsRestored cwd to %s(RR+RRIR,RR	RR6tgetcwdt__name__tdirnamet__file__RRtargvtchdirRHtkeysRKRStstdoutRLt_exec_command_pythont_exec_command_posixt
_exec_command(tcommandt
execute_inRWRXt_with_pythonRGtkvtoldcwdtexec_dirtoldenvtst((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyR�sN1!


		
c
Ks�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)RWRRi����s
(RR+RRtlistR
R,RtsystemRRcRtreadtintRtremove(
RdRWRXRGtcommand_strttmpfiletstsfiletfiltert
command_posixtstatustftstatus_textttext((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyRb�sF
				



cKs�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
simport 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 failedRR(RR+RR
topentwriteRR-RRntRuntimeErrorRqRRpRo(RdRVRGt
python_exetcmdfileRttoutfileRxtcmdRwRz((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyRas>
				









cCs(|ddkr$d|kr$d|S|S(NiR'Rls"%s"((targ((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt	quote_argDscKs�tjd�|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�|d}|stt|�|d<n|
�|�|r�tj|j�|�n|	r�|r�tj|j�|�q�tj|j�|�ny|tj||tj�}WnDt k
rDt!t"��}d}tjj#d||df�nX|
�|�|rltj|
|�n|	r�tj||�n|j$�t%|d�}|j&�}|j$�tj'|�|r8|j$�t%|d�}|j&�}|j$�tj'|�|r8|r8|r#|d}n||}|GHq8n|ddkrU|d }n|dkrjd}n|rx|GHn||fS(Ns_exec_command(...)R$itSHELLs/bin/shs-cRltspawnvpesExecutable %s does not existRRtCOMSPECs/CiR{sRunning %s(%s,%r,%r,os.environ)i�s%s: %sRRs
i����(sntsdos((RR+R,RR	R-R.RRRmR RRMR�tspawnveRRRRRSRR`tstderrtflushRLtdupR
R|RZtP_WAITR�tdup2tOSErrortstrRR}RRRoRq(RdRWRXRGt
using_commandtshR]t
spawn_commandt_so_has_filenot_se_has_filenotso_flushtse_flusht	so_filenotso_dupt	se_filenotse_dupR�tfoutterrfiletferrtargv0RwterrmessRz((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyRcIs�
!
				

"









	cKst�}td�}|dk}|r�tjd�t|d�\}}|dkrb|dkstt||f��t|ddd�\}}|dkr�|dks�t||f��d	tjd
<t|d�\}}|dkr�|d	kst||f��t|dd
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��dGHdS(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(RRRRRRRR-(tkwsRR�tusing_cygwin_echoRDto((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttest_nt�sL	
**
**-**$***cKs�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��tdd
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��dGHdS(Ns
echo HelloitHellos	echo $AAAR(secho "$AAA"R�R�R�R�secho "$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�RDR�((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt
test_posixs<****
******$***cKst�}t�}tjj|�}tjj|�}t|d�}|jd�|j�t	d||f|�\}}|r�|dks�t
||f��t	d||fd||�\}}|dkr�|dks�t
||f��tj|�dGHdS(	NR{R�s:%s -c "print 'Ignore the following IOError:',open(%r,'r')"R(s!%s -c "print open(%r,'r').read()"ReiR�(RR
RRtbasenameR[R|R}RRRRq(R�RRsRttmpdirRxRDR�((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttest_execute_in<s		

$*
cKs<tddg|�\}}|s3t||f��dGHdS(NtsvnRwssvn ok(RR(R�RDR�((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttest_svnNscKsNtjdkrJtddg|�\}}|sBt||f��dGHndS(NRtcls/Vscl ok(RR	RR(R�RDR�((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyttest_clSsR$RRsexec_command tests for t__main__RXi(sntsdos($t__doc__t__all__RRR tnumpy.distutils.misc_utilRRtnumpy.distutilsRtnumpy.distutils.compatRtnumpy.compatRR
RR"R,RRHRKRSRRbRaR�RcR�R�R�R�R�R	ttesttNotImplementedErrorRZ(((sB/usr/lib64/python2.7/site-packages/numpy/distutils/exec_command.pyt<module>.sN				3				J4'	w	L	0