
    kh	*                         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     m/var/www/histauto/venv/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
           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--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   rL   zCommand.add_arguments_   s   f%%%c(N 	 	
 	
 	
 	 = 	 	
 	
 	
 	  5 	 	
 	
 	
 	P	 	 	
 	
 	
 	4T	 	 	
 	
 	
 	+ 	 	
 	
 	
 	2 	 	
 	
 	
  	$)!      #'J       	 	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   )runrQ   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                             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 t+          |          }Y nw xY wt,          j                            j                            d|z            dz              
                    d           Y d }~d S d }~wt8          $ r) rt	                     t-          j        d           Y d S w xY w) Nr   rH   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           
        	 | d         r/t          fd             D                       r | |          S                     d                              dd          pd}d|                                fz  }j                            |          }                                }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|                    |                               |t#          |          t#                                                    	          }
j                            d
|
z            }
                    ||
           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|                    |                               |t#          |          t#                                                    	          }
j                            d
|
z            }
                    ||
           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>   sB       ( (45	,,Q//( ( ( ( ( (r   /.rootz
%s.%d.profg     @@wr\   z%s.prof)anystripreplacer_   r]   joinProfiler   nowruncallsecondsmicrosecondsr   openr   
dump_statsformatr   rename)environstart_response	path_nameprofnameprofstartelapelapmskgf	profname2rp   
USE_LSPROFcProfilerh   inner_handlerrG   osrD   rC   r_   s              @r   handlerzQCommand.handle.<locals>.inner_run.<locals>.make_profiler_handler.<locals>.handler   sX    ' 4I   FC ( ( ( (9J9J9L9L( ( ( % % F  -}WnEEE ) 4 4 < <S# F F P&I+y$))++.FFH!w||Ix@@H#++--D$LNNE7#||M7NSS  (|~~5!%!69JV9S!S% 6!,T!2!2B!%h!4!4 - "		!- - - - - - - - - - - - - - - !OOH555$-$4$4!*S[[s499;;GWGW %5 % %	 %'GLLI	<Q$R$R			(I6666  (|~~5!%!69JV9S!S% 6!,T!2!2B!%h!4!4 - "		!- - - - - - - - - - - - - - - !OOH555$-$4$4!*S[[s499;;GWGW %5 % %	 %'GLLI	<Q$R$R			(I6666sD   G! %EE	E	!AK*.IK*IK*IBK*r5   )
r   r   r   r   rh   rG   r   rD   rC   r_   s
   ` r   make_profiler_handlerz@Command.handle.<locals>.inner_run.<locals>.make_profiler_handler   sM    7 7 7 7 7 7 7 7 7 7 7 7 7@ r   zPerforming system checks...T)display_num_errorsz
Django version z, using settings z(Development server is running at http://rU   rr   zQuit the server with %s.rI   rJ   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   r   printcheckget_versionr   SETTINGS_MODULEr   rN   DEBUGr   r   errorEACCES
EADDRINUSEEADDRNOTAVAILerrnoAttributeErrorKeyErrorr'   sysstderrr   styleERROR_exitKeyboardInterruptexit)r   r   rI   rJ   eERRORS
error_textr   r   rh   r   rD   rC   r_   addrdjangor   rG   optionsportquit_commandrS   r   rV   sockets          @@@@@@@r   	inner_runz!Command.handle.<locals>.inner_run   s   IIIKKKOOO .J,I,I##qq#AA <	M  
% % %! ! ! ! ! ! ! ! ! ! ! !F /000JJ$J///E%%'''')A)AC   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$   # ,*+++sD   $A1E 
H;!H9FH F*'H)F**AH2H;:H;)run_with_reloader)
autoreload)r   r   r   django.core.servers.basehttprS   split
ValueErrorisdigitr   getr   platformdjango.utils.autoreloadr   ImportErrordjango.utilsr   main)r   r9   argsr   r>   r   r   r   r   r   r   rG   r   r   rS   rV   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[b	 b	 b	 b	 b	 b	 b	 b	 b	 b	 b	 b	 b	 b	 b	H  
	+EEEEEE!!),,,,, + + +333333	******+
 IKKKKKs!   ? AA
C %DD)rQ   )	r2   r3   r4   r<   r   rL   r	   r   __classcell__)rP   s   @r   r7   r7   [   sk        DD3D> > > > >@ G G G ]G G G G Gr   r7   )__doc__r   r   django.confr   #django.contrib.staticfiles.handlersr   django.core.management.baser   r   r   r   "django_extensions.management.utilsr	   INSTALLED_APPSrN   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L L L L Lk L L L L Lr   