B
    v9a)                 @   s   d dl Z d dlZd dl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	 d dl
mZmZ dadd ZG dd de ddZd	d
 Zdd Zdd Zdd ZejejdddZdd Zd!ddZd"ddZd#ddZd$dd ZdS )%    N)source_from_cache)make_legacy_pycstrip_python_stderrc               C   sV   t dkrRdtjkrda dS yttjdddg W n tjk
rL   da Y nX da t S )a   
    Returns True if our sys.executable interpreter requires environment
    variables in order to be able to run at all.

    This is designed to be used with @unittest.skipIf() to annotate tests
    that need to use an assert_python*() function to launch an isolated
    mode (-I) or no environment mode (-E) sub-interpreter process.

    A normal build & test does not run into this situation but it can happen
    when trying to run the standard library test suite from an interpreter that
    doesn't have an obvious home with Python's current home finding logic.

    Setting PYTHONHOME is one way to get most of the testsuite to run in that
    situation.  PYTHONPATH or PYTHONUSERSITE are other common environment
    variables that might impact whether or not the interpreter can start.
    NZ
PYTHONHOMETz-Ez-czimport sys; sys.exit(0)F)$__cached_interp_requires_environmentosenviron
subprocessZ
check_callsys
executableZCalledProcessError r   r   #/usr/lib/python3.7/script_helper.py interpreter_requires_environment   s    


r   c               @   s   e Zd ZdZdd ZdS )_PythonRunResultz2Helper for reporting Python subprocess run resultsc             C   s   d}| j | j }}t||kr0d|| d  }t||krNd|| d  }|dd }|dd }td| j|||f dS )z4Provide helpful details about failed subcommand runsi@  s   (... truncated stdout ...)Ns   (... truncated stderr ...)asciireplacezRProcess return code is %d
command line: %r

stdout:
---
%s
---

stderr:
---
%s
---)outerrlendecoderstripAssertionErrorrc)selfcmd_linemaxlenr   r   r   r   r   fail;   s    z_PythonRunResult.failN)__name__
__module____qualname____doc__r   r   r   r   r   r   8   s   r   )r   r   r   c           	   O   s&  t  }|dd }d|kr&|d}n| o0| }tjddg}|rN|d n|s`|s`|d |dd ri }tjdkrtjd	 |d	< n
tj }d
|krd|d
< |	| |
|  tj|tjtjtj||d}|* z| \}}	W d |  t  X W d Q R X |j}
t|	}	t|
||	|fS )NZ__cwdZ
__isolatedz-XZfaulthandlerz-Iz-EZ
__cleanenvwin32Z
SYSTEMROOTTERM )stdinstdoutstderrenvcwd)r   popr	   r
   appendplatformr   r   copyupdateextendr   PopenPIPEZcommunicatekill_cleanup
returncoder   r   )argsenv_varsZenv_requiredr'   isolatedr   r&   procr   r   r   r   r   r   run_python_until_endX   s<    






r7   c             O   s0   t ||\}}|jr| s"|js,| s,|| |S )N)r7   r   r   )Zexpected_successr3   r4   Zresr   r   r   r   _assert_python   s    
r8   c              O   s   t d| |S )a|  
    Assert that running the interpreter with `args` and optional environment
    variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
    stderr) tuple.

    If the __cleanenv keyword is set, env_vars is used as a fresh environment.

    Python is started in isolated mode (command line option -I),
    except if the __isolated keyword is set to False.
    T)T)r8   )r3   r4   r   r   r   assert_python_ok   s    r9   c              O   s   t d| |S )z
    Assert that running the interpreter with `args` and optional environment
    variables `env_vars` fails (rc != 0) and return a (return code, stdout,
    stderr) tuple.

    See assert_python_ok() for more options.
    F)F)r8   )r3   r4   r   r   r   assert_python_failure   s    r:   )r$   r%   c             O   sX   t jg}t s|d || |dttj}d|d< t	j
|ft	j| |d|S )zRun a Python subprocess with the given arguments.

    kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
    object.
    z-Er&   Zvt100r!   )r#   r$   r%   )r	   r
   r   r)   r-   
setdefaultdictr   r   r   r.   r/   )r$   r%   r3   kwr   r&   r   r   r   spawn_python   s    

r>   c             C   s2   | j   | j }| j  |   t  |S )z?Run the given Popen process until completion and return stdout.)r#   closer$   readwaitr   r1   )pdatar   r   r   kill_python   s    


rD   Fc             C   sP   |}|s|t jd 7 }t j| |}t|ddd}|| |  t  |S )Npywzutf-8)encoding)	r   extseppathjoinopenwriter?   	importlibinvalidate_caches)Z
script_dirscript_basenamesourceZomit_suffixZscript_filenamescript_nameZscript_filer   r   r   make_script   s    
rR   c       	      C   s   |t j d }t j| |}t|d}|d kr~|t j}t|dkrr|d dkrrt	t
|}t j|}|}nt j|}||| |  |t j||fS )NziprF      __pycache__)r   rH   rI   rJ   zipfileZipFilesplitsepr   r   r   basenamerL   r?   )	zip_dirzip_basenamerQ   Zname_in_zipzip_filenamezip_namezip_filepartsZ
legacy_pycr   r   r   make_zip_script   s    rb   r"   c             C   s   t |  t| d| d S )N__init__)r   mkdirrR   )Zpkg_dirZinit_sourcer   r   r   make_pkg   s    
re      c                s0  g }t | dd}|| tj|}	t | ||}
||
 |rjtj|dd}tj|
dd}
|||
f  fddtd|d D }tj	|d tj|
}|tj
 d	 }tj	| |}t|d
}x&|D ]}tj	||	}||| qW ||
| |  x|D ]}t| q
W |tj	||fS )Nrc   r"   T)doraisec                s   g | ]}t j g| qS r   )r   rZ   rJ   ).0i)pkg_namer   r   
<listcomp>   s    z make_zip_pkg.<locals>.<listcomp>rf   rS   rF   )rR   r)   r   rI   r[   
py_compilecompiler-   rangerJ   rH   rW   rX   rL   r?   unlink)r\   r]   rj   rO   rP   depthZcompiledrp   Z	init_nameZinit_basenamerQ   Z	pkg_namesZscript_name_in_zipr^   r_   r`   nameZinit_name_in_zipr   )rj   r   make_zip_pkg   s.    



rs   )F)N)r"   )rf   F)collectionsrM   r	   r   Zos.pathr   rm   rW   importlib.utilr   Ztest.supportr   r   r   r   
namedtupler   r7   r8   r9   r:   r/   ZSTDOUTr>   rD   rR   rb   re   rs   r   r   r   r   <module>   s.   $4



