o
    /h	*                     @   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)getstatsdataout_file)selfprofiler r   q/var/www/html/env_mimamsha/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  | |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
 tr`|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--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   rB   _   sx   	
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}|rzddlm} || W d S  ty   ddlm} || Y d S w |  d S )Nr   )runrH   8000:z	127.0.0.1z%r is not a valid port number.r4   shutdown_messager=   win32z
CTRL-BREAKz	CONTROL-Cc                     s  dd l dd ldd l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
rsd }d }|rstjso|rst|}| |}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y   rt td Y d S w )Nr   r>   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|} }t }z[|	| |W t | }|j
d |jd  }rrt|}t|d	}	||	 W d    n1 slw   Y  n|| j|t|t	 d
}
jd|
 }
||
 S t | }|j
d |jd  }rt|}t|d	}	||	 W d    n1 sw   Y  n|| j|t|t	 d
}
jd|
 }
||
 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     @@wrQ   z%s.prof)anystripreplacerT   rR   joinProfiler   nowruncallsecondsmicrosecondsr	   openr   
dump_statsformatr   rename)environstart_response	path_nameprofnameprofstartelapelapmskgf	profname2)	
USE_LSPROFcProfiler\   inner_handlerr=   osr:   r9   rT   ra   r   handler   sP   


zQCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handlerr   )r   r   )r   r   r\   r=   r   r:   r9   rT   )r   r   make_profiler_handler   s    z@Command.handle.<locals>.inner_run.<locals>.make_profiler_handlerzPerforming system checks...T)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@   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   rT   r   rs   printcheckget_versionr   SETTINGS_MODULEr   rD   DEBUGr   r   errorEACCES
EADDRINUSEEADDRNOTAVAILerrnoAttributeErrorKeyErrorr!   sysstderrr   styleERROR_exitKeyboardInterruptexit)r   r   r?   r@   eERRORS
error_textaddrdjangor   r=   optionsportquit_commandrI   r   rL   socket)r   r   r\   r   r:   r9   rT   r   	inner_run   sb   
#z!Command.handle.<locals>.inner_run)run_with_reloader)
autoreload)r   r   r   django.core.servers.basehttprI   split
ValueErrorisdigitr   getr   platformdjango.utils.autoreloadr   ImportErrordjango.utilsr   main)r   r/   argsr   r4   r   r   r   r   r   r   handle   s<    d
zCommand.handle)rH   )	r+   r,   r-   r2   r   rB   r   r   __classcell__r   r   rF   r   r.   [   s    @r.   )__doc__r   r   django.confr   #django.contrib.staticfiles.handlersr   django.core.management.baser   r   r   r   "django_extensions.management.utilsr   INSTALLED_APPSrD   r	   r.   r   r   r   r   <module>   s   
A