U
    P^ݎ                     @   sH  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	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mZ d d	l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!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,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZBmCZC d dlDmEZE d dlFmGZG d dlHmIZI d dlJmKZK eGr*d dlLmMZMmNZNmOZOmPZPmQZQmRZRmSZS d dlmTZT d dlUmVZV d d lWmXZX d d!lYmZZZ d d"l[m\Z\ d d#l]m^Z^ e_e`ZaG d$d% d%ebZcdS )&    )absolute_importN)change_root)pkg_resourcessix)Requirement)canonicalize_name)Version)parse)Pep517HookCaller)
pep425tagswheel)NoOpBuildEnvironment)InstallationError)Link)get_metadata_generator)load_pyproject_tomlmake_pyproject_path)UninstallPathSet)
native_str)Hashes)
indent_log)PIP_DELETE_MARKER_FILENAMEhas_delete_marker_file)_make_build_dirask_path_exists
backup_dirdisplay_pathdist_in_site_packagesdist_in_usersite
ensure_dirget_installed_versionhide_urlredact_auth_from_urlrmtree)get_metadata)make_setuptools_shim_args)call_subprocessrunner_with_spinner_message)TempDirectory)MYPY_CHECK_RUNNING)running_under_virtualenv)vcs)AnyDictIterableListOptionalSequenceUnion)BuildEnvironment)
WheelCache)PackageFinder)Distribution)SpecifierSet)Markerc                
   @   sp  e Zd ZdZdPd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dQddZe	dd ZdRddZdd Zd d! Zd"d# Zd$d% Zd&d' Ze	d(d) ZdSd*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!dTd@dAZ"dUdBdCZ#dVdDdEZ$dFdG Z%dHdI Z&dJdK Z'dWdLdMZ(dNdO Z)dS )X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.
    NF c                 C   s2  |d kst |tst||| _|| _|| _|d kr<d | _ntj	tj
|| _|| _|
| _|d krz|rz|jrzt|j}| | _| _|r|| _n |rdd |jD | _nt | _|d kr|r|j}|| _d | _d | _d | _d | _d | _|	r|	ni | _d| _d| _|| _t | _d | _ d | _!g | _"d | _#|| _$d S )Nc                 S   s   h | ]}t |qS r:   )r   
safe_extra.0extrar:   r:   >/tmp/pip-install-220asx0h/pip/pip/_internal/req/req_install.py	<setcomp>v   s    z.InstallRequirement.__init__.<locals>.<setcomp>F)%
isinstancer   AssertionErrorreq
comes_from
constraint
source_dirospathnormpathabspatheditable_wheel_cacheurlr   linkoriginal_linkextrassetmarkermarkerssatisfied_byconflicts_with_temp_build_dir_ideal_build_dirinstall_succeededoptionsprepared	is_directisolatedr   	build_envmetadata_directorypyproject_requiresrequirements_to_checkpep517_backend
use_pep517)selfrC   rD   rF   rK   rN   rS   rb   r\   rY   wheel_cacherE   rP   r:   r:   r?   __init__S   sJ    

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))rC   strrN   r"   rM   rT   r   locationrD   rA   r   string_types	from_pathrc   srD   r:   r:   r?   __str__   s     


zInstallRequirement.__str__c                 C   s   d| j jt| | jf S )Nz<%s object: %s editable=%r>)	__class____name__rf   rK   rc   r:   r:   r?   __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)r=   attr
attributesr:   r?   	<genexpr>   s    z2InstallRequirement.format_debug.<locals>.<genexpr>z<{name} object: {{{state}}}>z, )namestate)varssortedrq   rm   rn   join)rc   namesrw   r:   rs   r?   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)rN   package_namesupported_tagszUsing cached wheel link: %s)	rN   find_requirementrL   r   get_supportedgetrv   loggerdebug)rc   finderupgraderequire_hashesold_linkr~   r:   r:   r?   populate_link   s    

z InstallRequirement.populate_linkc                 C   s    | j d krd S tt| j jS N)rC   r   r   	safe_namerv   ro   r:   r:   r?   rv      s    
zInstallRequirement.namec                 C   s   | j jS r   )rC   	specifierro   r:   r:   r?   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)rc   
specifiersr:   r:   r?   	is_pinned   s    zInstallRequirement.is_pinnedc                 C   s
   t | jS r   )r    rv   ro   r:   r:   r?   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 )r>   N)rS   evaluater<   ro   r:   r?   ru     s   z3InstallRequirement.match_markers.<locals>.<genexpr>T)rS   any)rc   extras_requestedr:   ro   r?   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)boolrY   r   ro   r:   r:   r?   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   )
rY   r   copyrN   rO   hash
setdefault	hash_nameappendr   )rc   trust_internetgood_hashesrN   r:   r:   r?   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->)rC   rf   rD   rA   r   rh   ri   rj   r:   r:   r?   ri   8  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rNtdd| _|| _| jjS | jr`| j }n| j}t	j
|std| t| t	j||S )Nz	req-buildkindzCreating directory %s)rB   rV   rH   rC   r(   rW   rK   rv   lowerrG   existsr   r   r   rz   )rc   	build_dirrv   r:   r:   r?   ensure_build_locationH  s    

z(InstallRequirement.ensure_build_locationc                 C   s  | j dk	rdS | jdk	st| js&t| jdk	r8| jjs<t| j}d| _| | j}tj|rpt	dt
| td| t
|jt
| t|j| tjtj|| _ t|dd| _| jr| j}tjj||jd}tj||}tjtj|}|| _d| _dS )a  Move self._temp_build_dir to "self._ideal_build_dir/self.req.name"

        For some requirements (e.g. a path to a directory), the name of the
        package is not available until we run egg_info, so the build_location
        will return a temporary directory and store the _ideal_build_dir.

        This is only called to "fix" the build directory after generating
        metadata.
        Nz<A package already exists in %s; please remove it to continuez,Moving package %s from %s to new location %szreq-install)rH   r   )start)rF   rC   rB   rV   rW   rH   r   rG   r   r   r   r   r   shutilmoverI   rJ   r(   r^   relpathrz   )rc   old_locationnew_locationold_metarelnew_metar:   r:   r?   move_to_correct_build_directorye  sH    

   z2InstallRequirement.move_to_correct_build_directoryc                 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)rF   r   r   r   r#   rV   cleanupr]   ro   r:   r:   r?   remove_temporary_source  s    

z*InstallRequirement.remove_temporary_sourcec                 C   s   | j dkrdS zHtt| j }d|_tt|| _| jrT| jrT| j| _d| _W dS W nx tj	k
rn   Y dS  tj
k
r   t| j j}|rt|r|| _qt rt|rtd|j|jf n|| _Y nX dS )zFind an installed distribution that satisfies or conflicts
        with this requirement, and set self.satisfied_by or
        self.conflicts_with appropriately.
        NFTzVWill not install to the user site because it will lack sys.path precedence to %s in %s)rC   r   rf   rR   r   get_distributionrT   rK   rU   DistributionNotFoundVersionConflictrv   r   r*   r   r   project_namerg   )rc   use_user_site	no_markerexisting_distr:   r:   r?   check_if_exists  s:    


z"InstallRequirement.check_if_existsc                 C   s   | j s
dS | j jS )NF)rN   is_wheelro   r:   r:   r?   r     s    zInstallRequirement.is_wheelc                 C   s(   t j| j| j||||||| j|d
 d S )N)userhomerootprefix	pycompiler\   warn_script_location)r   move_wheel_filesrv   rC   r\   )rc   wheeldirr   r   r   r   r   r   r:   r:   r?   r     s      z#InstallRequirement.move_wheel_filesc                 C   s   t j| j| jr| jjpdS )Nr   )rG   rH   rz   rF   rN   subdirectory_fragmentro   r:   r:   r?   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)rF   rB   rG   rH   rz   r   r   PY2rA   	text_typeencodesysgetfilesystemencoding)rc   setup_pyr:   r:   r?   setup_py_path  s
    z InstallRequirement.setup_py_pathc                 C   s   | j std|  t| jS )Nr   )rF   rB   r   r   ro   r:   r:   r?   pyproject_toml_path  s    z&InstallRequirement.pyproject_toml_pathc                 C   sX   t | j| j| jt| }|dkr*d| _dS d| _|\}}}|| _|| _t| j|| _	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)
r   rb   r   r   rf   r`   r_   r
   r   ra   )rc   Zpyproject_toml_datarequiresbackendcheckr:   r:   r?   r     s"    	
 z&InstallRequirement.load_pyproject_tomlc              	   C   s   | j s
tt| }t  || | _W 5 Q R X | js~tt| jd t	rNd}nd}t
d| jd || jd g| _|   n<t| jd }t| jj|krtd| j|| j t
|| _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.
        r   r   r   r   NamezeGenerating metadata for package %s produced metadata for project name %s. Fix your #egg=%s fragments.N)rF   rB   r   r   r^   rC   rA   parse_versionmetadatar   r   rz   r   r   rv   r   warning)rc   Zmetadata_generatoropmetadata_namer:   r:   r?   prepare_metadata(  s2    

  z#InstallRequirement.prepare_metadatac              
   C   sx   | j d k	sttdd}t|j |j}| j4 td}| j }|	| |
|}W 5 Q R X W 5 Q R X tj||S )Nzmodern-metadatar   zPreparing wheel metadata)ra   rB   r(   atexitregisterr   rH   r]   r'   subprocess_runner prepare_metadata_for_build_wheelrG   rz   )rc   Zmetadata_tmpdirmetadata_dirrunnerr   distinfo_dirr:   r:   r?   prepare_pep517_metadataM  s    
z*InstallRequirement.prepare_pep517_metadatac                 C   s   t | dst|  | _| jS )N	_metadata)hasattrr$   get_distr   ro   r:   r:   r?   r   d  s    
zInstallRequirement.metadatac                 C   sn   | j tj}|dr tj}n|ds.ttj}tj	
|\}}tj	|d }t||}||||dS )z8Return a pkg_resources.Distribution for this requirement	.egg-infoz
.dist-infor   )r   r   )r^   rstriprG   sependswithr   r6   rB   DistInfoDistributionrH   splitsplitextPathMetadata)rc   Zdist_dirZdist_clsbase_dirZdist_dir_name	dist_namer   r:   r:   r?   r   l  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)	rF   rB   r   rC   r   r   r   r   r   )rc   r   r:   r:   r?   assert_source_matches_version  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)rF   r   )rc   
parent_dirr:   r:   r?   ensure_has_source_dir  s    
z(InstallRequirement.ensure_has_source_dirc              
   C   s   t d| j |r*d|g}t|| }t| j|| jd}t 6 | j	$ t
|ddg t| | jd W 5 Q R X W 5 Q R X d| _d S )NzRunning setup.py develop for %sz--prefix={})global_optionsno_user_configdevelopz	--no-deps)cwdT)r   inforv   rq   listr%   r   r\   r   r]   r&   r   rX   )rc   install_optionsr   r   prefix_parambase_cmdr:   r:   r?   install_editable  s(    z#InstallRequirement.install_editablec                 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	
|}|rt| j j}|r|j| j|d q|j| j|d ndstd| j |f d S )	Nz>Cannot update repository at %s; repository location is unknownfile+zbad url: %rr   )rM   r   z+Unexpected version control type (in %s): %s)rN   r   r   rF   rK   rB   schemerM   r   r+   get_backendr!   obtainexport)rc   r  vc_typerM   vcs_backendZ
hidden_urlr:   r:   r?   update_editable  s.    


z"InstallRequirement.update_editablec                 C   sB   |  |std| j dS | jp&| j}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.N)	r   r   r   rv   rT   rU   r   	from_distremove)rc   auto_confirmverboser   distuninstalled_pathsetr:   r:   r?   	uninstall  s    

zInstallRequirement.uninstallc                 C   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   /)
startswithrG   rH   r   rB   r   replace)rc   rv   r   r:   r:   r?   _clean_zip_name  s    
z"InstallRequirement._clean_zip_namec                 C   s(   t j||}| ||}| jd | S )Nr  )rG   rH   rz   r  rv   )rc   rH   	parentdirrootdirrv   r:   r:   r?   _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) rF   rB   rv   r   rG   rH   rz   r   r   r   r   r   r
  r   r   r   r   exitzipfileZipFileZIP_DEFLATEDnormcaserJ   r   walkr  ZipInfoexternal_attrwritestrr   writer   )rc   r   create_archivearchive_namearchive_pathresponse	dest_fileZ
zip_outputdirdirpathdirnames	filenamesdirnamedir_arcnamezipdirfilenamefile_arcnamer:   r:   r?   archive  sz    

   

  
  zInstallRequirement.archivec	                    sh  |d k	r|ng }| j r*| j|||d d S | jrnt| j}	t|	| j | j| j |||||d d| _	d S t
|| jdg  }t
|| jdg  }tdd}
tj|
jd}| || ||}td	| j}t * | j ||| | jd
 W 5 Q R X W 5 Q R X tj|s8td| W 5 Q R  d S d| _	 fdd}t|\}|D ],}tj|}|drX||} qqXtd|  W 5 Q R  W 5 Q R  d S W 5 Q R X g }t|L}|D ]@}| }tj|r|tjj 7 }|!tj"||| qW 5 Q R X |#  t$| tj|d}t|d}|%d|d  W 5 Q R X W 5 Q R X d S )N)r   )r   r   r   r   r   r   Tr   r   recordr   zinstall-record.txtzRunning setup.py install for {})cmdr   zRecord file %s not foundc                    s&    d kst j| s| S t | S d S r   )rG   rH   isabsr   )rH   r   r:   r?   prepend_root  s    z0InstallRequirement.install.<locals>.prepend_rootr   z;Could not find .egg-info directory in install record for %szinstalled-files.txtr  
)&rK   r   r   r   wheel_versionrF   check_compatibilityrv   r   rX   r   rY   r   r(   rG   rH   rz   get_install_argsr'   rq   r   r]   r   r   r   r   openr0  r   r   stripisdirr   r   r   sortr   r&  )rc   r   r   r   r   r   r   r   r   r   temp_dirrecord_filenameinstall_argsr   r:  fline	directoryegg_info_dir	new_linesr3  inst_files_pathr:   r9  r?   installD  s              


"
zInstallRequirement.installc              	   C   s   t | j|| jdd}|dd|g7 }|dg7 }|d k	r@|d|g7 }|d k	rT|d|g7 }|rd|dg7 }n
|d	g7 }t rd
t  }|dtjt	j
dd|| jg7 }|S )NT)r   r   Zunbuffered_outputrL  z--recordz#--single-version-externally-managedz--rootz--prefixz	--compilez--no-compilepythonz--install-headersincludesite)r%   r   r\   r*   	sysconfigget_python_versionrG   rH   rz   r   r   rv   )rc   r   rD  r   r   r   rE  
py_ver_strr:   r:   r?   r>    s0    	

 z#InstallRequirement.get_install_args)
NFNNNFNNFr:   )N)T)NNNTFT)r:   N)T)FFF)NNNNTFT)*rn   
__module____qualname____doc__re   rl   rp   r|   r   propertyrv   r   r   r   r   r   r   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r5  rL  r>  r:   r:   r:   r?   r9   L   s   
          
W








8+
	      



%
  

  
	B       
`r9   )d
__future__r   r   loggingrG   r   r   rP  r  distutils.utilr   pip._vendorr   r   Z"pip._vendor.packaging.requirementsr   pip._vendor.packaging.utilsr   Zpip._vendor.packaging.versionr   r	   r   pip._vendor.pep517.wrappersr
   pip._internalr   r   pip._internal.build_envr   pip._internal.exceptionsr   pip._internal.models.linkr   Z*pip._internal.operations.generate_metadatar   pip._internal.pyprojectr   r   pip._internal.req.req_uninstallr   pip._internal.utils.compatr   pip._internal.utils.hashesr   pip._internal.utils.loggingr   Z pip._internal.utils.marker_filesr   r   pip._internal.utils.miscr   r   r   r   r   r   r   r    r!   r"   r#   pip._internal.utils.packagingr$   $pip._internal.utils.setuptools_buildr%   Zpip._internal.utils.subprocessr&   r'   pip._internal.utils.temp_dirr(   pip._internal.utils.typingr)   Zpip._internal.utils.virtualenvr*   pip._internal.vcsr+   typingr,   r-   r.   r/   r0   r1   r2   r3   pip._internal.cacher4   pip._internal.indexr5   Zpip._vendor.pkg_resourcesr6   Z pip._vendor.packaging.specifiersr7   Zpip._vendor.packaging.markersr8   	getLoggerrn   r   objectr9   r:   r:   r:   r?   <module>   sV   4$
