B
    i\|?                 @   s  d Z ddlmZ 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	Z	ddl
Z
ddlZddlmZ ddlmZ yddlZW n* ek
r   ejdk r nddlZY nX ddlZddlZddlZddlmZ dd	lmZ eejZeed
d ZdadadZ dd Z!eddk	Z"eddk	Z#eddk	Z$ed
dk	Z%dd Z&dd Z'dd Z(d.ddZ)d/ddZ*dd Z+e,ej-dsG d d! d!Z.G d"d# d#e.Z/G d$d% d%e/Z0G d&d' d'ej-Z1G d(d) d)e2Z3G d*d+ d+e2Z4G d,d- d-e1Z5dS )0z$Utilities for PyTables' test suites.    )print_function)absolute_importN)LooseVersion)resource_filename)      )detect_number_of_cores)min_blosc_bitshuffle_versionblosc   Fc             C   sF   d| krda | d d| kr,da | d d| krBda| d | S )NverboseTZsilentFz--heavy)r   removeheavy)argv r   2lib/python3.7/site-packages/tables/tests/common.py
parse_argv:   s    


r   zliblzobzip2c             C   s"   | rt d nt d t d d S )Nz#Performing the complete test suite!aQ  Performing only a light (yet comprehensive) subset of the test suite.
If you want a more complete test, try passing the --heavy flag to this script
(or set the 'heavy' parameter in case you are using tables.test() call).
The whole suite will take more than 4 hours to complete on a relatively
modern CPU and around 512 MB of main memory.zL-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=)print)r   r   r   r   print_heavyR   s
    
r   c              C   s4  t d t dtj  t dtdd   t dtj  td} tjrlt }t	d|d	 }d
| }nd}t dtj|f  | dk	rt d| d df  td} | dk	rt d| d | d f  td} | dk	rt d| d | d f  td} | dk	r| d 
 d }t d| d |f  t }dd t| D }t dd|  dg}| d tkrx|d t dd|  yd	dlm} t d|  W n   Y nX t d tj  t d!t   t d"tj  t d#t   t d$t   t d%t   t d&t   t d tj  dS )'z;Print all the versions of software that PyTables relies on.zL-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=zPyTables version:    %szHDF5 version:        %sZhdf5r   zNumPy version:       %sr   z[0-9.]+r   zusing VML/MKL %sznot using Intel's VML/MKLzNumexpr version:     %s (%s)NzZlib version:        %s (%s)zin Python interpreterr   zLZO version:         %s (%s)r   r   zBZIP2 version:       %s (%s)r
   zBlosc version:       %s (%s)c             S   s    g | ]\}}d ||d f qS )z%s (%s)r   r   ).0kvr   r   r   
<listcomp>~   s    z"print_versions.<locals>.<listcomp>zBlosc compressors:   %sz, ZshuffleZ
bitshufflezBlosc filters:       %s)__version__zCython version:      %szPython version:      %szPlatform:            %szByte-ordering:       %szDetected cores:      %szDefault encoding:    %szDefault FS encoding: %szDefault locale:      (%s, %s))r   tablesr   which_lib_versionnumpynumexprZuse_vmlZget_vml_versionrefindallsplitZblosc_get_complib_infosorteditemsjoinr	   appendZCythonsysversionplatform	byteorderr   getdefaultencodinggetfilesystemencodinglocaleZgetdefaultlocalestdoutflush)ZtinfoZvml_versionZ	vml_availZ
blosc_dateZblosc_cinfoZblosc_finfoZcython_versionr   r   r   print_versions_   sZ    








r1   c             C   s
   t d| S )Nztables.tests)r   )filenamer   r   r   test_filename   s    r3   c             C   s&   t sdS |rt| dd nt|  dS )z4Print out the `string` if verbose output is enabled.N )end)r   r   )stringZnonlr   r   r   verbosePrint   s
    r7   r   c             C   s`  t |ds| |kS t | dr&| jdkrBt |dr:|jdkrB| |kS | j|jkrhtrdtd| jd|j dS t |dr| jjdd |jjdd krtrtd	| jd|j dS t| jdkr| d |d krdS trtd| jd|j dS | jdkr6|jdkrdS tr2td
 tdt| j tdt|j dS | |k}t	
|}|s\tr\td |S )z*Checks if two numerical objects are equal.shaper   zShape is not equal:z!=r   dtyper   Nzdtype is not equal:zlength is not equalzlen(a.data) ==>zlen(b.data) ==>z,Some of the elements in arrays are not equal)hasattrr8   r   r   r9   strlensizedatar   all)abZflavorresultr   r   r   allequal   s@    
*
rC   c             C   sL   t | }t |}t| dr&| j|jks>t||s>t||s>dS t| |kS )zAre both `arr1` and `arr2` equal arrays?

    Arguments can be regular NumPy arrays, chararray arrays or
    structured arrays (including structured record arrays). They are
    checked for type and value equality.

    r9   F)typer:   r9   
issubclassr   r?   )Zarr1Zarr2Zt1Zt2r   r   r   areArraysEqual   s    	rF   assertWarnsc               @   s   e Zd Zdd Zdd ZdS )_BaseTestCaseContextc             C   s
   || _ d S )N)	test_case)selfrI   r   r   r   __init__   s    z_BaseTestCaseContext.__init__c             C   s    | j | j|}| j |d S )N)rI   Z_formatMessagemsgZfailureException)rJ   ZstandardMsgrL   r   r   r   _raiseFailure   s    z"_BaseTestCaseContext._raiseFailureN)__name__
__module____qualname__rK   rM   r   r   r   r   rH      s   rH   c               @   s   e Zd ZdddZdd ZdS )_AssertRaisesBaseContextNc             C   sv   t | | || _|| _|d k	rNy|j| _W qT tk
rJ   t|| _Y qTX nd | _|d k	rft	|}|| _
d | _d S )N)rH   rK   expectedrI   rN   obj_nameAttributeErrorr;   r!   compileexpected_regexrL   )rJ   rR   rI   callable_objrV   r   r   r   rK      s    
z!_AssertRaisesBaseContext.__init__c          	   C   s8   |dkr| dd| _| S |  ||| W dQ R X dS )z
            If callable_obj is None, assertRaises/Warns is being used as a
            context manager, so check for a 'msg' kwarg and return self.
            If callable_obj is not None, call it passing args and kwargs.
            NrL   )poprL   )rJ   namerW   argskwargsr   r   r   handle  s
    z_AssertRaisesBaseContext.handle)NN)rN   rO   rP   rK   r\   r   r   r   r   rQ      s    
rQ   c               @   s   e Zd Zdd Zdd ZdS )_AssertWarnsContextc             C   sV   x(t tj D ]}t|dd ri |_qW tjdd| _| j	 | _t
d| j | S )N__warningregistry__T)recordalways)listr(   modulesvaluesgetattrr^   warningscatch_warningswarnings_manager	__enter__simplefilterrR   )rJ   r   r   r   r   rh     s    
z_AssertWarnsContext.__enter__c             C   s  | j ||| |d k	rd S y| jj}W n tk
rF   t| j}Y nX d }xb| jD ]X}|j}t|| jslqT|d krx|}| j	d k	r| j	
t|sqT|| _|j| _|j| _d S W |d k	r| d| j	jt| | jr| d|| j n| d| d S )Nz"{0}" does not match "{1}"z{0} not triggered by {1}z{0} not triggered)rg   __exit__rR   rN   rT   r;   re   message
isinstancerV   searchZwarningr2   linenorM   formatpatternrS   )rJ   exc_type	exc_valuetbZexc_nameZfirst_matchingmwr   r   r   rj   #  s:    
z_AssertWarnsContext.__exit__N)rN   rO   rP   rh   rj   r   r   r   r   r]     s   	r]   c                   s^   e Zd Z fddZdd Zdd Zdd Zeej	d	sBdddZ
dddZdddZ  ZS )PyTablesTestCasec                s>   t t|   x*| jD ] }| j| jjdkrd | j|< qW d S )NZinstancemethod)superrv   tearDown__dict__	__class__rN   )rJ   key)rz   r   r   rx   H  s    zPyTablesTestCase.tearDownc             C   s   |   dd S )zGet the name of this test case..)idr#   )rJ   r   r   r   _getNameN  s    zPyTablesTestCase._getNamec             C   s   |   dd S )z>Get the name of the method currently running in the test case.r|   )r~   r#   )rJ   r   r   r   _getMethodNameR  s    zPyTablesTestCase._getMethodNamec             C   s<   t r8|  }|  }d||f }td|dt| f  dS )z;Print a nice header for the current test method if verbose.zRunning %s.%sz%s
%s-N)r   r   r   r   r<   )rJ   rY   Z
methodNametitler   r   r   _verboseHeaderV  s
    zPyTablesTestCase._verboseHeaderrG   Nc             O   s   t || |}|d|||S )NrG   )r]   r\   )rJ   Zexpected_warningrW   rZ   r[   contextr   r   r   rG   b  s    zPyTablesTestCase.assertWarnsFc             C   s^   t rtd| td| |r2| |j|jkd n| |j|jkd | |j|jkd d S )NzGroup 1:zGroup 2:z(node1 and node2 have the same pathnames.z1node1 and node2 does not have the same pathnames.z0node1 and node2 does not have the same children.)r   r   
assertTrue_v_pathnameZ_v_children)rJ   node1node2hardlinkr   r   r   _checkEqualityGroupg  s    




z$PyTablesTestCase._checkEqualityGroupc             C   sl   t rtd| td| |r2| |j|jkd n| |j|jkd | t|d d  |d d  d d S )NzLeaf 1:zLeaf 2:z(node1 and node2 have the same pathnames.z1node1 and node2 does not have the same pathnames.z.node1 and node2 does not have the same values.)r   r   r   r   rF   )rJ   r   r   r   r   r   r   _checkEqualityLeafw  s    



z#PyTablesTestCase._checkEqualityLeaf)N)F)F)rN   rO   rP   rx   r   r   r   r:   unittestTestCaserG   r   r   __classcell__r   r   )rz   r   rv   G  s   

rv   c                   s0   e Zd ZdZi Z fddZ fddZ  ZS )TestFileMixinNc                s2   t t|   tj| jfd|  i| j| _d S )Nr   )	rw   r   setUpr   	open_fileh5fnamer   open_kwargsh5file)rJ   )rz   r   r   r     s    zTestFileMixin.setUpc                s   | j   tt|   dS )zClose ``h5file``.N)r   closerw   r   rx   )rJ   )rz   r   r   rx     s    
zTestFileMixin.tearDown)rN   rO   rP   r   r   r   rx   r   r   r   )rz   r   r     s   r   c                   sB   e Zd ZdZi Zdd Z fddZ fddZdd	d
Z  Z	S )TempFileMixinru   c             C   s   t j|  ddS )Nz.h5)prefixsuffix)tempfileZmktempr   )rJ   r   r   r   _getTempFileName  s    zTempFileMixin._getTempFileNamec                s@   t t|   |  | _tj| j| jfd|  i| j	| _
dS )zSet ``h5file`` and ``h5fname`` instance attributes.

        * ``h5fname``: the name of the temporary HDF5 file.
        * ``h5file``: the writable, empty, temporary HDF5 file.

        r   N)rw   r   r   r   r   r   r   	open_moder   r   r   )rJ   )rz   r   r   r     s
    
zTempFileMixin.setUpc                s.   | j   d| _ t| j tt|   dS )z(Close ``h5file`` and remove ``h5fname``.N)r   r   osr   r   rw   r   rx   )rJ   )rz   r   r   rx     s    
zTempFileMixin.tearDownrc             K   s"   | j   tj| j|f|| _ dS )zReopen ``h5file`` in the specified ``mode``.

        Returns a true or false value depending on whether the file was
        reopenend or not.  If not, nothing is changed.

        T)r   r   r   r   r   )rJ   moder[   r   r   r   _reopen  s    
zTempFileMixin._reopen)r   )
rN   rO   rP   r   r   r   r   rx   r   r   r   r   )rz   r   r     s   r   c               @   s   e Zd Ze Zdd ZdS )ShowMemTimec             C   s  xt dD ]}|dr*t| d }q
|drFt| d }q
|drbt| d }q
|dr~t| d }q
|drt| d }q
|dr
t| d }q
W td	t | j  td
|    td||f  td||f  td||f  dS )z$Showing memory and time consumption.z/proc/self/statuszVmSize:r   zVmRSS:zVmData:zVmStk:zVmExe:zVmLib:z
WallClock time:z Memory usage: ******* %s *******zVmSize: %7s kB	VmRSS: %7s kBzVmData: %7s kB	VmStk: %7s kBzVmExe:  %7s kB	VmLib: %7s kBN)open
startswithintr#   r   timetrefr   )rJ   lineZvmsizeZvmrssZvmdataZvmstkZvmexeZvmlibr   r   r   test00  s$    





zShowMemTime.test00N)rN   rO   rP   r   r   r   r   r   r   r   r     s   r   )F)r   )6__doc__Z
__future__r   r   r   r!   r(   r   r.   r*   r   re   Zdistutils.versionr   Zpkg_resourcesr   Z	unittest2r   ImportErrorversion_infor   r    r   Ztables.utilsr   Ztables.req_versionsr	   Zhdf5_versionr   Zblosc_versionr   r   Zshow_memoryr   Z
zlib_availZ	lzo_availZbzip2_availZblosc_availr   r1   r3   r7   rC   rF   r:   r   rH   rQ   r]   rv   objectr   r   r   r   r   r   r   <module>   s^   

;


5.A*