
    ¯wg<'                         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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)Path)Listz<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      X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/utils/_freeze.pywrapperzindent_msg.<locals>.wrapperP   s;    Q!$!&!Q!
    )	functoolswraps)r   r   s   ` r   
indent_msgr   O   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   Z   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__g   s    24$r   pathcodec                     | j                   sy t        | j                        D ]  }t        dd        t        | d|        y )Nz     )end )r,   ranger   print)r/   r1   r2   is       r   msgzFreezer.msgl   sC    || t{{# 	"A&b!	"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)r7   NUM_BYTECODE_FILESopenosr1   join	itertoolscycler.   write_frozennextclose)	r/   install_rootr9   bytecode_file_namesnamebytecode_filesitmfs	            r   write_bytecodezFreezer.write_bytecodex   s     ;@@R:STQ1#R0TTDW
<@DlD137
 
 __^,$$ 	+Aab*	+   	AGGI	 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@   r1   rA   writeMAIN_INCLUDESr.   r   MAIN_PREFIX_TEMPLATEformatr   r    MAIN_SUFFIXFAKE_PREFIX)r/   rG   osssymbol_nameoutfprL   s         r   
write_mainzFreezer.write_main   s    "'',,|X6< 	)KK&(( E3AHH:UCDE KK,33K@A(( QeAMM?#ahhZr!&&OPQKK$K(K(	) 	) 	)s   C5D))D2rL   c                 8   |j                  d|j                   d       t        dt        |j                        d      D ]G  }|j                  d       t        |j                  ||dz          D ]  }|j                  d|z          I |j                  d       y)	z<Write a single frozen module's bytecode out to a C variable.zunsigned char z[] = {r      z
	z%d,z
};
N)rP   r   r7   lenr!   r(   )r/   rL   rX   r9   cs        r   rD   zFreezer.write_frozen   s    nQXXJg67q#ajj/2. 	'AKK1::a!b&12 'EAI&'	' 	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/   r1   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)rI   ).0childs     r   	<genexpr>z*Freezer.compile_package.<locals>.<genexpr>   s     UUUZZ=8Us   SP)r`   rI   	DENY_LISTr:   anyiterdirrc   )r/   r1   r^   is_package_dirri   s        r   ra   zFreezer.compile_package   s     {{}}99	!HHT3 UdllnUUHHT3s\\^ 	7E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 )Nrg   )relative_toparentrI   partsstemlist)r/   rq   r^   normalized_pathmodule_basenamemodule_parents         r   get_module_qualnamezFreezer.get_module_qualname   s    
 $//0@0G0GH=0
 .4499O+2299??M-22O+2288M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NNre   F__M_rg   .)is_filesuffixr:   rI   rm   r|   rA   r?   r   readmarshaldumpsr\   r.   appendr   )
r/   r1   r^   module_qualnamemodule_mangled_namer   src_filecor!   r    s
             r   rb   zFreezer.compile_file   s    ||~~;;%HHT399	!HHT3s2249IJ"ii8++$Z 	68$$X]]_5B	6 ==$8}99%5D""/2FD(K	
	6 	6s    D00D9N)r"   r#   r$   boolr0   r   r%   r:   rN   rY   r   rD   rc   r   ra   r   r|   typesCodeTyper   rb   r)   r   r   r+   r+   f   s    % %

  
 C 
 ")l 6 6 6 7D 7D 7 7$7T 7T 7dSVi 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   defaultrg   )argparseArgumentParseradd_argument
parse_argsr+   r,   r   r   r`   existssortedglobrc   rN   install_dirrY   rV   rW   )parserr   rM   pr1   mods         r   mainr      s^   $$1DEF
s1CD
L?QR
/T   D  
 E/	   DAZZ 
'Aw;;=TM-A!B
 diin- )sC() NN4&
' T%%&LL!!488T-=-=>r   __main__)rr   N)__doc__r   r   rB   r   r@   r   dataclassesr   pathlibr   typingr   r   rQ   rR   rS   rU   rT   rm   r>   r   r   r+   r   r"   r)   r   r   <module>r      s   .     	  !   -  #))*CD	$     R
 R
j$?N zF r   