Ë
    [^(h	'  ã                   óè   — d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 dZdZdZej                  d«      Zd	Zg d
¢ZdZd„ Ze G d„ d«      «       Z G d„ d«      Zdd„Zedk(  r e«        yy)a4  
Freeze Python packages.

Freezing makes it possible to ship arbitrary Python modules as part of a C++
library. The Python source of the module is compiled to bytecode and written
to `.c` files, to be imported by Python's built-in FrozenImporter.

In a normal Python installation, FrozenImporter is only used to bootstrap the
initialization of the import machinery. Python's importers are defined in
Python (see `_bootstrap.py` and `_bootstrap_external.py`) but need to be
retrieved before any importers are available. Freezing the module bytecode
resolves this circular dependency.

This script will freeze the Python standard library. It produces two things:
- Bytecode files: A set of `.c` that define C variables containing Python bytecode.
- Main file: A `main.c` file listing all of these modules in the right form to be
  consumed by FrozenImporter.

The library that wishes to these modules make them available to the local
Python instance by extending `PyImport_FrozenModules` appropriately (see
https://docs.python.org/3/c-api/import.html#c.PyImport_FrozenModules).
é    N)Ú	dataclass)ÚPathz<Generated by torch::deploy>z#include <Python.h>

zž
// Compiled standard library modules. These should be appended to the existing
// `PyImport_FrozenModules` that ships with CPython.
struct _frozen {}[] = {{
Ú_PyImport_FrozenModulesz     {0, 0, 0} /* sentinel */
};
)
ÚdbmÚcursesÚtkinterr   ÚtestÚtestsÚ	idle_testz__phello__.foo.pyz_bootstrap.pyz_bootstrap_external.pyé   c                 óB   ‡ — t        j                  ‰ «      ˆ fd„«       }|S )Nc                  óx   •— | d   xj                   dz  c_          ‰| i |¤Ž}| d   xj                   dz  c_         |S )Nr   é   )Úindent)ÚargsÚkwargsÚretÚfns      €úQ/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/utils/_freeze.pyÚwrapperzindent_msg.<locals>.wrapperO   s;   ø€ àˆQ‰�Š˜!Ñ�Ù�$Ð!˜&Ñ!ˆØˆQ‰�Š˜!Ñ�Øˆ
ó    )Ú	functoolsÚwraps)r   r   s   ` r   Ú
indent_msgr   N   s%   ø€ Ü‡_�_�RÓóó ðð €Nr   c                   ó6   — e Zd ZU eed<   eed<   eed<   eed<   y)ÚFrozenModuleÚmodule_nameÚc_nameÚsizeÚbytecodeN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__ÚintÚbytes© r   r   r   r   Y   s   … ð ÓàƒKà
ƒIà„Or   r   c                   óÂ   — e Zd Zdefd„Zdedefd„Zd„ Zd„ Z	de
fd	„Zded
efd„Zeded
efd„«       Zded
edee   fd„Zdedej$                  fd„Zeded
efd„«       Zy)ÚFreezerÚverbosec                 ó.   — g | _         d| _        || _        y )Nr   )Úfrozen_modulesr   r+   )Úselfr+   s     r   Ú__init__zFreezer.__init__f   s   € Ø24ˆÔØˆŒØ$ˆ�r   ÚpathÚcodec                 ór   — | j                   sy t        d| j                  z  d¬«       t        |› d|› �«       y )Nz    Ú )Úendú )r+   Úprintr   )r.   r0   r1   s      r   ÚmsgzFreezer.msgk   s4   € Ø�|Š|Øô 	ˆf�t—{‘{Ñ"¨Õ+Ü���a˜�vÐÕr   c           	      óv  — t        t        «      D �cg c]  }d|› d�‘Œ
 }}|D �cg c],  }t        t        j                  j                  ||«      d«      ‘Œ. }}t        j                  |«      }| j                  D ]  }| j                  |t        |«      «       Œ |D ]  }|j                  «        Œ yc c}w c c}w )z~
        Write the `.c` files containing the frozen bytecode.

        Shared frozen modules evenly across the files.
        Ú	bytecode_z.cÚwN)ÚrangeÚNUM_BYTECODE_FILESÚopenÚosr0   ÚjoinÚ	itertoolsÚcycler-   Úwrite_frozenÚnextÚclose)	r.   Úinstall_rootÚiÚbytecode_file_namesÚnameÚbytecode_filesÚitÚmÚfs	            r   Úwrite_bytecodezFreezer.write_bytecodev   s¶   € ô ;@Ô@RÓ:SÖT°Q ¨1¨#¨RÒ0ÐTÐÐTàDWö
Ø<@ŒD”—‘—‘˜l¨DÓ1°3Õ7ð
ˆð 
ô �_‰_˜^Ó,ˆØ×$Ñ$ò 	+ˆAØ×Ñ˜a¤ b£Õ*ð	+ð  ò 	ˆAØ�G‰G�Iñ	ùò Uùò
s
   ’B1¥1B6c                 ój  — t        t        j                  j                  |d«      d«      5 }|j	                  t
        «       | j                  D ]!  }|j	                  d|j                  › d�«       Œ# |j	                  t        j                  |«      «       | j                  D ];  }|j	                  d|j                  › d|j                  › d|j                  › d�«       Œ= |j	                  t        «       |r*|j	                  t        «       |j	                  t        «       d	d	d	«       y	# 1 sw Y   y	xY w)
zNWrite the `main.c` file containing a table enumerating all the frozen modules.zmain.cr:   zextern unsigned char z[];
z	{"z", z, z},
N)r=   r>   r0   r?   ÚwriteÚMAIN_INCLUDESr-   r   ÚMAIN_PREFIX_TEMPLATEÚformatr   r   ÚMAIN_SUFFIXÚFAKE_PREFIX)r.   rE   ÚossÚsymbol_nameÚoutfprK   s         r   Ú
write_mainzFreezer.write_main‡   sô   € ä”"—'‘'—,‘,˜|¨XÓ6¸Ó<ð 	)ÀØ�K‰KœÔ&Ø×(Ñ(ò E�Ø—‘Ð3°A·H±H°:¸UÐCÕDðEð �K‰KÔ,×3Ñ3°KÓ@ÔAØ×(Ñ(ò Q�Ø—‘˜e A§M¡M ?°#°a·h±h°Z¸rÀ!Ç&Á&ÀÈÐOÕPðQà�K‰KœÔ$ÙØ—‘œKÔ(Ø—‘œKÔ(÷	)÷ 	)ñ 	)ús   «C5D)Ä)D2rK   c                 ó:  — |j                  d|j                  › d�«       t        dt        |j                  «      d«      D ]H  }|j                  d«       t        |j                  ||dz    «      D ]  }|j                  |d›d�«       Œ ŒJ |j                  d«       y	)
z<Write a single frozen module's bytecode out to a C variable.zunsigned char z[] = {r   é   z
	Údú,z
};
N)rO   r   r;   Úlenr    r'   )r.   rK   rW   rF   Úcs        r   rB   zFreezer.write_frozen–   sŒ   € à�‰�n Q§X¡X J¨gÐ6Ô7Ü�qœ#˜aŸj™j›/¨2Ó.ò 	'ˆAØ�K‰K˜ÔÜ˜1Ÿ:™: a¨!¨b©&Ð1Ó2ò '�Ø—‘˜q ˜e 1˜IÕ&ñ'ð	'ð 	�‰�HÕr   Útop_package_pathc                 ón   — |j                  «       r| j                  ||«       y| j                  ||«       y)z(Entry point for compiling a Path object.N)Úis_dirÚcompile_packageÚcompile_file)r.   r0   r_   s      r   Úcompile_pathzFreezer.compile_pathŸ   s-   € à�;‰;Œ=Ø× Ñ  Ð'7Õ8à×Ñ˜dÐ$4Õ5r   c                 óN  — |j                  «       sJ ‚|j                  t        v r| j                  |d«       yt	        d„ |j                  «       D «       «      }|s| j                  |d«       y| j                  |d«       |j                  «       D ]  }| j                  ||«       Œ y)z2Compile all the files within a Python package dir.ÚXNc              3   ó:   K  — | ]  }|j                   d k(  –— Œ y­w)ú__init__.pyN)rH   )Ú.0Úchilds     r   ú	<genexpr>z*Freezer.compile_package.<locals>.<genexpr>¯   s   è ø€ ÒU¸U˜UŸZ™Z¨=Õ8ÑUùs   ‚ÚSÚP)ra   rH   Ú	DENY_LISTr7   ÚanyÚiterdirrd   )r.   r0   r_   Úis_package_dirrj   s        r   rb   zFreezer.compile_package¦   s�   € ð �{‰{Œ}Ðˆ}Ø�9‰9œ	Ñ!Ø�H‰H�T˜3ÔØô ÑUÀdÇlÁlÃnÔUÓUˆÙØ�H‰H�T˜3ÔØà�‰��sÔà—\‘\“^ò 	7ˆEØ×Ñ˜eÐ%5Õ6ñ	7r   Ú	file_pathÚreturnc                 ó&  — |j                  |j                  «      }|j                  dk(  r7|j                  j                  }|j                  j                  j                  }n"|j                  }|j                  j                  }t        |«      |gz   S )Nrh   )Úrelative_toÚparentrH   ÚpartsÚstemÚlist)r.   rr   r_   Únormalized_pathÚmodule_basenameÚmodule_parents         r   Úget_module_qualnamezFreezer.get_module_qualname¹   s‚   € ð
 $×/Ñ/Ð0@×0GÑ0GÓHˆà×Ñ =Ò0ð
 .×4Ñ4×9Ñ9ˆOà+×2Ñ2×9Ñ9×?Ñ?‰Mà-×2Ñ2ˆOØ+×2Ñ2×8Ñ8ˆMÜ�MÓ" oÐ%6Ñ6Ð6r   Úfile_contentc                 ó(   — t         }t        ||d«      S )NÚexec)ÚPATH_MARKERÚcompile)r.   r~   Úpath_markers      r   Úcompile_stringzFreezer.compile_stringÍ   s   € ô "ˆÜ�| [°&Ó9Ð9r   c                 óx  — |j                  «       sJ ‚|j                  dk7  r| j                  |d«       y|j                  t        v r| j                  |d«       y| j                  |d«       | j                  ||«      }dj                  |«      }d|z   }t        |«      5 }| j                  |j                  «       «      }ddd«       t        j                  «      }t        |«      }	|j                  dk(  r|	 }	| j                  j                  t        d	j                  |«      ||	|«      «       y# 1 sw Y   ŒrxY w)
zw
        Compile a Python source file to frozen bytecode.

        Append the result to `self.frozen_modules`.
        z.pyÚNNrf   ÚFÚ__ÚM_rh   ú.)Úis_fileÚsuffixr7   rH   rn   r}   r?   r=   r„   ÚreadÚmarshalÚdumpsr]   r-   Úappendr   )
r.   r0   r_   Úmodule_qualnameÚmodule_mangled_namer   Úsrc_fileÚcor    r   s
             r   rc   zFreezer.compile_file×   s  € ð �|‰|Œ~Ðˆ~Ø�;‰;˜%ÒØ�H‰H�T˜3ÔØà�9‰9œ	Ñ!Ø�H‰H�T˜3ÔØà�‰��sÔØ×2Ñ2°4Ð9IÓJˆØ"Ÿi™i¨Ó8ÐØÐ+Ñ+ˆä�$‹Zð 	6˜8Ø×$Ñ$ X§]¡]£_Ó5ˆB÷	6ô —=‘= Ó$ˆÜ�8‹}ˆØ�9‰9˜Ò%à�5ˆDØ×Ñ×"Ñ"Ü˜Ÿ™ /Ó2°F¸DÀ(ÓKõ	
÷	6ð 	6ús   Â D0Ä0D9N)r!   r"   r#   Úboolr/   r   r$   r7   rM   rX   r   rB   rd   r   rb   ry   r}   ÚtypesÚCodeTyper„   rc   r(   r   r   r*   r*   e   sÄ   „ ð% ó %ð
	 ˜ð 	  Có 	 òò")ð˜ló ð6 ð 6¸ó 6ð ð7 Dð 7¸Dò 7ó ð7ð$7¨Tð 7ÀTð 7ÈdÐSVÉió 7ð(:¨3ð :°5·>±>ó :ð ð
 ð 
¸ò 
ó ñ
r   r*   c                  óì  — t        j                  d¬«      } | j                  ddd¬«       | j                  ddd	¬
«       | j                  ddd¬«       | j                  ddd¬
«       | j                  dddd¬«       | j                  «       }t	        |j
                  «      }|j                  D ]y  }t        |«      }|j                  «       rJt        j                  |dz  «      s2t        |j                  d«      «      D ]  }|j                  ||«       Œ Œh|j                  ||«       Œ{ |j                  |j                  «       |j                  |j                  |j                   |j"                  «       y )NzCompile py source)ÚdescriptionÚpathsÚ*zPaths to freeze.)ÚnargsÚhelpz	--verboseÚ
store_truezPrint debug logs)Úactionr�   z--install-dirz--install_dirz#Root directory for all output files)r�   z--ossz5If it's OSS build, add a fake _PyImport_FrozenModulesz--symbol-namez--symbol_namez6The name of the frozen module array symbol to generateÚ_PyImport_FrozenModules_torch)r�   Údefaultrh   )ÚargparseÚArgumentParserÚadd_argumentÚ
parse_argsr*   r+   rš   r   ra   ÚexistsÚsortedÚglobrd   rM   Úinstall_dirrX   rU   rV   )Úparserr   rL   Úpr0   Úmods         r   Úmainr­   ù   s^  € Ü×$Ñ$Ð1DÔE€FØ
×Ñ˜ sÐ1CÐÔDØ
×Ñ˜¨LÐ?QÐÔRØ
×ÑØ˜Ð/Tð ô ð ×ÑØØØDð ô ð
 ×ÑØØØEØ/ð	 ô ð ×ÑÓ€Dä�—‘Ó€Aà�Z‰Zò 
'ˆÜ�A‹wˆØ�;‰;Œ=¤§¡¨T°MÑ-AÔ!Bô
 ˜dŸi™i¨›nÓ-ò )�Ø—‘˜s CÕ(ñ)ð �N‰N˜4 Õ&ð
'ð ×Ñ�T×%Ñ%Ô&Ø‡L�L�×!Ñ! 4§8¡8¨T×-=Ñ-=Õ>r   Ú__main__)rs   N)Ú__doc__r¢   r   r@   rŽ   r>   r–   Údataclassesr   Úpathlibr   r�   rP   rQ   rR   rT   rS   rn   r<   r   r   r*   r­   r!   r(   r   r   ú<module>r²      s¨   ðñó. Û Û Û Û 	Û Ý !Ý ð -€ð€ðÐ ð #×)Ñ)Ð*CÓD€ð€ò€	ð$ Ð òð ÷ð ó ð÷Q
ñ Q
óh$?ðN ˆzÒÙ…Fð r   