a
    œcl_ú"  ã                   @   s\   d 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„ Zdd„ Z	G dd	„ d	eƒZ
dS )
zSource level Python tests.é    N)ÚTestCasec            	      c   s’  t j t j t j t¡ddd¡¡} t j | ¡s6J | ƒ‚t  | ¡D ]6\}}}|D ]&}| d¡rNt j t j ||¡¡V  qNq@t j t j t j t¡ddddd¡¡}t j |¡s²J |ƒ‚t  	|¡D ]J}t j t j ||¡¡}t j 
|¡säq¼t  |¡}t j |¡ d¡r¼|V  q¼t j t j t j t¡dddddd¡¡}t j |¡sFJ |ƒ‚t  |¡D ]<\}}}|D ]*}| d¡r^t j t j ||¡¡V  q^qPdS )	z%Iterate over all Python source files.z..Zsambaz.pyÚbinzscripting/binZ
buildtoolsÚwafsambaN)ÚosÚpathÚabspathÚjoinÚdirnameÚ__file__ÚisdirÚwalkÚendswithÚlistdirÚislinkÚreadlink)	Zlibrary_dirÚrootÚdirsÚfilesÚfZbindirÚpÚtargetZwafsambadir© r   ú4/usr/lib/python3/dist-packages/samba/tests/source.pyÚget_python_source_files!   s*    "
&
(r   c                  c   sž   t ƒ D ]’} ztj| ddd}W nN tyl } z6|jtjkrVt d|  ¡ W Y d}~qn‚ W Y d}~n
d}~0 0 z| ¡ }W | 	¡  n
| 	¡  0 | |fV  qdS )z.Iterate over the contents of all python files.Úrzutf-8)ÚmodeÚencodingzsource file %s broken link?N)
r   ÚioÚopenÚIOErrorÚerrnoZENOENTÚwarningsÚwarnÚreadÚclose)Úfnamer   ÚeÚtextr   r   r   Úget_source_file_contents<   s    

r(   c                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )Ú
TestSourcec                 C   s²   g }t  dt j¡}tƒ D ]R\}}| d¡r,qd|v r6q| d¡rBq| d¡rNq| |¡}|s| |df¡ q|r®ddg}|D ] \}}| |¡ | d	| ¡ q||  d
 |¡¡ dS )z<Test that all Python files have a valid copyright statement.z#\s*copyright.*(?=
)zms_schema.pyr   ú"python/samba/tests/krb5/kcrypto.pyú)python/samba/tests/krb5/rfc4120_pyasn1.pyzno copyright line found
z:Some files have missing or incorrect copyright statements.Ú ú    Ú
N)	ÚreÚcompileÚIr(   r   ÚsearchÚappendÚfailr   )ÚselfÚ	incorrectZcopyright_rer%   r'   ÚmatchÚ	help_textZcommentr   r   r   Útest_copyrightP   s,    



þ
zTestSource.test_copyrightc                 C   s¦   g }d}t  t  |¡t j¡}tƒ D ]J\}}d|v r4q"| d¡r@q"| d¡rLq"| d¡rXq"| |¡s"| |¡ q"|r¢d|g}|D ]}| d| ¡ q~|  d 	|¡¡ d	S )
z.Test that all .py files have a GPL disclaimer.a{  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
r   z/python/samba/subunit/run.pyr*   r+   z3Some files have missing or incomplete GPL statementr-   r.   N)
r/   r0   ÚescapeÚ	MULTILINEr(   r   r2   r3   r4   r   )r5   r6   Zgpl_txtZgpl_rer%   r'   r8   r   r   r   Útest_gplq   s*    



ÿzTestSource.test_gplc                 C   s&   ||vr|g||< n||   |¡ d S )N)r3   )r5   Údict_r%   Úline_nor   r   r   Ú
_push_filež   s    zTestSource._push_filec                 C   s,   dd„ |  ¡ D ƒ}| ¡  |dd |¡  S )Nc                 S   s,   g | ]$\}}d |d  dd„ |D ƒ¡f ‘qS )z%s: %sz, c                 S   s   g | ]}t |d  ƒ‘qS )é   )Ústr)Ú.0Úir   r   r   Ú
<listcomp>¥   ó    z9TestSource._format_message.<locals>.<listcomp>.<listcomp>)r   )rB   r   Úlinesr   r   r   rD   ¥   s   ÿz.TestSource._format_message.<locals>.<listcomp>z

    %sz
    )ÚitemsÚsortr   )r5   r=   Úmessager   r   r   r   Ú_format_message¤   s
    ÿzTestSource._format_messagec                 c   s<   t ƒ D ]0\}}| d¡}t|ƒD ]\}}|||fV  q qd S )NT)r(   Ú
splitlinesÚ	enumerate)r5   r%   r'   rF   r>   Úliner   r   r   Ú_iter_source_files_linesª   s    
z#TestSource._iter_source_files_linesc                 C   sH   i }|   ¡ D ] \}}}d|v r|  |||¡ q|rD|  |  |d¡¡ dS )z-Check that there are no tabs in Python files.ú	zmTab characters were found in the following source files.
They should either be replaced by "\t" or by spaces:N)rN   r?   r4   rJ   )r5   Ztabsr%   r>   rM   r   r   r   Útest_no_tabs°   s    
ÿzTestSource.test_no_tabsc                 C   sT   i }|   ¡ D ],\}}}| d¡r*| d¡r|  |||¡ q|rP|  |  |d¡¡ dS )zCheck for unix new lines.r.   z
z;Non-unix newlines were found in the following source files:N)rN   r   r?   r4   rJ   )r5   Zillegal_newlinesr%   r>   rM   r   r   r   Útest_unix_newlines»   s    
ÿzTestSource.test_unix_newlinesc                 C   sP   i }|   ¡ D ](\}}}| d¡ d¡r|  |||¡ q|rL|  |  |d¡¡ dS )z<Check that there is not trailing whitespace in Python files.r.   ú z<Trailing whitespace was found in the following source files.N)rN   Úrstripr   r?   r4   rJ   )r5   Ztrailing_whitespacer%   r>   rM   r   r   r   Útest_trailing_whitespaceÅ   s    
ÿz#TestSource.test_trailing_whitespacec                 C   sœ   i }i }|   ¡ D ]Z\}}}|dkr$qt |¡jd@ }| d¡}|rT|sT|  |||¡ |s|r|  |||¡ q|r‚|  |  |d¡¡ |r˜|  |  |d¡¡ dS )zBCheck that files with shebang lines and only those are executable.r@   éI   z#!z0Files with shebang line that are not executable:z/Files without shebang line that are executable:N)rN   r   ÚstatÚst_modeÚ
startswithr?   r4   rJ   )r5   Zfiles_with_shebangZfiles_without_shebangr%   r>   rM   Ú
executableZhas_shebangr   r   r   Útest_shebang_linesÏ   s&    

ÿ
ÿzTestSource.test_shebang_linesN)Ú__name__Ú
__module__Ú__qualname__r9   r<   r?   rJ   rN   rP   rQ   rT   rZ   r   r   r   r   r)   N   s   !-

r)   )Ú__doc__r   r    r   r/   r!   Zsamba.testsr   r   r(   r)   r   r   r   r   Ú<module>   s   