a
    ÆZEgi-  ã                   @   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v 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   |  ¡ | _d | _d S ©N)ZgetstatsÚdataÚout_file)ÚselfZprofiler© r   úo/var/www/sistema_ama/venv/lib/python3.9/site-packages/django_extensions/management/commands/runprofileserver.pyÚ__init__   s    
zKCacheGrind.__init__c                 C   s4   || _ | j  d¡ |  ¡  | jD ]}|  |¡ q d S )Nzevents: Ticks
)r   ÚwriteÚ_print_summaryr   Ú_entry)r   r   Úentryr   r   r   Úoutput   s
    
zKCacheGrind.outputc                 C   s>   d}| j D ]}t|jd ƒ}t||ƒ}q
| j d|f ¡ d S )Nr   éè  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&| d| ¡ n | d|j ¡ | d|j ¡ t|jd ƒ}t|tƒrn| d| ¡ n| d|j	|f ¡ |j
r�|j
}ng }t|tƒr¤d}n|j	}|D ]}|  ||¡ q®| d¡ d S )Nzfn=%s
zfl=%s
r   z0  %s
ú%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| d| ¡ | d|jf ¡ n6| d|j ¡ | d|j ¡ | d|j|jf ¡ t	|j
d ƒ}| d||f ¡ d S )Nzcfn=%s
zcalls=%d 0
zcfl=%s
zcalls=%d %d
r   r   )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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.)r2   r3   r.   z--prof-fileÚ	prof_fileú{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?   _   s`    þýþþýþýýýýzCommand.add_argumentsÚ c                    s"  dd l ‰dd l‰
dd l‰ddlm‰ |s2d‰ d‰n0z| d¡\‰ ‰W n ty`   d| ‰ ‰Y n0 ˆ sjd‰ ˆ ¡ s~tdˆ ƒ‚ˆd }ˆ 	d	d¡‰	ˆd
 ‰t
jdkr¨dpªd‰‡ ‡‡‡‡‡‡‡‡‡	‡
fdd„}|�rzddlm} ||ƒ W n* t�y   ddlm} | |¡ Y n0 n|ƒ  d S )Nr   )ÚrunrE   Z8000ú:z	127.0.0.1z%r is not a valid port number.r0   Úshutdown_messager9   Úwin32z
CTRL-BREAKz	CONTROL-Cc                     s\  dd l ‰dd l‰zdd l‰d} W n ty6   d} Y n0 ˆd ‰ ˆd ‰ˆrPd‰ ˆ r„zdd l‰d‰ W n ty‚   tdƒ d‰ Y n0 ˆr”ˆ s”tdƒ‚| s¤ˆ s¤tdƒ‚ˆd	 ‰ˆd
 ‰ˆjdddd�sÐd‰tdƒ dd„ ‰‡ ‡‡‡‡‡‡‡‡‡f
dd„}tdƒ ˆjdd� tdˆ
 	¡ t
jf ƒ tdˆ	ˆf ƒ tdˆ ƒ z\tƒ }t�rvˆd }ˆd }|�rvt
j�sn|�rvt|ƒ}||ƒ}ˆˆ	tˆƒ|ˆd d� W n¾ ˆj�y, } zxˆjdˆjdˆjd i}z||j }W n  ttf�yð   t|ƒ}Y n0 tj ˆj d!| ¡d" ¡ ˆ d#¡ W Y d }~n4d }~0  t�yV   ˆ�rHtˆƒ t  d¡ Y n0 d S )$Nr   TFr:   r;   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)r5   r6   Ú1é   é   ©ÚpathÚdurationÚtimer7   zPFilename format is wrong. Default format used: '{path}.{duration:06d}ms.{time}'.c                  S   s<   g } t tdd ƒ}|r|  |¡ t tdd ƒ}|r8|  |¡ | 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 ˆ  d¡ dd¡pBd}d|ˆ ¡ f }ˆj ˆ
|¡}ˆrpˆ ¡ }n
ˆ |¡}t ¡ }�z8| 	ˆ| |¡W t ¡ | }|j
d |jd  }ˆröt|ƒ}t|d	ƒ�}	| |	¡ W d   ƒ n1 sê0    Y  nˆ�r| |¡ ˆ	j|t|ƒtˆ ¡ ƒd
�}
ˆj ˆ
d|
 ¡}
ˆ�sB| ¡  ˆ ||
¡ S 1 �sX0    Y  nˆ�rt| |¡ ˆ	j|t|ƒtˆ ¡ ƒd
�}
ˆj ˆ
d|
 ¡}
ˆ�s°| ¡  ˆ ||
¡ nÀt ¡ | }|j
d |jd  }ˆ�r$t|ƒ}t|d	ƒ�}	| |	¡ W d   ƒ n1 �s0    Y  nˆ�r4| |¡ ˆ	j|t|ƒtˆ ¡ ƒd
�}
ˆj ˆ
d|
 ¡}
ˆ�sp| ¡  ˆ ||
¡ 0 d S )NZ	PATH_INFOc                 3   s   | ]}ˆ   |¡V  qd S r
   )Ú
startswith)Ú.0Úp©Z	path_infor   r   Ú	<genexpr>Ý   ó    zdCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handler.<locals>.<genexpr>ú/Ú.Úrootz
%s.%d.profg     @�@ÚwrM   z%s.prof)ÚanyÚstripÚreplacerP   rN   Ú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ÚcProfilerS   ÚhotshotÚinner_handlerr9   Úosr6   r5   rP   rW   r   ÚhandlerÙ   sh    


*
ñ     
ô,
zQCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handlerr   )rp   rr   )
rl   rm   rn   rS   ro   r9   rq   r6   r5   rP   )rp   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.r<   r=   r4   )Ú	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   é   )!rq   rP   ro   ÚImportErrorrn   Úprintr   rf   ÚcheckÚget_versionr   ZSETTINGS_MODULEr   rA   ÚDEBUGr   r   ÚerrorÚEACCESZ
EADDRINUSEZEADDRNOTAVAILÚerrnoÚAttributeErrorÚKeyErrorr   ÚsysÚstderrr   ÚstyleÚERRORÚ_exitÚKeyboardInterruptÚexit)ZHAS_HOTSHOTrs   rr   r<   r=   ÚeZERRORSZ
error_text©ÚaddrÚdjangor}   r9   ÚoptionsÚportZquit_commandrF   r   rH   Úsocket)	rl   rm   rn   rS   ro   rq   r6   r5   rP   r   Ú	inner_run®   st    


"ýz!Command.handle.<locals>.inner_run)Úrun_with_reloader)Ú
autoreload)rŠ   r�   r}   Údjango.core.servers.basehttprF   ÚsplitÚ
ValueErrorÚisdigitr   Úgetr€   ÚplatformZdjango.utils.autoreloadr�   rv   Zdjango.utilsr�   Úmain)r   r+   Úargsr‹   r0   rŽ   r�   r�   r   rˆ   r   Úhandle”   s8     mzCommand.handle)rE   )	r'   r(   r)   r.   r˜   r?   r   r™   Ú__classcell__r   r   rC   r   r*   [   s
   5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_APPSrA   r	   r*   r   r   r   r   Ú<module>   s   
A