B
    '(b                  @   sj  d dl m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Zd dl	Z	d dl
mZmZmZ d dlZd dlmZmZmZ d dlmZ d dlmZmZ d dlmZ d d	lmZmZ G d
d deZeejfeeeeef ddf dddZ ejddfeee!e!ddddZ"eddddZ#eeee ee f dddZ$eee dddZ%ddddZ&dS )     )contextmanagerN)PIPE)TupleList	Generator)package_pathpip_lockpip_timeout)try_find_python2_interpreter)DataDrivenTestCase	DataSuite)test_temp_dir)assert_string_arrays_equalperform_file_operationsc               @   s&   e Zd ZdgZdZeddddZdS )PEP561Suitezpep561.test.N)	test_casereturnc             C   s   t | d S )N)test_pep561)selfr    r   C/home/dcms/DCMS/lib/python3.7/site-packages/mypy/test/testpep561.pyrun_case   s    zPEP561Suite.run_case)__name__
__module____qualname__files	base_pathr   r   r   r   r   r   r      s   r   )python_executabler   c          	   c   s   t  }tjtjddd|  |gt ttd}|j	dkr`|j
d|jd }td| tjdkr|tjtj|d	d
fV  n|tjtj|dd
fV  W dQ R X dS )zyContext manager that creates a virtualenv in a temporary directory

    returns the path to the created Python executablez-m
virtualenvz-p)cwdstdoutstderrr   zutf-8z9Failed to create venv. Do you have virtualenv installed?
win32ZScriptspythonbinN)tempfileTemporaryDirectory
subprocessrunsys
executableosgetcwdr   
returncoder!   decoder"   	Exceptionplatformpathabspathjoin)r   venv_dirprocerrr   r   r   r   "   s    	


 r   TF)pkgr   use_pipeditabler   c       
      C   s  t jt| }t }|rB|dddg}|r6|d |d n"|dg}|rZ|d n
|d d|i}|t j y2t	j
ttd	 tj||tt|d
}W dQ R X W n4 t	jk
r }	 ztdt|	W dd}	~	X Y nX W dQ R X |jdkrt|jd|jd dS )z.Install a package from test-data/packages/pkg/z-mpipinstallz-er   zsetup.pydevelopZ	PIP_BUILD)timeout)r    r!   r"   envNzFailed to acquire {}r   zutf-8)r,   r2   r4   r   r&   r'   appendupdateenvironfilelockZFileLockr   r	   r(   r)   r   Timeoutr0   formatr.   r!   r/   r"   )
r8   r   r9   r:   Zworking_dirdirZinstall_cmdr?   r6   r7   r   r   r   install_package:   s0    


,rG   )testcaser   c          
   C   s  | j dk	stdd| j kr8t }|dkr>t  ntj}|dk	sNtdt	| j
d \}}t| j
d }d}d}x&|D ]}|d	krd}q||d
kr|d}q|W |g kstdt|}|\}	}
x|D ]}t||
|| qW t|}d|kpd|k }|rN| jd }t|ddd(}x | j
D ]}|| d q W W dQ R X || |dg |
tjkrv|d|
  |  }|g gkrg g| }x t|D ]\}}t| g }tj|\}}}xr||  D ]b}|ttj r||tttj d d n&| ttj td }||d qW |dkrHdnd|d  }|dkrf| j!n| j"#|d g }t$||d%| j&| j'| qW |rt(| W dQ R X dS )z;Test running mypy on files that depend on PEP 561 packages.Nztest was not properly set upZpython2zShould be impossibler      TFzno-pipr:   z(No packages to install for PEP 561 test?z-pz	--packagez.pywzutf-8)encoding
z--no-error-summaryz--python-executable=z
/ z on iteration zInvalid output ({}, line {}){}))Zold_cwdAssertionErrornamelowerr
   pytestskipr*   r+   
parse_pkgsinputparse_mypy_argsr   rG   listopenwriter@   extendZ
find_steps	enumerater   mypyapir)   
splitlines
startswithr   r,   seplenrstripreplaceoutputZoutput2getr   rE   filelineremove)rH   r$   pkgsZpip_argsZ	mypy_argsr9   r:   argvenvr5   r   r8   Zcmd_lineZhas_programprogramfsZstepsi
operationsrd   outr7   r.   rg   Z
iter_countexpectedr   r   r   r   ^   sf    



 

$"r   )commentr   c             C   sN   |  dsg g fS | dd  d^}}dd |dD dd |D fS d S )Nz# pkgs:   ;c             S   s   g | ]}|  qS r   )strip).0r8   r   r   r   
<listcomp>   s    zparse_pkgs.<locals>.<listcomp>,c             S   s   g | ]}|  qS r   )rv   )rw   rj   r   r   r   rx      s    )r_   split)rs   Zpkgs_strargsr   r   r   rT      s    
rT   )rg   r   c             C   s"   t d| }|sg S |d S )Nz# flags: (.*)$rI   )rematchgrouprz   )rg   mr   r   r   rV      s    rV   )r   c           
   C   s6  dst d} d}t }t }t| ztj|| |}t| tj|| |d}t	|ddd
  tj|d}t	|d"}|d	 |d
|  d W d Q R X t ^}|\}	}
g }|
tjkr|d|
  |d|d|g tj|\}}}|dkst W d Q R X W d t| X W d Q R X d S )NFpackagesaz__init__.pyrJ   utf8)rK   zmypy.iniz[mypy]
zmypy_path = ./rL   z--python-executable=z--config-filez	--packager   )rO   r&   r'   r,   r-   chdirr2   r4   makedirsrX   closerY   r   r*   r+   r@   rZ   r\   r]   r)   )r   pkg_nametemp_dirZold_dirZfull_pkg_nameZpkg_init_nameZmypy_config_pathZ	mypy_filerk   r5   r   Zcmd_line_argsrq   r7   r.   r   r   r   test_mypy_path_is_respected   s2    




r   )'
contextlibr   rC   r,   rR   r|   r(   r   r*   r&   typingr   r   r   Zmypy.apir\   Zmypy.test.configr   r   r	   Z	mypy.utilr
   Zmypy.test.datar   r   r   Zmypy.test.helpersr   r   r   r+   strr   boolrG   r   rT   rV   r   r   r   r   r   <module>   s4   
"!E 