3
gi-                 @   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G d
d dZG d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               @   s4   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   t/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django_extensions/management/commands/runprofileserver.py__init__   s    
zKCacheGrind.__init__c             C   s8   || _ | j jd | j  x| jD ]}| j| q"W d S )Nzevents: Ticks
)r   write_print_summaryr
   _entry)r   r   entryr   r   r   output   s
    zKCacheGrind.outputc             C   sB   d}x&| 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r&|jd|  n |jd|j  |jd|j  t|jd }t|trn|jd|  n|jd|j	|f  |j
r|j
}ng }t|trd}n|j	}x|D ]}| j|| qW |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r8|jd|  |jd|jf  n6|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                   s2   e Zd ZdZdZ fddZedddZ  ZS )	Commandz7Starts a lightweight Web server with profiling enabled.z&[optional port number, or ipaddr:port]c                s   t  j| |jdddd |jddddd	d
 |jdddddd
 |jddddd |jddddd |jdddddd
 |jdddddd
 |jddd dd!d
 tr|jd"dd#dd$d
 |jd%dd&dd'd
 d S )(Naddrport?z$Optional port number, or ipaddr:port)nargshelpz
--noreloadstore_falseuse_reloaderTz*Tells Django to NOT use the auto-reloader.)actiondestdefaultr+   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.)r/   r0   r+   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.)super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 |s2d dn2y|jd\ W n tk
rb   d|  Y nX  sld j std d }j	d	d	d
 t
jdkrdpd 	
fdd}|ryddlm} || W n, tk
r   ddlm} |j| Y nX n|  d S )Nr   )runr@   Z8000:z	127.0.0.1z%r is not a valid port number.r-   shutdown_messager5   win32z
CTRL-BREAKz	CONTROL-Cc           (      sh  dd l dd lydd ld} W n tk
r8   d} Y nX d  d rRd  rydd ld W n  tk
r   td d Y nX r  rtd|  r  rtdd	 d
 jddddsdtd dd  f
dd}td jdd td
j	 t
jf  td	f  td  y\t }trd }d }|rt
jsx|rt|}||}	t|d d W n jk
r6 } ztjdjdjd i}y||j }W n" ttfk
r   t|}Y nX tjjjjd!| d"  jd# W Y d d }~X n. tk
rb   rTt tj d Y nX d 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   r3   1      )pathdurationtimez{path}.{duration:06d}ms.{time}zPFilename format is wrong. Default format used: '{path}.{duration:06d}ms.{time}'.c              S   s<   g } t tdd }|r| j| t tdd }|r8| j| | 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                s$    	
fdd}|S )Nc                s<  | d  r.t  fdd D r.| |S  jdjddpBd}d|j f }jj
|}rpj }n
j|}tj }z|j	| |S tj | }|j
d |jd  }rt|}t|d	}	|j|	 W d Q R X nr|j| 	j|t|tj d
}
jj
d|
 }
s*|j  j||
 X d S )NZ	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     @@w)rH   rI   rJ   z%s.prof)anystripreplacerJ   rH   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   rJ   )rQ   r   handler   s2    



zQCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handlerr   )ri   rk   )
re   rf   rg   rM   rh   r5   rj   r3   r2   rJ   )ri   r   make_profiler_handler   s     z@Command.handle.<locals>.inner_run.<locals>.make_profiler_handlerzPerforming system checks...)Z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   r1   )	threadingz.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      )!rj   rJ   rh   ImportErrorrg   printr   r_   checkget_versionr   ZSETTINGS_MODULEr   r=   DEBUGr   r   errorEACCES
EADDRINUSEZEADDRNOTAVAILerrnoAttributeErrorKeyErrorr   sysstderrr   styleERROR_exitKeyboardInterruptexit)ZHAS_HOTSHOTrl   rk   r8   r9   eZERRORSZ
error_text)addrdjangorw   r5   optionsportquit_commandrA   r   rC   socket)	re   rf   rg   rM   rh   rj   r3   r2   rJ   r   	inner_run   sr    



"
z!Command.handle.<locals>.inner_run)run_with_reloader)
autoreload)r   r   rw   django.core.servers.basehttprA   split
ValueErrorisdigitr   getrz   platformZdjango.utils.autoreloadr   ro   Zdjango.utilsr   main)r   r(   argsr   r-   r   r   r   r   )r   r   rw   r5   r   r   r   rA   r   rC   r   r   handle   s8     mzCommand.handle)r@   )	r$   r%   r&   r+   r   r;   r   r   __classcell__r   r   )r?   r   r'   [   s
   5r'   )__doc__rz   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=   r	   r'   r   r   r   r   <module>   s   
A