ó
    èÎi½  ã                   óp  • % S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SKJrJ	r	  S SK
Jr  SrSr\\S'    S SKr\R"                  R%                  \R"                  R'                  \R(                  5      5      r\R"                  R-                  \S5      rS
\S\4S jr " S S5      rg! \ a    S	r Nf = f)é    N)ÚStats)Ú
json_statsÚ
table_rows)ÚtemplateTÚ Úsnakeviz_templates_dirÚ	templatesFÚstatsÚhtml_filenamec                 ó  • SnSn[        U[        5      (       d  [        S5      e[        R                  " [
        5      nUR                  S5      nUR                  S5      n[        U5      S:  a  [        SU S35      eUS	   nUR                  U[        U 5      [        U 5      S
9nUR                  S5      n	[        R                  " X#U	5      n	[        US5       n
U
R!                  U	5        SSS5        g! , (       d  f       g= f)ad  
Parses pstats data and populates viz.html template stored under templates dir.
This utility allows to export html file without kicking off webserver.

Note that it relies js scripts stored at rawgit cdn. This is not super
reliable, however it does allow one to not have to rely on webserver and
local rendering. On the other hand, for local rendering please follow
the main snakeviz tutorial

Inspiration for this util is from https://gist.github.com/jiffyclub/6b5e0f0f05ab487ff607.

Args:
    stats: Stats generated from cProfile data
    html_filename: Output filename in which populated template is rendered
z(?<!] \+ ")/static/zFhttps://cdn.jsdelivr.net/gh/jiffyclub/snakeviz@v0.4.2/snakeviz/static/z#A valid file name must be provided.zviz.htmlÚ.é   z([0;32;40m Provided filename [0;31;47m z) [0;32;40m does not contain . separator.r   )Úprofile_namer   Úcalleeszutf-8ÚwN)Ú
isinstanceÚstrÚ
ValueErrorr   ÚLoaderr   ÚloadÚsplitÚlenÚgenerater   r   ÚdecodeÚreÚsubÚopenÚwrite)r
   r   ÚRESTRÚREPLACE_WITHÚviz_html_loaderÚhtml_bytes_rendererÚ
file_splitr   Ú
html_bytesÚhtml_stringÚfs              Ú^/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/util/python_profiler.pyÚ_from_pstat_to_static_htmlr(   "   sû   € ð  #€EàPð ô m¤S×)Ñ)ÜÐ>Ó?Ð?ä—o’oÔ&<Ó=€OØ)×.Ñ.¨zÓ:ÐØ×$Ñ$ SÓ)€JÜ
ˆ:ƒ˜ÓÜØ<¸]¸OÐKwÐxó
ð 	
ð ˜a‘=€LØ$×-Ñ-Ø!Ü˜eÓ$Ü˜5Ó!ð .ð €Jð
 ×#Ñ# GÓ,€KÜ—&’&˜¨kÓ:€KÜ	ˆm˜SÔ	! QØ	‰Ô÷ 
"×	!Ö	!ús   ÃC6Ã6
Dc                   ó.   • \ rS rSrS\4S jrS rS rSrg)ÚCProfilerFlameGraphéM   Úfilenamec                 ó<   • [         (       d  [        S5      eXl        g )NzhPlease install snakeviz to use CProfilerFlameGraph. Follow cprofiler_flamegraph.md for more information.)Úmodule_foundÚ	Exceptionr,   )Úselfr,   s     r'   Ú__init__ÚCProfilerFlameGraph.__init__N   s   € ßŠ|ÜØzóð ð !ó    c                 ól   • [         R                  " 5       U l        U R                  R                  5         g )N)ÚcProfileÚProfileÚprÚenable)r0   s    r'   Ú	__enter__ÚCProfilerFlameGraph.__enter__U   s    € Ü×"Ò"Ó$ˆŒØ‰‰Õr3   c                 óþ   • Ub  [         R                  " SXU4S9  U R                  R                  5         [        R
                  " 5       n[        R                  " U R                  US9n[        XPR                  5        g )NzException occurred)Úexc_info)Ústream)
ÚloggingÚerrorr7   ÚdisableÚioÚStringIOÚpstatsr   r(   r,   )r0   Úexc_typeÚexc_valÚexc_tbÚsÚpss         r'   Ú__exit__ÚCProfilerFlameGraph.__exit__Y   sU   € ØÑÜMŠMÐ.¸(ÈVÐ9TÒUà‰‰ÔÜKŠK‹MˆÜ\Š\˜$Ÿ'™'¨!Ñ,ˆÜ" 2§}¡}Õ5r3   )r,   r7   N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r   r1   r9   rI   Ú__static_attributes__© r3   r'   r*   r*   M   s   † ð! ô !òõ6r3   r*   )r5   rA   r>   ÚosrC   r   r   Úsnakeviz.statsr   r   Útornador   r.   r   r   Ú__annotations__ÚsnakevizÚpathÚdirnameÚabspathÚ__file__Úsnakeviz_dirÚjoinÚImportErrorr(   r*   rP   r3   r'   Ú<module>r]      s­   ðô Û 	Û Û 	Û Û 	Ý ÷õ à€Ø Ð ˜Ó  ðÛà—7‘7—?‘? 2§7¡7§?¡?°8×3DÑ3DÓ#EÓF€LØŸW™WŸ\™\¨,¸ÓDÐð
( eð (¸Cô (÷V6ò 6øð_ ó Ø‚Lðús   ¸AB* Â*B5Â4B5