U
    «ÃT_äY  ã                   @   sö  d Z ddlm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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mZ dd	lmZ dd
lmZ ddlmZ ddlmZ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%�rbddl*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9 e1e:df Z;e <e=¡Z>dd„ Z?d3dd„Z@dd„ ZAdd„ ZBdd„ ZCd d!„ ZDd"d#„ ZEd$d%„ ZFd&d'„ ZGG d(d)„ d)eHƒZId*d+„ ZJG d,d-„ d-eƒZKd4d/d0„ZLd5d1d2„ZMdS )6zGSupport for installing and building the "wheel" binary package format.
é    )Úabsolute_importN)Úurlsafe_b64encode)ÚZipFile)Úpkg_resources)ÚScriptMaker)Úget_export_entry)ÚStringIO)ÚInstallationError)Úget_major_minor_version)Úcaptured_stdoutÚ
ensure_dirÚ	hash_file)ÚTempDirectory)ÚMYPY_CHECK_RUNNING)Úunpack_file)Úparse_wheel)ÚMessage)ÚDictÚListÚOptionalÚSequenceÚTupleÚIOÚTextÚAnyÚIterableÚCallableÚSet)ÚScheme.c                 C   s   t j | |¡ t jjd¡S )Nú/)ÚosÚpathÚrelpathÚreplaceÚsep)ÚsrcÚp© r'   úJ/tmp/pip-unpacked-wheel-8u7dkzi1/pip/_internal/operations/install/wheel.pyÚnormpath2   s    r)   é   c                 C   s6   t | |ƒ\}}dt| ¡ ƒ d¡ d¡ }|t|ƒfS )z?Return (encoded_digest, length) for path using hashlib.sha256()zsha256=Úlatin1ú=)r   r   ÚdigestÚdecodeÚrstripÚstr)r!   Ú	blocksizeÚhÚlengthr-   r'   r'   r(   Úrehash7   s    ÿþþr4   c                 C   s6   t jd dk ri }d}nddi}d}t| || f|ŽS )Nr   é   ÚbÚnewlineÚ )ÚsysÚversion_infoÚopen)ÚnameÚmodeÚnlÚbinr'   r'   r(   Úopen_for_csvB   s    r@   c              	   C   s    t j | ¡rœt| dƒ�R}| ¡ }| d¡s8W 5 Q R £ dS tj t 	¡ ¡}d| t j
 d¡ }| ¡ }W 5 Q R X t| dƒ�}| |¡ | |¡ W 5 Q R X dS dS )	zQReplace #!python with #!/path/to/python
    Return True if file was changed.
    Úrbs   #!pythonFs   #!ÚasciiÚwbTN)r    r!   Úisfiler;   ÚreadlineÚ
startswithr9   Ú
executableÚencodeÚgetfilesystemencodingÚlinesepÚreadÚwrite)r!   ÚscriptÚ	firstlineÚexenameÚrestr'   r'   r(   Ú
fix_scriptM   s    

rQ   c                 C   s   |   dd¡ ¡ dkS )NzRoot-Is-Purelibr8   Útrue)ÚgetÚlower)Úmetadatar'   r'   r(   Úwheel_root_is_purelibb   s    rV   c              	      sÂ   t j | ¡si i fS t| ƒ�8}tƒ }|D ]}| | ¡ ¡ | d¡ q(| d¡ W 5 Q R X tj	 
|¡}| di ¡}| di ¡}dd„ ‰ t‡ fdd„| ¡ D ƒƒ}t‡ fd	d„| ¡ D ƒƒ}||fS )
NÚ
r   Úconsole_scriptsÚgui_scriptsc                 S   s&   t | ƒ dd¡ d¡}|d |d fS )z[get the string representation of EntryPoint,
        remove space and split on '='
        ú r8   r,   r   é   )r0   r#   Úsplit)ÚsÚsplit_partsr'   r'   r(   Ú	_split_ep|   s    z"get_entrypoints.<locals>._split_epc                 3   s   | ]}ˆ |ƒV  qd S ©Nr'   ©Ú.0Úv©r_   r'   r(   Ú	<genexpr>…   s     z"get_entrypoints.<locals>.<genexpr>c                 3   s   | ]}ˆ |ƒV  qd S r`   r'   ra   rd   r'   r(   re   †   s     )r    r!   Úexistsr;   r   rL   ÚstripÚseekr   Ú
EntryPointÚ	parse_maprS   ÚdictÚvalues)ÚfilenameÚfpÚdataÚlineÚentry_pointsÚconsoleÚguir'   rd   r(   Úget_entrypointsg   s    
	rt   c                    s‚  | sdS t  t¡}| D ]*}tj |¡}tj |¡}||  |¡ qdd„ tj 	dd¡ 
tj¡D ƒ‰ ˆ  tj tj tj¡¡¡ ‡ fdd„| ¡ D ƒ}|sœdS g }| ¡ D ]b\}}t|ƒ}t|ƒdkrÔd	 |d
 ¡}	n$d d |dd… ¡d |d  ¡}	| d |	|¡¡ q¨d}
t|ƒdk�r0| |
 d¡¡ n| |
 d¡¡ tdd„ tj 	dd¡ 
tj¡D ƒƒ}|�rxd}| |¡ d |¡S )z�Determine if any scripts are not on PATH and format a warning.
    Returns a warning message if one or more scripts are not on PATH,
    otherwise None.
    Nc                 S   s    g | ]}t j |¡ t j¡‘qS r'   )r    r!   Únormcaser/   r$   ©rb   Úir'   r'   r(   Ú
<listcomp>›   s    z5message_about_scripts_not_on_PATH.<locals>.<listcomp>ÚPATHr8   c                    s&   i | ]\}}t j |¡ˆ kr||“qS r'   )r    r!   ru   )rb   Ú
parent_dirÚscripts©Únot_warn_dirsr'   r(   Ú
<dictcomp>¢   s   ÿ z5message_about_scripts_not_on_PATH.<locals>.<dictcomp>r[   zscript {} isr   zscripts {} arez, éÿÿÿÿz and z.The {} installed in '{}' which is not on PATH.zeConsider adding {} to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.zthis directoryzthese directoriesc                 s   s   | ]}|r|d  dkV  qdS )r   ú~Nr'   rv   r'   r'   r(   re   Ã   s     z4message_about_scripts_not_on_PATH.<locals>.<genexpr>ziNOTE: The current PATH contains path(s) starting with `~`, which may not be expanded by all applications.rW   )ÚcollectionsÚdefaultdictÚsetr    r!   ÚdirnameÚbasenameÚaddÚenvironrS   r\   ÚpathsepÚappendru   r9   rG   ÚitemsÚsortedÚlenÚformatÚjoinÚany)r{   Úgrouped_by_dirÚdestfilerz   Úscript_nameÚwarn_forÚ	msg_linesÚdir_scriptsÚsorted_scriptsÚ
start_textÚlast_line_fmtÚwarn_for_tildeÚtilde_warning_msgr'   r|   r(   Ú!message_about_scripts_not_on_PATHŠ   sV    
þ
ÿÿ ÿÿÿÿÿ
r›   c                 C   s   t | dd„ d�S )a6  Return the given rows of a RECORD file in sorted order.

    Each row is a 3-tuple (path, hash, size) and corresponds to a record of
    a RECORD file (see PEP 376 and PEP 427 for details).  For the rows
    passed to this function, the size can be an integer as an int or string,
    or the empty string.
    c                 S   s   t dd„ | D ƒƒS )Nc                 s   s   | ]}t |ƒV  qd S r`   )r0   )rb   Úxr'   r'   r(   re   â   s     z3sorted_outrows.<locals>.<lambda>.<locals>.<genexpr>)Útuple)Úrowr'   r'   r(   Ú<lambda>â   ó    z sorted_outrows.<locals>.<lambda>)Úkey)r‹   )Úoutrowsr'   r'   r(   Úsorted_outrowsÑ   s    r£   c                 C   sÒ   g }| D ]v}t |ƒdkr(t d |¡¡ t|ƒ}|d }| ||¡}||d< ||krpt|ƒ\}	}
|	|d< |
|d< | t|ƒ¡ q|D ]*}t|ƒ\}	}
| t	||ƒ|	t
|
ƒf¡ q„|D ]}| || ddf¡ q´|S )z_
    :param installed: A map from archive RECORD path to installation RECORD
        path.
    r5   z,RECORD line has more than three elements: {}r   r[   é   r8   )rŒ   ÚloggerÚwarningr�   ÚlistÚpopr4   r‰   r�   r)   r0   )Úold_csv_rowsÚ	installedÚchangedÚ	generatedÚlib_dirÚinstalled_rowsrž   Úold_pathÚnew_pathr-   r3   Úfr'   r'   r(   Úget_csv_rows_for_installedå   s*    ÿr²   c                   @   s   e Zd ZdS )ÚMissingCallableSuffixN)Ú__name__Ú
__module__Ú__qualname__r'   r'   r'   r(   r³   	  s   r³   c                 C   s*   t | ƒ}|d k	r&|jd kr&tt|ƒƒ‚d S r`   )r   Úsuffixr³   r0   )ÚspecificationÚentryr'   r'   r(   Ú_raise_for_invalid_entrypoint  s    rº   c                       s   e Zd Zd‡ fdd„	Z‡  ZS )ÚPipScriptMakerNc                    s   t |ƒ tt| ƒ ||¡S r`   )rº   Úsuperr»   Úmake)Úselfr¸   Úoptions©Ú	__class__r'   r(   r½     s    zPipScriptMaker.make)N)r´   rµ   r¶   r½   Ú__classcell__r'   r'   rÀ   r(   r»     s   r»   Tc           .   
      s@  ˆ  tjj¡tjj }t|| ƒ\}}	t|	ƒr4|j‰n|j‰t |¡}
dd„ |
D ƒ}i ‰t	ƒ ‰ g }|r²t
ƒ �4}t ¡ �  t d¡ tj|ddd� W 5 Q R X W 5 Q R X t | ¡ ¡ d+‡ ‡‡‡fdd„	‰d,‡fd
d„	}||ˆdƒ tj ˆ|¡}tj |d¡}t|ƒ\‰‰‡‡fdd„}|D ]l}d	}d	}t tj ˆ|¡¡D ]H}d	}|dk�rTt}|}tj ˆ||¡}t||ƒ}|||d||d� �q:�qtd	|jƒ}d|_dh|_d|_g }ˆ dd	¡}|�r8dtjk�rØ| d| ¡ tj dd¡dk�r| dt j!d |f ¡ | dt"ƒ |f ¡ dd„ ˆD ƒ}|D ]}ˆ|= �q*ˆ dd	¡}|�r˜dtjk�rd| d| ¡ | dt"ƒ |f ¡ dd„ ˆD ƒ}|D ]}ˆ|= �qŠ| #dd„ ˆ $¡ D ƒ¡ dd„ ˆ $¡ D ƒ}g }z.| %|¡}| #|¡ | #| %|d di¡¡ W n> t&k
�r2 }  z| j'd }!t(d! )|!|¡ƒ‚W 5 d	} ~ X Y nX |�rVt*|ƒ}"|"d	k	�rVt +|"¡ tj |d"¡}#tj |d#¡}$t,|$d$ƒ�}%|% -d%¡ W 5 Q R X t. /|$|#¡ | |#¡ tj |d&¡}&tj |d'¡}'t0|&d(ƒ�\}(t0|'d)ƒ�F})t1 2|(¡}*t3|*ˆˆ |ˆd*�}+t1 4|)¡},t5|+ƒD ]}-|, 6|-¡ �q
W 5 Q R X W 5 Q R X t. /|'|&¡ d	S )-aÇ  Install a wheel.

    :param name: Name of the project to install
    :param wheeldir: Base directory of the unpacked wheel
    :param wheel_zip: open ZipFile for wheel being installed
    :param scheme: Distutils scheme dictating the install directories
    :param req_description: String used in place of the requirement, for
        logging
    :param pycompile: Whether to byte-compile installed Python files
    :param warn_script_location: Whether to check that scripts are installed
        into a directory on PATH
    :raises UnsupportedWheel:
        * when the directory holds an unpacked wheel with incompatible
          Wheel-Version
        * when the .dist-info dir does not match the wheel
    c                 S   s   g | ]}|  d ¡r|‘qS ©z.data©Úendswith©rb   r]   r'   r'   r(   rx   C  s     
 z*install_unpacked_wheel.<locals>.<listcomp>ÚignoreT)ÚforceÚquietFc                    s.   t | ˆƒ}t |ˆƒ}|ˆ|< |r*ˆ  |¡ dS )z6Map archive RECORD paths to installation RECORD paths.N)r)   r†   )Úsrcfiler‘   ÚmodifiedÚoldpathÚnewpath)r«   rª   r­   Úwheeldirr'   r(   Úrecord_installedU  s
    

z0install_unpacked_wheel.<locals>.record_installedNc                    sR  t |ƒ t | ¡D �]8\}}}|t| ƒd …  tjj¡}tj ||¡}	|rh|dkrhdd„ |D ƒ|d d …< |D ]Þ}
|r~||
ƒr~qltj ||
¡}tj |||
¡}t |	ƒ tj |¡rºt 	|¡ t
 ||¡ t |¡}ttdƒrît ||j|jf¡ t |tj¡�r,t |¡}|jtjB tjB tjB }t ||¡ d}|�r>||ƒ}ˆ |||ƒ qlqd S )Nr8   c                 S   s   g | ]}|  d ¡s|‘qS rÃ   rÄ   rÆ   r'   r'   r(   rx   l  s     
 z;install_unpacked_wheel.<locals>.clobber.<locals>.<listcomp>ÚutimeF)r   r    ÚwalkrŒ   Úlstripr!   r$   rŽ   rf   ÚunlinkÚshutilÚcopyfileÚstatÚhasattrrÐ   Úst_atimeÚst_mtimeÚaccessÚX_OKÚst_modeÚS_IXUSRÚS_IXGRPÚS_IXOTHÚchmod)ÚsourceÚdestÚis_baseÚfixerÚfilterÚdirÚsubdirsÚfilesÚbasedirÚdestdirr±   rÊ   r‘   ÚstÚpermissionsr«   )rÏ   r'   r(   Úclobber^  s6    




ÿz'install_unpacked_wheel.<locals>.clobberzentry_points.txtc                    sh   |   ¡  d¡r| d d… }n<|   ¡  d¡r8| d d… }n |   ¡  d¡rT| d d… }n| }|ˆ kpf|ˆkS )Nz.exeéüÿÿÿz
-script.pyiöÿÿÿz.pya)rT   rÅ   )r<   Ú	matchname)rr   rs   r'   r(   Úis_entrypoint_wrapper¦  s    z5install_unpacked_wheel.<locals>.is_entrypoint_wrapperr{   )rä   rå   r8   ÚpipÚENSUREPIP_OPTIONSzpip = Ú
altinstallz
pip%s = %sr   c                 S   s   g | ]}t  d |¡r|‘qS )zpip(\d(\.\d)?)?$©ÚreÚmatch©rb   Úkr'   r'   r(   rx     s      Úeasy_installzeasy_install = zeasy_install-%s = %sc                 S   s   g | ]}t  d |¡r|‘qS )zeasy_install(-\d\.\d)?$rô   r÷   r'   r'   r(   rx     s     c                 s   s   | ]}d | V  qdS )ú%s = %sNr'   ©rb   Úkvr'   r'   r(   re     s    z)install_unpacked_wheel.<locals>.<genexpr>c                 S   s   g | ]}d | ‘qS )rú   r'   rû   r'   r'   r(   rx     s    rs   z®Invalid script entry point: {} for req: {} - A callable suffix is required. Cf https://packaging.python.org/specifications/entry-points/#use-for-scripts for more information.Ú	INSTALLERzINSTALLER.piprC   s   pip
ÚRECORDz
RECORD.pipÚrzw+)rª   r«   r¬   r­   )F)NN)7r/   r    r!   r$   r   rV   ÚpurelibÚplatlibÚlistdirrƒ   r   ÚwarningsÚcatch_warningsÚfilterwarningsÚ
compileallÚcompile_dirr¥   ÚdebugÚgetvaluerŽ   rt   rQ   Úgetattrr»   r{   rí   ÚvariantsÚset_moder¨   r‡   r‰   rS   r9   r:   r
   ÚextendrŠ   Úmake_multipler³   Úargsr	   r�   r›   r¦   r;   rL   rÔ   Úmover@   ÚcsvÚreaderr²   Úwriterr£   Úwriterow).r<   rÎ   Ú	wheel_zipÚschemeÚreq_descriptionÚ	pycompileÚwarn_script_locationrá   Úinfo_dirrU   rç   Ú	data_dirsr¬   Ústdoutrí   Údest_info_dirÚep_filerð   Údatadirrä   rå   Úsubdirrâ   ÚmakerÚscripts_to_generateÚ
pip_scriptÚpip_eprø   Úeasy_install_scriptÚeasy_install_epÚgui_scripts_to_generateÚgenerated_console_scriptsÚer¹   ÚmsgÚ	installerÚtemp_installerÚinstaller_fileÚrecordÚtemp_recordÚ	record_inÚ
record_outr  r¢   r  rž   r'   )r«   rr   rs   rª   r­   rÏ   rÎ   r(   Úinstall_unpacked_wheel  sî    


$  û@

#ÿÿ
ÿ ÿÿÿ

ÿÿ

ÿ
 ýÿ



   þ
"r2  c           	      C   sX   t |dd��B}t|dd��*}t||jƒ t| |j|||||d� W 5 Q R X W 5 Q R X d S )Nzunpacked-wheel)r!   ÚkindT)Ú
allowZip64)r<   rÎ   r  r  r  r  r  )r   r   r   r!   r2  )	r<   Ú
wheel_pathr  r  r  r  Ú_temp_dir_for_testingÚunpacked_dirÚzr'   r'   r(   Úinstall_wheelQ  s$    
 ÿ 
þùr9  )r*   )TT)TTN)NÚ__doc__Ú
__future__r   r�   r  r  ÚloggingÚos.pathr    rõ   rÔ   rÖ   r9   r  Úbase64r   Úzipfiler   Úpip._vendorr   Úpip._vendor.distlib.scriptsr   Zpip._vendor.distlib.utilr   Zpip._vendor.sixr   Úpip._internal.exceptionsr	   Úpip._internal.locationsr
   Úpip._internal.utils.miscr   r   r   Úpip._internal.utils.temp_dirr   Úpip._internal.utils.typingr   Úpip._internal.utils.unpackingr   Úpip._internal.utils.wheelr   Úemail.messager   Útypingr   r   r   r   r   r   r   r   r   r   r   Úpip._internal.models.schemer   r0   ZInstalledCSVRowÚ	getLoggerr´   r¥   r)   r4   r@   rQ   rV   rt   r›   r£   r²   Ú	Exceptionr³   rº   r»   r2  r9  r'   r'   r'   r(   Ú<module>   sf   4

#G$  ù
  =   ù