o
    5h%                     @   s  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ZddlZddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlmZmZ d	d
lmZ d	dlmZ d	dlmZ ddlmZ e dej!j"Z#dZ$ej%dd Z&dddZ'ej(dd Z)G dd dZ*dS )zWheels support.    N)Requirementsys_tags)canonicalize_name)Version)_unpack_zipfile_obj)_egg_basenamewrite_requirements   )extras_from_deps)metadata)_read_utf8_with_fallback)get_platformz^(?P<project_name>.+?)-(?P<version>\d.*?)
    ((-(?P<build>\d.*?))?-(?P<py_version>.+?)-(?P<abi>.+?)-(?P<platform>.+?)
    )\.whl$z8__import__('pkg_resources').declare_namespace(__name__)
c                   C   s   dd t  D S )Nc                 S   s   h | ]
}|j |j|jfqS  )Zinterpreterabiplatform.0tr   r   0lib/python3.10/site-packages/setuptools/wheel.py	<setcomp>*   s    z&_get_supported_tags.<locals>.<setcomp>r   r   r   r   r   _get_supported_tags%   s   r   returnc                 C   s   t | D ]Q\}}}t j|| }|D ]}t j||}t j|||}t || qttt|D ]"\}	}
t j||
}t j|||
}t j	|sUt || ||	= q3qt j| ddD ]\}}}|rgJ t 
| q^dS )zDMove everything under `src_dir` to `dst_dir`, and delete the former.T)topdownN)oswalkpathrelpathjoinrenamesreversedlist	enumerateexistsrmdir)Zsrc_dirZdst_dirdirpathdirnames	filenamessubdirfsrcdstndr   r   r   unpack-   s$   	r.   c               	   c   s>    ddl m}  | | j}zdV  W | | dS | | w )z*
    Temporarily disable info traces.
    r   )logN)	distutilsr/   Zset_thresholdZWARN)r/   Zsavedr   r   r   disable_info_tracesC   s   r1   c                   @   sx   e Zd ZdddZdd Zdd Zd	d
 Zdd ZdddZdd Z	e
dd Ze
dd Ze
dd Ze
dd ZdS )Wheelr   Nc                 C   sR   t tj|}|d u rtd||| _|  D ]
\}}t| || qd S )Nzinvalid wheel name: )	
WHEEL_NAMEr   r   basename
ValueErrorfilename	groupdictitemssetattr)selfr6   matchkvr   r   r   __init__R   s   zWheel.__init__c                 C   s&   t | jd| jd| jdS )z>List tags (py_version, abi, platform) supported by this wheel..)	itertoolsproductZ
py_versionsplitr   r   r:   r   r   r   tagsZ   s
   


z
Wheel.tagsc                 C   s   t dd |  D dS )z2Is the wheel compatible with the current platform?c                 s   s    | ]
}|t  v rd V  qdS )TN)r   r   r   r   r   	<genexpr>d   s    z&Wheel.is_compatible.<locals>.<genexpr>F)nextrD   rC   r   r   r   is_compatibleb   s   zWheel.is_compatiblec                 C   s&   t | j| j| jdkrd nt dd S )Nany)r   z.egg)r   project_nameversionr   r   rC   r   r   r   egg_namef   s   zWheel.egg_namec                 C   sF   |  D ]}t|}|drt|t| jr|  S qtd)Nz
.dist-infoz.unsupported wheel format. .dist-info not found)Znamelist	posixpathdirnameendswithr   
startswithrI   r5   )r:   zfmemberrM   r   r   r   get_dist_infop   s   
zWheel.get_dist_infoc                 C   s>   t | j}| || W d   dS 1 sw   Y  dS )z"Install wheel as an egg directory.N)zipfileZipFiler6   _install_as_egg)r:   destination_eggdirrP   r   r   r   install_as_eggz   s   "zWheel.install_as_eggc                 C   s`   | j  d| j }| |}| d}tj|d}| |||| | || | || d S )N-z.dataEGG-INFO)	rI   rJ   rR   r   r   r   _convert_metadata_move_data_entries_fix_namespace_packages)r:   rV   rP   Zdist_basename	dist_info	dist_dataegg_infor   r   r   rU      s   

zWheel._install_as_eggc                    s   fdd}|d}t |d}t d|  kot dk n  }|s+td| t| tj|  t| \}}	t	 | t	tj|dtj|d	 t
jt||	d
d}
t  t|
dd tj|d W d    d S 1 s|w   Y  d S )Nc                    sT    t | }| d}tj |W  d    S 1 s#w   Y  d S )Nutf-8)	openrL   r   readdecodeemailparserZParserZparsestr)namefpvaluer]   rP   r   r   get_metadata   s   $z-Wheel._convert_metadata.<locals>.get_metadataZWHEELzWheel-Versionz1.0z2.0dev0z"unsupported wheel format version: ZMETADATAzPKG-INFO)install_requiresextras_require)Zattrsr_   zrequires.txt)parse_versiongetr5   r   r   r   r   r2   _convert_requiresrename
setuptoolsDistributiondictr1   r	   Zget_command_obj)rP   rV   r]   r_   rj   Zwheel_metadataZwheel_versionZwheel_v1rk   rl   Z
setup_distr   ri   r   rZ      s:    
"zWheel._convert_metadatac              	      s   t j|j }|dpg }ttt|t|}dd dd  dd ttt t	
} fdd	|D }||fS )
NzRequires-Distc                 S   s   t t| } d | _t| S N)r   strmarker)reqr   r   r   raw_req   s   z(Wheel._convert_requires.<locals>.raw_reqc                 [   s   | j  p	| j |S rt   )rv   Zevaluate)rw   envr   r   r   eval   s   z%Wheel._convert_requires.<locals>.evalc                 S   s6   z| j j}W n ty   d}Y nw tdd |D S )Nr   c                 s   s2    | ]}t |tr|d  jdkr|d jV  qdS )r   extra   N)
isinstancetuplerh   )r   rv   r   r   r   rE      s    z=Wheel._convert_requires.<locals>.for_extra.<locals>.<genexpr>)rv   Z_markersAttributeErrorset)rw   Zmarkersr   r   r   	for_extra   s   z*Wheel._convert_requires.<locals>.for_extrac              	      s.   i | ]  t t fd dD qS )c                 3   s(    | ]}|r |d r|V  qdS )r{   Nr   )r   rw   )rz   r{   r   r   r   rE      s   & z5Wheel._convert_requires.<locals>.<dictcomp>.<genexpr>)r!   map)r   rz   r   rx   Zreqsr   r   
<dictcomp>   s    z+Wheel._convert_requires.<locals>.<dictcomp>)r   rr   ZatZget_allr!   r   r   r   filterr@   filterfalse)rV   r]   ZmdZdepsextrasrk   rl   r   r   r   ro      s   	zWheel._convert_requiresc                    s   t j|   t j d}t j|rNt j| dd}t | t |D ]"}|dr8t t j|| q&t t j||t j|| q&t 	| t
t jj fdddD D ]}t||  q\t j rqt 	  dS dS )z,Move data entries to their correct location.ZscriptsrY   z.pycc                 3   s    | ]
}t j |V  qd S rt   )r   r   r   )r   r-   r^   r   r   rE      s
    
z+Wheel._move_data_entries.<locals>.<genexpr>)dataZheadersZpurelibZplatlibN)r   r   r   r#   mkdirlistdirrN   unlinkrp   r$   r   r.   )rV   r^   Zdist_data_scriptsZegg_info_scriptsentryr(   r   r   r   r[      s.   



zWheel._move_data_entriesc              	   C   s   t j| d}t j|rYt| }|D ]E}t jj|g|dR  }t j|d}t j|s6t | t j|sXt|ddd}|t	 W d    n1 sSw   Y  qd S d S )Nznamespace_packages.txtr?   z__init__.pywr`   )encoding)
r   r   r   r#   r   rB   r   ra   writeNAMESPACE_PACKAGE_INIT)r_   rV   Znamespace_packagesmodZmod_dirZmod_initrg   r   r   r   r\      s   
zWheel._fix_namespace_packagesr   N)__name__
__module____qualname__r>   rD   rG   rK   rR   rW   rU   staticmethodrZ   ro   r[   r\   r   r   r   r   r2   Q   s     





$
+
r2   r   )+__doc__
contextlibrd   	functoolsr@   r   rL   rerS   Zpackaging.requirementsr   Zpackaging.tagsr   Zpackaging.utilsr   Zpackaging.versionr   rm   rq   Zsetuptools.archive_utilr   Zsetuptools.command.egg_infor   r	   Z
_discoveryr   Z
_importlibr   Zunicode_utilsr   Zdistutils.utilr   compileVERBOSEr;   r3   r   cacher   r.   contextmanagerr1   r2   r   r   r   r   <module>   sB    


