
     +hi-                         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          Z G 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                   ,    e Zd Zd Zd Zd Zd Zd ZdS )KCacheGrindc                 F    |                                 | _        d | _        d S N)getstatsdataout_file)selfprofilers     z/var/www/html/e360mart/e360mart_env/lib/python3.11/site-packages/django_extensions/management/commands/runprofileserver.py__init__zKCacheGrind.__init__   s     %%''	    c                     || _         | j                             d           |                                  | j        D ]}|                     |           d S )Nzevents: Ticks
)r   write_print_summaryr   _entry)r   r   entrys      r   outputzKCacheGrind.output   sb     -...Y 	 	EKK	 	r   c                     d}| j         D ])}t          |j        dz            }t          ||          }*| j                            d|fz             d S )Nr     zsummary: %d
)r   int	totaltimemaxr   r   )r   max_costr   r   s       r   r   zKCacheGrind._print_summary&   s`    Y 	0 	0EEOd233I8Y//HHOxk9:::::r   c                 z   | j         }|j        }t          |t                    r|                    d|z             n:|                    d|j        z             |                    d|j        z             t          |j        dz            }t          |t                    r|                    d|z             n|                    d|j	        |fz             |j
        r|j
        }ng }t          |t                    rd}n|j	        }|D ]}|                     ||           |                    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subentrys           r   r   zKCacheGrind._entry-   sI   =zdC   	5NN9t+,,,,NN9t'77888NN9t|3444)D011
dC   	JNN9z12222NN9(;Z'HHIII ; 	KEEEdC   	)FF(F 	- 	-HNN68,,,,tr   c                    | j         }|j        }t          |t                    r7|                    d|z             |                    d|j        fz             n^|                    d|j        z             |                    d|j        z             |                    d|j        |j        fz             t          |j
        dz            }|                    d||fz             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   s         r   r-   zKCacheGrind._subentryL   s    =}dC   	XNN:,---NN>X-?,AABBBBNN:(88999NN:4555NN?h.@$BU-VVWWW*T122	yFI#6677777r   N)__name__
__module____qualname__r   r   r   r   r-    r   r   r   r      s_            ; ; ;  >8 8 8 8 8r   r   c                   >     e Zd ZdZdZ fdZedd            Z xZS )Commandz7Starts a lightweight Web server with profiling enabled.z&[optional port number, or ipaddr:port]c                 \   t                                          |           |                    ddd           |                    ddddd	
           |                    ddddd
           |                    dddd           |                    dddd           |                    ddddd
           |                    ddddd
           |                    ddd dd!
           t          r6|                    d"dd#d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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@   rA   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__s     r   rM   zCommand.add_arguments_   s   f%%%c7 	 	
 	
 	
 	^= 	 	? 	? 	? 	MQU5 	 	
 	
 	
 	V 	 	
 	
 	

 	40 	 	
 	
 	
 	:u+ 	 	
 	
 	
 	\, 	 	
 	
 	
 	L|2 	 	
 	
 	
  		L]9M!   " " "
 \8JJ   L L L L L		L 		Lr    c                 *   	
 dd l 	dd ldd l
ddlm |sddn/	 |                    d          \  n# t          $ r d|cY nw xY wsd                                st          dz            d         }	                    d	d          d
         t          j        dk    rdpd	
 fd}|r@	 ddlm}  ||           d S # t          $ r ddlm} |                    |           Y d S w xY w |             d S )Nr   )runrR   8000:z	127.0.0.1z%r is not a valid port number.r>   shutdown_messagerG   win32z
CTRL-BREAKz	CONTROL-Cc            
      j  	
 dd l dd l	 dd ld} n# t          $ r d} Y nw xY wd         d         		rdr)	 dd l
dn!# t          $ r t          d           dY nw xY w	rst          d          | sst          d          d	         d
                             ddd          sdt          d           d 	
f
d}t          d                               d           t          d	                                dt          j                   t          ddd           t          dz             	 t                      }t          r/d         }d         }|rt          j        s|rt          |          } ||          } t!                    |d                    d S # j        $ r}j        dj        d j        d!i}	 ||j                 }n&# t,          t.          f$ r t1          |          }Y nw xY wt2          j                            j                            d"|z            d#z                                  d$           Y d }~d S d }~wt>          $ r) rt                     t3          j         d           Y d S w xY w)%Nr   TFrH   rI   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)rC   rD   1      pathdurationtimerE   zPFilename format is wrong. Default format used: '{path}.{duration:06d}ms.{time}'.c                      g } t          t          dd           }|r|                     |           t          t          dd           }|r|                     |           | S )N	MEDIA_URL
STATIC_URL)getattrr   append)exclude_paths	media_url
static_urls      r   get_exclude_pathsz<Command.handle.<locals>.inner_run.<locals>.get_exclude_paths   sg     "#Hk4@@	 4!((333$X|TBB
 5!((444$$r   c                 (   
   	
fd}|S )Nc           
      j   | d         r/t          fd             D                       r | |          S                     d                              dd          pd}d|                                fz  }j                            |          }r                                }n                    |          }t          j                    }	 |	                    | |          t          j                    |z
  }|j
        dz  |j        dz  z   }rMt          |          }t          |d          5 }	|                    |	           d d d            n# 1 swxY w Y   nr|                    |                               |t#          |          t#                                                    	          }
j                            d
|
z            }
s|                                                     ||
           S # t          j                    |z
  }|j
        dz  |j        dz  z   }rMt          |          }t          |d          5 }	|                    |	           d d d            n# 1 swxY w Y   nr|                    |                               |t#          |          t#                                                    	          }
j                            d
|
z            }
s|                                                     ||
           w xY w)N	PATH_INFOc              3   B   K   | ]}                     |          V  d S r   )
startswith).0p	path_infos     r   	<genexpr>zdCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handler.<locals>.<genexpr>   s1      ']']A	(<(<Q(?(?']']']']']']r   /.rootz
%s.%d.profg     @@wr]   z%s.prof)anystripreplacer`   r^   joinProfiler   nowruncallsecondsmicrosecondsr   openr   
dump_statsformatr   closerename)environstart_response	path_nameprofnameprofstartelapelapmskgf	profname2rq   USE_CPROFILE
USE_LSPROFcProfileri   hotshotinner_handlerrG   osrD   rC   r`   s              @r   handlerzQCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handler   s    ' 4I   FC']']']']IZIZI\I\']']']$]$] F,}WnEEE ) 4 4 < <S# F F P&I+y$))++.FFH!w||Ix@@H# 9'//11&x88$LNNE7#||M7NSS  (|~~5!%!69JV9S!S% 6!,T!2!2B!%h!4!4 - "		!- - - - - - - - - - - - - - -) 6 OOH555$-$4$4)cRXkk`cdhdmdmdodo`p`p$4$q$q	$&GLLI	<Q$R$R	+ ) JJLLL		(I6666  (|~~5!%!69JV9S!S% 6!,T!2!2B!%h!4!4 - "		!- - - - - - - - - - - - - - -) 6 OOH555$-$4$4)cRXkk`cdhdmdmdodo`p`p$4$q$q	$&GLLI	<Q$R$R	+ ) JJLLL		(I6666sD   H =EE#	&E#	AL2J 4L2 JL2JB*L2r5   )r   r   r   r   r   ri   r   rG   r   rD   rC   r`   s   ` r   make_profiler_handlerz@Command.handle.<locals>.inner_run.<locals>.make_profiler_handler   sV    7 7 7 7 7 7 7 7 7 7 7 7 7 7 7> r   zPerforming system checks...)display_num_errorsz
Django version z, using settings z(Development server is running at http://rV   rs   zQuit the server with %s.rJ   rK   rB   )	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   r`   r   ImportErrorr   printr   r   checkget_versionr   SETTINGS_MODULEr   rO   DEBUGr   r   errorEACCES
EADDRINUSEEADDRNOTAVAILerrnoAttributeErrorKeyErrorr'   sysstderrr   styleERROR_exitKeyboardInterruptexit)HAS_HOTSHOTr   r   rJ   rK   eERRORS
error_textr   r   r   ri   r   r   rD   rC   r`   addrdjangor   rG   optionsportquit_commandrT   r   rW   sockets           @@@@@@@@@r   	inner_runz!Command.handle.<locals>.inner_run   s   IIIKKK$" $ $ $#$">2L .J $# ))#OOO#'LL" ) ) )IJJJ#(LLL)  m, m"#klll b| b"#`aaa,I,I##qq#AA j<	hiii% % %                           D /000JJ$J///Ef>P>P>R>R>R>RT\TlTlmnnnEdddDDDQRRR,|;<<<799" >)01E)F&'./A'B$) >x~ >AQ >"4W"="=//88D#d))W8PQQQQQQ<    L"R$&D')P
(!'JJ&1 ( ( (!$QJJJ(
  !1!1+
2J!K!Kd!RSSS$   # ,*+++sb    **A A)(A)A1G 
J2I<0G>=I<> H!I< H!!AI<<2J21J2)run_with_reloader)
autoreload)r   r   r   django.core.servers.basehttprT   split
ValueErrorisdigitr   getr   platformdjango.utils.autoreloadr   r   django.utilsr   main)r   r9   argsr   r>   r   r   r   r   r   r   rG   r   r   rT   rW   r   s   `  `    @@@@@@@@@r   handlezCommand.handle   s   444444 	*DDD*%^^C00
dd * * *
ddd* 	D||~~ 	H?$FGGG~.";;'92>>:&/A\P[l	 l	 l	 l	 l	 l	 l	 l	 l	 l	 l	 l	 l	 l	 l	Z  	+EEEEEE!!),,,,, + + +333333	******+ IKKKKKs!   ? AA
C %DD)rR   )	r2   r3   r4   r<   r   rM   r	   r   __classcell__)rQ   s   @r   r7   r7   [   sp        DD3D3L 3L 3L 3L 3Lj N N N ]N N N N Nr   r7   )__doc__r   r   django.confr   #django.contrib.staticfiles.handlersr   django.core.management.baser   r   r   r   "django_extensions.management.utilsr	   INSTALLED_APPSrO   r   r7   r5   r   r   <module>r      s   	 	 


                   B B B B B B A A A A A A A A F F F F F F < < < < < <.(2II>8 >8 >8 >8 >8 >8 >8 >8BH H H H Hk H H H H Hr   