B { `p(@sdZddgZddlZddlZddlZddlZddlZddlmZm Z ddl m Z ddZ d d Z d d Zd dZdifddZddZddZdddZdddZddZdS)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. exec_commandfind_executableN) is_sequencemake_temp_file)logcCsPtd}|dkrd}|j|dd}|dd}|dddkrL|dd}|S) z Convert `bytes` in the encoding used by a subprocess into a filesystem-appropriate `str`. Inherited from `exec_command`, and possibly incorrect. FNasciireplace)errorsz  )localegetpreferredencodingdecoder)outputmylocaler@/tmp/pip-unpacked-wheel-4iou4664/numpy/distutils/exec_command.pyfilepath_from_subprocess_output?s   rcCsZttjdrtjj|n:ttjdr@tj|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. bufferencodingutf8r)r N)hasattrsysstdoutrwriterr)valrrrforward_bytes_to_stdoutPs   rcCs&tjdtddt\}}||S)NzLtemp_file_name is deprecated since NumPy v1.17, use tempfile.mkstemp instead) stacklevel)warningswarnDeprecationWarningrclose)fonamerrrtemp_file_namecs   r%cCs\tj}tjdkrXtj|\}}|dd}tj||}tj |sXt d|f|S)N)ntdosZPYTHONWPYTHONz%r is not a file) r executableosr$pathsplitupperrjoinisfileAssertionError)Z pythonexeZfdirfnrrr get_pythonexeks r2c Csz||f}y||Stk r$YnXtd||}|dkrPtjdtj}tjdkrdtjj }ndd}| dr|dd }d g}tjd krtj |\}}d d dg} | | kr| }tj |rd g} ndd|tjD} x| D]x}tj||}xd|D]\} || } tj| s&|| } tj| rt| tjrtd| | ||<| SqWqWtd|dS)zTReturn full path of a executable or None. Symbolic links are not followed. zfind_executable(%r)NPATHposixcSs|S)Nr)arrrz!find_executable.."rr )r&r'Zos2z.exez.comz.batcSsg|]}tj|qSr)r*r+abspath).0prrr sz#find_executable..zFound executable %szCould not locate executable %s)KeyErrorrdebugr*environgetdefpathr$r+realpath startswithsplitextlowerisabsr,pathsepr.islinkr/accessX_OKinfor ) Zexer+_cachekeyZorig_exerCsuffixesr1extZextra_suffixespathssZf_extrrrrtsF          cCs td|dd|D}|S)Nz_preserve_environment(%r)cSsi|]}tj||qSr)r*r@rA)r;r$rrr sz)_preserve_environment..)rr?)namesenvrrr_preserve_environmentsrVcKs2tdx"|D]\}}|p"dtj|<qWdS)Nz_update_environment(...)r9)rr?itemsr*r@)rUr$valuerrr_update_environments rYr9rc 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|} Wd||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)rzexec_command(%r,%s),cSsg|] }d|qS)z%s=%rr)r;kvrrrr=sz exec_command..Nr4irzexec_command.py.rz New cwd: %szRetaining cwd: %s) use_shelluse_teezRestored cwd to %s)rr r!rr?r.rWr*r$r+r:getcwd__name__dirname__file__r/rargvchdirrVlistkeysrY _exec_command) commandZ execute_inr]r^Z _with_pythonrUZoldcwdZexec_dirZoldenvstrrrrsB               c KsD|dkrtjdk}|dkr$tjdk}tjdkrj|rjtjdd}t|rZ|dd|g}n |d|g}d}n&tjdkrt|rdd d |D}|pd}ytj|||tjtj dd }Wnt k rd SX| \}}t d}|dkrd }|j|dd}|dd}|dddkr&|dd}|r:|r:t||j|fS)z0 Internal workhorse for exec_command(). Nr4SHELLz/bin/shz-c Fr&css|]}t|VqdS)N) _quote_arg)r;argrrr sz _exec_command..)shellrUrstderruniversal_newlines)r9rr)r z r r )r*r$r@rArr. subprocessPopenPIPESTDOUTEnvironmentError communicater r rrprint returncode) rhr]r^rUshproctexterrrrrrrgs>          rgcCsd|krd|krd|S|S)zB Quote the argument for safe use in a shell command line. r8rkz"%s"r)rmrrrrl2srl)r9NNr)NN)__doc____all__r*rrsr rZnumpy.distutils.misc_utilrrZnumpy.distutilsrrrr%r2rrVrYrrgrlrrrr3s&  3 K 5