B
    XM\7*  ã               @   s°   d Z ddlmZmZmZ ddlZddlmZmZ ddl	Z	ddl
mZmZmZmZmZ ddd„Zdd	„ Zdd
d„Zddd„Zddd„Zddd„Zddd„Zddd„Zd dd„ZdS )!z)
Module to handle generating test files.
é    )Úabsolute_importÚdivisionÚprint_functionN)ÚjoinÚexists)Ú	copy_intoÚensure_listÚglobÚon_winÚrm_rfc          	   C   s†   |s| j j}d}tj |¡s&t |¡ xZt|  dg ¡ƒD ]F}d}tt	| j|ƒƒ}x,|D ]$}t
|| | j|¡| j jddd qVW q8W |S )a  
    Create the test files for pkg in the directory given.  The resulting
    test files are configuration (i.e. platform, architecture, Python and
    numpy version, ...) independent.
    Return False, if the package has no tests (for any configuration), and
    True if it has.
    Fz
test/filesT)ÚlockingZclobber)ÚconfigÚtest_dirÚosÚpathÚisdirÚmakedirsr   Ú	get_valuer	   r   r   ÚreplaceÚtimeout)Úmr   Z	has_filesÚpatternÚfilesÚf© r   ú6lib/python3.7/site-packages/conda_build/create_test.pyÚcreate_files   s    

r   c             C   s~   |rdnd}d| }| j rzd}xZ| j dg ¡D ]H}|  ¡ | d¡kr.| di ¡ dd¡}tj |¡d	  ¡ |kr.|}P q.W |S )
Nz.batz.shÚrun_testzno-fileÚoutputsÚnameÚtestÚscripté   )Ú	is_outputÚmetaÚgetr   r   r   ÚsplitextÚlower)r   Ú
win_statusÚextr   ÚoutÚout_test_scriptr   r   r   Ú_get_output_script_name%   s    r,   c       
   
   C   sv  |s| j j}d}tg}| jr$ddg}g }xF|D ]<}t| |ƒ}t||ƒ}tt| j|ƒƒrvtt| j|ƒ|| j j	dd t
j |¡dkrXt|  dg ¡ƒ}|rX|dkrÊd |r²dnd	¡}|tkrÊt||ƒ}tt||ƒd
ƒx}| d¡ |sô| d¡ | d¡ x2|D ]*}	| |	¡ | d¡ |r| d¡ qW |rD| d¡ n
| d¡ W d Q R X t
j |¡r0| |¡ q0W |S )Nzno-fileFT)r   Ztest_tmpztest/commandszrun_test.{}ZbatZshÚaz

z	set -ex

Ú
z IF %ERRORLEVEL% NEQ 0 exit /B 1
z
exit /B 0
zexit 0
)r   r   r
   Znoarchr,   r   r   r   r   r   r   Úbasenamer   r   ÚformatÚopenÚwriteÚisfileÚappend)
r   r   r   r(   Úshell_filesZstatusZ	dest_fileZcommandsr   Úcmdr   r   r   Úcreate_shell_files6   sF    








r7   ú# c          
   C   sŠ  d| }| j r`d}xL| j dg ¡D ]:}|  ¡ | d¡kr"| di ¡ dd¡}| |¡r"|}P q"W t|d| ƒ}|r^tj | j|¡}tj |¡r^t	|dƒ¼}	|	 
d	||  ¡ f ¡ |	 
d
|  ¡  ¡ yZt	|ƒH}
|	 
d |¡¡ |	 
d ||¡¡ |	 
|
 ¡ ¡ |	 
d ||¡¡ W d Q R X W n  tk
r@   |	 
d¡ Y nX |	 
d|  ¡  ¡ W d Q R X |t|ƒo†tj |¡o†tj |¡dkfS )Nr   Ú r   r   r    r!   zno-fileÚwz,%s tests for %s (this is a generated file);
z*print('===== testing package: %s =====');
zprint('running {0}');
z{0} --- {1} (begin) ---
z{0} --- {1} (end) ---
z># tests were not packaged with this module, and cannot be run
z
print('===== %s OK =====');
)r#   r$   r%   r   Úendswithr   r   r   r3   r1   r2   Zdistr0   ÚreadÚAttributeErrorÚboolr/   )r   r   r)   Zcomment_charr   r*   r+   Zout_fileZ	test_fileÚfoZfir   r   r   Ú_create_test_filesa   s4    

 r@   c          	   C   s   |s| j j}t| |dƒ\}}|  ¡ }| d¡}| d¡}| d¡}|pL|pL|}|ržg }	xdt|  dg ¡ƒD ]0}
t|
dƒrhd|
krh|
d dkrh|	 |
d	 ¡ qhW nt|  dg ¡ƒ}	d
d„ |	D ƒ}	|	rt	|dƒ:}x2|	D ]*}| 
d| ¡ | 
d| ¡ | 
d¡ qÔW W d Q R X |s|	r|S dS )Nz.pyzr-zlua-zperl-ztest/importsÚkeysÚlangÚpythonÚimportsc             S   s.   g | ]&}t |d ƒr&d|kr|d dkr|‘qS )rA   rB   rC   )Úhasattr)Ú.0Úitemr   r   r   ú
<listcomp>™   s    z#create_py_files.<locals>.<listcomp>r-   zprint("import: %r")
z
import %s
r.   F)r   r   r@   r   Ú
startswithr   r   rE   Úextendr1   r2   )r   r   ÚtfÚ	tf_existsÚpkg_nameZlikely_r_pkgZlikely_lua_pkgZlikely_perl_pkgZlikely_non_python_pkgrD   Úimport_itemr?   r   r   r   r   Úcreate_py_files‚   s.    



rO   c          	   C   sà   |s| j j}t| |dƒ\}}d }|  ¡  d¡r@t|  dg ¡ƒ}nBx@t|  dg ¡ƒD ],}t|dƒrRd|krR|d dkrR|d }P qRW |rÐt|dƒ:}x2|D ]*}| 	d	| ¡ | 	d
| ¡ | 	d¡ q˜W W d Q R X |sØ|rÜ|S dS )Nz.rzr-ztest/importsrA   rB   ÚrrD   r-   zprint("library(%r)")
zlibrary(%s)
r.   F)
r   r   r@   r   rI   r   r   rE   r1   r2   )r   r   rK   rL   rD   rN   r?   r   r   r   r   Úcreate_r_files¤   s$    
rQ   c          	   C   s  |s| j j}t| |dƒ\}}d }|  ¡  d¡r@t|  dg ¡ƒ}nBx@t|  dg ¡ƒD ],}t|dƒrRd|krR|d dkrR|d }P qRW |sŒ|rt|dƒl}t	d	|  
¡  d
¡ |d |rþxF|D ]>}t	d| |d t	d| |d d|kr¼t	d |¡|d q¼W W d Q R X |s|r|S dS )Nz.plzperl-ztest/importsrA   rB   ZperlrD   r-   zmy $expected_version = "%s";Ú0)Úfilezprint("import: %s\n");zuse %s;
ú a  if (defined {0}->VERSION) {{
	my $given_version = {0}->VERSION;
	$given_version =~ s/0+$//;
	die('Expected version ' . $expected_version . ' but found ' . $given_version) unless ($expected_version eq $given_version);
	print('	using version ' . {0}->VERSION . '
');

}}F)r   r   r@   r   rI   r   r   rE   r1   ÚprintÚversionÚrstripr0   )r   r   rK   rL   rD   rN   r?   r   r   r   r   Úcreate_pl_files¾   s0    

rX   c          	   C   sÚ   |s| j j}t| |dƒ\}}d }|  ¡  d¡r@t|  dg ¡ƒ}nBx@t|  dg ¡ƒD ],}t|dƒrRd|krR|d dkrR|d }P qRW |rÊt|dƒ4}x,|D ]$}t	d	| |d
 t	d| |d
 q˜W W d Q R X |sÒ|rÖ|S dS )Nz.luazlua-ztest/importsrA   rB   ZluarD   za+zprint("require \"%s\"\n");)rS   zrequire "%s"
F)
r   r   r@   r   rI   r   r   rE   r1   rU   )r   r   rK   rL   rD   rN   r?   r   r   r   r   Úcreate_lua_filesà   s"    
rY   c       
   	   C   s²   |r^t |ƒ t |¡ | j di ¡ dg ¡}|rfttj |d¡dƒ}t 	||¡ W d Q R X n| j
j}t| |ƒ}t| |ƒ}t| |ƒ}t| |ƒ}t| |ƒ}t| |ƒ}	||||||	fS )Nr    Zrequiresztest_time_dependencies.jsonr:   )r   r   r   r$   r%   r1   r   r   ÚjsonÚdumpr   r   r   rX   rO   rQ   rY   r7   )
r   r   Z	test_depsr   r   Zpl_filesZpy_filesZr_filesZ	lua_filesr5   r   r   r   Úcreate_all_test_filesõ   s    






r\   )N)N)r8   )N)N)N)N)N)Ú__doc__Z
__future__r   r   r   r   Úos.pathr   r   rZ   Zconda_build.utilsr   r   r	   r
   r   r   r,   r7   r@   rO   rQ   rX   rY   r\   r   r   r   r   Ú<module>   s   

+
!
"

"
