U C^*@sdZddlmZmZmZddgZddlZddlZddlZddl Z ddl Z ddl m Z m Z ddlmZdd Zd d Zd d ZddZdifddZddZddZdddZdddZddZdS)a0 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 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. )divisionabsolute_importprint_function exec_commandfind_executableN) is_sequencemake_temp_file)logcCshtd}|dkrd}|j|dd}|dd}|dddkrL|dd}tjd krd|jddd}|S) z Convert `bytes` in the encoding used by a subprocess into a filesystem-appropriate `str`. Inherited from `exec_command`, and possibly incorrect. FNasciireplaceerrors  r)localegetpreferredencodingdecoder sys version_infoencode)outputmylocaler?/tmp/pip-install-6_kvzl1k/numpy/numpy/distutils/exec_command.pyfilepath_from_subprocess_outputAs    rcCsttjjdkrtj|nVttjdr6tjj|n:ttjdrZtj|tjjntj|jddddS)z Forward bytes from a subprocess call to the console, without attempting to decode them. The assumption is that the subprocess call already returned bytes in a suitable encoding. rbufferencodingutf8r r N) rrmajorstdoutwritehasattrrrr)valrrrforward_bytes_to_stdoutVs   r&cCs&tjdtddt\}}||S)NzLtemp_file_name is deprecated since NumPy v1.17, use tempfile.mkstemp instead stacklevel)warningswarnDeprecationWarningrclose)fonamerrrtemp_file_namels r0cCs\tj}tjdkrXtj|\}}|dd}tj||}tj |sXt d|f|S)N)ntdosZPYTHONWPYTHONz%r is not a file) r executableosr/pathsplitupperr joinisfileAssertionError)Z pythonexeZfdirfnrrr get_pythonexets r=c Cs|||f}z ||WStk r&YnXtd||}|dkrRtjdtj}tjdkrftjj }ndd}| dr|dd }d g}tjd krtj |\}}d d dg} | | kr| }tj |rd g} ndd|tjD} | D]|}tj||}|D]d} || } tj| s$|| } tj| rt| tjrtd| | ||<| Sqqtd|dS)zTReturn full path of a executable or None. Symbolic links are not followed. zfind_executable(%r)NPATHposixcSs|SNr)arrrz!find_executable.."r'r)r1r2Zos2z.exez.comz.batcSsg|]}tj|qSr)r5r6abspath).0prrr sz#find_executable..zFound executable %szCould not locate executable %s)KeyErrorr debugr5environgetdefpathr/r6realpath startswithsplitextlowerisabsr7pathsepr9islinkr:accessX_OKinfor+) Zexer6_cachekeyZorig_exerOsuffixesr<extZextra_suffixespathssZf_extrrrr}sF         cCs td|dd|D}|S)Nz_preserve_environment(%r)cSsi|]}|tj|qSr)r5rLrM)rGr/rrr sz)_preserve_environment..)r rK)namesenvrrr_preserve_environmentsrbcKs.td|D]\}}|p dtj|<qdS)Nz_update_environment(...)rE)r rKitemsr5rL)rar/valuerrr_update_environments rerEr'c Ksztjdtddtd|ddd|Df|dkrHtjd k}|dkrZtjd k}tj |}tj t }t d dd krtj tj t}nDtj d rtj d }n*tj tjd}tj |rtj |}||krt|td|ntd|tt|}tf|zt|f||d|} W5||krjt|td|tf|X| S)a Return (status,output) of executed command. .. deprecated:: 1.17 Use subprocess.Popen instead 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. zJexec_command is deprecated since NumPy v1.17, use subprocess.Popen insteadr'r(zexec_command(%r,%s),cSsg|] }d|qS)z%s=%rr)rGkvrrrrIsz exec_command..Nr?irzexec_command.py.rz New cwd: %szRetaining cwd: %szRestored cwd to %s) use_shelluse_tee)r*r+r,r rKr9rcr5r/r6rFgetcwd__name__dirname__file__r:rargvchdirrblistkeysre _exec_command) commandZ execute_inrirjZ _with_pythonraZoldcwdZexec_dirZoldenvstrrrrsL              c Ks`|dkrtjdk}|dkr$tjdk}tjdkrj|rjtjdd}t|rZ|dd|g}n |d|g}d}n&tjdkrt|rdd d |D}|pd}ztj|||tjtj dd }Wnt k rYd SX| \}}t d}|dkrd }|j|dd}|dd}|dddkr(|dd}tjdkrB|jd dd}|rV|rVt||j|fS)z0 Internal workhorse for exec_command(). Nr?SHELLz/bin/shz-c Fr1css|]}t|VqdSr@) _quote_arg)rGargrrr sz _exec_command..)shellrar"stderruniversal_newlines)rEr r r rrrr)r5r/rLrMrr9 subprocessPopenPIPESTDOUTEnvironmentError communicaterrrr rrrprint returncode) rtrirjrashproctexterrrrrrrssD           rscCsd|krd|krd|S|S)zB Quote the argument for safe use in a shell command line. rDrwz"%s"r)ryrrrrx@srx)rENNr')NN)__doc__ __future__rrr__all__r5rrrr*Znumpy.distutils.misc_utilrrZnumpy.distutilsr rr&r0r=rrbrerrsrxrrrrs*3  3 L :