U
    
Has                     @   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
Z
ddlZddl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mZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$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/m0Z0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9 ddl:m;Z;m<Z< ddl=m>Z>m?Z? ddl@mAZAmBZB ddlCmDZDmEZEmFZFmGZG ddlHmIZImJZJmKZKmLZL ddlMmNZN erddlmOZO G dd deOZPeQeRZSedeTZUe#eUeTe$eVeTf f ZWdEddZXdd ZYdd ZZd d! Z[e8e#eeTeTf eeTeTf f d"d#d$Z\d%d& Z]d'd( Z^d)d* Z_dFd+d,Z`d-d. Zad/d0 Zbd1d2 ZcG d3d4 d4ZdG d5d6 d6ZeG d7d8 d8e4Zfd9d: ZgG d;d< d<e*ZhdGd?d@ZiejjdAdB ZkdHdCdDZldS )IzGSupport for installing and building the "wheel" binary package format.
    N)urlsafe_b64encode)Message)chainfilterfalsestarmap)IOTYPE_CHECKINGAnyBinaryIOCallableDictIterableIteratorListNewTypeOptionalSequenceSetTupleUnioncast)ZipFileZipInfo)ScriptMaker)get_export_entry)canonicalize_name)
ensure_strensure_textreraise)InstallationError)get_major_minor_version)BaseDistributionget_wheel_distribution)DIRECT_URL_METADATA_NAME	DirectUrl)SCHEME_KEYSScheme)adjacent_tmp_filereplace)captured_stdout
ensure_dir	hash_file	partition)current_umaskis_within_directory2set_extracted_file_to_default_mode_plus_executablezip_item_is_executable)parse_wheel)Protocolc                   @   s    e Zd ZdZdZdZdd ZdS )FileNc                 C   s   d S N selfr5   r5   J/tmp/pip-unpacked-wheel-tx790h60/pip/_internal/operations/install/wheel.pysaveB   s    z	File.save)__name__
__module____qualname__src_record_path	dest_pathchangedr9   r5   r5   r5   r8   r3   =   s   r3   
RecordPath   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)path	blocksizehlengthrD   r5   r5   r8   rehashM   s    rL   c                 C   s   | dddS )zPReturn keyword arguments to properly open a CSV file
    in the given mode.
     utf-8)modenewlineencodingr5   )rO   r5   r5   r8   csv_io_kwargsW   s    rR   c              	   C   s   t j| stt| dR}| }|ds<W 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 )zQReplace #!python with #!/path/to/python
    Return True if file was changed.
    rbs   #!pythonFs   #!asciiwbT)osrH   isfileAssertionErroropenreadline
startswithsys
executableencodegetfilesystemencodinglinesepreadwrite)rH   script	firstlineexenamerestr5   r5   r8   
fix_script_   s    

rg   c                 C   s   |  dd dkS )NzRoot-Is-PurelibrM   true)getlower)metadatar5   r5   r8   wheel_root_is_purelibt   s    rl   )distreturnc                 C   sL   i }i }|   D ]2}|jdkr,|j||j< q|jdkr|j||j< q||fS )Nconsole_scriptsgui_scripts)iter_entry_pointsgroupvaluename)rm   ro   rp   entry_pointr5   r5   r8   get_entrypointsy   s    

rv   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 r5   )rV   rH   normcaserF   sep.0ir5   r5   r8   
<listcomp>   s    z5message_about_scripts_not_on_PATH.<locals>.<listcomp>PATHrM   c                    s&   i | ]\}}t j| kr||qS r5   )rV   rH   rw   )rz   
parent_dirscriptsnot_warn_dirsr5   r8   
<dictcomp>   s    z5message_about_scripts_not_on_PATH.<locals>.<dictcomp>   zscript {} isr   zscripts {} are, 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   ~Nr5   ry   r5   r5   r8   	<genexpr>   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.
)collectionsdefaultdictsetrV   rH   dirnamebasenameaddenvironri   splitpathsepappendrw   r\   r]   itemssortedlenformatjoinany)r   grouped_by_dirdestfiler~   script_namewarn_for	msg_linesdir_scriptssorted_scripts
start_textlast_line_fmtwarn_for_tildetilde_warning_msgr5   r   r8   !message_about_scripts_not_on_PATH   sV    

 
r   c                 C   s   t dd | D S )a  Normalize the given rows of a RECORD file.

    Items in each row are converted into str. Rows are then sorted to make
    the value more predictable for tests.

    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|t|fV  qdS )rN   rQ   N)r   rG   )rz   record_pathZhash_sizer5   r5   r8   r      s   z&_normalized_outrows.<locals>.<genexpr>)r   )outrowsr5   r5   r8   _normalized_outrows   s    r   c                 C   s   | S r4   r5   )r   r5   r5   r8   _record_to_fs_path   s    r   c                 C   sX   |d k	r>t j| d  t j|d  kr>t j| |} | t jjd} td| S )Nr   /r@   )rV   rH   
splitdriverj   relpathr(   rx   r   )rH   relative_tor5   r5   r8   _fs_to_record_path   s    r   c                 C   s   t | dd}td|S )NrN   r   r@   )r   r   )Zrecord_columnpr5   r5   r8   _parse_record_path   s    r   c                 C   s   g }| D ]}t |dkr$td| t|d }|||}||krVtt|\}	}
n0t |dkrj|d nd}	t |dkr|d nd}
|||	|
f q|D ]*}t||}t|\}	}
|||	|
f q|	 D ]}||ddf q|S )z_
    :param installed: A map from archive RECORD path to installation RECORD
        path.
       z,RECORD line has more than three elements: %sr   r   rM      )
r   loggerwarningr   poprL   r   r   r   values)old_csv_rows	installedr?   	generatedlib_dirinstalled_rowsrowZold_record_pathZnew_record_pathrD   rK   frH   Zinstalled_record_pathr5   r5   r8   get_csv_rows_for_installed   s$    
r   c                 C   s  |   } g }| dd}|rdtjkr4|d|  tjdddkr^|dtjd | |dt	  d	|  d
d | D }|D ]
}| |= q| dd}|rdtjkr|d|  |dt	 | dd | D }|D ]
}| |= q|
tdj|   |S )zk
    Given the mapping from entrypoint name to callable, return the relevant
    console script specs.
    pipNENSUREPIP_OPTIONSzpip = rM   
altinstallz
pip{} = {}r   z = c                 S   s   g | ]}t d |r|qS )zpip(\d(\.\d)?)?$rematchrz   kr5   r5   r8   r|   W  s      z,get_console_script_specs.<locals>.<listcomp>easy_installzeasy_install = zeasy_install-{} = {}c                 S   s   g | ]}t d |r|qS )zeasy_install(-\d\.\d)?$r   r   r5   r5   r8   r|   g  s     {} = {})copyr   rV   r   r   ri   r   r\   version_infor    extendr   r   )consolescripts_to_generate
pip_scriptpip_epr   easy_install_scripteasy_install_epr5   r5   r8   get_console_script_specs  sF    #

 r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ZipBackedFilec                 C   s   || _ || _|| _d| _d S NF)r=   r>   	_zip_filer?   )r7   r=   r>   zip_filer5   r5   r8   __init__t  s    zZipBackedFile.__init__c                 C   s   | j | jS r4   )r   getinfor=   r6   r5   r5   r8   _getinfo{  s    zZipBackedFile._getinfoc              
   C   s   t j| j}t| t j| jr0t | j |  }| j	|*}t	| jd}t
|| W 5 Q R X W 5 Q R X t|rt| j d S )NrU   )rV   rH   r   r>   r*   existsunlinkr   r   rY   shutilcopyfileobjr0   r/   )r7   r~   zipinfor   destr5   r5   r8   r9     s    
 zZipBackedFile.saveN)r:   r;   r<   r   r   r9   r5   r5   r5   r8   r   s  s   r   c                   @   s   e Zd Zdd Zdd ZdS )
ScriptFilec                 C   s$   || _ | j j| _| j j| _d| _d S r   )_filer=   r>   r?   )r7   filer5   r5   r8   r     s    

zScriptFile.__init__c                 C   s   | j   t| j| _d S r4   )r   r9   rg   r>   r?   r6   r5   r5   r8   r9     s    
zScriptFile.saveN)r:   r;   r<   r   r9   r5   r5   r5   r8   r     s   r   c                       s   e Zd Z fddZ  ZS )MissingCallableSuffixc                    s   t  d| d S )NzInvalid script entry point: {} - A callable suffix is required. Cf https://packaging.python.org/specifications/entry-points/#use-for-scripts for more information.)superr   r   )r7   ru   	__class__r5   r8   r     s
    zMissingCallableSuffix.__init__)r:   r;   r<   r   __classcell__r5   r5   r   r8   r     s   r   c                 C   s*   t | }|d k	r&|jd kr&tt|d S r4   )r   suffixr   rG   )specificationentryr5   r5   r8   _raise_for_invalid_entrypoint  s    r   c                       s   e Zd Zd fdd	Z  ZS )PipScriptMakerNc                    s   t | t ||S r4   )r   r   make)r7   r   optionsr   r5   r8   r     s    zPipScriptMaker.make)N)r:   r;   r<   r   r   r5   r5   r   r8   r     s   r   TFc           :   
      s  t | \}}	t|	r|jn|ji t g }
d.fdd	}fdd}dd }fdd	  fd
d} fdd}dd }| }t||}t||\}}|tt	 d}t
||}dd }t||\}}||}t
||}t||}tt| }t|\fdd}t
||}t||}t
t|}t||}|D ] }|  ||j|j|j qLfdd} dd }!|r6t }"t ~ td |  D ]f}#t|#t	 d}$tj|$ddd}%|%r|!|#}&tj|&sttd|& tjj!d}'||'|& qW 5 Q R X W 5 Q R X t"#|"$  t%d|j&}(d|(_'dh|(_(d|(_)t*})t+t,d j-. }*|(/|)}+|
0|+ |
0|(/|*d!di |rt1|+},|,dk	rt"2|, d"t3  @ t4j5fd#d$}-tj6|}.tj6|.d%}/|-|/}0|07d& W 5 Q R X |
8|/ |dk	rjtj6|.t9}1|-|1}2|27|: ;d' W 5 Q R X |
8|1 |rtj6|.d(}3t<|3d) W 5 Q R X |
8|3 |=d*}4t+t>?|4@ }5tA|5|
d+}6tj6|.d*}7|-|7ftBd,$}8t>Ctd-|8}9|9DtE|6 W 5 Q R X dS )/a  Install a wheel.

    :param name: Name of the project to install
    :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
    Fc                    s(   t |}|| < |r$ t | dS )z6Map archive RECORD paths to installation RECORD paths.N)r   r   )srcfiler   modifiednewpath)r?   r   r   r5   r8   record_installed  s    
z(_install_wheel.<locals>.record_installedc                  3   s,      } tt| }|D ]}td|V  qd S )Nr@   )namelistmapr   r   )namesZdecoded_namesrt   )	wheel_zipr5   r8   	all_paths  s    
z!_install_wheel.<locals>.all_pathsc                 S   s
   |  dS )Nr   )endswithrH   r5   r5   r8   is_dir_path  s    z#_install_wheel.<locals>.is_dir_pathc                    s$   t | |s d}t| || d S )NzRThe wheel {!r} has a file {!r} trying to install outside the target directory {!r})r.   r   r   )Zdest_dir_pathtarget_pathmessage)
wheel_pathr5   r8   assert_no_path_traversal  s    
z0_install_wheel.<locals>.assert_no_path_traversalc                    s    fdd}|S )Nc                    s0   t j| }t j|} | t| |S r4   )rV   rH   normpathr   r   )r   normed_pathr>   )r   r   r   r5   r8   make_root_scheme_file  s    
zM_install_wheel.<locals>.root_scheme_file_maker.<locals>.make_root_scheme_filer5   )r   r   r  )r   )r   r   r8   root_scheme_file_maker  s    z._install_wheel.<locals>.root_scheme_file_makerc                    sF   i  t D ]&}t|}tt||t d |< q fdd}|S )Nr   c           	   	      s   t j| }z|t jjd\}}}W n( tk
rN   d| }t|Y nX z| }W n: tk
r   d	t
}d|| |}t|Y nX t j	||} || t| |S )Nr   zbUnexpected file in {}: {!r}. .data directory contents should be named like: '<scheme key>/<path>'.r   zUnknown scheme key used in {}: {} (for file {!r}). .data directory contents should be in subdirectories named with a valid scheme key ({}))rV   rH   r   r   rx   
ValueErrorr   r   KeyErrorr   r   r   )	r   r  _Z
scheme_keyZdest_subpathr   Zscheme_pathZvalid_scheme_keysr>   )r   scheme_pathsr   r   r5   r8   make_data_scheme_file   s2        
zM_install_wheel.<locals>.data_scheme_file_maker.<locals>.make_data_scheme_file)r%   r   getattrr\   r_   )r   schemekeyZencoded_keyr  )r   r   )r  r   r8   data_scheme_file_maker  s     z._install_wheel.<locals>.data_scheme_file_makerc                 S   s   |  ddd dS )Nr   r   r   .data)r   r   r   r5   r5   r8   is_data_scheme_path?  s    z+_install_wheel.<locals>.is_data_scheme_pathr   c                 S   s2   |  dd}t|dko0|d do0|d dkS )Nr   r   r   r  r   r   )r   r   r   )rH   partsr5   r5   r8   is_script_scheme_pathO  s    
z-_install_wheel.<locals>.is_script_scheme_pathc                    sz   | j }tj|}| dr.|d d }n<| drJ|d d }n | drf|d d }n|}| kpx|kS )Nz.exez
-script.pyiz.pya)r>   rV   rH   r   rj   r   )r   rH   rt   	matchname)r   guir5   r8   is_entrypoint_wrapperd  s    z-_install_wheel.<locals>.is_entrypoint_wrapperc                  3   sH   t t  D ]2} tj| }tj|s0q|ds<q|V  qd S )Nz.py)r   r   r   rV   rH   r   rW   r   )Zinstalled_pathZfull_installed_path)r   r   r5   r8   pyc_source_file_paths  s    
z-_install_wheel.<locals>.pyc_source_file_pathsc                 S   s   t j| S )zAReturn the path the pyc file would have been written to.
        )	importlibutilcache_from_sourcer   r5   r5   r8   pyc_output_path  s    z'_install_wheel.<locals>.pyc_output_pathignoreT)forcequietr@   r   NrM   r   r  i  c              	   ;   s<   t | f|}|V  W 5 Q R X t|j  t|j|  d S r4   )r'   rV   chmodrt   r(   )rH   kwargsr   )generated_file_moder5   r8   _generate_file  s    z&_install_wheel.<locals>._generate_file	INSTALLERs   pip
rN   Z	REQUESTEDrU   RECORD)r   r?   r   r   wzIO[str])F)Fr1   rl   purelibplatlibr   r   r,   r   r\   r_   r   r   r"   r   rv   r   r9   r=   r>   r?   r)   warningscatch_warningsfilterwarningsr   
compileallcompile_filerV   rH   r   rX   r   r(   rx   r   debuggetvaluer   r   clobbervariantsset_moder   listr   r   r   make_multipler   r   r   r-   
contextlibcontextmanagerr   rb   r   r#   Zto_jsonr^   rY   	read_textcsvreader
splitlinesr   rR   writer	writerowsr   ):rt   r   r   r
  	pycompilewarn_script_location
direct_url	requestedinfo_dirrk   r   r   r   r   r  r  r  pathsZ
file_pathsZroot_scheme_pathsZdata_scheme_pathsr  filesr  Zother_scheme_pathsZscript_scheme_pathsr  Zother_scheme_filesdistributionr  Zscript_scheme_filesr   r  r  stdoutrH   Zpath_argsuccessZpyc_pathZpyc_record_pathmakerr   gui_scripts_to_generategenerated_console_scriptsmsgr   dest_info_dirZinstaller_pathinstaller_fileZdirect_url_pathZdirect_url_fileZrequested_pathZrecord_textZrecord_rowsZrowsr   Zrecord_filer8  r5   )	r   r?   r   r  r  r   r   r   r   r8   _install_wheel  s    	(
 
	 



 




    "











rJ  c              
   c   s\   z
d V  W nL t k
rV } z.d| |jd }tt t |t d  W 5 d }~X Y nX d S )NzFor req: {}. {}r   r   )r   r   argsr   r\   exc_info)req_descriptioner   r5   r5   r8   req_error_context  s    
  
rO  c           	      C   sH   t |dd2}t| t| |||||||d W 5 Q R X W 5 Q R X d S )NT)
allowZip64)rt   r   r   r
  r:  r;  r<  r=  )r   rO  rJ  )	rt   r   r
  rM  r:  r;  r<  r=  zr5   r5   r8   install_wheel  s    
rR  )rA   )N)TTNF)TTNF)m__doc__r   r)  r2  r5  r  loggingos.pathrV   r   r   r\   r&  base64r   email.messager   	itertoolsr   r   r   typingr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   zipfiler   r   pip._vendor.distlib.scriptsr   Zpip._vendor.distlib.utilr   pip._vendor.packaging.utilsr   Zpip._vendor.sixr   r   r   pip._internal.exceptionsr   pip._internal.locationsr    Zpip._internal.metadatar!   r"   Zpip._internal.models.direct_urlr#   r$   pip._internal.models.schemer%   r&   pip._internal.utils.filesystemr'   r(   pip._internal.utils.miscr)   r*   r+   r,   pip._internal.utils.unpackingr-   r.   r/   r0   pip._internal.utils.wheelr1   r2   r3   	getLoggerr:   r   rG   r@   intZInstalledCSVRowrL   rR   rg   rl   rv   r   r   r   r   r   r   r   r   r   r   r   r   rJ  r3  rO  rR  r5   r5   r5   r8   <module>   s   H




(G
!V)    
  @
    