o
    "g*(                     @   s   d Z ddgZddlZddl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 Zd	d
 Zdd Zdd Zdi fddZdd Zdd Z		dddZdddZdd ZdS )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 <pearu@cens.ioc.ee>
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_commandfind_executable    N)is_sequencemake_temp_file)logc                 C   sP   t d}|du rd}| j|dd} | dd} | dd dkr&| dd } | S )	z
    Convert `bytes` in the encoding used by a subprocess into a filesystem-appropriate `str`.

    Inherited from `exec_command`, and possibly incorrect.
    FNasciireplaceerrors

)localegetpreferredencodingdecoder   )outputmylocale r   ^/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/numpy/distutils/exec_command.pyfilepath_from_subprocess_output?   s   
r   c                 C   s^   t tjdrtjj|  dS t tjdr"tj| tjj dS tj| jddd dS )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.
    bufferencodingutf8r   r	   N)hasattrsysstdoutr   writer   r   )valr   r   r   forward_bytes_to_stdoutP   s
   r   c                  C   s&   t jdtdd t \} }|   |S )NzLtemp_file_name is deprecated since NumPy v1.17, use tempfile.mkstemp instead   
stacklevel)warningswarnDeprecationWarningr   close)fonamer   r   r   temp_file_namec   s   
r(   c                  C   s\   t j} tjdv r,tj| \}}| dd}tj||} tj	| s,J d| f | S )N)ntdosPYTHONWPYTHONz%r is not a file)
r   
executableosr'   pathsplitupperr   joinisfile)	pythonexefdirfnr   r   r   get_pythonexek   s   
r7   c                 C   sp  | |f}z|| W S  t y   Y nw td|   | }|du r(tjdtj}tjdkr2tjj	}ndd }| 
drA| dd	 } d
g}tjdv r]tj| \}}g d}	| |	vr]|	}tj| rgd
g}
ndd |tjD }
|
D ]:}tj|| }|D ].}|| }tj|s||}tj|rt|tjrtd|  |||< |    S qqttd|  dS )zTReturn full path of a executable or None.

    Symbolic links are not followed.
    zfind_executable(%r)NPATHposixc                 S   s   | S Nr   )ar   r   r   <lambda>   s    z!find_executable.<locals>.<lambda>"r   r    )r)   r*   os2)z.exez.comz.batc                 S   s   g | ]}t j|qS r   )r.   r/   abspath).0pr   r   r   
<listcomp>   s    z#find_executable.<locals>.<listcomp>zFound executable %szCould not locate executable %s)KeyErrorr   debugr.   environgetdefpathr'   r/   realpath
startswithsplitextlowerisabsr0   pathsepr2   islinkr3   accessX_OKinfor#   )exer/   _cachekeyorig_exerI   suffixesr6   extextra_suffixespathssf_extr   r   r   r   t   sL   




	c                 C   s    t d|   dd | D }|S )Nz_preserve_environment(%r)c                 S   s   i | ]	}|t j|qS r   )r.   rF   rG   )rA   r'   r   r   r   
<dictcomp>   s    z)_preserve_environment.<locals>.<dictcomp>)r   rE   )namesenvr   r   r   _preserve_environment   s   r`   c                  K   s.   t d |  D ]\}}|pdtj|< q	d S )Nz_update_environment(...)r>   )r   rE   itemsr.   rF   )r_   r'   valuer   r   r   _update_environment   s   
rc   r>   r   c           
   
   K   s  t jdtdd td| ddd | D f  |du r$tjd	k}|du r-tjd	k}tj	
|}tj	
t }td
d dkrNtj	tj	
t}n"tj	dr[tj	
d}ntj	
tjd }tj	|rptj	|}||krt| td|  ntd|  tt| }tdi | z%t| f||d|}	W ||krt| td|  tdi | |	S ||krt| td|  tdi | w )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    zexec_command(%r,%s),c                 S   s   g | ]}d | qS )z%s=%rr   )rA   kvr   r   r   rC      s    z exec_command.<locals>.<listcomp>Nr9   ir   zexec_command.py.r   zNew cwd: %szRetaining cwd: %s)	use_shelluse_teezRestored cwd to %sr   )r"   r#   r$   r   rE   r2   ra   r.   r'   r/   r@   getcwd__name__dirname__file__r3   r   argvchdirr`   listkeysrc   _exec_command)
command
execute_inrg   rh   _with_pythonr_   oldcwdexec_diroldenvstr   r   r   r      sR    




c           	      K   s>  |du r	t jdk}|du rt jdk}t jdkr5|r5t jdd}t| r-|dd| g} n|d| g} d}nt jdkrHt| rHdd	d
 | D } |pKd}ztj| ||dtjtj	d}W n
 t
ye   Y dS w | \}}td}|du rwd}|j|dd}|dd}|dd dkr|dd }|r|rt| |j|fS )z0
    Internal workhorse for exec_command().
    Nr9   SHELLz/bin/shz-c Fr)   c                 s   s    | ]}t |V  qd S r:   )
_quote_arg)rA   argr   r   r   	<genexpr>  s    z _exec_command.<locals>.<genexpr>)shellr_   textr   stderr)   r>   r   r   r	   r   r   r   )r.   r'   rF   rG   r   r2   
subprocessPopenPIPESTDOUTOSErrorcommunicater   r   r   r   print
returncode)	rr   rg   rh   r_   shprocr   errr   r   r   r   rq      s@   





rq   c                 C   s   d| vrd| v rd|  S | S )zB
    Quote the argument for safe use in a shell command line.
    r=   rz   z"%s"r   )r|   r   r   r   r{   1  s   r{   )r>   NNr   )NN)__doc____all__r.   r   r   r   r"   numpy.distutils.misc_utilr   r   numpy.distutilsr   r   r   r(   r7   r   r`   rc   r   rq   r{   r   r   r   r   <module>   s*    3	3

L4