U
    @vgv                     @   sL  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mZ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 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. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6m7Z7m8Z8 d dl9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZB d dlCmDZD d dlEmFZF d dlGmHZH d dlImJZJ d dlKmLZL eHr&d dlMmNZNmOZOmPZPmQZQmRZRmSZSmTZT d dlmUZU d d lVmWZW d d!lXmYZY d d"lZm[Z[ d d#l\m]Z] d d$l^m_Z_ e`eaZbd%d& ZcG d'd( d(edZedS ))    )absolute_importN)pkg_resourcessix)Requirement)canonicalize_name)Version)parse)Pep517HookCaller)
pep425tags)NoOpBuildEnvironment)InstallationError)
get_scheme)Link)generate_metadata)install_editable)install)install_wheel)load_pyproject_tomlmake_pyproject_path)UninstallPathSet)
deprecated)Hashes)
indent_log)PIP_DELETE_MARKER_FILENAMEhas_delete_marker_filewrite_delete_marker_file)	ask_path_exists
backup_dirdisplay_pathdist_in_site_packagesdist_in_usersiteget_installed_versionhide_urlredact_auth_from_urlrmtree)get_metadata)TempDirectory)MYPY_CHECK_RUNNING)running_under_virtualenv)vcs)AnyDictIterableListOptionalSequenceUnion)BuildEnvironment)
WheelCache)PackageFinder)Distribution)SpecifierSet)Markerc                 C   sl   |  tj}|drtj}n|ds,ttj}tj	|\}}tj
|d }t||}||||dS )zQReturn a pkg_resources.Distribution for the provided
    metadata directory.
    z	.egg-infoz
.dist-infor   )project_namemetadata)rstripossependswithr   r4   AssertionErrorDistInfoDistributionpathsplitsplitextPathMetadata)metadata_directorydist_dirdist_clsbase_dirdist_dir_name	dist_namer8    rI   A/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/req/req_install.py	_get_distI   s    
rK   c                
   @   sT  e Zd ZdZdJddZdd Zd	d
 Zdd Zdd Ze	dd Z
e	dd Ze	dd Ze	dd ZdKddZe	dd ZdLddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Ze	d*d+ Ze	d,d- Ze	d.d/ Ze	d0d1 Zd2d3 Zd4d5 Zd6d7 Ze	d8d9 Zd:d; Zd<d= Z d>d? Z!dMd@dAZ"dNdBdCZ#dDdE Z$dFdG Z%dOdHdIZ&dS )PInstallRequirementz
    Represents something that may be installed later on, may have information
    about where to fetch the relevant requirement and also contains logic for
    installing the said requirement.
    NFrI   c                 C   sL  |d kst |tst||| _|| _|| _|d kr<d | _ntj	tj
|| _|| _|
| _|d krz|rz|jrzt|j}| | _| _d | _| jr| jjr| jj| _|r|| _n |rdd |jD | _nt | _|d kr|r|j}|| _d | _d| _d | _d | _|	r
|	ni | _d| _d| _|| _t  | _!d | _"d | _#g | _$d | _%|| _&d S )Nc                 S   s   h | ]}t |qS rI   )r   
safe_extra.0extrarI   rI   rJ   	<setcomp>   s    z.InstallRequirement.__init__.<locals>.<setcomp>F)'
isinstancer   r=   req
comes_from
constraint
source_dirr:   r?   normpathabspatheditable_wheel_cacheurlr   linkoriginal_linklocal_file_pathis_file	file_pathextrassetmarkermarkerssatisfied_byshould_reinstall_temp_build_dirinstall_succeededoptionsprepared	is_directisolatedr   	build_envrC   pyproject_requiresrequirements_to_checkpep517_backend
use_pep517)selfrS   rT   rV   rY   r\   rd   rq   rl   ri   wheel_cacherU   ra   rI   rI   rJ   __init__j   sN    


zInstallRequirement.__init__c                 C   s   | j r,t| j }| jrD|dt| jj 7 }n| jr@t| jj}nd}| jd k	rb|dt| jj 7 }| jrt	| jt
jr~| j}n
| j }|r|d| 7 }|S )Nz from %sz<InstallRequirement>z in %sz
 (from %s))rS   strr\   r#   r[   re   r   locationrT   rR   r   string_types	from_pathrr   srT   rI   rI   rJ   __str__   s     


zInstallRequirement.__str__c                 C   s   d| j jt| | jf S )Nz<%s object: %s editable=%r>)	__class____name__ru   rY   rr   rI   rI   rJ   __repr__   s
      zInstallRequirement.__repr__c                    s>   t |  t } fddt|D }dj| jjd|dS )z>An un-tested helper for getting state, for debugging.
        c                 3   s   | ]}d  | | V  qdS )z{}={!r}N)format)rO   attr
attributesrI   rJ   	<genexpr>   s    z2InstallRequirement.format_debug.<locals>.<genexpr>z<{name} object: {{{state}}}>z, )namestate)varssortedr   r|   r}   join)rr   namesr   rI   r   rJ   format_debug   s    
zInstallRequirement.format_debugc                 C   sh   | j dkr|| || _ | jdk	rd|sd| j }t }| jj| j | j|d| _ || j krdtd| j  dS )a  Ensure that if a link can be found for this, that it is found.

        Note that self.link may still be None - if Upgrade is False and the
        requirement is already installed.

        If require_hashes is True, don't use the wheel cache, because cached
        wheels, always built locally, have different hashes than the files
        downloaded from the index server and thus throw false hash mismatches.
        Furthermore, cached wheels at present have undeterministic contents due
        to file modification times.
        N)r\   package_namesupported_tagszUsing cached wheel link: %s)	r\   find_requirementrZ   r
   get_supportedgetr   loggerdebug)rr   finderupgraderequire_hashesold_linkr   rI   rI   rJ   populate_link   s    

z InstallRequirement.populate_linkc                 C   s"   | j d krd S tt| j jS N)rS   r   
ensure_strr   	safe_namer   r~   rI   rI   rJ   r     s    
zInstallRequirement.namec                 C   s   | j jS r   )rS   	specifierr~   rI   rI   rJ   r     s    zInstallRequirement.specifierc                 C   s$   | j }t|dko"tt|jdkS )zReturn whether I am pinned to an exact version.

        For example, some-package==1.2 is pinned; some-package>1.2 is not.
           >   =====)r   lennextiteroperator)rr   
specifiersrI   rI   rJ   	is_pinned  s    zInstallRequirement.is_pinnedc                 C   s
   t | jS r   )r!   r   r~   rI   rI   rJ   installed_version  s    z$InstallRequirement.installed_versionc                    s0   |sd} j d k	r(t fdd|D S dS d S )N) c                 3   s   | ]} j d |iV  qdS )rP   N)rd   evaluaterN   r~   rI   rJ   r   )  s   z3InstallRequirement.match_markers.<locals>.<genexpr>T)rd   any)rr   extras_requestedrI   r~   rJ   match_markers"  s    
z InstallRequirement.match_markersc                 C   s   t | jdi S )zReturn whether any known-good hashes are specified as options.

        These activate --require-hashes mode; hashes specified as part of a
        URL do not.

        hashes)boolri   r   r~   rI   rI   rJ   has_hash_options/  s    	z#InstallRequirement.has_hash_optionsTc                 C   sJ   | j di  }|r| jn| j}|rB|jrB||jg |j t	|S )a  Return a hash-comparer that considers my option- and URL-based
        hashes to be known-good.

        Hashes in URLs--ones embedded in the requirements file, not ones
        downloaded from an index server--are almost peers with ones from
        flags. They satisfy --require-hashes (whether it was implicitly or
        explicitly activated) but do not activate it. md5 and sha224 are not
        allowed in flags, which should nudge people toward good algos. We
        always OR all hashes together, even ones from URLs.

        :param trust_internet: Whether to trust URL-based (#md5=...) hashes
            downloaded from the internet, as by populate_link()

        r   )
ri   r   copyr\   r]   hash
setdefault	hash_nameappendr   )rr   trust_internetgood_hashesr\   rI   rI   rJ   r   :  s
    
zInstallRequirement.hashesc                 C   sR   | j dkrdS t| j }| jrNt| jtjr4| j}n
| j }|rN|d| 7 }|S )z@Format a nice indicator to show where this "comes from"
        Nz->)rS   ru   rT   rR   r   rw   rx   ry   rI   rI   rJ   rx   P  s    


zInstallRequirement.from_pathc                 C   s   |d k	st | jd k	r*| jjs"t | jjS | jd krHtdd| _| jjS | jrZ| j }n| j}tj	|st
d| t| t| tj||S )Nz	req-build)kindzCreating directory %s)r=   rg   r?   rS   r&   rY   r   lowerr:   existsr   r   makedirsr   r   )rr   	build_dirr   rI   rI   rJ   ensure_build_location`  s    


z(InstallRequirement.ensure_build_locationc                 C   sn   | j dkst| jdk	st| jdk	s*ttt| jd trDd}nd}td| jd || jd g| _ dS )z3Set requirement after generating metadata.
        Nr   r   r   r   Name)	rS   r=   r8   rV   rR   parse_versionr   r   r   )rr   oprI   rI   rJ   _set_requirementy  s    z#InstallRequirement._set_requirementc                 C   sD   t | jd }t | jj|kr"d S td| j|| j t|| _d S )Nr   zeGenerating metadata for package %s produced metadata for project name %s. Fix your #egg=%s fragments.)r   r8   rS   r   r   warningr   )rr   metadata_namerI   rI   rJ   warn_on_mismatching_name  s      z+InstallRequirement.warn_on_mismatching_namec                 C   sR   | j r(t| j r(td| j  t| j  d| _ | jrD| j  d| _| j  dS )zVRemove the source files from this requirement, if they are marked
        for deletionzRemoving source in %sN)rV   r   r   r   r$   rg   cleanuprm   r~   rI   rI   rJ   remove_temporary_source  s    

z*InstallRequirement.remove_temporary_sourcec                 C   s   | j dkrdS tt| j }d|_ztt|| _W nx tjk
rN   Y dS  tjk
r   t| j j	}|rt
|rd| _qt rt|rtd|j|jf nd| _Y nX | jr| jrd| _d| _dS )zFind an installed distribution that satisfies or conflicts
        with this requirement, and set self.satisfied_by or
        self.should_reinstall appropriately.
        NTzVWill not install to the user site because it will lack sys.path precedence to %s in %s)rS   r   ru   rc   r   get_distributionre   DistributionNotFoundVersionConflictr   r    rf   r(   r   r   r7   rv   rY   )rr   use_user_site	no_markerexisting_distrI   rI   rJ   check_if_exists  s6    

z"InstallRequirement.check_if_existsc                 C   s   | j s
dS | j jS )NF)r\   is_wheelr~   rI   rI   rJ   r     s    zInstallRequirement.is_wheelc                 C   s   t j| j| jr| jjpdS )Nr   )r:   r?   r   rV   r\   subdirectory_fragmentr~   rI   rI   rJ   unpacked_source_directory  s    z,InstallRequirement.unpacked_source_directoryc                 C   sF   | j std|  tj| jd}tjrBt|tj	rB|
t }|S )NNo source dir for %szsetup.py)rV   r=   r:   r?   r   r   r   PY2rR   	text_typeencodesysgetfilesystemencoding)rr   setup_pyrI   rI   rJ   setup_py_path  s
    z InstallRequirement.setup_py_pathc                 C   s   | j std|  t| jS )Nr   )rV   r=   r   r   r~   rI   rI   rJ   pyproject_toml_path  s    z&InstallRequirement.pyproject_toml_pathc                 C   s^   t | j| j| jt| }|dkr*d| _dS d| _|\}}}}|| _|| _t| j||d| _	dS )aA  Load the pyproject.toml file.

        After calling this routine, all of the attributes related to PEP 517
        processing for this requirement have been set. In particular, the
        use_pep517 attribute can be used to determine whether we should
        follow the PEP 517 or legacy (setup.py) code path.
        NFT)backend_path)
r   rq   r   r   ru   ro   rn   r	   r   rp   )rr   pyproject_toml_datarequiresbackendcheckr   rI   rI   rJ   r     s$    	  z&InstallRequirement.load_pyproject_tomlc              	   C   sZ   | j s<| jstt| j| j| j| j| j| jp6d	| j
dS | jdk	sJtt| j| jdS )zKInvokes metadata generator functions, with the required arguments.
        zfrom {})rm   r   rV   rY   rl   detailsN)rm   r   )rq   r   r=   generate_metadata_legacyrm   r   rY   rl   r   r   r\   rp   r   r~   rI   rI   rJ   _generate_metadata  s    
	z%InstallRequirement._generate_metadatac              	   C   sJ   | j s
tt  |  | _W 5 Q R X | js6|   n|   |   dS )zEnsure that project metadata is available.

        Under PEP 517, call the backend hook to prepare the metadata.
        Under legacy processing, call setup.py egg-info.
        N)	rV   r=   r   r   rC   r   r   r   assert_source_matches_versionr~   rI   rI   rJ   prepare_metadata*  s    

z#InstallRequirement.prepare_metadatac                 C   s   t | dst|  | _| jS )N	_metadata)hasattrr%   get_distr   r~   rI   rI   rJ   r8   >  s    
zInstallRequirement.metadatac                 C   s
   t | jS r   )rK   rC   r~   rI   rI   rJ   r   F  s    zInstallRequirement.get_distc                 C   sR   | j s
t| jd }| jjr8|| jjkr8td| | ntdt| j ||  d S )Nversionz'Requested %s, but installing version %sz;Source in %s has version %s, which satisfies requirement %s)	rV   r=   r8   rS   r   r   r   r   r   )rr   r   rI   rI   rJ   r   J  s    

z0InstallRequirement.assert_source_matches_versionc                 C   s   | j dkr| || _ dS )aA  Ensure that a source_dir is set.

        This will create a temporary build dir if the name of the requirement
        isn't known yet.

        :param parent_dir: The ideal pip parent_dir for the source_dir.
            Generally src_dir for editables and build_dir for sdists.
        :return: self.source_dir
        N)rV   r   )rr   
parent_dirrI   rI   rJ   ensure_has_source_dir]  s    
z(InstallRequirement.ensure_has_source_dirc                 C   s  | j std| j d S | js"t| js,t| j jdkr<d S d| j jksXtd| j j | j jdd\}}t	
|}|r| j jsd| j j}d }| j jdrd}t||d	d
d t| j j}|r|j| j|d n|j| j|d ndstd| j |f d S )Nz>Cannot update repository at %s; repository location is unknownfile+zbad url: %rr   z5This form of VCS requirement is being deprecated: {}.zgit+git@zmgit+https://git@example.com/..., git+ssh://git@example.com/..., or the insecure git+git://git@example.com/...z21.0i  )gone_inissue)r[   r   z+Unexpected version control type (in %s): %s)r\   r   r   rV   rY   r=   schemer[   r@   r)   get_backendis_vcsr   
startswithr   r"   obtainexport)rr   r   vc_typer[   vcs_backendreasonreplacement
hidden_urlrI   rI   rJ   update_editablel  sB    


z"InstallRequirement.update_editablec                 C   sj   | j s
tzt| j j}W n& tjk
rB   td| j Y dS X td| t	
|}||| |S )a  
        Uninstall the distribution currently satisfying this requirement.

        Prompts before removing or modifying files unless
        ``auto_confirm`` is True.

        Refuses to delete or modify files outside of ``sys.prefix`` -
        thus uninstallation within a virtual environment can only
        modify that virtual environment, even if the virtualenv is
        linked to global site-packages.

        z#Skipping %s as it is not installed.NzFound existing installation: %s)rS   r=   r   r   r   r   r   r   infor   	from_distremove)rr   auto_confirmverbosedistuninstalled_pathsetrI   rI   rJ   	uninstall  s    

zInstallRequirement.uninstallc                 C   s.   dd }t j||}|||}| jd | S )Nc                 S   sJ   |  |tjj s"td| |f | t|d d  } | tjjd} | S )Nz$name %r doesn't start with prefix %rr   /)r   r:   r?   r;   r=   r   replace)r   prefixrI   rI   rJ   _clean_zip_name  s    
z=InstallRequirement._get_archive_name.<locals>._clean_zip_namer   )r:   r?   r   r   )rr   r?   	parentdirrootdirr  r   rI   rI   rJ   _get_archive_name  s    	
z$InstallRequirement._get_archive_namec              	   C   s  | j s
td}d| j| jd f }tj||}tj|rtdt	| d}|dkr\d}nj|dkrt
d	t	| t| nF|d
krt|}t
dt	|t	| t|| n|dkrtd |sdS tj|dtjdd}| tjtj| j}t|D ]\}	}
}d|
kr&|
d |
D ]6}| j||	|d}t|d }d|_||d q*|D ]>}|tkrxqf| j||	|d}tj|	|}||| qfqW 5 Q R X t
dt	| dS )z}Saves archive to provided build_dir.

        Used for saving downloaded VCS requirements as part of `pip download`.
        Tz	%s-%s.zipr   z8The file %s exists. (i)gnore, (w)ipe, (b)ackup, (a)bort )iwbar  Fr  zDeleting %sr  zBacking up %s to %sr	  N)
allowZip64zpip-egg-info)r  r  r   i  r   zSaved %s) rV   r=   r   r8   r:   r?   r   r   r   r   r   r   r   r   shutilmover   exitzipfileZipFileZIP_DEFLATEDnormcaserX   r   walkr  ZipInfoexternal_attrwritestrr   writer   )rr   r   create_archivearchive_namearchive_pathresponse	dest_file
zip_outputdirdirpathdirnames	filenamesdirnamedir_arcnamezipdirfilenamefile_arcnamerI   rI   rJ   archive  sz    

   

  
  zInstallRequirement.archivec	           
      C   s   t | j|||| j|d}	|d k	r$|ng }| jr^t|||||| j| j| j| j| jd
 d| _d S | j	r| j
sntt| j| j
|	t| j||d d| _d S t| ||||||||	d	 d S )N)userhomerootrl   r  )r  r)  r   r   r   rl   rm   r   T)r   req_description	pycompilewarn_script_location)install_optionsglobal_optionsr*  r)  r  r   r,  r   )r   r   rl   rY   install_editable_legacyr   rm   r   rh   r   r^   r=   r   ru   rS   install_legacy)
rr   r.  r/  r*  r)  r  r-  r   r,  r   rI   rI   rJ   r     s^    	
zInstallRequirement.install)
NFNNNFNNFrI   )N)T)T)FF)NNNNTFT)'r}   
__module____qualname____doc__rt   r{   r   r   r   propertyr   r   r   r   r   r   r   rx   r   r   r   r   r   r   r   r   r   r   r   r   r8   r   r   r   r   r   r  r'  r   rI   rI   rI   rJ   rL   c   s|   
          
X








*





+
B       rL   )f
__future__r   loggingr:   r  r   r  pip._vendorr   r   "pip._vendor.packaging.requirementsr   pip._vendor.packaging.utilsr   Zpip._vendor.packaging.versionr   r   r   pip._vendor.pep517.wrappersr	   Zpip._internalr
   pip._internal.build_envr   pip._internal.exceptionsr   pip._internal.locationsr   pip._internal.models.linkr   'pip._internal.operations.build.metadatar   .pip._internal.operations.build.metadata_legacyr   0pip._internal.operations.install.editable_legacyr   r0  'pip._internal.operations.install.legacyr   r1  &pip._internal.operations.install.wheelr   pip._internal.pyprojectr   r   pip._internal.req.req_uninstallr   pip._internal.utils.deprecationr   pip._internal.utils.hashesr   pip._internal.utils.loggingr    pip._internal.utils.marker_filesr   r   r   pip._internal.utils.miscr   r   r   r   r    r!   r"   r#   r$   pip._internal.utils.packagingr%   pip._internal.utils.temp_dirr&   pip._internal.utils.typingr'   pip._internal.utils.virtualenvr(   pip._internal.vcsr)   typingr*   r+   r,   r-   r.   r/   r0   r1   pip._internal.cacher2   "pip._internal.index.package_finderr3   Zpip._vendor.pkg_resourcesr4   Z pip._vendor.packaging.specifiersr5   pip._vendor.packaging.markersr6   	getLoggerr}   r   rK   objectrL   rI   rI   rI   rJ   <module>   sX   ,$
