ó
¸9—^c           @   s·   d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m Z m	 Z	 d d l
 m Z d d l m Z d	 e j k Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sÜ   
runprofileserver.py

    Starts a lightweight Web server with profiling enabled.

Credits for kcachegrind support taken from lsprofcalltree.py go to:
 David Allouche
 Jp Calderone & Itamar Shtull-Trauring
 Johan Dahlin
iÿÿÿÿN(   t   datetime(   t   settings(   t   StaticFilesHandler(   t   BaseCommandt   CommandError(   t   get_internal_wsgi_application(   t   signalcommands   django.contrib.staticfilest   KCacheGrindc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | j  ƒ  |  _ d  |  _ d  S(   N(   t   getstatst   datat   Nonet   out_file(   t   selft   profiler(    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyt   __init__   s    c         C   sH   | |  _  |  j  j d ƒ |  j ƒ  x |  j D] } |  j | ƒ q- Wd  S(   Ns   events: Ticks
(   R   t   writet   _print_summaryR	   t   _entry(   R   R   t   entry(    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyt   output   s
    	
c         C   sW   d } x3 |  j  D]( } t | j d ƒ } t | | ƒ } q W|  j j d | f ƒ d  S(   Ni    iè  s   summary: %d
(   R	   t   intt	   totaltimet   maxR   R   (   R   t   max_costR   R   (    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyR   &   s
    c         C   s  |  j  } | j } t | t ƒ r5 | j d | ƒ n( | j d | j ƒ | j d | j ƒ t | j d ƒ } t | t ƒ r“ | j d | ƒ n | j d | j	 | f ƒ | j
 rÂ | j
 } n g  } t | t ƒ rà d } n	 | j	 } x | D] } |  j | | ƒ qð W| j d ƒ d  S(   Ns   fn=%s
s   fl=%s
iè  s   0  %s
s   %d %d
i    s   
(   R   t   codet
   isinstancet   strR   t   co_filenamet   co_nameR   t
   inlinetimet   co_firstlinenot   callst	   _subentry(   R   R   R   R   R   R   t   linenot   subentry(    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyR   -   s&    					c         C   s¿   |  j  } | j } t | t ƒ rL | j d | ƒ | j d | j f ƒ nE | j d | j ƒ | j d | j ƒ | j d | j | j f ƒ t	 | j
 d ƒ } | j d | | f ƒ d  S(   Ns   cfn=%s
s   calls=%d 0
s   cfl=%s
s   calls=%d %d
iè  s   %d %d
(   R   R   R   R   R   t	   callcountR   R   R   R   R   (   R   R!   R"   R   R   R   (    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyR    L   s    		(   t   __name__t
   __module__R   R   R   R   R    (    (    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyR      s
   				t   Commandc           B   s/   e  Z d  Z d Z d „  Z e d d „ ƒ Z RS(   s7   Starts a lightweight Web server with profiling enabled.s&   [optional port number, or ipaddr:port]c      
   C   s}  t  t |  ƒ j | ƒ | j d d d d d ƒ| j d d d d	 d
 d t d d ƒ| j d d d d	 d d t d d ƒ| j d d	 d d d d d ƒ| j d d	 d d d d d ƒ| j d d d d	 d d t d d ƒ| j d d d d	 d d t d d ƒ| j d d d d	 d  d t d d! ƒt ry| j d" d d d	 d# d t d d$ ƒ| j d% d d d	 d& d t d d' ƒn  d  S((   Nt   addrportt   nargst   ?t   helps$   Optional port number, or ipaddr:ports
   --noreloadt   actiont   store_falset   destt   use_reloadert   defaults*   Tells Django to NOT use the auto-reloader.s   --nothreadingt   use_threadings"   Tells Django to NOT use threading.s   --prof-patht	   prof_paths   /tmps=   Specifies the directory which to save profile information in.s   --prof-filet	   prof_files   {path}.{duration:06d}ms.{time}sA   Set filename format, default if "{path}.{duration:06d}ms.{time}".s	   --nomediat
   store_truet   no_medias   Do not profile MEDIA_URLs   --use-cprofilet   use_cprofilesU   Use cProfile if available, this is disabled per default because of incompatibilities.s   --kcachegrindt
   use_lsprofs]   Create kcachegrind compatible lsprof files, this requires and automatically enables cProfile.s
   --nostatict   use_static_handlersC   Tells Django to NOT automatically serve static files at STATIC_URL.s
   --insecuret   insecure_servings3   Allows serving static files even if DEBUG is False.(   t   superR&   t   add_argumentst   add_argumentt   Truet   Falset   USE_STATICFILES(   R   t   parser(    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyR:   _   sL    t    c            s‚  d d  l  ‰ d d  l ‰
 d d  l ‰ d d l m ‰ | sI d ‰  d ‰ n: y | j d ƒ \ ‰  ‰ Wn t k
 r‚ d | ‰  ‰ n Xˆ  s’ d ‰  n  ˆ j ƒ  s± t d ˆ ƒ ‚ n  ˆ d } ˆ j	 d	 d ƒ ‰	 ˆ d
 ‰ t
 j d k rì d pï d ‰ ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡	 ‡
 f d †  } | rwy d d l m } | | ƒ Wq~t k
 rsd d l m } | j | ƒ q~Xn | ƒ  d  S(   Niÿÿÿÿ(   t   runR@   t   8000t   :s	   127.0.0.1s   %r is not a valid port number.R.   t   shutdown_messageR4   t   win32s
   CTRL-BREAKs	   CONTROL-Cc       
      s
  d d  l  ‰ d d  l ‰ y d d  l ‰ t }  Wn t k
 rG t }  n Xˆ d ‰  ˆ d ‰ ˆ rk t ‰  n  ˆ  r© y d d  l ‰ t ‰  Wq© t k
 r¥ d GHt ‰  q© Xn  ˆ rÅ ˆ  rÅ t d ƒ ‚ n  |  râ ˆ  râ t d ƒ ‚ n  ˆ d ‰ ˆ d ‰ ˆ j d	 d
 d d d d ƒ s"d ‰ d GHn  d „  ‰ ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ f
 d †  } d GHt	 ˆ d ƒ r|ˆ j
 d t ƒ n ˆ j d t ƒ d ˆ
 j ƒ  t j f GHd ˆ	 ˆ f GHd ˆ GHyz t ƒ  } t r	ˆ d } ˆ d } | r	t j s÷| r	t | ƒ } q	n  | | ƒ } ˆ ˆ	 t ˆ ƒ | d ˆ d ƒWnÍ ˆ j k
 rÚ} i d ˆ j 6d ˆ j 6d ˆ j 6} y | | j } Wn# t t f k
 r¥t | ƒ } n Xt j j ˆ j j  d  | ƒ d! ƒ ˆ j! d" ƒ n, t" k
 rˆ rõˆ GHn  t j# d# ƒ n Xd  S($   NiÿÿÿÿR5   R6   s-   cProfile disabled, module cannot be imported!sF   Kcachegrind compatible output format required cProfile from Python 2.5s;   Hotshot profile library not found. (and not using cProfile)R1   R2   t   patht   1t   durationi   t   timei   s   {path}.{duration:06d}ms.{time}sP   Filename format is wrong. Default format used: '{path}.{duration:06d}ms.{time}'.c          S   sZ   g  }  t  t d d  ƒ } | r. |  j | ƒ n  t  t d d  ƒ } | rV |  j | ƒ n  |  S(   Nt	   MEDIA_URLt
   STATIC_URL(   t   getattrR   R
   t   append(   t   exclude_pathst	   media_urlt
   static_url(    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyt   get_exclude_pathsÎ   s    c            s1   ‡ ‡ ‡ ‡ ‡ ‡  ‡ ‡ ‡ ‡	 ‡
 f d †  } | S(   Nc            s»  |  d ‰  ˆ r< t  ‡  f d †  ˆ ƒ  Dƒ ƒ r< ˆ |  | ƒ Sˆ  j d ƒ j d d ƒ pZ d } d | ˆ j ƒ  f } ˆ j j ˆ
 | ƒ } ˆ r ˆ j ƒ  } n ˆ j | ƒ } t j ƒ  } z | j	 ˆ |  | ƒ SWd  t j ƒ  | } | j
 d | j d } ˆ r4t | ƒ } t | d ƒ  }	 | j |	 ƒ Wd  QXn ˆ rJ| j | ƒ n  ˆ	 j d	 | d
 t | ƒ d t ˆ j ƒ  ƒ ƒ }
 ˆ j j ˆ
 d |
 ƒ }
 ˆ s¦| j ƒ  n  ˆ j | |
 ƒ Xd  S(   Nt	   PATH_INFOc         3   s   |  ] } ˆ  j  | ƒ Vq d  S(   N(   t
   startswith(   t   .0t   p(   t	   path_info(    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pys	   <genexpr>Ý   s    t   /t   .t   roots
   %s.%d.profg     @@t   wRF   RH   RI   s   %s.prof(   t   anyt   stript   replaceRI   RF   t   joint   ProfileR    t   nowt   runcallt   secondst   microsecondsR   t   openR   t
   dump_statst   formatR   t   closet   rename(   t   environt   start_responset	   path_namet   profnamet   proft   startt   elapt   elapmst   kgt   ft	   profname2(   t   USE_CPROFILEt
   USE_LSPROFt   cProfileRQ   t   hotshott   inner_handlerR4   t   osR2   R1   RI   (   RV   sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyt   handlerÙ   s2    
%!0(    (   Rx   Rz   (
   Rt   Ru   Rv   RQ   Rw   R4   Ry   R2   R1   RI   (   Rx   sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyt   make_profiler_handlerØ   s    -s   Validating models...t   checkt   display_num_errorss%   
Django version %s, using settings %rs.   Development server is running at http://%s:%s/s   Quit the server with %s.R7   R8   t	   threadingR0   s.   You don't have permission to access that port.s   That port is already in use.s%   That IP address can't be assigned-to.s	   Error: %ss   
i   i    ($   Ry   RI   Rw   R<   t   ImportErrorR=   Rv   R   Rf   t   hasattrR|   t   validatet   get_versionR   t   SETTINGS_MODULER   R>   t   DEBUGR   R   t   errort   EACCESt
   EADDRINUSEt   EADDRNOTAVAILt   errnot   AttributeErrort   KeyErrorR   t   syst   stderrR   t   stylet   ERRORt   _exitt   KeyboardInterruptt   exit(   t   HAS_HOTSHOTR{   Rz   R7   R8   t   et   ERRORSt
   error_text(   t   addrt   djangoR‰   R4   t   optionst   portt   quit_commandRA   R   RD   t   socket(	   Rt   Ru   Rv   RQ   Rw   Ry   R2   R1   RI   sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyt	   inner_run®   sx    



	


	
*"		

$

$(   t   run_with_reloader(   t
   autoreload(   R˜   Rœ   R‰   t   django.core.servers.basehttpRA   t   splitt
   ValueErrort   isdigitR   t   getRŒ   t   platformt   django.utils.autoreloadRž   R   t   django.utilsRŸ   t   main(   R   R'   t   argsR™   R.   R   Rž   RŸ   (    (   R—   R˜   R‰   R4   R™   Rš   R›   RA   R   RD   Rœ   sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyt   handle”   s8    		

-o(   R$   R%   R*   R©   R:   R   Rª   (    (    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyR&   [   s
   	5(   t   __doc__RŒ   R    t   django.confR   t#   django.contrib.staticfiles.handlersR   t   django.core.management.baseR   R   R    R   t"   django_extensions.management.utilsR   t   INSTALLED_APPSR>   t   objectR   R&   (    (    (    sX   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/runprofileserver.pyt   <module>   s   A