a
    QSic:>                     @   s\  d Z ddlZddlZddl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jfdejfdejfdejfdejfdejfdejfgZedejfg ZdejfdejfgZdejfdejfdejfdejfdejfgZdejfdejfdejfdejfdejfdejfgZdejfdejfgZdejfdejfdejd fdejfdejfd ejfd!ejfd"ejfd#ejfd$ejfdejfgZdejfdejfdejd fdejfdejfd ejfd!ejfd"ejfd#ejfd$ejfdejfgZed%ejfd&ejfg Zed'ejfd(ejfd&ejfd)ejfg Zd*d+ Zd8d,d-Zd.d/ Zd0d1 Z d9d2d3Z!d4d5 Z"d6d7 Z#dS ):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.
    Nl   : l   ~u} l   : l   ~u l   z} l   m l   z} l   m $   2   i  magiccputypeZ
cpusubtypeZfiletypencmdsZ
sizeofcmdsflagsreserved	nfat_archoffsetsizeZaligncmdcmdsizeZsegname   ZvmaddrZvmsizeZfileofffilesizeZmaxprotZinitprotZnsectsversionZsdkplatformminosZntoolsc                 C   s0   | d> d@ | d> d@ B | d? d@ B | d? d@ B S )N   l      ~    i          )xr   r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/wheel/macosx_libfile.pyswap32   s    



r   c                 C   s   |d u r|   }n
| | tj| ttjj}|tt	t
tfv rjtjdkrZtj}ntj}t|}nt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	byteorderZBigEndianStructureZLittleEndianStructurer   Z	Structure)lib_filer   magic_number	BaseClassr   r   r   get_base_class_and_magic_number   s    




r-   c                 C   s   |  |t| S N)r    r!   r   r"   )Zstruct_classr*   r   r   r   	read_data   s    r/   c           	   
      s  t | dtd\}}|ttttfvr<W d    d S |ttfv rFG dd d|}t|}|tkr~G dd d| nG dd d|  fdd	t|j	D }g }|D ]^}zDt
|j}|d ur|jtkrt|d
kr|dkrW q|| W q ty   Y q0 qt|dkr4t|W  d    S W d    d S n>zt
dW W  d    S  ty   Y W d    d S 0 W d    n1 s0    Y  d S )Nrbr   c                   @   s   e Zd ZeZdS )z4extract_macosx_min_system_version.<locals>.FatHeaderN)__name__
__module____qualname__fat_header_fields_fields_r   r   r   r   	FatHeader  s   r6   c                   @   s   e Zd ZeZdS z2extract_macosx_min_system_version.<locals>.FatArchN)r1   r2   r3   fat_arch_fieldsr5   r   r   r   r   FatArch  s   r9   c                   @   s   e Zd ZeZdS r7   )r1   r2   r3   fat_arch_64_fieldsr5   r   r   r   r   r9     s   c                    s   g | ]}t  qS r   )r/   ).0_r9   r*   r   r   
<listcomp>      z5extract_macosx_min_system_version.<locals>.<listcomp>   )   r   r   )openr-   	FAT_MAGICFAT_MAGIC_64MH_MAGICMH_MAGIC_64r%   r/   ranger	   read_mach_headerr
   r   CPU_TYPE_ARM64lenappend
ValueErrormax)	Zpath_to_libr,   r+   r6   Z
fat_headerZfat_arch_listZversions_listelr   r   r=   r   !extract_macosx_min_system_version   s:    
rO   c                 C   s  |dur|  | t| \}}|tkr*dnd}G dd d|}|dkrXG dd d|}nG dd d|}t|| }t|jD ]}|  }	t|| }
|  |	 |
jtkrG d	d
 d
|}t|| }t	|j
  S |
jtkrG dd d|}t|| }t	|j  S |  |	|
j  q|q|dS )z
    This funcition parse mach-O header and extract
    information about minimal system version

    :param lib_file: reference to opened library file with pointer
    NZ32Z64c                   @   s   e Zd ZeZdS )z%read_mach_header.<locals>.SegmentBaseN)r1   r2   r3   segment_base_fieldsr5   r   r   r   r   SegmentBase=  s   rQ   c                   @   s   e Zd ZeZdS z$read_mach_header.<locals>.MachHeaderN)r1   r2   r3   mach_header_fieldsr5   r   r   r   r   
MachHeaderB  s   rT   c                   @   s   e Zd ZeZdS rR   )r1   r2   r3   mach_header_fields_64r5   r   r   r   r   rT   G  s   c                   @   s   e Zd ZeZdS )z+read_mach_header.<locals>.VersionMinCommandN)r1   r2   r3   version_min_command_fieldsr5   r   r   r   r   VersionMinCommandP  s   rW   c                   @   s   e Zd ZeZdS )z&read_mach_header.<locals>.VersionBuildN)r1   r2   r3   build_version_command_fieldsr5   r   r   r   r   VersionBuildV  s   rY   )r   r-   rE   r/   rG   r   r   r   LC_VERSION_MIN_MACOSXparse_versionr   LC_BUILD_VERSIONr   r   )r*   r   
base_classr+   archrQ   rT   Zmach_headerZ_iposZsegment_baserW   version_inforY   r   r   r   rH   1  s.    






rH   c                 C   s*   | d@ d? }| d@ d? }| d@ }|||fS )Nl      r   r   r   r   r   )r   r   yzr   r   r   r[   `  s    r[   c              	      s`  | d\}}}tdd | dD }|dd }|d dkrL|d df}t|dks\J d	tjv rtd
d tjd	  dD }|dd }|d dkr|d df}||k rtjdd	dd |D d	dd |D  n|}t|dksJ | i }t
| D ]~\}}}	|	D ]l}
|
ds6|
drtj	||
}t|}|dur|dd }|d dkr||d df}|||< qqt|dkrt|t| }d	dd |D } |k rH fdd| D }d	|}t|dkrd}nd}d| d | d | }d	tjv r2|d}n
|d}tj| |d | d | }|S )z
    Calculate proper macosx platform tag basing on files which are included to wheel

    Example platform tag `macosx-10.14-x86_64`
    -c                 S   s   g | ]}t |qS r   intr;   r   r   r   r   r>   n  r?   z1calculate_macosx_platform_tag.<locals>.<listcomp>.N   r   
   MACOSX_DEPLOYMENT_TARGETc                 S   s   g | ]}t |qS r   rd   rf   r   r   r   r>   t  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                 s   s   | ]}t |V  qd S r.   strrf   r   r   r   	<genexpr>}  r?   z0calculate_macosx_platform_tag.<locals>.<genexpr>c                 s   s   | ]}t |V  qd S r.   rk   rf   r   r   r   rm   ~  r?   z.dylibz.sor<   c                 S   s   g | ]}t |qS r   rk   rf   r   r   r   r>     r?   c                    s   g | ]\}}| kr|qS r   r   )r;   kvZstart_versionr   r   r>     r?   
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tuplerJ   osenvironr(   stderrwriteformatjoinwalkendswithpathrO   rM   valuesitems)archive_rootZplatform_tagprefixbase_versionsuffixZdeploy_targetZversions_dictdirpathdirnames	filenamesfilenameZlib_pathZmin_verZfin_base_versionZproblematic_filesZ
files_formZerror_messager   rp   r   calculate_macosx_platform_tagg  s    




r   )N)N)$__doc__r   rt   r(   rC   r$   rD   r%   rE   r&   rF   r'   rZ   r\   rI   r   c_intrS   rU   r4   r8   Zc_uint64r:   rP   Zc_charZsegment_command_fieldsZsegment_command_fields_64rV   rX   r   r-   r/   rO   rH   r[   r   r   r   r   r   <module>   sv   (	
6
/