ó
J9•Qc           @   s¿   d  Z  y  d d l Z d d l m Z Wn e k
 rE d	 \ Z Z n Xd 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 j d ƒ Z d e
 f d „  ƒ  YZ d S(
   s/  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
iÿÿÿÿN(   t   stats(   t   Plugin(   t   tolists   nose.pluginst   Profilec           B   sn   e  Z d  Z d	 Z e Z d „  Z d „  Z e	 e ƒ Z d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z RS(
   sC   
    Use this plugin to run tests using the hotshot profiler. 
    c         C   sÆ   |  j  ƒ  s d St j |  | | ƒ | j d d d d d d | j d d	 ƒ d
 d d d ƒ| j d d d d d d
 d d | j d ƒ d d ƒ| j d d d d d d
 d d | j d ƒ d d ƒd S(   s&   Register commandline options.
        Ns   --profile-sortt   actiont   storet   destt   profile_sortt   defaultt   NOSE_PROFILE_SORTt
   cumulativet   metavart   SORTt   helps"   Set sort order for profiler outputs   --profile-stats-filet   profile_stats_filet   FILEt   NOSE_PROFILE_STATS_FILEs;   Profiler stats file; default is a new temp file on each runs   --profile-restrictt   appendt   profile_restrictt   RESTRICTt   NOSE_PROFILE_RESTRICTs?   Restrict profiler output. See help for pstats.Stats for details(   t	   availableR   t   optionst
   add_optiont   get(   t   selft   parsert   env(    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyR   !   s"    c         C   s
   t  d  k	 S(   N(   t   hotshott   None(   t   cls(    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyR   8   s    c         C   s3   |  j  ƒ  s d S|  j ƒ  t j |  j ƒ |  _ d S(   s5   Create profile stats file and load profiler.
        N(   R   t   _create_pfileR   R   t   pfilet   prof(   R   (    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyt   begin<   s    
c         C   s“   |  j  ƒ  s t |  _ d St j |  | | ƒ | |  _ | j rV | j |  _ t |  _ n d |  _ t
 |  _ d |  _ | j |  _ t | j ƒ |  _ d S(   s   Configure plugin.
        N(   R   t   Falset   enabledR   t	   configuret   confR   R    t   clean_stats_fileR   t   Truet   filenoR   t   sortR   R   t   restrict(   R   R   R&   (    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyR%   D   s    						c            s=   ˆ  j  ƒ  s d St j d | ƒ ˆ  j | ‡  f d † } | S(   s6   Wrap entire test run in :func:`prof.runcall`.
        Ns   preparing test %sc            s   ˆ  j  ƒ  | j | |  ƒ d  S(   N(   R   t   runcall(   t   resultR!   t   test(   R   (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyt   run_and_profile\   s    
(   R   t   logt   debugR!   (   R   R.   R/   (    (   R   s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyt   prepareTestV   s
    c         C   sØ   t  j d ƒ |  j j ƒ  t j |  j ƒ } | j |  j ƒ t	 | d ƒ } | rf | j
 } | | _
 n t j } | t _ z= |  j rª t  j d |  j ƒ | j |  j Œ  n
 | j ƒ  Wd | rÊ | | _
 n	 | t _ Xd S(   s    Output profiler report.
        s   printing profiler reportt   streams"   setting profiler restriction to %sN(   R0   R1   R!   t   closeR    t   loadR    t
   sort_statsR*   t   hasattrR3   t   syst   stdoutR+   t   print_stats(   R   R3   t
   prof_statst	   compat_25t   tmp(    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyt   reporta   s$    				c         C   s¡   |  j  ƒ  s d Sy |  j j ƒ  Wn t k
 r4 n X|  j r |  j rr y t j |  j ƒ Wqr t k
 rn qr Xn  y t j |  j	 ƒ Wq t k
 r™ q Xn  d S(   s5   Clean up stats file, if configured to do so.
        N(   R   R!   R4   t   AttributeErrorR'   R)   t   ost   OSErrort   unlinkR    R   (   R   R-   (    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyt   finalize   s"    		c         C   s1   |  j  s- t j ƒ  \ |  _ |  _  t |  _ n  d  S(   N(   R    t   tempfilet   mkstempR)   R(   R'   (   R   (    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyR   —   s    	N(   t   __name__t
   __module__t   __doc__R   R    R#   R'   R   R   t   classmethodR"   R%   R2   R>   RC   R   (    (    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyR      s   							(   NN(   RH   R   R    t   ImportErrorR   t   loggingR@   R8   RD   t   nose.plugins.baseR   t	   nose.utilR   t	   getLoggerR0   R   (    (    (    s5   /sys/lib/python2.7/site-packages/nose/plugins/prof.pyt   <module>   s   