B
    `dM]                 @   s6  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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  ddl!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z(m)Z)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3 e.rddl4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZB ddl$mCZC e;eDdf ZEeFeGZHdd ZId7ddZJdd ZKd d! ZLd"d# ZMd$d% ZNd&d' ZOd(d) ZPd*d+ ZQG d,d- d-eRZSd.d/ ZTG d0d1 d1eZUd8d3d4ZVd9d5d6ZWdS ):zGSupport for installing and building the "wheel" binary package format.
    )absolute_importN)urlsafe_b64encode)starmap)ZipFile)pkg_resources)ScriptMaker)get_export_entry)StringIO)InstallationError)get_major_minor_version)DIRECT_URL_METADATA_NAME	DirectUrl)adjacent_tmp_filereplace)captured_stdout
ensure_dir	hash_file)TempDirectory)MYPY_CHECK_RUNNING)current_umaskunpack_file)parse_wheel)Message)
DictListOptionalSequenceTupleAnyIterableIteratorCallableSet)Scheme)NamedTemporaryFileResult.c             C   s   t j| |t jjdS )N/)ospathrelpathr   sep)srcp r,   J/tmp/pip-unpacked-wheel-hu9qqap1/pip/_internal/operations/install/wheel.pynormpath7   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lengthr2   r,   r,   r-   rehash<   s
    
r9   c             C   s(   t jjdk rdd| iS | ddS dS )zPReturn keyword arguments to properly open a CSV file
    in the given mode.
       modez{}b )r;   newlineN)sysversion_infomajorformat)r;   r,   r,   r-   csv_io_kwargsG   s    rB   c          	   C   s   t j| rt| dH}| }|ds.dS tjt	 }d| t j
d }| }W dQ R X t| d}|| || W dQ R X dS dS )	zQReplace #!python with #!/path/to/python
    Return True if file was changed.
    rbs   #!pythonFs   #!asciiNwbT)r&   r'   isfileopenreadline
startswithr>   
executableencodegetfilesystemencodinglinesepreadwrite)r'   script	firstlineexenamerestr,   r,   r-   
fix_scriptR   s    

rT   c             C   s   |  dd dkS )NzRoot-Is-Purelibr<   true)getlower)metadatar,   r,   r-   wheel_root_is_purelibg   s    rY   c          	      s   t j| si i fS t| <}t }x$|D ]}||  |d q*W |d W d 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 '='
         r<   r1   r      )r5   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)rb   r,   r-   	<genexpr>   s    z"get_entrypoints.<locals>.<genexpr>c             3   s   | ]} |V  qd S )Nr,   )rc   rd   )rb   r,   r-   re      s    )r&   r'   existsrG   r	   rO   stripseekr   
EntryPoint	parse_maprV   dictvalues)filenamefpdatalineentry_pointsconsoleguir,   )rb   r-   get_entrypointsl   s    

	rt   c                s  | sdS t t}x2| D ]*}tj|}tj|}|| | qW dd tj	dd
tjD   tjtjtj  fdd| D }|sdS g }xn| D ]b\}}t|}t|dkrd	|d
 }	n$dd|dd d |d  }	|d|	| qW d}
t|dkr8||
d n||
d tdd tj	dd
tjD }|rd}|| 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'   normcaser4   r)   )rc   ir,   r,   r-   
<listcomp>   s    z5message_about_scripts_not_on_PATH.<locals>.<listcomp>PATHr<   c                s&   i | ]\}}t j| kr||qS r,   )r&   r'   ru   )rc   
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,   )rc   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.rZ   )collectionsdefaultdictsetr&   r'   dirnamebasenameaddenvironrV   r_   pathsepappendru   r>   rJ   itemssortedlenrA   joinany)rz   grouped_by_dirdestfilery   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   sD    


 "
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 )N)r5   )rc   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 }x~| D ]v}t |dkr*td| t|}|d }|||}||d< ||krrt|\}	}
|	|d< |
|d< |t| q
W x2|D ]*}t|\}	}
|t	|||	t
|
f qW x |D ]}||| ddf qW |S )z_
    :param installed: A map from archive RECORD path to installation RECORD
        path.
    r:   z,RECORD line has more than three elements: {}r   r^      r<   )r   loggerwarningrA   listpopr9   r   r   r.   r5   )old_csv_rows	installedchanged	generatedlib_dirinstalled_rowsr   old_pathnew_pathr2   r8   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 )N)r   suffixr   r5   )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 )N)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 dQ R X W dQ R X t|  d* fdd		d+fd
d	}||d tj|	}tj|d}t|\fdd}xx|D ]p}d}d}x`ttj|D ]H}d}|dkrXt}|}tj||}t||}|||d||d q>W qW td|j}d|_dh|_d|_g }dd}|rDdtjkr|d|  tjdddkr|d t!j"d | |d t# | dd D }x|D ]}|= q4W dd}|rdtjkrp|d|  |d t# | dd D }x|D ]}|= qW |$t%dj &  t't%dj & }g } y.|(|} |$|  |$|(|ddi W n> t)k
rB }! z|!j*d }"t+d |"|W dd}!~!X Y nX |rft,| }#|#dk	rft-|# d t.  @ t/j0fd!d"}$tj|d#}%|$|%}&|&1d$ W dQ R X ||% |dk	rtj|t2}'|$|'}(|(1|3 4d% W dQ R X ||' tj|d&})t5|)ft6d'}*t7t89|* |d(}+W dQ R X |$|)ft6d)}*t8:|*},|,;t<|+ W dQ R X 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)rc   r`   r,   r,   r-   rw   I  s    z*install_unpacked_wheel.<locals>.<listcomp>ignoreT)forcequietNFc                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_installed[  s
    

z0install_unpacked_wheel.<locals>.record_installedc                s\  t | xLt| D ]<\}}}|t| d  tjj}tj||}	|rl|dkrldd |D |d d < x|D ]}
|r||
rqrtj||
}tj|||
}t |	 tj|rt	| t
|| t|}ttdrt||j|jf t|tjr2t|}|jtjB tjB tjB }t|| d}|rD||} ||| qrW qW d S )Nr<   c             S   s   g | ]}| d s|qS )z.data)r   )rc   r`   r,   r,   r-   rw   r  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-   clobberd  s4    





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)rW   r   )name	matchname)rr   rs   r,   r-   is_entrypoint_wrapper  s    z5install_unpacked_wheel.<locals>.is_entrypoint_wrapperrz   )r   r   r<   pipENSUREPIP_OPTIONSzpip = 
altinstallz
pip{} = {}r   c             S   s   g | ]}t d |r|qS )zpip(\d(\.\d)?)?$)rematch)rc   kr,   r,   r-   rw     s    easy_installzeasy_install = zeasy_install-{} = {}c             S   s   g | ]}t d |r|qS )zeasy_install(-\d\.\d)?$)r   r   )rc   r   r,   r,   r-   rw     s    z{} = {}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.i  c          	   ;   s<   t | f|}|V  W d Q R X t|j  t|j|  d S )N)r   r&   r   r   r   )r'   kwargsr   )generated_file_moder,   r-   _generate_file<  s    z.install_unpacked_wheel.<locals>._generate_file	INSTALLERs   pip
zutf-8RECORDr)r   r   r   r   w)F)NN)=r4   r&   r'   r)   r   rY   purelibplatliblistdirr   r   warningscatch_warningsfilterwarnings
compileallcompile_dirr   debuggetvaluer   rt   rT   getattrr   rz   r   variantsset_moder   r   r   rV   rA   r>   r?   r   extendr   r   r   make_multipler   argsr
   r   r   r   
contextlibcontextmanagerrO   r   to_jsonrK   rG   rB   r   csvreaderwriter	writerowsr   )-r   r   	wheel_zipschemereq_description	pycompilewarn_script_location
direct_urlr   info_dirrX   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   msgr   installer_pathinstaller_filedirect_url_pathdirect_url_filerecord_pathrecord_filerowsr  r,   )r   rr   r   rs   r   r   r   r   r-   install_unpacked_wheel   s    


$ ;


#







	





r.  c       
      C   sZ   t |ddD}t|dd,}	t||j t| |j|	|||||d W d Q R X W d Q R X d S )Nzunpacked-wheel)r'   kindT)
allowZip64)r   r   r  r  r  r  r  r  )r   r   r   r'   r.  )
r   
wheel_pathr  r  r  r  _temp_dir_for_testingr  unpacked_dirzr,   r,   r-   install_wheel_  s    
r5  )r/   )TTN)TTNN)X__doc__
__future__r   r   r   r	  r  loggingos.pathr&   r   r   r   r>   r   base64r   	itertoolsr   zipfiler   pip._vendorr   pip._vendor.distlib.scriptsr   Zpip._vendor.distlib.utilr   Zpip._vendor.sixr	   pip._internal.exceptionsr
   pip._internal.locationsr   pip._internal.models.direct_urlr   r   pip._internal.utils.filesystemr   r   pip._internal.utils.miscr   r   r   pip._internal.utils.temp_dirr   pip._internal.utils.typingr   pip._internal.utils.unpackingr   r   pip._internal.utils.wheelr   email.messager   typingr   r   r   r   r   r   r   r    r!   r"   pip._internal.models.schemer#   r$   r5   ZInstalledCSVRow	getLoggerr   r   r.   r9   rB   rT   rY   rt   r   r   r   	Exceptionr   r   r   r.  r5  r,   r,   r,   r-   <module>   sp   0

#G$  
  >   