
     hiQ                         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m	Z	 ddl
mZmZmZ ddlmZmZmZ  G d d      Z G d d	      Z G d
 d      Zy)zl
Utilities to create data structures for embedding Python modules and additional files into the executable.
    N)get_code_objectreplace_filename_in_code_object)BYTECODE_MAGICis_winstrict_collect_mode)PYZ_ITEM_MODULEPYZ_ITEM_NSPKGPYZ_ITEM_PKGc                   4    e Zd ZdZdZdZdZddZed        Z	y)	ZlibArchiveWriterz
    Writer for PyInstaller's PYZ (ZlibArchive) archive. The archive is used to store collected byte-compiled Python
    modules, as individually-compressed entries.
    s   PYZ       Nc                 >   |xs i }t        |d      5 }|j                  d| j                  z         g }|D ]&  }| j                  |||      }|j	                  |       ( |j                         }t        j                  |      }	|j                  |	       |j                  dt        j                         |j                  | j                         |j                  t               |j                  t        j                  d|             ddd       y# 1 sw Y   yxY w)a*  
        filename
            Target filename of the archive.
        entries
            An iterable containing entries in the form of tuples: (name, src_path, typecode), where `name` is the name
            under which the resource is stored (e.g., python module name, without suffix), `src_path` is name of the
            file from which the resource is read, and `typecode` is the Analysis-level TOC typecode (`PYMODULE`).
        code_dict
            Optional code dictionary containing code objects for analyzed/collected python modules.
        wb    r   z!iN)openwrite_HEADER_LENGTH_write_entryappendtellmarshaldumpsseekosSEEK_SET_PYZ_MAGIC_PATTERNr   structpack)
selffilenameentries	code_dictfptocentry	toc_entry
toc_offsettoc_datas
             GC:\des-py\Monitor\venv\Lib\site-packages\PyInstaller/archive/writers.py__init__zZlibArchiveWriter.__init__$   s     O	(D! 	4RHHUT0001 C  & --b%C	

9%&
 J}}S)HHHX GGAr{{#HHT,,-HH^$HHV[[z231	4 	4 	4s   C7DDc                    |\  }}}|dv sJ |dv r|t         |j                         dffS ||   }t        j                  j	                  t        j                  j                  |            \  }}	|dk(  r7t        }t        j                  j                  g |j                  d      d }
n4t        }t        j                  j                  |j                  d       dz   }
t        ||
      }t        j                  |      }t        j                  || j                        }|||j                         t!        |      ff}|j#                  |       |S )N>   
PYMODULE-1
PYMODULE-2PYMODULE>   N-r   r+   .z__init__.py.py)r	   r   r   pathsplitextbasenamer
   joinsplitr   r   r   r   zlibcompress_COMPRESSION_LEVELlenr   )clsr$   r&   r#   namesrc_pathtypecodecode_objectsrc_basename_co_filenamedataobjr'   s                 r*   r   zZlibArchiveWriter._write_entryK   s(   #( hCCCC{" >2779a899o''**277+;+;H+EFa:%#H'',,G

3GGK&H'',,

385@K 6k;O }}[) mmD#"8"89 HbggiS:;	 	    )N)
__name__
__module____qualname____doc__r   r   r:   r+   classmethodr    rF   r*   r   r      s4     "N%4N # #rF   r   c                       e Zd ZdZdZdZ ej                  e      ZdZ	 ej                  e	      Z
dZd Zd Zddefd	Zdd
Zed        Zy)CArchiveWriterah  
    Writer for PyInstaller's CArchive (PKG) archive.

    This archive contains all files that are bundled within an executable; a PYZ (ZlibArchive), DLLs, Python C
    extensions, and other data files that are bundled in onefile mode.

    The archive can be read from either C (bootloader code at application's run-time) or Python (for debug purposes).
    s   MEI
z
!8sIIII64sz!IIIIBc	   c                 L   t               | _        t        |d      5 }g }|D ]%  }| j                  ||      }|j	                  |       ' |j                         }| j                  |      }	t        |	      }
|j                  |	       ||
z   | j                  z   }t        j                  d   dz  t        j                  d   z   }t        j                  | j                  | j                  |||
||j!                  d            }|j                  |       ddd       y# 1 sw Y   yxY w)aH  
        filename
            Target filename of the archive.
        entries
            An iterable containing entries in the form of tuples: (dest_name, src_name, compress, typecode), where
            `dest_name` is the name under which the resource is stored in the archive (and name under which it is
            extracted at runtime), `src_name` is name of the file from which the resouce is read, `compress` is a
            boolean compression flag, and `typecode` is the Analysis-level TOC typecode.
        pylib_name
            Name of the python shared library.
        r   r   d      asciiN)set_collected_namesr   r   r   r   _serialize_tocr;   r   _COOKIE_LENGTHsysversion_infor   r   _COOKIE_FORMAT_COOKIE_MAGIC_PATTERNencode)r    r!   r"   
pylib_namer$   r%   r&   r'   r(   r)   
toc_lengtharchive_lengthpyverscookie_datas                 r*   r+   zCArchiveWriter.__init__   s    !$(D! 	"RC  & --b%8	

9%&
 J**3/HXJHHX (*4t7J7JJN%%a(3.1A1A!1DDF ++##**!!'*K HH[!7	" 	" 	"s   C5DD#c                 x   |\  }}}}|dk(  r| j                  |d||      S t        j                  j                  |      }t        rvt        j                  j
                  dk(  rY|j                  t        j                  j
                  d      }|dk(  r*|j                  t        j                  j
                  d      }t        rcd }|dv rnt        j                  j                  |      }|r;|| j                  v rt        d| d| d	      | j                  j                  |       |d
k(  r| j                  |d| d| |      S |dv rdddd|   }t        |||      }	t        j                  j                  |      d   t        j                  j                  |	j                        d   z   }
t        |	|
      }	| j                  |t!        j"                  |	      |d|      S |dv rt%        |d      5 }|j'                         }d d d        d d t(        k(  sJ t!        j*                  |dd        }	|dz   }
t        |	|
      }	| j                  |t!        j"                  |	      |||      S |dk(  r*|j-                  d      dz   }| j                  |||||      S | j/                  |||||      S # 1 sw Y   xY w)NorF   /\n>   Mmss1s2z7Attempting to collect a duplicated file into CArchive: z (type: )d:>   ri   rj   rk   r   rR      )ri   rj   rk   )optimizeri   )r9   )rh   rg   rb      r2   utf-8r   )_write_blobr   r3   normpathr   sepreplacer   normcaserU   
ValueErroraddr   r4   rC   r   r   r   r   readr   loadsr\   _write_file)r    r$   r&   	dest_namesrc_namer9   r?   normalized_destoptim_levelcoderC   in_fprD   s                r*   r   zCArchiveWriter._write_entry   s   27/	8Xx s?##BYAA GG$$Y/	bggkkS( "))"''++t<I 3#++BGGKK> "O66 #%''"2"29"="d&;&;;$QRaQbbjksjttuv  %%))/:s? ##Bz9+-FQQ** !"!4X>K"9hMD ''**95a8277;K;KDL\L\;]^_;``K24ED##Bd(;YV^#__# h% $zz|$8~---==bc+D#e+K24ED ##Bd(;Y[c#dd_??7+g5D##BiH#UU##B)XPX#YY$ $s   2J00J9blobc                     |j                         }t        |      }|r!t        j                  || j                        }|j                  |       |t        |      |t        |      ||fS )z
        Write the binary contents (**blob**) of a small file to the archive and return the corresponding CArchive TOC
        entry.
        )level)r   r;   r8   r9   r:   r   int)r    out_fpr   r   r?   r9   data_offsetdata_lengths           r*   ru   zCArchiveWriter._write_blob  sX    
 kkm$i==T-D-DEDTSYS]HiXXrF   c                    |j                         }t        j                  |      j                  }t	        |d      5 }|rt        d      }	t        j                  | j                        }
	 |j                  |	      }|sn$|j                  |
j                  |	d|              8|j                  |
j                                nt        j                  ||       ddd       ||j                         |z
  |t        |      ||fS # 1 sw Y   +xY w)zl
        Stream copy a large file into the archive and return the corresponding CArchive TOC entry.
        rq   i @  N)r   r   statst_sizer   	bytearrayr8   compressobjr:   readintor   r9   flushshutilcopyfileobjr   )r    r   r   r   r?   r9   r   r   r   
tmp_buffer
compressornum_reads               r*   r~   zCArchiveWriter._write_file  s     kkmggh'//(D! 	2U&y1
!--d.E.EF
$~~j9H#LL!4!4Z	5J!KL	 
 Z--/0""5&1	2 V[[][8+s8}V^`ijj	2 	2s   BDDc                    g }|D ]  }|\  }}}}}}	|	j                  d      }	t        |	      dz   }
| j                  |
z   }|dz  dk7  rd|dz  z
  }|
|z  }
t        j                  | j
                  |
 dz   | j                  |
z   |||||j                  d      |	      }|j                  |        dj                  |      S )Nrt   rR   rs   r   ri   rS   rF   )r\   r;   _TOC_ENTRY_LENGTHr   r   _TOC_ENTRY_FORMATr   r6   )r<   r%   serialized_tocr'   r   compressed_lengthr   r9   r?   r=   name_lengthentry_lengthpadding_lengthserialized_entrys                 r*   rV   zCArchiveWriter._serialize_toc#  s     	4IT]QK*K8T ;;w'Dd)a-K
 00;>Lb A%!#|b'8!9~-  &{{%%;-q(99%%3!(	  !!"237	4: xx''rF   N)F)rG   rH   rI   rJ   r[   rZ   r   calcsizerW   r   r   r:   r+   r   bytesru   r~   rK   rV   rL   rF   r*   rN   rN   r   sz     7 "N$V__^4N!'(9:)"VNZ`Y Yk* ( (rF   rN   c                   >    e Zd ZdZdZ ej                  e      Zd Zy)SplashWriterz
    Writer for the splash screen resources archive.

    The resulting archive is added as an entry into the CArchive with the typecode PKG_ITEM_SPLASH.
    z!32s 32s 16s 16s II II IIc	                    d }	|D 
cg c]
  }
 |	|
       }}
t        |d      5 }|j                  d| j                  z         d}|j                         }|D ]5  }
|
j	                  d      dz   }
|j                  |
       |t        |
      z  }7 |j                         }t        |      }|j                  |j	                  d             |j                         }t        |t              rt        |      }|j                  |       nBt        |d      5 }|j                         }ddd       t              }|j                  |       ~d }t        j                  | j                   ||d	d
       ||dd
       ||dd       ||dd      ||||||      }|j                  dt        j                         |j                  |       ddd       yc c}
w # 1 sw Y   xY w# 1 sw Y   yxY w)a  
        Writer for splash screen resources that are bundled into the CArchive as a single archive/entry.

        :param filename: The filename of the archive to create
        :param requirements_list: List of filenames for the requirements array
        :param str tcl_shared_library_name: Basename of the Tcl shared library
        :param str tk_shared_library_name: Basename of the Tk shared library
        :param str tcl_module_directory_name: Basename of the Tcl module directory (e.g., tcl/)
        :param str tk_module_directory_name: Basename of the Tk module directory (e.g., tk/)
        :param Union[str, bytes] image: Image like object
        :param str script: The tcl/tk script to execute to create the screen.
        c                     t         j                  j                  |       } t        rGt         j                  j                  dk(  r*| j                  t         j                  j                  d      } | S )Nrd   re   )r   r3   rv   r   rw   rx   )r!   s    r*   _normalize_filenamez2SplashWriter.__init__.<locals>._normalize_filename  sI    ww''1H"''++, $++BGGKK>OrF   r   r   r   rt   rq   Nc           	          | j                  d      }t        |      |k\  rt        d|dt        |       d| d      |S )Nrt   zLength of the encoded field z (z&) is greater or equal to the limit of z characters!)r\   r;   rz   )value
field_namelimit	enc_values       r*   _encode_strz*SplashWriter.__init__.<locals>._encode_str  sU    !LL1	y>U*$6znBs9~FV W$$)7,8 
 ! rF   tcl_shared_library_name    tk_shared_library_nametcl_module_directory_namers   tk_module_directory_name)r   r   r   r   r\   r;   
isinstancer   r|   r   r   _HEADER_FORMATr   r   r   )r    r!   requirements_listr   r   r   r   imagescriptr   r=   r$   requirements_lenrequirements_offsetscript_offset
script_lenimage_offset	image_lenimage_fp
image_datar   header_datas                         r*   r+   zSplashWriter.__init__g  s   4	 DUU406UU(D! ?	"RHHUT0001
  !"$'')) .{{7+e3 CI- . GGIMVJHHV]]7+, 779L%'J	 %& 1(!)J1
O	$
! !++##35NPRS24LbQ57RTVW46PRTU #K GGAr{{#HH[!?	" ?	" V<1 19?	" ?	"s*   GC/G!G%B"G!G	G!!G*N)	rG   rH   rI   rJ   r   r   r   r   r+   rL   rF   r*   r   r   F  s$    6 1N$V__^4Nc"rF   r   )rJ   r   r   r   r   rX   r8   PyInstaller.building.utilsr   r   PyInstaller.compatr   r   r   #PyInstaller.loader.pyimod01_archiver   r	   r
   r   rN   r   rL   rF   r*   <module>r      sS     	   
  W J J ] ]T TnQ( Q(hD" D"rF   