
    Yh@                       d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 e	rddlm
Z
 e
eej                  e   f   Z	 dZdZd	Zd
ZdZdZdZdZdZdZdZdej2                  fdej4                  fdej4                  fdej2                  fdej2                  fdej2                  fdej2                  fgZ	 edej2                  fgz   Z	 dej2                  fdej2                  fgZ	 dej4                  fdej4                  fdej2                  fdej2                  fdej2                  fgZ	 dej4                  fdej4                  fdej>                  fdej>                  fdej2                  fdej2                  fgZ 	 dej2                  fdej2                  fgZ!	 dej2                  fdej2                  fd ejD                  d!z  fd"ej2                  fd#ej2                  fd$ej2                  fd%ej2                  fd&ej4                  fd'ej4                  fd(ej2                  fdej2                  fgZ#	 dej2                  fdej2                  fd ejD                  d!z  fd"ej>                  fd#ej>                  fd$ej>                  fd%ej>                  fd&ej4                  fd'ej4                  fd(ej2                  fdej2                  fgZ$	 e!d)ej2                  fd*ej2                  fgz   Z%	 e!d+ej2                  fd,ej2                  fd*ej2                  fd-ej2                  fgz   Z&	 d5d.Z'	 d6	 	 	 	 	 d7d/Z(d8d0Z)d9d1Z*	 d6	 	 	 	 	 d:d2Z+d;d3Z,d<d4Z-y)=a  
This module contains function to analyse dynamic library
headers to extract system information

Currently only for MacOSX

Library file on macosx system starts with Mach-O or Fat field.
This can be distinguish by first 32 bites and it is called magic number.
Proper value of magic number is with suffix _MAGIC. Suffix _CIGAM means
reversed bytes order.
Both fields can occur in two types: 32 and 64 bytes.

FAT field inform that this library contains few version of library
(typically for different types version). It contains
information where Mach-O headers starts.

Each section started with Mach-O header contains one library
(So if file starts with this field it contains only one version).

After filed Mach-O there are section fields.
Each of them starts with two fields:
cmd - magic number for this command
cmdsize - total size occupied by this section information.

In this case only sections LC_VERSION_MIN_MACOSX (for macosx 10.13 and earlier)
and LC_BUILD_VERSION (for macosx 10.14 and newer) are interesting,
because them contains information about minimal system version.

Important remarks:
- For fat files this implementation looks for maximum number version.
  It not check if it is 32 or 64 and do not compare it with currently built package.
  So it is possible to false report higher version that needed.
- All structures signatures are taken form macosx header files.
- I think that binary format will be more stable than `otool` output.
  and if apple introduce some changes both implementation will need to be updated.
- The system compile will set the deployment target no lower than
  11.0 for arm64 builds. For "Universal 2" builds use the x86_64 deployment
  target when the arm64 target is 11.0.
    )annotationsN)BufferedIOBase)TYPE_CHECKING)Unionl   : l   ~u} l   : l   ~u l   z} l   m l   z} l   m $   2   i  magiccputype
cpusubtypefiletypencmds
sizeofcmdsflagsreserved	nfat_archoffsetsizealigncmdcmdsizesegname   vmaddrvmsizefileofffilesizemaxprotinitprotnsectsversionsdkplatformminosntoolsc                H    | dz  dz  | dz  dz  z  | dz	  dz  z  | dz	  dz  z  S )N   l      ~    i          )xs    e/var/www/django-portfolio.reubendavern.link/venv/lib/python3.12/site-packages/wheel/macosx_libfile.pyswap32r-      sH    
r'Z	Fj 	"Fj 	" Gz!	#    c                   || j                         }n| j                  |       t        j                  j	                  | j                  t        j                  t        j                                    j                  }|t        t        t        t        fv r@t        j                  dk(  rt        j                  }nt        j                  }t!        |      }nt        j"                  }| j                  |       ||fS )Nlittle)tellseekctypesc_uint32from_buffer_copyreadsizeofvalue	FAT_CIGAMFAT_CIGAM_64MH_CIGAMMH_CIGAM_64sys	byteorderBigEndianStructureLittleEndianStructurer-   	Structure)lib_filer2   magic_number	BaseClasss       r,   get_base_class_and_magic_numberrE     s     |}}d??33fmmFOO45e 
 	<;GG==H$11I44Il+$$	MM$l""r.   c                h    | j                  |j                  t        j                  |                   S N)r5   r6   r3   r7   )struct_classrB   s     r,   	read_datarI     s%    ((v}}\7R)STTr.   c                   t        | d      5 }t        |d      \  }}|t        t        t        t
        fvr
	 d d d        y |t        t        fv r G d d|      }t        ||      }|t        k(  r G d d|      }n G d d|      }t        |j                        D cg c]  }t        ||       }}g }	|D ]S  }
	 t        ||
j                        }|8|
j                  t        k(  rt        |      dk7  r|d	k(  rC|	j                  |       U t        |	      dkD  rt#        |	      cd d d        S 	 d d d        y 	 t        |d      cd d d        S c c}w # t         $ r Y w xY w# t         $ r 	 Y d d d        y w xY w# 1 sw Y   y xY w)
Nrbr   c                      e Zd ZeZy)4extract_macosx_min_system_version.<locals>.FatHeaderN)__name__
__module____qualname__fat_header_fields_fields_r*   r.   r,   	FatHeaderrM   (  s    ,r.   rS   c                      e Zd ZeZy2extract_macosx_min_system_version.<locals>.FatArchN)rN   rO   rP   fat_arch_fieldsrR   r*   r.   r,   FatArchrV   .  s    .Hr.   rX   c                      e Zd ZeZyrU   )rN   rO   rP   fat_arch_64_fieldsrR   r*   r.   r,   rX   z2extract_macosx_min_system_version.<locals>.FatArch3  s    1Hr.      )   r   r   )openrE   	FAT_MAGICFAT_MAGIC_64MH_MAGICMH_MAGIC_64r:   rI   ranger   read_mach_headerr   r
   CPU_TYPE_ARM64lenappend
ValueErrormax)path_to_librB   rD   rC   rS   
fat_headerrX   _fat_arch_listversions_listelr    s               r,   !extract_macosx_min_system_versionro      s   	k4	  6H"A(A"N	<	<;OO6 6
 I|44-I - #9h7Jy(/i /
2i 2 7<J<P<P6Q12	'8,M  9;M# .xCG*::7C<NRS<S  '*4 (%,,W5$ =!A%=)[6 6^ _6 6d'!4g6 6*( "   ?m6 6hi6 6sw   )E8AE8E.	E88>E6E87EE8,E87E E8	EE8EE8 
E5*E84E55E88Fc                p   t        | |      \  }}|t        k(  rdnd} G d d|      }|dk(  r G d d|      }n G d d|      }t        ||       }t        |j                        D ]  }| j                         }	t        ||       }
| j                  |	       |
j                  t        k(  r. G d d	|      }t        ||       }t        |j                        c S |
j                  t        k(  r. G d
 d|      }t        ||       }t        |j                        c S | j                  |	|
j                  z           y)z
    This function parses a Mach-O header and extracts
    information about the minimal macOS version.

    :param lib_file: reference to opened library file with pointer
    3264c                      e Zd ZeZy)%read_mach_header.<locals>.SegmentBaseN)rN   rO   rP   segment_base_fieldsrR   r*   r.   r,   SegmentBasert   g  s    &r.   rv   c                      e Zd ZeZy$read_mach_header.<locals>.MachHeaderN)rN   rO   rP   mach_header_fieldsrR   r*   r.   r,   
MachHeaderry   l  s    )Hr.   r{   c                      e Zd ZeZyrx   )rN   rO   rP   mach_header_fields_64rR   r*   r.   r,   r{   z$read_mach_header.<locals>.MachHeaderq  s    ,Hr.   c                      e Zd ZeZy)+read_mach_header.<locals>.VersionMinCommandN)rN   rO   rP   version_min_command_fieldsrR   r*   r.   r,   VersionMinCommandr   {  s    5r.   r   c                      e Zd ZeZy)&read_mach_header.<locals>.VersionBuildN)rN   rO   rP   build_version_command_fieldsrR   r*   r.   r,   VersionBuildr     s    7r.   r   N)rE   r`   rI   rb   r   r1   r2   r   LC_VERSION_MIN_MACOSXparse_versionr    LC_BUILD_VERSIONr#   r   )rB   r2   
base_classrC   archrv   r{   mach_header_ipossegment_baser   version_infor   s                 r,   rc   rc   Z  s*     ?xNJ8+4D'j ' t|	* 	*
	- 	- J1KK%%& mmo h7c446J 6 %%6AL !5!566!118z 8 %\8<L !3!344MM# 4 445)r.   c                6    | dz  dz	  }| dz  dz	  }| dz  }|||fS )Nl      r   r(   r'   r)   r*   )r    r+   yzs       r,   r   r     s5    	:	"$A	:	!#A*Aa7Nr.   c           	        |j                  d      \  }}}t        d |j                  d      D              }|dd }|d   dkD  r|d   df}t        |      dk(  sJ dt        j                  v rt        d	 t        j                  d   j                  d      D              }|dd }|d   dkD  r|d   df}||k  r\t
        j                  j                  d
j                  dj                  d |D              dj                  d |D                           n|}t        |      dk(  sJ |}i }t        j                  |       D ]w  \  }}	}
|
D ]l  }|j                  d      s|j                  d      s&t        j                  j                  ||      }t        |      }|T|dd }|d   dkD  r|d   df}|||<   n y t        |      dkD  r#t        |t        |j                                     }dj                  |D cg c]  }t!        |       c}      }||k  r|j#                         D cg c]  \  }}||kD  s| }}}dj                  |      }t        |      dk(  rd}nd}d|z   dz   |z   dz   |z   }dt        j                  v r|j                  d      }n|j                  d      }t
        j                  j                  |       |dz   |z   dz   |z   }|S c c}w c c}}w )z
    Calculate proper macosx platform tag basing on files which are included to wheel

    Example platform tag `macosx-10.14-x86_64`
    -c              3  2   K   | ]  }t        |        y wrG   int.0r+   s     r,   	<genexpr>z0calculate_macosx_platform_tag.<locals>.<genexpr>  s     AAQA   .N   r   
   MACOSX_DEPLOYMENT_TARGETc              3  2   K   | ]  }t        |        y wrG   r   r   s     r,   r   z0calculate_macosx_platform_tag.<locals>.<genexpr>  s      
CF
r   z[WARNING] MACOSX_DEPLOYMENT_TARGET is set to a lower value ({}) than the version on which the Python interpreter was compiled ({}), and will be ignored.
c              3  2   K   | ]  }t        |        y wrG   strr   s     r,   r   z0calculate_macosx_platform_tag.<locals>.<genexpr>  s     ;SV;r   c              3  2   K   | ]  }t        |        y wrG   r   r   s     r,   r   z0calculate_macosx_platform_tag.<locals>.<genexpr>  s     :SV:r   z.dylibz.sork   
r[   z	this filezthese filesz}[WARNING] This wheel needs a higher macOS version than {}  To silence this warning, set MACOSX_DEPLOYMENT_TARGET to at least z or recreate z( with lower MACOSX_DEPLOYMENT_TARGET:  
z,is set in MACOSX_DEPLOYMENT_TARGET variable.z8the version your Python interpreter is compiled against.)splittuplere   osenvironr=   stderrwriteformatjoinwalkendswithpathro   rh   valuesr   items)archive_rootplatform_tagprefixbase_versionsuffixdeploy_targetstart_versionversions_dictdirpath	_dirnames	filenamesfilenamelib_pathmin_verr+   fin_base_versionkvproblematic_files
files_formerror_messages                        r,   calculate_macosx_platform_tagr     s+    $0#5#5c#: FL&A););C)@AAL#LA$Q+|!!!!RZZ/ 
JJ'ABHHM
 
 &bq)b *1-q1M<'JJ%%+VHH;];;HH:\::& )L|!!! M02M)+)> 	6%I! 	6H  *h.?.?.F77<<:;HE&%alGqzB#*1:q/.5M(+	6	6 =A<]-A-A-C)DE xx >AQ >?|#+8+>+>+@V41aADUQVV II&78 !Q&$J&JQ  	
,, /@@ 	 &3)00>M *00JM 	

'C<"22S86AL= !?Vs   9K*K8K)r+   r   returnr   rG   )rB   r   r2   
int | Noner   z"tuple[type[ctypes.Structure], int])rH   ztype[ctypes.Structure]rB   r   )ri   r   )rB   r   r2   r   r   ztuple[int, int, int] | None)r    r   r   ztuple[int, int, int])r   StrPathr   r   r   r   ).__doc__
__future__r   r3   r   r=   ior   typingr   r   r   PathLiker   r^   r9   r_   r:   r`   r;   ra   r<   r   r   rd   r4   c_intrz   r}   rQ   rW   c_uint64rZ   ru   c_charsegment_command_fieldssegment_command_fields_64r   r   r-   rE   rI   ro   rc   r   r   r*   r.   r,   <module>r      s  &P #  	 
   CS))*G ?		   foo6<< !foo6??#foo  +z6??.K-LL  v/+v1OP  6<< vV__foo 6<< vV__foo! 	 v/)V__1MN  # FOO "#vv !vfoo $ FOO "#vv !vfoo   1 
FOO4  
  3!foo
FOOv	6   
 ##
# (#6U7x //
/ !/dOr.   