
    jgi-                         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y)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y)KCacheGrindc                 <    |j                         | _        d | _        y N)getstatsdataout_file)selfprofilers     q/var/www/html/Stanley/venv/lib/python3.12/site-packages/django_extensions/management/commands/runprofileserver.py__init__zKCacheGrind.__init__   s    %%'	    c                     || _         | j                   j                  d       | j                          | j                  D ]  }| j	                  |        y )Nzevents: Ticks
)r   write_print_summaryr   _entry)r   r   entrys      r   outputzKCacheGrind.output   sB     -.YYEKK r   c                     d}| j                   D ]&  }t        |j                  dz        }t        ||      }( | j                  j                  d|fz         y )Nr     zsummary: %d
)r   int	totaltimemaxr   r   )r   max_costr   r   s       r   r   zKCacheGrind._print_summary&   sO    YYEEOOd23I8Y/H  	Oxk9:r   c                 x   | j                   }|j                  }t        |t              r|j	                  d|z         n<|j	                  d|j
                  z         |j	                  d|j                  z         t        |j                  dz        }t        |t              r|j	                  d|z         n |j	                  d|j                  |fz         |j                  r|j                  }ng }t        |t              rd}n|j                  }|D ]  }| j                  ||        |j	                  d       y )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-   s    ==zzdC NN9t+,NN9t'7'778NN9t||34))D01
dC NN9z12NN9(;(;Z'HHI ;;KKEEdC F((FHNN68, tr   c                    | j                   }|j                  }t        |t              r4|j	                  d|z         |j	                  d|j
                  fz         nf|j	                  d|j                  z         |j	                  d|j                  z         |j	                  d|j
                  |j                  fz         t        |j                  dz        }|j	                  d||fz         y )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 NN:,-NN>X-?-?,AABNN:(8(889NN:45NN?h.@.@$BUBU-VVW**T12	yFI#667r   N)__name__
__module____qualname__r   r   r   r   r-    r   r   r   r      s    ;>8r   r   c                   8     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         |   |       |j                  ddd       |j                  ddddd	
       |j                  ddddd
       |j                  dddd       |j                  dddd       |j                  ddddd
       |j                  ddddd
       |j                  ddd dd!
       t        r-|j                  d"dd#dd$
       |j                  d%dd&dd'
       y y )(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_   sl   f%c7 	 	
 	^= 	 	? 	MQU5 	 	
 	V 	 	

 	40 	 	
 	:u+ 	 	
 	\, 	 	
 	L|2 	 	
 ]9M!   "
 \8JJ   L r   c                    	
 dd l 	dd ldd l
ddlm |sddn	 |j                  d      \  sdj                         st        dz        d   }j                  d	d      d
   t        j                  dk(  xr dxs d	
 fd}|r	 ddlm}  ||       y  |        y # t        $ r d|cY w xY w# t        $ r ddlm} |j#                  |       Y y w xY w)Nr   )run 8000:z	127.0.0.1z%r is not a valid port number.r>   shutdown_messagerG   win32z
CTRL-BREAKz	CONTROL-Cc            
        	
 dd l dd l	 dd ld} d   d   		rdr	 dd l
d	rst        d      | sst        d      d	   d
   j                  ddd      sdt        d       d 	
f
d}t        d       j                  d       t        dj                         dt        j                         t        ddd       t        dz         	 t               }t        r)d   }d   }|rt        j                  s|rt        |      } ||      } t!              |d          y # t        $ r d} Y Dw xY w# t        $ r t        d       dY Jw xY w# j"                  $ r}j$                  dj&                  d j(                  d!i}	 ||j*                     }n # t,        t.        f$ r t1        |      }Y nw xY wt2        j4                  j7                  j8                  j;                  d"|z        d#z          j=                  d$       Y d }~y d }~wt>        $ r% rt               t3        j@                  d       Y y 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| j                  |       t        t        dd       }|r| j                  |       | 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   sK     "#Hk4@	!((3$X|TB
!((4$$r   c                 (   
   	
fd}|S )Nc           
         | d   r"t        fd        D              r	 | |      S j                  d      j                  dd      xs d}d|j                         fz  }j                  j                  |      }rj                         }nj                  |      }t        j                         }	 |j                  | |      t        j                         |z
  }|j                  dz  |j                  dz  z   }r2t        |      }t        |d      5 }	|j                  |	       d d d        nr|j                  |       j!                  |t#        |      t#        j                               	      }
j                  j                  d
|
z        }
s|j%                          j'                  ||
       S # 1 sw Y   xY w# t        j                         |z
  }|j                  dz  |j                  dz  z   }r>t        |      }t        |d      5 }	|j                  |	       d d d        n# 1 sw Y   nxY wr|j                  |       j!                  |t#        |      t#        j                               	      }
j                  j                  d
|
z        }
s|j%                          j'                  ||
       w xY w)N	PATH_INFOc              3   @   K   | ]  }j                  |        y wr   )
startswith).0p	path_infos     r   	<genexpr>zdCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handler.<locals>.<genexpr>   s     ']I\A	(<(<Q(?I\s   /.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   sZ    ' 4I  C']IZI\']$],WnEE ) 4 < <S# F P&I+y$))+.FFH!ww||Ix@H#'//1&x8$LLNE7#||M7NS  (||~5!%!69J9JV9S!S%!,T!2B!%h!4 "		! "5!4) OOH5$-$4$4)cRXk`cdhdmdmdo`p$4$q	$&GGLLI	<Q$R	+ JJL		(I6 "5!4	  (||~5!%!69J9JV9S!S%!,T!2B!%h!4 "		! "5!4!4) OOH5$-$4$4)cRXk`cdhdmdmdo`p$4$q	$&GGLLI	<Q$R	+ JJL		(I6s2   ;G GG	AKH90	K9I>BK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   s    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_commandrS   r   rW   sockets           @@@@@@@@@r   	inner_runz!Command.handle.<locals>.inner_run   s\   $" #>2L .J#)##'L ,"#kll|"#`aa,I,I##qq#A<	hi%   D /0JJ$J/f>P>P>RT\TlTlmndDQR,|;<79")01E)F&'./A'B$)x~~AQ"4W"=/8D#d)W8PQi  $#$ # )IJ#(L)V <<  LL"R$$&D'')P
(!'J&1 (!$QJ(

  !1!1+
2J!Kd!RS$ #*+sf   E E" 6AE? EE"E<;E<?I*&H95GH9G"H9!G""AH99.I*)I*)run_with_reloader)
autoreload)r   r   r   django.core.servers.basehttprS   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   rS   rW   r   s   `  `    @@@@@@@@@r   handlezCommand.handle   s    4DD*%^^C0
d D||~?$FGG~.";;'92>:&/A\P[l	 l	Z +E!),
 KE  *
d*|  +3	*+s#   B< %C <CC C32C3)rT   )	r2   r3   r4   r<   r   rM   r	   r   __classcell__)rQ   s   @r   r7   r7   [   s*    DD3D3Lj 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      sJ   	     B A F <.(2I2II>8 >8BHk Hr   