B
    Uvg                 @   s  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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 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) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZB eCeDZEG dd deFZGdS )    )absolute_importN)change_root)pkg_resourcessix)Requirement)canonicalize_name)Version)parse)Pep517HookCaller)wheel)NoOpBuildEnvironment)InstallationError)PIP_DELETE_MARKER_FILENAMErunning_under_virtualenv)Link)load_pyproject_toml)UninstallPathSet)
native_str)Hashes)
indent_log)
_make_build_dirask_path_exists
backup_dircall_subprocessdisplay_pathdist_in_site_packagesdist_in_usersite
ensure_dirget_installed_versionrmtree)get_metadata)SETUPTOOLS_SHIM)TempDirectory)open_spinner)vcs)move_wheel_filesc            
   @   sd  e Zd ZdZdLd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dMddZedd ZdNddZdd Zdd Zd d! Zd"d# Zd$d% Zed&d' ZdOd(d)Zed*d+ Zed,d- Zed.d/ Zd0d1 Zd2d3 Zed4d5 Zed6d7 Zd8d9 Zd:d; Zd<d= Z dPd>d?Z!dQd@dAZ"dRdBdCZ#dDdE Z$dFdG Z%dSdHdIZ&dJdK Z'dS )TInstallRequirementz
    Represents something that may be installed later on, may have information
    about where to fetch the relavant requirement and also contains logic for
    installing the said requirement.
    NFT c             C   sT  |d kst |tst||| _|| _|| _|d k	rLtjtj	|| _
nd | _
|| _|
| _|d k	rt| | _| _n|o|jot|j | _| _|r|| _n |rdd |jD | _nt | _|d k	r|| _n|o|j| _d | _d | _d | _tdd| _d | _|| _d | _d | _|	r|	ni | _d| _d| _ || _!t" | _#d | _$g | _%d | _&d | _'d S )Nc             S   s   h | ]}t |qS r'   )r   
safe_extra).0extrar'   r'   >/tmp/pip-install-r_9ig3yj/pip/pip/_internal/req/req_install.py	<setcomp>K   s   z.InstallRequirement.__init__.<locals>.<setcomp>z	req-build)kindF)(
isinstancer   AssertionErrorreq
comes_from
constraintospathnormpathabspath
source_direditable_wheel_cachelinkoriginal_linkurlr   extrassetmarkersmarker_egg_info_pathsatisfied_byconflicts_withr"   _temp_build_dir_ideal_build_dirupdateinstall_succeededuninstalled_pathsetoptionsprepared	is_directisolatedr   	build_envpyproject_requiresrequirements_to_checkpep517_backend
use_pep517)selfr0   r1   r7   r8   r:   rF   r?   rL   rI   wheel_cacher2   r=   r'   r'   r+   __init__4   sL    zInstallRequirement.__init__c             C   s   | j r(t| j }| jr<|d| jj 7 }n| jr8| jj}nd}| jd k	rZ|dt| jj 7 }| jrt| jt	j
rv| j}n
| j }|r|d| 7 }|S )Nz from %sz<InstallRequirement>z in %sz
 (from %s))r0   strr:   r<   rB   r   locationr1   r.   r   string_types	from_path)rR   sr1   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__rU   r8   )rR   r'   r'   r+   __repr__   s    zInstallRequirement.__repr__c             C   s\   | j dkr|| || _ | jdk	rX|sX| j }| j| j | j| _ || j krXt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.
        NzUsing cached wheel link: %s)r:   find_requirementr9   getnameloggerdebug)rR   finderupgraderequire_hashesold_linkr'   r'   r+   populate_link   s    

z InstallRequirement.populate_linkc             C   s    | j d krd S tt| j jS )N)r0   r   r   	safe_namer`   )rR   r'   r'   r+   r`      s    
zInstallRequirement.namec             C   s   | j jS )N)r0   	specifier)rR   r'   r'   r+   ri      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.
           >   =====)ri   lennextiteroperator)rR   
specifiersr'   r'   r+   	is_pinned   s    zInstallRequirement.is_pinnedc             C   s
   t | jS )N)r   r`   )rR   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)r?   evaluate)r)   r*   )rR   r'   r+   	<genexpr>   s   z3InstallRequirement.match_markers.<locals>.<genexpr>T)r?   any)rR   extras_requestedr'   )rR   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)boolrI   r_   )rR   r'   r'   r+   has_hash_options   s    z#InstallRequirement.has_hash_optionsc             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()

        rz   )
rI   r_   copyr:   r;   hash
setdefault	hash_nameappendr   )rR   trust_internetgood_hashesr:   r'   r'   r+   rz      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->)r0   rU   r1   r.   r   rW   rX   )rR   rY   r1   r'   r'   r+   rX      s    


zInstallRequirement.from_pathc             C   s   |d k	st | jjd k	r | jjS | jd krB| j  || _| jjS | jrT| j }n| j}t	j
|sztd| t| t	j||S )NzCreating directory %s)r/   rD   r4   r0   createrE   r8   r`   lowerr3   existsra   rb   r   join)rR   	build_dirr`   r'   r'   r+   build_location   s    

z!InstallRequirement.build_locationc             C   s   | j dk	rdS | jdk	st| jjs(t| jjs4t| jj}d| j_| | j}tj|rlt	dt
| td| t
|t
| t|| || j_d| _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 by self.run_egg_info to fix the temporary build
        directory.
        Nz<A package already exists in %s; please remove it to continuez,Moving package %s from %s to new location %s)r7   r0   r/   rD   r4   rE   r   r3   r   r   r   ra   rb   shutilmover5   r6   rA   )rR   old_locationnew_locationr'   r'   r+   _correct_build_location  s(    

z*InstallRequirement._correct_build_locationc             C   sT   | j r6tjtj| j tr6td| j  t| j  d| _ | j	
  | j
  dS )zVRemove the source files from this requirement, if they are marked
        for deletionzRemoving source in %sN)r7   r3   r4   r   r   r   ra   rb   r   rD   cleanuprM   )rR   r'   r'   r+   remove_temporary_source:  s    

z*InstallRequirement.remove_temporary_sourcec             C   s   | j dkrdS yFtt| j }d|_tt|| _| jrR| jrR| j| _d| _dS W nv tj	k
rj   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)r0   r   rU   r@   r   get_distributionrB   r8   rC   DistributionNotFoundVersionConflictr`   r   r   r   r   project_namerV   )rR   use_user_site	no_markerexisting_distr'   r'   r+   check_if_existsE  s2    

z"InstallRequirement.check_if_existsc             C   s   | j o| j jS )N)r:   is_wheel)rR   r'   r'   r+   r   o  s    zInstallRequirement.is_wheelc             C   s&   t | j| j||||||| j|d
 d S )N)userhomerootprefix	pycompilerL   warn_script_location)r%   r`   r0   rL   )rR   wheeldirr   r   r   r   r   r   r'   r'   r+   r%   s  s    
z#InstallRequirement.move_wheel_filesc             C   s   t j| j| jr| jjpdS )Nrt   )r3   r4   r   r7   r:   subdirectory_fragment)rR   r'   r'   r+   setup_py_dir  s    zInstallRequirement.setup_py_dirc             C   sF   | j std|  tj| jd}tjrBt|tj	rB|
t }|S )NzNo source dir for %szsetup.py)r7   r/   r3   r4   r   r   r   PY2r.   	text_typeencodesysgetfilesystemencoding)rR   setup_pyr'   r'   r+   r     s
    zInstallRequirement.setup_pyc             C   sF   | j std|  tj| jd}tjrBt|tj	rB|
t }|S )NzNo source dir for %szpyproject.toml)r7   r/   r3   r4   r   r   r   r   r.   r   r   r   r   )rR   pp_tomlr'   r'   r+   pyproject_toml  s
    z!InstallRequirement.pyproject_tomlc             C   sV   t | j| j| jt| }|dkr(d| _n*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   rQ   r   r   rU   rO   rN   r
   r   rP   )rR   pep517_datarequiresbackendcheckr'   r'   r+   r     s    

z&InstallRequirement.load_pyproject_tomlc          
   C   sd  | j s
t| jr$td| j| j ntd| j| j t  t| j }t	j
d|g}| jrd|dg7 }|dg }| jrzg }n tj| jd}t| ddg}| j t|| | jdd	d
 W d Q R X W d Q R X | jstt| jd trd}nd}td| jd || jd g| _|   nBt| jd }t| jj|kr`td| j| j|| j t|| _d S )Nz2Running setup.py (path:%s) egg_info for package %sz7Running setup.py (path:%s) egg_info for package from %sz-cz--no-user-cfgegg_infozpip-egg-infoz
--egg-baseFzpython setup.py egg_info)cwdshow_stdoutcommand_descr   z==z===rt   NamezuRunning setup.py (path:%s) egg_info for package %s produced metadata for project name %s. Fix your #egg=%s fragments.)r7   r/   r`   ra   rb   r   r:   r   r!   r   
executablerL   r8   r3   r4   r   r   r   rM   r   r0   r.   parse_versionmetadatar   r   r   r   warning)rR   scriptbase_cmdegg_info_cmdegg_base_optionegg_info_diropmetadata_namer'   r'   r+   run_egg_info  sR    




zInstallRequirement.run_egg_infoc          
      sX  | j d krR| jr| j}ntj| jd}t|}| jrg }xt|D ]\ }}x t	j
D ]}||kr^|| q^W xjt|D ]^}tjtj |ddstjtj |ddr|| q|dks|dkr|| qW | fdd	|D  qLW d
d	 |D }|s td| t|dkr>|jdd d tj||d | _ | j S )Nzpip-egg-infobinpythonScriptsz
Python.exetesttestsc                s   g | ]}t j |qS r'   )r3   r4   r   )r)   dir)r   r'   r+   
<listcomp>  s   z4InstallRequirement.egg_info_path.<locals>.<listcomp>c             S   s   g | ]}| d r|qS )z	.egg-info)endswith)r)   fr'   r'   r+   r     s    z!Files/directories not found in %srj   c             S   s(   |  tjjtjjr"|  tjjp$d S )Nr   )countr3   r4   sepaltsep)xr'   r'   r+   <lambda>!  s   z2InstallRequirement.egg_info_path.<locals>.<lambda>)keyr   )rA   r8   r7   r3   r4   r   r   listdirwalkr$   dirnamesremovelistlexistsr   extendr   rm   sort)rR   base	filenamesdirsfilesr   r'   )r   r+   egg_info_path  s>    

z InstallRequirement.egg_info_pathc             C   s   t | dst|  | _| jS )N	_metadata)hasattrr    get_distr   )rR   r'   r'   r+   r   '  s    
zInstallRequirement.metadatac             C   sX   | j tjj}tj|}t||}tjtj	|d }tj
tj|||dS )zAReturn a pkg_resources.Distribution built from self.egg_info_pathr   )r   r   )r   rstripr3   r4   r   dirnamer   PathMetadatasplitextbasenameDistribution)rR   r   base_dirr   	dist_namer'   r'   r+   r   .  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)	r7   r/   r   r0   ri   ra   r   rb   r   )rR   r   r'   r'   r+   assert_source_matches_version:  s    

z0InstallRequirement.assert_source_matches_versionc             C   s   | j dkr| || _ | j 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)r7   r   )rR   
parent_dirr'   r'   r+   ensure_has_source_dirL  s    

z(InstallRequirement.ensure_has_source_dirc          
   C   s   t d| j | jr"t|dg }|r>d|g}t|| }t N | j< tt	j
dt| j gt| ddg t| | jdd W d Q R X W d Q R X d	| _d S )
NzRunning setup.py develop for %sz--no-user-cfgz--prefix={}z-cdevelopz	--no-depsF)r   r   T)ra   infor`   rL   r   formatr   rM   r   r   r   r!   r   r   rG   )rR   install_optionsglobal_optionsr   prefix_paramr'   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sbd S | j j	dd\}}t
|}|r|| j j}|r|| j q|| j ndstd| j |f d S )Nz>Cannot update repository at %s; repository location is unknownfile+zbad url: %rrj   r   z+Unexpected version control type (in %s): %s)r:   ra   rb   r7   r8   r/   schemer<   rF   splitr$   get_backendobtainexport)rR   r   vc_typer<   r   vcs_backendr'   r'   r+   update_editabley  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   ra   r   r`   rB   rC   r   	from_distr   )rR   auto_confirmverboser   distrH   r'   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 %rrj   /)
startswithr3   r4   r   r/   rm   replace)rR   r`   r   r'   r'   r+   _clean_zip_name  s
    z"InstallRequirement._clean_zip_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 |rtj|dtjdd}tjtj| j}xt|D ]\}	}
}d|
kr |
d xR|
D ]J}tj|	|}| ||}t| jd | d }d|_||d q&W xL|D ]D}|tkrqztj|	|}| ||}||| jd |  qzW qW |  t
 dt	| d S )NTz	%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  )
allowZip64zpip-egg-infor  i  rt   zSaved %s)!r7   r/   r`   r   r3   r4   r   r   r   r   ra   r   r   r   r   r   r   exitzipfileZipFileZIP_DEFLATEDnormcaser6   r   r   r  ZipInfoexternal_attrwritestrr   writecloser   )rR   r   create_archivearchive_namearchive_pathresponse	dest_filezipr   dirpathr   r   r   r`   zipdirfilenamer'   r'   r+   archive  sX    







"zInstallRequirement.archivec	                sr  |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  }| jr|dg }tdd}
tj|
jd	}| || ||}d
| jf }t|@}t . | j t|| | jd|d W d Q R X W d Q R X W d Q R X tj|sRtd| d S d| _	 fdd}t|H}x@|D ](}tj|}|drt||}P qtW td|  d S W d Q R X g }t|P}xH|D ]@}| }tj |r|tjj!7 }|"tj#||| qW W d Q R X |$  t%| tj|d}t|d}|&d|d  W d Q R X W d Q R X d S )N)r   )r   r   r   r   r   r   Tr   r   z--no-user-cfgrecord)r-   zinstall-record.txtzRunning setup.py install for %sF)r   r   spinnerzRecord file %s not foundc                s&    d kst j| s| S t | S d S )N)r3   r4   isabsr   )r4   )r   r'   r+   prepend_root  s    z0InstallRequirement.install.<locals>.prepend_rootz	.egg-infoz;Could not find .egg-info directory in install record for %szinstalled-files.txtr  
)'r8   r   r   r   wheel_versionr7   check_compatibilityr`   r%   rG   r   rI   r_   rL   r"   r3   r4   r   get_install_argsr#   r   rM   r   r   r   ra   rb   openr   r   r   stripisdirr   r   relpathr   r   r  )rR   r   r   r   r   r   r   r   r   r   temp_dirrecord_filenameinstall_argsmsgr   r"  r   line	directoryr   	new_linesr  inst_files_pathr'   )r   r+   install  sz    



&



$zInstallRequirement.installc          	   C   s   t jdg}|d |t| j  |t|dd|g 7 }|dg7 }|d k	rX|d|g7 }|d k	rl|d|g7 }|r||dg7 }n
|d	g7 }t rd
t  }|dt	j
t jdd|| jg7 }|S )Nz-uz-cr3  z--recordz#--single-version-externally-managedz--rootz--prefixz	--compilez--no-compiler   z--install-headersincludesite)r   r   r   r!   r   r   r   	sysconfigget_python_versionr3   r4   r   r   r`   )rR   r   r,  r   r   r   r-  
py_ver_strr'   r'   r+   r&  C  s&    



z#InstallRequirement.get_install_args)
NFNTNFNNFr'   )N)T)NNNTFT)r'   N)T)FFF)NNNNTFT)(r\   
__module____qualname____doc__rT   rZ   r]   rg   propertyr`   ri   rr   rs   ry   r|   rz   rX   r   r   r   r   r   r%   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r3  r&  r'   r'   r'   r+   r&   -   sX      
H



!*  
;4

 

0  
Yr&   )H
__future__r   loggingr3   r   r   r6  r  distutils.utilr   pip._vendorr   r   "pip._vendor.packaging.requirementsr   pip._vendor.packaging.utilsr   Zpip._vendor.packaging.versionr   r	   r   pip._vendor.pep517.wrappersr
   pip._internalr   pip._internal.build_envr   pip._internal.exceptionsr   pip._internal.locationsr   r   pip._internal.models.linkr   pip._internal.pyprojectr   pip._internal.req.req_uninstallr   pip._internal.utils.compatr   pip._internal.utils.hashesr   pip._internal.utils.loggingr   pip._internal.utils.miscr   r   r   r   r   r   r   r   r   r   pip._internal.utils.packagingr    $pip._internal.utils.setuptools_buildr!   pip._internal.utils.temp_dirr"   pip._internal.utils.uir#   pip._internal.vcsr$   pip._internal.wheelr%   	getLoggerr\   ra   objectr&   r'   r'   r'   r+   <module>   s@   0
