
^Q\-                 @   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 Gd
 d   d e  Z Gd d   d e  Z d S)z
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
    N)datetime)settings)StaticFilesHandler)BaseCommandCommandError)get_internal_wsgi_application)signalcommandzdjango.contrib.staticfilesc               @   sL   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d S)KCacheGrindc             C   s   | j    |  _ d  |  _ d  S)N)Zgetstatsdataout_file)selfZprofiler r   g/var/www/dbchiro/venv/build/django-extensions/django_extensions/management/commands/runprofileserver.py__init__   s    zKCacheGrind.__init__c             C   sH   | |  _  |  j  j d  |  j   x |  j D] } |  j |  q- Wd  S)Nzevents: Ticks
)r   write_print_summaryr
   _entry)r   r   entryr   r   r   output   s
    	
zKCacheGrind.outputc             C   sW   d } x3 |  j  D]( } t | j d  } t | |  } q W|  j j d | f  d  S)Nr   i  zsummary: %d
)r
   int	totaltimemaxr   r   )r   Zmax_costr   r   r   r   r   r   &   s
    zKCacheGrind._print_summaryc             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)Nzfn=%s
zfl=%s
i  z0  %s
z%d %d
r   
)r   code
isinstancestrr   co_filenameco_namer   
inlinetimeco_firstlinenocalls	_subentry)r   r   r   r   r   r    linenosubentryr   r   r   r   -   s&    					zKCacheGrind._entryc             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)Nzcfn=%s
zcalls=%d 0
zcfl=%s
zcalls=%d %d
i  z%d %d
)r   r   r   r   r   Z	callcountr   r   r   r   r   )r   r"   r#   r   r   r   r   r   r   r!   L   s    		zKCacheGrind._subentryN)__name__
__module____qualname__r   r   r   r   r!   r   r   r   r   r	      s
   r	   c                   sC   e  Z d  Z d Z d Z   f d d   Z e d d d   Z   S)Commandz7Starts a lightweight Web server with profiling enabled.z&[optional port number, or ipaddr:port]c          
      s}  t  t |   j |  | j d d d d d | j d d d d	 d
 d d d d | j d d d d	 d d d 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 d d d | j d d d d	 d d d d d  | j d! d d d	 d" d d d d# t ry| j d$ d d d	 d% d d d d& | j d' d d d	 d( d d d d) n  d  S)*Naddrportnargs?helpz$Optional port number, or ipaddr:portz
--noreloadactionstore_falsedestuse_reloaderdefaultTz*Tells Django to NOT use the auto-reloader.z--nothreadinguse_threadingz"Tells Django to NOT use threading.z--prof-path	prof_pathz/tmpz=Specifies the directory which to save profile information in.z--prof-file	prof_filez{path}.{duration:06d}ms.{time}zASet filename format, default if "{path}.{duration:06d}ms.{time}".z	--nomedia
store_trueno_mediaFzDo not profile MEDIA_URLz--use-cprofileuse_cprofilezUUse cProfile if available, this is disabled per default because of incompatibilities.z--kcachegrind
use_lsprofz]Create kcachegrind compatible lsprof files, this requires and automatically enables cProfile.z
--nostaticuse_static_handlerzCTells Django to NOT automatically serve static files at STATIC_URL.z
--insecureinsecure_servingz3Allows serving static files even if DEBUG is False.)superr'   add_argumentsadd_argumentUSE_STATICFILES)r   parser)	__class__r   r   r;   _   sL    zCommand.add_arguments c                s  d d  l   d d  l 
 d d  l  d d l m  | rP t d  j   n  | se d   d  n; y | j d  \    Wn t k
 r d |    Yn X  s d   n   j	   s t d    n   j
 d	 d
  }  j
 d d  	  j
 d d   t j d k rd pd            	 
 f d d   } | rud d l m } | j |  n |   d  S)Nr   )runzUsage is runserver %sr@   Z8000:z	127.0.0.1z%r is not a valid port number.r/   Tshutdown_messager5   Fwin32z
CTRL-BREAKz	CONTROL-Cc           (      s  d d  l   d d  l  y d d  l  d }  Wn t k
 rH d }  Yn X j d d     j d d    r| d   n    r y d d  l  d   Wq t k
 r t d  d   Yq Xn   r   r t d   n  |  r   r t d   n   j d	 d
    j d d    j d d d d d d  sNd  t d  n  d d               f
 d d   } t d  t	  d  r j
 d d  n  j d d  t d 
 j   t j f  t d 	  f  t d   y t   } t r_ j d d  }  j d d  } | r_t j sM| r_t |  } q_n  | |  }  	 t   | d   j d! d  Wn  j k
 rK} z i d"  j 6d#  j 6d$  j 6} y | | j } Wn$ t t f k
 rt |  } Yn Xt j j  j j  d% |  d&   j! d'  WYd  d  } ~ Xn2 t" k
 r| rkt   n  t j# d  Yn Xd  S)(Nr   TFr6   r7   z-cProfile disabled, module cannot be imported!zFKcachegrind compatible output format required cProfile from Python 2.5z;Hotshot profile library not found. (and not using cProfile)r2   z/tmpr3   z{path}.{duration:06d}ms.{time}path1duration   time   zPFilename 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)NZ	MEDIA_URLZ
STATIC_URL)getattrr   append)Zexclude_pathsZ	media_urlZ
static_urlr   r   r   get_exclude_paths   s    z<Command.handle.<locals>.inner_run.<locals>.get_exclude_pathsc                s4             	 
 f d d   } | S)Nc                s  |  d    r? t    f d d      D  r?  |  |  S  j d  j d d  p] d } d |  j   f }  j j 
 |  }  r  j   } n  j |  } t j   } z | j	  |  |  SWd  t j   | } | j
 d | j d }  r7t |  } t | d	   }	 | j |	  Wd  QXn  rM| j |  n  	 j d
 | d t |  d t  j     }
  j j 
 d |
  }
  s| j   n   j | |
  Xd  S)N	PATH_INFOc             3   s   |  ] }   j  |  Vq d  S)N)
startswith).0p)	path_infor   r   	<genexpr>   s    zdCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handler.<locals>.<genexpr>/.rootz
%s.%d.profg     @@wrE   rG   rI   z%s.prof)anystripreplacerI   rE   joinZProfiler   nowZruncallsecondsmicrosecondsr	   openr   Z
dump_statsformatr   closerename)environZstart_responseZ	path_nameZprofnameZprofstartZelapZelapmsZkgfZ	profname2)USE_CPROFILE
USE_LSPROFcProfilerM   hotshotinner_handlerr5   osr3   r2   rI   )rR   r   handler   s2    
(!0zQCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handlerr   )rj   rl   )
rf   rg   rh   rM   ri   r5   rk   r3   r2   rI   )rj   r   make_profiler_handler   s    0z@Command.handle.<locals>.inner_run.<locals>.make_profiler_handlerzValidating models...checkZdisplay_num_errorsz%
Django version %s, using settings %rz.Development server is running at http://%s:%s/zQuit the server with %s.r8   r9   	threadingr1   z.You don't have permission to access that port.zThat port is already in use.z%That IP address can't be assigned-to.z	Error: %sr      )$rk   rI   ri   ImportErrorgetrh   printr   r`   hasattrrn   validateget_versionr   ZSETTINGS_MODULEr   r=   DEBUGr   r   errorZEACCESZ
EADDRINUSEZEADDRNOTAVAILerrnoAttributeErrorKeyErrorr   sysstderrr   styleERROR_exitKeyboardInterruptexit)ZHAS_HOTSHOTrm   rl   r8   r9   eZERRORSZ
error_text)addrdjangory   r5   optionsportquit_commandrA   r   rC   socket)	rf   rg   rh   rM   ri   rk   r3   r2   rI   r   	inner_run   sx    
	


-"
	,

$z!Command.handle.<locals>.inner_run)
autoreload)r   r   ry   django.core.servers.basehttprA   r   argssplit
ValueErrorisdigitrr   r|   platformZdjango.utilsr   main)r   r(   r   r   r/   r   r   r   )r   r   ry   r5   r   r   r   rA   r   rC   r   r   handle   s4    		0ozCommand.handle)r$   r%   r&   r+   r   r;   r   r   r   r   )r?   r   r'   [   s
   0r'   )__doc__r|   r   Zdjango.confr   Z#django.contrib.staticfiles.handlersr   Zdjango.core.management.baser   r   r   r   Z"django_extensions.management.utilsr   ZINSTALLED_APPSr=   objectr	   r'   r   r   r   r   <module>   s   A