o
    uvXjê-  ã                   @   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eƒ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)ÚgetstatsÚdataÚout_file)ÚselfÚprofiler© r   ús/var/www/html/myproject/venv/lib/python3.10/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   Ú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ƒr7| d| ¡ n
| d|j	|f ¡ |j
rH|j
}ng }t|tƒrRd}n|j	}|D ]}|  ||¡ qW| 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ƒr| d| ¡ | d|jf ¡ n| 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   Ú	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 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rl|jd"dd#dd$d
� |jd%dd&dd'd
� d S 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Údefaultr2   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.)r6   r7   r2   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.)Úsuperr.   Úadd_argumentsÚadd_argumentÚUSE_STATICFILES)r   Úparser©Ú	__class__r   r   rC   _   sb   þýþþýþýýý
ýúzCommand.add_argumentsÚ c                    s   dd l ‰dd l‰
dd l‰ddlm‰ |sd‰ d‰nz	| d¡\‰ ‰W n ty0   d|‰ ‰Y nw ˆ s5d‰ ˆ ¡ s?tdˆ ƒ‚ˆd }ˆ 	d	d¡‰	ˆd
 ‰t
jdkrTdpUd‰‡ ‡‡‡‡‡‡‡‡‡	‡
fdd„}|r‹zddlm} ||ƒ W d S  tyŠ   ddlm} | |¡ Y d S w |ƒ  d S )Nr   )ÚrunrI   Ú8000ú:z	127.0.0.1z%r is not a valid port number.r4   Úshutdown_messager=   Úwin32z
CTRL-BREAKz	CONTROL-Cc                     sn  dd l ‰dd l‰zdd l‰d} W n ty   d} Y nw ˆd ‰ ˆd ‰ˆr(d‰ ˆ rBzdd l‰d‰ W n tyA   tdƒ d‰ Y nw ˆrJˆ sJtdƒ‚| sRˆ sRtdƒ‚ˆd	 ‰ˆd
 ‰ˆjdddd�shd‰tdƒ dd„ ‰‡ ‡‡‡‡‡‡‡‡‡f
dd„}tdƒ tˆdƒr‹ˆj	dd� nˆj
dd� tdˆ
 ¡ tjf ƒ tdˆ	ˆf ƒ tdˆ ƒ z+tƒ }trÃˆd }ˆd }|rÃtjs¿|rÃt|ƒ}||ƒ}ˆˆ	tˆƒ|ˆd d� W d S  ˆj�y  } z=ˆjdˆjd ˆjd!i}z||j }W n ttf�y   t|ƒ}Y nw tj ˆj d"| ¡d# ¡ ˆ  d$¡ W Y d }~d S d }~w t!�y6   ˆ�r.tˆƒ t "d¡ Y d S w )%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)r9   r:   Ú1é   é   ©ÚpathÚdurationÚtimer;   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 ƒ}|r|  |¡ | S )NÚ	MEDIA_URLÚ
STATIC_URL)Úgetattrr   Úappend)Úexclude_pathsÚ	media_urlÚ
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¡p!d}d|ˆ ¡ f }ˆj ˆ
|¡}ˆr8ˆ ¡ }nˆ |¡}t ¡ }zc| 	ˆ| |¡W t ¡ | }|j
d |jd  }ˆrzt|ƒ}t|d	ƒ�}	| |	¡ W d   ƒ n1 stw   Y  nˆr�| |¡ ˆ	j|t|ƒtˆ ¡ ƒd
�}
ˆj ˆ
d|
 ¡}
ˆsž| ¡  ˆ ||
¡ S t ¡ | }|j
d |jd  }ˆrÖt|ƒ}t|d	ƒ�}	| |	¡ W d   ƒ n1 sÐw   Y  nˆrÝ| |¡ ˆ	j|t|ƒtˆ ¡ ƒd
�}
ˆj ˆ
d|
 ¡}
ˆsú| ¡  ˆ ||
¡ w )NÚ	PATH_INFOc                 3   s   � | ]}ˆ   |¡V  qd S r
   )Ú
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     @�@ÚwrR   z%s.prof)ÚanyÚstripÚreplacerU   rS   ÚjoinÚProfiler   ÚnowÚruncallÚsecondsÚmicrosecondsr	   Úopenr   Ú
dump_statsÚformatr   ÚcloseÚrename)ÚenvironÚstart_responseÚ	path_nameÚprofnameÚprofÚstartÚelapÚelapmsÚkgÚfÚ	profname2)ÚUSE_CPROFILEÚ
USE_LSPROFÚcProfiler]   ÚhotshotÚinner_handlerr=   Úosr:   r9   rU   rb   r   ÚhandlerÙ   sT   


ÿ€
ôÿ€
zQCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handlerr   )r†   rˆ   )
r‚   rƒ   r„   r]   r…   r=   r‡   r:   r9   rU   )r†   r   Úmake_profiler_handlerØ   s    z@Command.handle.<locals>.inner_run.<locals>.make_profiler_handlerzValidating models...Úcheck)Údisplay_num_errorsz%
Django version %s, using settings %rz.Development server is running at http://%s:%s/zQuit the server with %s.r@   rA   r8   )Ú	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   é   )#r‡   rU   r…   ÚImportErrorr„   Úprintr   rt   ÚhasattrrŠ   ÚvalidateÚget_versionr   ÚSETTINGS_MODULEr   rE   ÚDEBUGr   r   ÚerrorÚEACCESÚ
EADDRINUSEÚEADDRNOTAVAILÚerrnoÚAttributeErrorÚKeyErrorr!   ÚsysÚstderrr   ÚstyleÚERRORÚ_exitÚKeyboardInterruptÚexit)ÚHAS_HOTSHOTr‰   rˆ   r@   rA   ÚeÚERRORSÚ
error_text©ÚaddrÚdjangor™   r=   ÚoptionsÚportÚquit_commandrJ   r   rM   Úsocket)	r‚   rƒ   r„   r]   r…   r‡   r:   r9   rU   r   Ú	inner_run®   s‚   ÿþ
"
ýÿ€ýz!Command.handle.<locals>.inner_run)Úrun_with_reloader)Ú
autoreload)r©   r­   r™   Údjango.core.servers.basehttprJ   ÚsplitÚ
ValueErrorÚisdigitr   Úgetrœ   ÚplatformÚdjango.utils.autoreloadr¯   rŽ   Údjango.utilsr°   Úmain)r   r/   Úargsrª   r4   r®   r¯   r°   r   r§   r   Úhandle”   s<   ÿ oþ
zCommand.handle)rI   )	r+   r,   r-   r2   rº   rC   r   r»   Ú__classcell__r   r   rG   r   r.   [   s    5r.   )Ú__doc__rœ   r   Údjango.confr   Ú#django.contrib.staticfiles.handlersr   Údjango.core.management.baser   r   r±   r   Ú"django_extensions.management.utilsr   ÚINSTALLED_APPSrE   Úobjectr	   r.   r   r   r   r   Ú<module>   s   
A