U
    @vgY                     @   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-gw11q0wt/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| rt| 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 }|sdS g }| D ]b\}}t|}t|dkrd	|d
 }	n$dd|dd d |d  }	|d|	| qd}
t|dkr0||
d n||
d tdd tj	dd
tjD }|rxd}|| d|S )zDetermine 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 }|rt
 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	}|rd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})t12|(}*t3|* |d*}+t14|)},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|rt	| t
|| t|}ttdrt||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.exez
-script.pyiz.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   zInvalid 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$  
  =   