U
    @vgHX                     @   s  d Z 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	 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mZmZ dd	lmZmZ dd
lmZ ddlmZ er"ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, ddlm-Z- ddlm.Z. e(e&e/ e&e/ f Z0dgZ1e2e3Z4dd Z5dddZ6dd Z7G dd de8Z9G dd de:Z;G dd de:Z<e< Z=G dd de:Z>dS ) z)Handles all VCS (version control) support    )absolute_importN)pkg_resources)parse)
BadCommand)samefile)ask_path_exists
backup_dirdisplay_pathhide_url
hide_valuermtree)call_subprocessmake_command)MYPY_CHECK_RUNNING)get_url_scheme)AnyDictIterableIteratorListMappingOptionalTextTupleTypeUnion)SpinnerInterface)
HiddenText)CommandArgsvcsc                 C   s*   t | }|dkrdS |ddddgtj kS )z3
    Return true if the name looks like a URL.
    NFhttphttpsfileftp)r   r   all_schemes)namescheme r'   D/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/vcs/versioncontrol.pyis_url.   s    r)   c                 C   s.   t |}d| ||}|r*|d|7 }|S )z
    Return the URL for a VCS requirement.

    Args:
      repo_url: the remote VCS url, with any needed VCS prefix (e.g. "git+").
      project_name: the (unescaped) project name.
    z{}@{}#egg={}z&subdirectory={})r   to_filenameformat)repo_urlrevproject_namesubdiregg_project_namereqr'   r'   r(   make_vcs_requirement_url9   s
    	
r2   c                 C   s`   | }t jt j| dsD| }t j| } | |krtd| dS qt|| rRdS t j| |S )z
    Find the path to `setup.py` by searching up the filesystem from `location`.
    Return the path to `setup.py` relative to `repo_root`.
    Return None if `setup.py` is in `repo_root` or cannot be found.
    zsetup.pyzGCould not find setup.py for directory %s (tried all parent directories)N)	ospathexistsjoindirnameloggerwarningr   relpath)location	repo_rootorig_locationlast_locationr'   r'   r(   !find_path_to_setup_from_repo_rootJ   s    
r?   c                   @   s   e Zd ZdS )RemoteNotFoundErrorN)__name__
__module____qualname__r'   r'   r'   r(   r@   f   s   r@   c                   @   sF   e Zd ZdZdddZdd Zedd Zd	d
 Zdd Z	dd Z
dS )
RevOptionsz
    Encapsulates a VCS-specific revision to install, along with any VCS
    install options.

    Instances of this class should be treated as if immutable.
    Nc                 C   s(   |dkrg }|| _ || _|| _d| _dS )z
        Args:
          vc_class: a VersionControl subclass.
          rev: the name of the revision to install.
          extra_args: a list of extra options.
        N)
extra_argsr-   vc_classbranch_name)selfrF   r-   rE   r'   r'   r(   __init__s   s    zRevOptions.__init__c                 C   s   d | jj| jS )Nz<RevOptions {}: rev={!r}>)r+   rF   r%   r-   rH   r'   r'   r(   __repr__   s    zRevOptions.__repr__c                 C   s   | j d kr| jjS | j S N)r-   rF   default_arg_revrJ   r'   r'   r(   arg_rev   s    
zRevOptions.arg_revc                 C   s0   g }| j }|dk	r"|| j|7 }|| j7 }|S )z<
        Return the VCS-specific command arguments.
        N)rN   rF   get_base_rev_argsrE   )rH   argsr-   r'   r'   r(   to_args   s    
zRevOptions.to_argsc                 C   s   | j s
dS d| j S )N z (to revision {}))r-   r+   rJ   r'   r'   r(   
to_display   s    zRevOptions.to_displayc                 C   s   | j j|| jdS )z
        Make a copy of the current instance, but with a new rev.

        Args:
          rev: the name of the revision for the new object.
        rE   )rF   make_rev_optionsrE   )rH   r-   r'   r'   r(   make_new   s    zRevOptions.make_new)NN)rA   rB   rC   __doc__rI   rK   propertyrN   rQ   rS   rV   r'   r'   r'   r(   rD   j   s   
  

rD   c                       s   e Zd Zi ZddddddgZ fddZd	d
 Zedd Zedd Z	edd Z
dd Zdd Zdd Zdd Zdd Z  ZS )
VcsSupportsshgithgbzrsftpsvnc                    s:   t j| j tt dd r(t j| j tt|   d S )Nuses_fragment)	urllib_parseuses_netlocextendschemesgetattrr`   superrY   rI   rJ   	__class__r'   r(   rI      s    zVcsSupport.__init__c                 C   s
   | j  S rL   )	_registry__iter__rJ   r'   r'   r(   rj      s    zVcsSupport.__iter__c                 C   s   t | j S rL   )listri   valuesrJ   r'   r'   r(   backends   s    zVcsSupport.backendsc                 C   s   dd | j D S )Nc                 S   s   g | ]
}|j qS r'   )r7   ).0backendr'   r'   r(   
<listcomp>   s     z'VcsSupport.dirnames.<locals>.<listcomp>)rm   rJ   r'   r'   r(   dirnames   s    zVcsSupport.dirnamesc                 C   s    g }| j D ]}||j q
|S rL   )rm   rc   rd   )rH   rd   ro   r'   r'   r(   r$      s    
zVcsSupport.all_schemesc                 C   sH   t |dstd|j d S |j| jkrD| | j|j< td|j d S )Nr%   zCannot register VCS %szRegistered VCS backend: %s)hasattrr8   r9   rA   r%   ri   debug)rH   clsr'   r'   r(   register   s    
zVcsSupport.registerc                 C   s   || j kr| j |= d S rL   )ri   rH   r%   r'   r'   r(   
unregister   s    
zVcsSupport.unregisterc                 C   s6   | j  D ]&}||r
td||j |  S q
dS )zv
        Return a VersionControl object if a repository of that type is found
        at the given directory.
        zDetermine that %s uses VCS: %sN)ri   rl   controls_locationr8   rs   r%   )rH   r;   vcs_backendr'   r'   r(   get_backend_for_dir   s    
 
zVcsSupport.get_backend_for_dirc                 C   s&   | j  D ]}||jkr
|  S q
dS )9
        Return a VersionControl object or None.
        N)ri   rl   rd   )rH   r&   ry   r'   r'   r(   get_backend_for_scheme   s    

z!VcsSupport.get_backend_for_schemec                 C   s   |  }| j|S )r{   )lowerri   getrv   r'   r'   r(   get_backend   s    zVcsSupport.get_backend)rA   rB   rC   ri   rd   rI   rj   rX   rm   rq   r$   ru   rw   rz   r|   r   __classcell__r'   r'   rg   r(   rY      s   



	
rY   c                	   @   s@  e Zd ZdZdZdZdZdZdZe	dd Z
e	dd Ze	dd	 Ze	d
d Zedd Zdd Ze	d:ddZe	dd Zdd Ze	dd Ze	dd Zedd Zdd Zedd Ze	d d! Zd"d# Zd$d% Zd&d' Ze	d(d) Zd*d+ Zd,d- Ze	d.d/ Z e	d0d1 Z!e	d;d4d5Z"e	d6d7 Z#e	d8d9 Z$dS )<VersionControlrR   r'   Nc                 C   s   |  d| j S )z
        Return whether the vcs prefix (e.g. "git+") should be added to a
        repository's remote url when used in a requirement.
        z{}:)r}   
startswithr+   r%   )rt   
remote_urlr'   r'   r(   should_add_vcs_url_prefix  s    z(VersionControl.should_add_vcs_url_prefixc                 C   s   dS )z~
        Return the path to setup.py, relative to the repo root.
        Return None if setup.py is in the repo root.
        Nr'   rt   r;   r'   r'   r(   get_subdirectory  s    zVersionControl.get_subdirectoryc                 C   s
   |  |S )zR
        Return the revision string that should be used in a requirement.
        )get_revision)rt   repo_dirr'   r'   r(   get_requirement_revision$  s    z'VersionControl.get_requirement_revisionc                 C   sV   |  |}|dkrdS | |r.d| j|}| |}| |}t||||d}|S )aC  
        Return the requirement string to use to redownload the files
        currently at the given repository directory.

        Args:
          project_name: the (unescaped) project name.

        The return value has a form similar to the following:

            {repository_url}@{revision}#egg={project_name}
        Nz{}+{})r/   )get_remote_urlr   r+   r%   r   r   r2   )rt   r   r.   r,   revisionr/   r1   r'   r'   r(   get_src_requirement,  s    



z"VersionControl.get_src_requirementc                 C   s   t dS )z
        Return the base revision arguments for a vcs command.

        Args:
          rev: the name of a revision to install.  Cannot be None.
        NNotImplementedError)r-   r'   r'   r(   rO   H  s    	z VersionControl.get_base_rev_argsc                 C   s   dS )aZ  
        Return true if the commit hash checked out at dest matches
        the revision in url.

        Always return False, if the VCS does not support immutable commit
        hashes.

        This method does not check if there are local uncommitted changes
        in dest after checkout, as pip currently has no use case for that.
        Fr'   )rH   urldestr'   r'   r(   is_immutable_rev_checkoutS  s    z(VersionControl.is_immutable_rev_checkoutc                 C   s   t | ||dS )z
        Return a RevOptions object.

        Args:
          rev: the name of a revision to install.
          extra_args: a list of extra options.
        rT   )rD   )rt   r-   rE   r'   r'   r(   rU   a  s    
zVersionControl.make_rev_optionsc                 C   s&   t j|\}}|t jjp$t|S )zy
           posix absolute paths start with os.path.sep,
           win32 ones start with drive (like c:\folder)
        )r3   r4   
splitdriver   sepbool)rt   repodrivetailr'   r'   r(   _is_local_repositorym  s    z#VersionControl._is_local_repositoryc                 C   s   t dS )z
        Export the repository at the url to the destination location
        i.e. only download the files, without vcs informations

        :param url: the repository URL starting with a vcs prefix.
        Nr   rH   r;   r   r'   r'   r(   exportw  s    zVersionControl.exportc                 C   s   |dfS )aZ  
        Parse the repository URL's netloc, and return the new netloc to use
        along with auth information.

        Args:
          netloc: the original repository URL netloc.
          scheme: the repository URL's scheme without the vcs prefix.

        This is mainly for the Subversion class to override, so that auth
        information can be provided via the --username and --password options
        instead of through the URL.  For other subclasses like Git without
        such an option, auth information must stay in the URL.

        Returns: (netloc, (username, password)).
        )NNr'   )rt   netlocr&   r'   r'   r(   get_netloc_and_auth  s    z"VersionControl.get_netloc_and_authc           	      C   s   t |\}}}}}d|kr*td||ddd }| ||\}}d}d|krf|dd\}}t ||||df}|||fS )z
        Parse the repository URL to use, and return the URL, revision,
        and auth info to use.

        Returns: (url, rev, (username, password)).
        +zvSorry, {!r} is a malformed VCS url. The format is <vcs>+<protocol>://<url>, e.g. svn+http://myrepo/svn/MyApp#egg=MyApp   N@rR   )ra   urlsplit
ValueErrorr+   splitr   rsplit
urlunsplit)	rt   r   r&   r   r4   queryfrag	user_passr-   r'   r'   r(   get_url_rev_and_auth  s    	z#VersionControl.get_url_rev_and_authc                 C   s   g S )zM
        Return the RevOptions "extra arguments" to use in obtain().
        r'   )usernamepasswordr'   r'   r(   make_rev_args  s    zVersionControl.make_rev_argsc           
      C   sT   |  |j\}}}|\}}d}|dk	r.t|}| ||}| j||d}	t||	fS )z
        Return the URL and RevOptions object to use in obtain() and in
        some cases export(), as a tuple (url, rev_options).
        NrT   )r   secretr   r   rU   r
   )
rH   r   
secret_urlr-   r   r   secret_passwordr   rE   rev_optionsr'   r'   r(   get_url_rev_options  s    z"VersionControl.get_url_rev_optionsc                 C   s   t | dS )zi
        Normalize a URL for comparison by unquoting it and removing any
        trailing slash.
        /)ra   unquoterstripr   r'   r'   r(   normalize_url  s    zVersionControl.normalize_urlc                 C   s   |  ||  |kS )zV
        Compare two repo URLs for identity, ignoring incidental differences.
        )r   )rt   url1url2r'   r'   r(   compare_urls  s    zVersionControl.compare_urlsc                 C   s   t dS )z
        Fetch a revision from a repository, in the case that this is the
        first fetch from the repository.

        Args:
          dest: the directory to fetch the repository to.
          rev_options: a RevOptions object.
        Nr   rH   r   r   r   r'   r'   r(   	fetch_new  s    
zVersionControl.fetch_newc                 C   s   t dS )z}
        Switch the repo at ``dest`` to point to ``URL``.

        Args:
          rev_options: a RevOptions object.
        Nr   r   r'   r'   r(   switch  s    zVersionControl.switchc                 C   s   t dS )z
        Update an already-existing repo to the given ``rev_options``.

        Args:
          rev_options: a RevOptions object.
        Nr   r   r'   r'   r(   update  s    zVersionControl.updatec                 C   s   t dS )z
        Return whether the id of the current commit equals the given name.

        Args:
          dest: the repository directory.
          name: a string name.
        Nr   )rt   r   r%   r'   r'   r(   is_commit_id_equal  s    
z!VersionControl.is_commit_id_equalc           	      C   s  |  |\}}tj|s,| ||| dS | }| |r| |}| ||j	rt
d| j t|| | ||jst
dt|| j| | ||| n
t
d dS t
d| j| jt|| d}nt
d|| j| j d}t
d	| j| td
|d  |d }|dkr$td |dkrXt
dt| t| | ||| dS |dkrt|}t
dt|| t|| | ||| dS |dkrt
d| jt||| | ||| dS )a  
        Install or update in editable mode the package represented by this
        VersionControl object.

        :param dest: the repository directory in which to install or update.
        :param url: the repository URL starting with a vcs prefix.
        Nz)%s in %s exists, and has correct URL (%s)zUpdating %s %s%sz$Skipping because already up-to-date.z%s %s in %s exists with URL %s)z%(s)witch, (i)gnore, (w)ipe, (b)ackup )siwbz0Directory %s already exists, and is not a %s %s.)z(i)gnore, (w)ipe, (b)ackup )r   r   r   z+The plan is to install the %s repository %szWhat to do?  %sr   r   ar   zDeleting %sr   zBacking up %s to %sr   zSwitching %s %s to %s%s)r   r3   r4   r5   r   rS   is_repository_directoryr   r   r   r8   rs   	repo_nametitler	   r   r-   infor   r9   r%   r   sysexitr   r   shutilmover   )	rH   r   r   r   rev_displayexisting_urlpromptresponsedest_dirr'   r'   r(   obtain  s    	






  
zVersionControl.obtainc                 C   s&   t j|rt| | j||d dS )z
        Clean up current location and download the url repository
        (and vcs infos) into location

        :param url: the repository URL starting with a vcs prefix.
        r   N)r3   r4   r5   r   r   r   r'   r'   r(   unpack`  s    zVersionControl.unpackc                 C   s   t dS )z
        Return the url used at location

        Raises RemoteNotFoundError if the repository does not have a remote
        url configured.
        Nr   r   r'   r'   r(   r   l  s    	zVersionControl.get_remote_urlc                 C   s   t dS )zR
        Return the current commit id of the files at the given location.
        Nr   r   r'   r'   r(   r   w  s    zVersionControl.get_revisionTraisec
                 C   s|   t | jf| }z t|||||||| j||	d
W S  tk
rv }
 z(|
jtjkrdtd| j| jf n W 5 d}
~
X Y nX dS )z
        Run a VCS subcommand
        This is simply a wrapper around call_subprocess that adds the VCS
        command name, and checks that the VCS is available
        )on_returncodeextra_ok_returncodescommand_descextra_environunset_environspinnerlog_failed_cmdzCCannot find command %r - do you have %r installed and in your PATH?N)r   r%   r   r   OSErrorerrnoENOENTr   )rt   cmdshow_stdoutcwdr   r   r   r   r   r   er'   r'   r(   run_command  s&    
zVersionControl.run_commandc                 C   s,   t d|| j| j tjtj|| jS )zL
        Return whether a directory path is a repository directory.
        zChecking in %s for %s (%s)...)r8   rs   r7   r%   r3   r4   r5   r6   )rt   r4   r'   r'   r(   r     s      z&VersionControl.is_repository_directoryc                 C   s
   |  |S )a6  
        Check if a location is controlled by the vcs.
        It is meant to be overridden to implement smarter detection
        mechanisms for specific vcs.

        This can do more than is_repository_directory() alone.  For example,
        the Git override checks that Git is actually available.
        )r   r   r'   r'   r(   rx     s    z VersionControl.controls_location)NN)TNr   NNNNT)%rA   rB   rC   r%   r7   r   rd   r   rM   classmethodr   r   r   r   staticmethodrO   r   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rx   r'   r'   r'   r(   r     st   






	








]


        '
	r   )N)?rW   
__future__r   r   loggingr3   r   r   pip._vendorr   Zpip._vendor.six.moves.urllibr   ra   pip._internal.exceptionsr   pip._internal.utils.compatr   pip._internal.utils.miscr   r   r	   r
   r   r   pip._internal.utils.subprocessr   r   pip._internal.utils.typingr   pip._internal.utils.urlsr   typingr   r   r   r   r   r   r   r   r   r   r   pip._internal.utils.uir   r   r   strZAuthInfo__all__	getLoggerrA   r8   r)   r2   r?   	Exceptionr@   objectrD   rY   r   r   r'   r'   r'   r(   <module>   s<    4

IR