o
    hi-                     @   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   t/var/www/html/ictaz-jobs/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  | |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rj|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.)super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}|rz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                     sT  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 jdd td
	 t
jf  td	f  td  z+t }trd }d }|rt
js|rt|}||}	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)   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  }rt|}t|d	}	||	 W d    n1 sw   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Performing system checks...)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   check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<    m
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   r	   r.   r   r   r   r   <module>   s   
A