B
    î®fPí  ã               @   s   d Z yddlZddlmZ W n ek
r8   d\ZZY nX 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 e d¡ZG dd	„ d	e	ƒZdS )
a/  This plugin will run tests using the hotshot profiler, which is part
of the standard library. To turn it on, use the ``--with-profile`` option
or set the NOSE_WITH_PROFILE environment variable. Profiler output can be
controlled with the ``--profile-sort`` and ``--profile-restrict`` options,
and the profiler output file may be changed with ``--profile-stats-file``.

See the `hotshot documentation`_ in the standard library documentation for
more details on the various output options.

.. _hotshot documentation: http://docs.python.org/library/hotshot.html
é    N)Ústats)NN)ÚPlugin)Útolistznose.pluginsc               @   s`   e Zd ZdZdZdZdd„ Zdd„ ZeeƒZdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚProfilezC
    Use this plugin to run tests using the hotshot profiler. 
    NFc             C   st   |   ¡ sdS t | ||¡ |jddd| dd¡ddd	 |jd
ddd| d¡dd |jdddd| d¡dd dS )z&Register commandline options.
        Nz--profile-sortZstoreÚprofile_sortZNOSE_PROFILE_SORTZ
cumulativeZSORTz"Set sort order for profiler output)ÚactionÚdestÚdefaultÚmetavarÚhelpz--profile-stats-fileÚprofile_stats_fileZFILEZNOSE_PROFILE_STATS_FILEz;Profiler stats file; default is a new temp file on each run)r   r   r
   r	   r   z--profile-restrictÚappendÚprofile_restrictZRESTRICTZNOSE_PROFILE_RESTRICTz?Restrict profiler output. See help for pstats.Stats for details)Ú	availabler   ÚoptionsZ
add_optionÚget)ÚselfÚparserÚenv© r   ú0lib/python3.7/site-packages/nose/plugins/prof.pyr   !   s"    

zProfile.optionsc             C   s   t d k	S )N)Úhotshot)Úclsr   r   r   r   8   s    zProfile.availablec             C   s&   |   ¡ sdS |  ¡  t | j¡| _dS )z5Create profile stats file and load profiler.
        N)r   Ú_create_pfiler   r   ÚpfileÚprof)r   r   r   r   Úbegin<   s    zProfile.beginc             C   sf   |   ¡ sd| _dS t | ||¡ || _|jr<|j| _d| _nd| _d| _d| _|j	| _
t|jƒ| _dS )zConfigure plugin.
        FNT)r   Zenabledr   Ú	configureÚconfr   r   Úclean_stats_fileÚfilenor   Úsortr   r   Úrestrict)r   r   r   r   r   r   r   D   s    zProfile.configurec                s2   ˆ   ¡ sdS t d| ¡ ˆ j|f‡ fdd„	}|S )z6Wrap entire test run in :func:`prof.runcall`.
        Nzpreparing test %sc                s   ˆ   ¡  | || ¡ d S )N)r   Zruncall)Úresultr   Útest)r   r   r   Úrun_and_profile\   s    z,Profile.prepareTest.<locals>.run_and_profile)r   ÚlogÚdebugr   )r   r$   r%   r   )r   r   ÚprepareTestV   s
    zProfile.prepareTestc             C   sœ   t  d¡ | j ¡  t | j¡}| | j¡ t	|dƒ}|rH|j
}||_
ntj}|t_z.| jrxt  d| j¡ |j| jŽ  n| ¡  W d|r||_
n|t_X dS )z Output profiler report.
        zprinting profiler reportÚstreamz"setting profiler restriction to %sN)r&   r'   r   Úcloser   Úloadr   Z
sort_statsr!   Úhasattrr)   ÚsysÚstdoutr"   Zprint_stats)r   r)   Z
prof_statsZ	compat_25Ztmpr   r   r   Úreporta   s$    


zProfile.reportc             C   sŒ   |   ¡ sdS y| j ¡  W n tk
r.   Y nX | jrˆ| jrbyt | j¡ W n tk
r`   Y nX yt | j	¡ W n tk
r†   Y nX dS )z5Clean up stats file, if configured to do so.
        N)
r   r   r*   ÚAttributeErrorr   r    ÚosÚOSErrorÚunlinkr   )r   r#   r   r   r   Úfinalize   s"    zProfile.finalizec             C   s    | j st ¡ \| _| _ d| _d S )NT)r   ÚtempfileZmkstempr    r   )r   r   r   r   r   —   s    zProfile._create_pfile)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Úclassmethodr   r   r(   r/   r4   r   r   r   r   r   r      s   r   )r9   r   r   ÚImportErrorZloggingr1   r-   r5   Znose.plugins.baser   Z	nose.utilr   Z	getLoggerr&   r   r   r   r   r   Ú<module>   s   
