3
ûâdeÉB  ã               @   sÂ  d 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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'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8 ddl9m:Z: ej;e<ƒZ=G dd„ deƒZ>G dd„ dee>ƒZ?e8j@e8jAe8jBgZCddœdd „ZDe	e	d!œd"d#„ZEG d$d%„ d%e?ƒZFdS )&a  Contains the Command base classes that depend on PipSession.

The classes in this module are in a separate module so the commands not
needing download / PackageFinder capability don't unnecessarily import the
PackageFinder machinery and all its vendored dependencies, etc.
é    N)Úpartial)ÚValues)ÚAnyÚListÚOptionalÚTuple)Ú
WheelCache)Ú
cmdoptions)ÚCommand)ÚCommandContextMixIn)ÚCommandErrorÚPreviousBuildDirError)ÚLinkCollector)ÚPackageFinder)ÚSelectionPreferences)ÚTargetPython)Ú
PipSession)ÚRequirementPreparer)Úinstall_req_from_editableÚinstall_req_from_lineÚ#install_req_from_parsed_requirementÚinstall_req_from_req_string)Úparse_requirements)ÚInstallRequirement)ÚRequirementTracker)ÚBaseResolver)Úpip_self_version_check)Ú
deprecated)ÚTempDirectoryÚTempDirectoryTypeRegistryÚtempdir_kinds)Úrunning_under_virtualenvc                   sp   e Zd ZdZddœ‡ fdd„Zeeeee	  dœdd„ƒZ
eedœd	d
„Zdeee ee edœdd„Z‡  ZS )ÚSessionCommandMixinzE
    A class mixin for command classes needing _build_session().
    N)Úreturnc                s   t ƒ jƒ  d | _d S )N)ÚsuperÚ__init__Ú_session)Úself)Ú	__class__© ú</tmp/pip-build-red6nood/pip/pip/_internal/cli/req_command.pyr%   6   s    
zSessionCommandMixin.__init__)Úoptionsr#   c             C   sL   g }t |ddƒs*t |ddƒ}|r*|j|ƒ t |ddƒ}|rD|j|ƒ |pJdS )z7Return a list of index urls from user-provided options.Úno_indexFÚ	index_urlNÚextra_index_urls)ÚgetattrÚappendÚextend)Úclsr+   Ú
index_urlsÚurlÚurlsr)   r)   r*   Ú_get_index_urls:   s    

z#SessionCommandMixin._get_index_urlsc             C   s0   | j dkr*| j| j|ƒƒ| _ | j dk	s*t‚| j S )zGet a default-managed session.N)r&   Úenter_contextÚ_build_sessionÚAssertionError)r'   r+   r)   r)   r*   Úget_default_sessionH   s    
z'SessionCommandMixin.get_default_session)r+   ÚretriesÚtimeoutr#   c             C   s¶   |j  stjj|j ƒst‚t|j r2tjj|j dƒnd |d k	r@|n|j|j| j	|ƒd�}|j
rd|j
|_|jrr|j|_
|js||r�|d k	rˆ|n|j|_|jr¦|j|jdœ|_|j |j_|S )NÚhttp)Úcacher;   Útrusted_hostsr3   )r=   Úhttps)Ú	cache_dirÚosÚpathÚisabsr9   r   Újoinr;   r?   r6   ÚcertÚverifyÚclient_certr<   ÚproxyÚproxiesÚno_inputÚauthÚ	prompting)r'   r+   r;   r<   Úsessionr)   r)   r*   r8   R   s"    
z"SessionCommandMixin._build_session)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   Úclassmethodr   r   r   Ústrr6   r   r:   Úintr8   Ú__classcell__r)   r)   )r(   r*   r"   0   s    r"   c               @   s    e Zd ZdZeddœdd„ZdS )ÚIndexGroupCommandz–
    Abstract base class for commands with the index_group options.

    This also corresponds to the commands that permit the pip version check.
    N)r+   r#   c          
   C   sT   t |dƒst‚|js|jrdS | j|dtd|jƒd�}|� t||ƒ W dQ R X dS )z€
        Do the pip version check if not disabled.

        This overrides the default behavior of not doing the check.
        r,   Nr   é   )r;   r<   )Úhasattrr9   Údisable_pip_version_checkr,   r8   Úminr<   r   )r'   r+   rN   r)   r)   r*   Úhandle_pip_version_checkƒ   s    z*IndexGroupCommand.handle_pip_version_check)rO   rP   rQ   rR   r   r\   r)   r)   r)   r*   rW   {   s   rW   )r#   c               C   sN   t ƒ r
dS ttdƒsdS tjdks,tjdkr0dS tjƒ dkr@dS tjdƒ dS )züOutput a warning for sudo users on Unix.

    In a virtual environment, sudo pip still writes to virtualenv.
    On Windows, users may run pip as Administrator without issues.
    This warning only applies to Unix root users outside of virtualenv.
    NÚgetuidÚwin32Úcygwinr   zÔRunning pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv)r!   rY   rB   ÚsysÚplatformr]   ÚloggerÚwarningr)   r)   r)   r*   Úwarn_if_run_as_rootž   s    
rd   )Úfuncr#   c                s6   t ddœdd„‰ tttt tt dœ‡ ‡fdd„}|S )zNDecorator for common logic related to managing temporary
    directories.
    N)Úregistryr#   c             S   s   xt D ]}| j|dƒ qW d S )NF)ÚKEEPABLE_TEMPDIR_TYPESZ
set_delete)rf   Útr)   r)   r*   Úconfigure_tempdir_registryÁ   s    
z0with_cleanup.<locals>.configure_tempdir_registry)r'   r+   Úargsr#   c                sP   | j d k	st‚|jrˆ | j ƒ yˆ| ||ƒS  tk
rJ   ˆ | j ƒ ‚ Y nX d S )N)Ztempdir_registryr9   Úno_cleanr   )r'   r+   rj   )ri   re   r)   r*   ÚwrapperÅ   s    

zwith_cleanup.<locals>.wrapper)r   ÚRequirementCommandr   r   r   r   rU   )re   rl   r)   )ri   re   r*   Úwith_cleanup¼   s    
rn   c                   sð   e Zd Zeeddœ‡ fdd„Zeeedœdd„ƒZe	de
eeeeeee edœd	d
„ƒZe	deeeee eeeeeee eeedf  edœdd„ƒZee eeeee dœdd„Zeeddœdd„ƒZdeeee ee edœdd„Z‡  ZS )rm   N)rj   Úkwr#   c                s"   t ƒ j||Ž | jjtjƒ ƒ d S )N)r$   r%   Úcmd_optsÚ
add_optionr	   rk   )r'   rj   ro   )r(   r)   r*   r%   Ù   s    zRequirementCommand.__init__)r+   r#   c             C   s   d| j krdS dS )zEDetermines which resolver should be used, based on the given options.zlegacy-resolverÚlegacyz2020-resolver)Údeprecated_features_enabled)r+   r)   r)   r*   Údetermine_resolver_variantÞ   s    
z-RequirementCommand.determine_resolver_variant)Útemp_build_dirr+   Úreq_trackerrN   ÚfinderÚuse_user_siteÚdownload_dirr#   c             C   s¸   |j }|dk	st‚| j|ƒ}	|	dkr>d|jk}
|
rVtjdƒ nd}
d|jkrVtjdƒ d|jk}d|jkrxtd	d
dd� d|jkr�tdddd� t||j	||j
|||j||j||
|d�S )zQ
        Create a RequirementPreparer instance for the given parameters.
        Nz2020-resolverz	fast-depszÊpip is using lazily downloaded wheels using HTTP range requests to obtain dependency information. This experimental feature is enabled through --use-feature=fast-deps and it is not ready for production.Fz;fast-deps has no effect when used with the legacy resolver.zout-of-tree-buildzin-tree-buildz#In-tree builds are now the default.z.to remove the --use-feature=in-tree-build flagz22.1)ÚreasonÚreplacementZgone_inz"Out-of-tree builds are deprecated.)Ú	build_dirÚsrc_dirry   Úbuild_isolationrv   rN   Úprogress_barrw   Úrequire_hashesrx   Ú
lazy_wheelÚin_tree_build)rC   r9   rt   Zfeatures_enabledrb   rc   rs   r   r   r}   r~   r   r€   )r2   ru   r+   rv   rN   rw   rx   ry   Ztemp_build_dir_pathÚresolver_variantr�   r‚   r)   r)   r*   Úmake_requirement_prepareræ   sH    





z,RequirementCommand.make_requirement_preparerFTúto-satisfy-only.)Úpreparerrw   r+   Úwheel_cacherx   Úignore_installedÚignore_requires_pythonÚforce_reinstallÚupgrade_strategyÚ
use_pep517Úpy_version_infor#   c             C   s†   t t|j|
d�}| j|ƒ}|dkrTddl}|jjjjj	||||||j
||||	|d�S ddl}|jjjjj	||||||j
||||	|d�S )zF
        Create a Resolver instance for the given parameters.
        )ÚisolatedrŒ   z2020-resolverr   N)r†   rw   r‡   Úmake_install_reqrx   Úignore_dependenciesrˆ   r‰   rŠ   r‹   r�   )r   r   Úisolated_modert   Z,pip._internal.resolution.resolvelib.resolverZ	_internalÚ
resolutionZ
resolvelibÚresolverZResolverr�   Z(pip._internal.resolution.legacy.resolverrr   )r2   r†   rw   r+   r‡   rx   rˆ   r‰   rŠ   r‹   rŒ   r�   r�   rƒ   Úpipr)   r)   r*   Úmake_resolver&  s@    
z RequirementCommand.make_resolver)rj   r+   rw   rN   r#   c             C   sf  g }xD|j D ]:}x4t|d|||d�D ]}t||jdd�}|j|ƒ q$W qW x,|D ]$}	t|	d|j|jdd�}|j|ƒ qPW x,|jD ]"}	t|	d|j|jd�}|j|ƒ q€W xF|j	D ]<}x6t||||d�D ]"}t||j|jdd�}|j|ƒ qÄW q®W t
d	d
„ |D ƒƒ�rd|_|�p|j�p|j	�sbd| ji}
|j�rRtdjf t|
dj|jƒd�Žƒ‚ntdjf |
Žƒ‚|S )zS
        Parse command-line arguments into the corresponding requirements.
        T)Ú
constraintrw   r+   rN   F)rŽ   Úuser_suppliedN)rŽ   rŒ   r—   )r—   rŽ   rŒ   )rw   r+   rN   c             s   s   | ]}|j V  qd S )N)Úhas_hash_options)Ú.0Úreqr)   r)   r*   ú	<genexpr>œ  s    z6RequirementCommand.get_requirements.<locals>.<genexpr>ÚnamezXYou must give at least one requirement to {name} (maybe you meant "pip {name} {links}"?)ú )ÚlinkszHYou must give at least one requirement to {name} (see "pip help {name}"))Úconstraintsr   r   r‘   r0   r   rŒ   Ú	editablesr   ÚrequirementsÚanyr€   rœ   Ú
find_linksr   ÚformatÚdictrE   )r'   rj   r+   rw   rN   r¡   ÚfilenameZ
parsed_reqZ
req_to_addrš   Úoptsr)   r)   r*   Úget_requirementsa  sb    



z#RequirementCommand.get_requirements)rw   r#   c             C   s    | j }|jƒ }|rtj|ƒ dS )zE
        Trace basic information about the provided objects.
        N)Úsearch_scopeZget_formatted_locationsrb   Úinfo)rw   r©   Ú	locationsr)   r)   r*   Útrace_basic_info°  s    z#RequirementCommand.trace_basic_info)r+   rN   Útarget_pythonr‰   r#   c             C   s6   t j||d�}td|j|j|j|d�}tj|||d�S )zÛ
        Create a package finder appropriate to this requirement command.

        :param ignore_requires_python: Whether to ignore incompatible
            "Requires-Python" values in links. Defaults to False.
        )r+   T)Zallow_yankedÚformat_controlÚallow_all_prereleasesÚprefer_binaryr‰   )Úlink_collectorÚselection_prefsr­   )r   Úcreater   r®   Úprer°   r   )r'   r+   rN   r­   r‰   r±   r²   r)   r)   r*   Ú_build_package_finder»  s    z(RequirementCommand._build_package_finder)N)NFTFFr…   NN)NN)rO   rP   rQ   r   r%   Ústaticmethodr   rT   rt   rS   r   r   r   r   Úboolr   r   r„   r   r   rU   r   r•   r   r   r¨   r¬   r   rµ   rV   r)   r)   )r(   r*   rm   Ø   s4   	6       ,/I rm   )GrR   ÚloggingrB   r`   Ú	functoolsr   Úoptparser   Útypingr   r   r   r   Zpip._internal.cacher   Zpip._internal.clir	   Zpip._internal.cli.base_commandr
   Z!pip._internal.cli.command_contextr   Zpip._internal.exceptionsr   r   Zpip._internal.index.collectorr   Z"pip._internal.index.package_finderr   Z$pip._internal.models.selection_prefsr   Z"pip._internal.models.target_pythonr   Zpip._internal.network.sessionr   Z pip._internal.operations.preparer   Zpip._internal.req.constructorsr   r   r   r   Zpip._internal.req.req_filer   Zpip._internal.req.req_installr   Zpip._internal.req.req_trackerr   Zpip._internal.resolution.baser   Z!pip._internal.self_outdated_checkr   Zpip._internal.utils.deprecationr   Zpip._internal.utils.temp_dirr   r   r    Zpip._internal.utils.virtualenvr!   Ú	getLoggerrO   rb   r"   rW   Z	BUILD_ENVZEPHEM_WHEEL_CACHEZ	REQ_BUILDrg   rd   rn   rm   r)   r)   r)   r*   Ú<module>   sF   
K