U
    
Ha@                     @   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l3m4Z4m5Z5m6Z6 ddl7m8Z8 e9e:Z;G dd deZ<G dd dee<Z=e6j>e6j?e6j@gZAddddZBe	e	d d!d"ZCG d#d$ d$e=ZDd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)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    d | _d S N)super__init___session)self	__class__ A/tmp/pip-unpacked-wheel-tx790h60/pip/_internal/cli/req_command.pyr&   5   s    
zSessionCommandMixin.__init__optionsr#   c                 C   sL   g }t |dds*t |dd}|r*|| t |dd}|rD|| |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_urls9   s    

z#SessionCommandMixin._get_index_urlsc                 C   s0   | j dkr*| | || _ | 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_sessionG   s    
z'SessionCommandMixin.get_default_session)r.   retriestimeoutr#   c                 C   s   |j rtj|j stt|j r0tj|j dnd |d k	r>|n|j|j| 	|d}|j
rb|j
|_|jrp|j|_
|jsz|r|d k	r|n|j|_|jr|j|jd|_|j |j_|S )Nhttp)cacher>   trusted_hostsr6   )r@   https)	cache_dirospathisabsr<   r   joinr>   rB   r9   certverifyclient_certr?   proxyproxiesno_inputauth	prompting)r(   r.   r>   r?   sessionr+   r+   r,   r;   Q   s&    

z"SessionCommandMixin._build_session)NN)__name__
__module____qualname____doc__r&   classmethodr   r   r   strr9   r   r=   intr;   __classcell__r+   r+   r)   r,   r!   /   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.
    Nr-   c              	   C   sT   t |dst|js|jrdS | j|dtd|jd}| t|| W 5 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?   )hasattrr<   disable_pip_version_checkr/   r;   minr?   r   )r(   r.   rQ   r+   r+   r,   handle_pip_version_check   s      
z*IndexGroupCommand.handle_pip_version_check)rR   rS   rT   rU   r   r_   r+   r+   r+   r,   rZ   z   s   rZ   r"   c                   C   sb   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sDtjdkrTt dkrTdS td dS )	zOutput 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cygwindarwinlinuxr   zRunning 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    r\   rE   sysplatformr`   loggerwarningr+   r+   r+   r,   warn_if_run_as_root   s    
ri   )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   t D ]}| |d qd S )NF)KEEPABLE_TEMPDIR_TYPES
set_delete)rk   tr+   r+   r,   configure_tempdir_registry   s    z0with_cleanup.<locals>.configure_tempdir_registry)r(   r.   argsr#   c                    sR   | j d k	st|jr | j  z| ||W S  tk
rL    | j   Y nX d S r$   )tempdir_registryr<   no_cleanr   )r(   r.   rp   ro   rj   r+   r,   wrapper   s    

zwith_cleanup.<locals>.wrapper)r   RequirementCommandr   r   r   r   rX   )rj   rt   r+   rs   r,   with_cleanup   s      rv   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 )ru   N)rp   kwr#   c                    s"   t  j|| | jt  d S r$   )r%   r&   cmd_opts
add_optionr	   rr   )r(   rp   rw   r)   r+   r,   r&      s    zRequirementCommand.__init__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legacy2020-resolver)Zdeprecated_features_enabledr.   r+   r+   r,   determine_resolver_variant   s    
z-RequirementCommand.determine_resolver_variant)temp_build_dirr.   req_trackerrQ   finderuse_user_sitedownload_dirr#   c                 C   s   |j }|dk	st| |}	|	dkr>d|jk}
|
rVtd nd}
d|jkrVtd t||j||j|||j	||j
||
d|jkdS )	zQ
        Create a RequirementPreparer instance for the given parameters.
        Nr{   z	fast-depszpip 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in-tree-build)	build_dirsrc_dirr   build_isolationr   rQ   progress_barr   require_hashesr   
lazy_wheelZin_tree_build)rF   r<   r}   Zfeatures_enabledrg   rh   r   r   r   r   r   )r5   r~   r.   r   rQ   r   r   r   temp_build_dir_pathresolver_variantr   r+   r+   r,   make_requirement_preparer   s8    


z,RequirementCommand.make_requirement_preparerFTto-satisfy-only.)preparerr   r.   wheel_cacher   ignore_installedignore_requires_pythonforce_reinstallupgrade_strategy
use_pep517py_version_infor#   c                 C   s   t t|j|
d}| |}|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   r{   r   N)r   r   r   make_install_reqr   ignore_dependenciesr   r   r   r   r   )r   r   isolated_moder}   Z,pip._internal.resolution.resolvelib.resolver	_internal
resolutionZ
resolvelibresolverResolverr   Z(pip._internal.resolution.legacy.resolverrz   )r5   r   r   r.   r   r   r   r   r   r   r   r   r   r   pipr+   r+   r,   make_resolver  sF    
z RequirementCommand.make_resolver)rp   r.   r   rQ   r#   c                 C   sL  g }|j D ]6}t|d|||dD ]}t||jdd}|| q q
|D ]$}	t|	d|j|jdd}|| qF|jD ]"}	t|	d|j|jd}|| qr|j	D ]8}t||||dD ]"}t||j|jdd}|| qqt
d	d
 |D rd|_|sH|jsH|j	sHd| ji}
|jr8tdjf t|
d|jdntdjf |
|S )zS
        Parse command-line arguments into the corresponding requirements.
        T)
constraintr   r.   rQ   F)r   user_suppliedN)r   r   r   )r   r   r   )r   r.   rQ   c                 s   s   | ]}|j V  qd S r$   )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   r3   r   r   	editablesr   requirementsanyr   r   
find_linksr   formatdictrH   )r(   rp   r.   r   rQ   r   filenameZ
parsed_req
req_to_addr   optsr+   r+   r,   get_requirementsQ  s|    




   

z#RequirementCommand.get_requirements)r   r#   c                 C   s    | j }| }|rt| dS )zE
        Trace basic information about the provided objects.
        N)search_scopeget_formatted_locationsrg   info)r   r   	locationsr+   r+   r,   trace_basic_info  s    z#RequirementCommand.trace_basic_info)r.   rQ   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)allow_yankedformat_controlallow_all_prereleasesprefer_binaryr   )link_collectorselection_prefsr   )r   creater   r   prer   r   )r(   r.   rQ   r   r   r   r   r+   r+   r,   _build_package_finder  s    z(RequirementCommand._build_package_finder)N)NFTFFr   NN)NN)rR   rS   rT   r   r&   staticmethodr   rW   r}   rV   r   r   r   r   boolr   r   r   r   r   rX   r   r   r   r   r   r   r   r   rY   r+   r+   r)   r,   ru      sn   	 1        <O  ru   )ErU   loggingrE   re   	functoolsr   optparser   typingr   r   r   r   pip._internal.cacher   Zpip._internal.clir	   pip._internal.cli.base_commandr
   !pip._internal.cli.command_contextr   pip._internal.exceptionsr   r   pip._internal.index.collectorr   "pip._internal.index.package_finderr   $pip._internal.models.selection_prefsr   "pip._internal.models.target_pythonr   pip._internal.network.sessionr    pip._internal.operations.preparer   pip._internal.req.constructorsr   r   r   r   Zpip._internal.req.req_filer   pip._internal.req.req_installr   pip._internal.req.req_trackerr   Zpip._internal.resolution.baser   !pip._internal.self_outdated_checkr   pip._internal.utils.temp_dirr   r   r   pip._internal.utils.virtualenvr    	getLoggerrR   rg   r!   rZ   Z	BUILD_ENVZEPHEM_WHEEL_CACHEZ	REQ_BUILDrl   ri   rv   ru   r+   r+   r+   r,   <module>   sF   
K