3
ÀÞL]BJ  ã               @   s8  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mZmZmZ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  dd
l!m"Z" eee# ee# f Z$dgZ%ej&e'ƒZ(ddd„Z)G dd„ de*ƒZ+G dd„ de,ƒZ-G dd„ de,ƒZ.e.ƒ Z/G dd„ de,ƒZ0dS )z)Handles all VCS (version control) supporté    )Úabsolute_importN)Úpkg_resources)Úparse)Ú
BadCommand)Úask_path_existsÚ
backup_dirÚcall_subprocessÚdisplay_pathÚrmtree)ÚMYPY_CHECK_RUNNING)	ÚAnyÚDictÚIterableÚListÚMappingÚOptionalÚTextÚTupleÚType)ÚSpinnerInterfaceÚvcsc             C   s.   t j|ƒ}dj| ||ƒ}|r*|dj|ƒ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Úreq© r   ú?/tmp/pip-build-69lrc5qx/pip/pip/_internal/vcs/versioncontrol.pyÚmake_vcs_requirement_url!   s
    
r!   c               @   s   e Zd ZdS )ÚRemoteNotFoundErrorN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r    r"   1   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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)Úselfr(   r   r'   r   r   r    Ú__init__>   s
    zRevOptions.__init__c             C   s   dj | jj| jƒS )Nz<RevOptions {}: rev={!r}>)r   r(   Únamer   )r)   r   r   r    Ú__repr__R   s    zRevOptions.__repr__c             C   s   | j d kr| jjS | j S )N)r   r(   Údefault_arg_rev)r)   r   r   r    Úarg_revU   s    
zRevOptions.arg_revc             C   s0   g }| j }|dk	r"|| jj|ƒ7 }|| j7 }|S )z<
        Return the VCS-specific command arguments.
        N)r.   r(   Úget_base_rev_argsr'   )r)   Úargsr   r   r   r    Úto_args]   s    
zRevOptions.to_argsc             C   s   | j s
dS dj| j ƒS )NÚ z (to revision {}))r   r   )r)   r   r   r    Ú
to_displayj   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.
        )r'   )r(   Úmake_rev_optionsr'   )r)   r   r   r   r    Úmake_newq   s    zRevOptions.make_new)NN)r#   r$   r%   Ú__doc__r*   r,   Úpropertyr.   r1   r3   r5   r   r   r   r    r&   5   s    
r&   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‡  ZS )Ú
VcsSupportÚsshÚgitÚhgÚbzrÚsftpÚsvnc                s:   t jj| jƒ tt dd ƒr(t jj| jƒ tt| ƒjƒ  d S )NÚuses_fragment)	Úurllib_parseÚuses_netlocÚextendÚschemesÚgetattrr?   Úsuperr8   r*   )r)   )Ú	__class__r   r    r*   €   s    zVcsSupport.__init__c             C   s
   | j jƒ S )N)Ú	_registryÚ__iter__)r)   r   r   r    rH   Š   s    zVcsSupport.__iter__c             C   s   t | jjƒ ƒS )N)ÚlistrG   Úvalues)r)   r   r   r    Úbackends�   s    zVcsSupport.backendsc             C   s   dd„ | j D ƒS )Nc             S   s   g | ]
}|j ‘qS r   )Údirname)Ú.0Úbackendr   r   r    ú
<listcomp>•   s    z'VcsSupport.dirnames.<locals>.<listcomp>)rK   )r)   r   r   r    Údirnames’   s    zVcsSupport.dirnamesc             C   s$   g }x| j D ]}|j|jƒ qW |S )N)rK   rB   rC   )r)   rC   rN   r   r   r    Úall_schemes—   s    zVcsSupport.all_schemesc             C   sH   t |dƒstjd|jƒ d S |j| jkrD|ƒ | j|j< tjd|jƒ d S )Nr+   zCannot register VCS %szRegistered VCS backend: %s)ÚhasattrÚloggerÚwarningr#   r+   rG   Údebug)r)   Úclsr   r   r    ÚregisterŸ   s    
zVcsSupport.registerc             C   s   || j kr| j |= d S )N)rG   )r)   r+   r   r   r    Ú
unregister¨   s    
zVcsSupport.unregisterc             C   s6   x0| j jƒ D ]"}|j|ƒrtjd||jƒ |S qW 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)rG   rJ   Úcontrols_locationrS   rU   r+   )r)   ÚlocationÚvcs_backendr   r   r    Úget_backend_for_dir­   s    

zVcsSupport.get_backend_for_dirc             C   s   |j ƒ }| jj|ƒS )z9
        Return a VersionControl object or None.
        )ÚlowerrG   Úget)r)   r+   r   r   r    Úget_backendº   s    zVcsSupport.get_backend)r#   r$   r%   rG   rC   r*   rH   r7   rK   rP   rQ   rW   rX   r\   r_   Ú__classcell__r   r   )rF   r    r8   |   s   
	r8   c               @   s8  e Zd ZdZdZdZf Zf ZdZe	dd„ ƒZ
e	dd„ ƒZe	dd„ ƒZe	d	d
„ ƒZedd„ ƒZe	d7d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	d-d.„ ƒZ e	d8d1d2„ƒZ!e	d3d4„ ƒZ"e	d5d6„ ƒZ#dS )9ÚVersionControlr2   Nc             C   s   |j ƒ jdj| 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+   )rV   Z
remote_urlr   r   r    Úshould_add_vcs_url_prefixÐ   s    z(VersionControl.should_add_vcs_url_prefixc             C   s   dS )zI
        Return the path to setup.py, relative to the repo root.
        Nr   )rV   Úrepo_dirr   r   r    Úget_subdirectoryØ   s    zVersionControl.get_subdirectoryc             C   s
   | j |ƒS )zR
        Return the revision string that should be used in a requirement.
        )Úget_revision)rV   rd   r   r   r    Úget_requirement_revisionß   s    z'VersionControl.get_requirement_revisionc             C   sV   | j |ƒ}|dkrdS | j|ƒr.dj| j|ƒ}| j|ƒ}| 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_urlrc   r   r+   rg   re   r!   )rV   rd   r   r   Úrevisionr   r   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    r/     s    z VersionControl.get_base_rev_argsc             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.
        )r'   )r&   )rV   r   r'   r   r   r    r4     s    
zVersionControl.make_rev_optionsc             C   s&   t jj|ƒ\}}|jt jjƒp$t|ƒS )zy
           posix absolute paths start with os.path.sep,
           win32 ones start with drive (like c:\folder)
        )ÚosÚpathÚ
splitdriverb   ÚsepÚbool)rV   ÚrepoÚdriveÚtailr   r   r    Ú_is_local_repository  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.
        N)rk   )r)   rZ   Úurlr   r   r    Úexport!  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)).
        N)NNr   )rV   ÚnetlocÚschemer   r   r    Úget_netloc_and_auth*  s    z"VersionControl.get_netloc_and_authc       	      C   s„   t j|ƒ\}}}}}d|kr*tdj|ƒƒ‚|jddƒd }| j||ƒ\}}d}d|krf|jddƒ\}}t j||||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ú@r2   )r@   ÚurlsplitÚ
ValueErrorr   Úsplitry   ÚrsplitÚ
urlunsplit)	rV   ru   rx   rw   rm   Ú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_argsV  s    zVersionControl.make_rev_argsc             C   s:   | j |ƒ\}}}|\}}| j||ƒ}| j||d�}||fS )zŒ
        Return the URL and RevOptions object to use in obtain() and in
        some cases export(), as a tuple (url, rev_options).
        )r'   )r…   rˆ   r4   )r)   ru   r   r„   r†   r‡   r'   Úrev_optionsr   r   r    Úget_url_rev_options]  s
    z"VersionControl.get_url_rev_optionsc             C   s   t j| ƒjdƒS )zi
        Normalize a URL for comparison by unquoting it and removing any
        trailing slash.
        ú/)r@   ÚunquoteÚrstrip)ru   r   r   r    Únormalize_urlj  s    zVersionControl.normalize_urlc             C   s   | j |ƒ| j |ƒkS )zV
        Compare two repo URLs for identity, ignoring incidental differences.
        )rŽ   )rV   Úurl1Úurl2r   r   r    Úcompare_urlss  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.
        N)rk   )r)   Údestru   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.
        N)rk   )r)   r’   ru   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.
        N)rk   )r)   r’   ru   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.
        N)rk   )rV   r’   r+   r   r   r    Úis_commit_id_equal˜  s    	z!VersionControl.is_commit_id_equalc       	      C   sÌ  | j |ƒ\}}tjj|ƒs,| j|||ƒ dS |jƒ }| j|ƒrÐ| j|ƒ}| j||ƒr°t	j
d| jjƒ t|ƒ|ƒ | j||jƒs¢t	jdt|ƒ| j|ƒ | j|||ƒ n
t	jdƒ dS t	jd| j| jt|ƒ|ƒ d}nt	jd|| j| jƒ d}t	jd| j|ƒ td|d  |d ƒ}|dk�r"tjdƒ |d	k�rVt	jdt|ƒƒ t|ƒ | j|||ƒ dS |d
k�r˜t|ƒ}t	jdt|ƒ|ƒ tj||ƒ | j|||ƒ dS |dk�rÈt	jd| jt|ƒ||ƒ | j|||ƒ 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ú%(s)witch, (i)gnore, (w)ipe, (b)ackup ÚsÚiÚwÚbz0Directory %s already exists, and is not a %s %s.ú(i)gnore, (w)ipe, (b)ackup z+The plan is to install the %s repository %szWhat to do?  %sr   r{   ÚazDeleting %szBacking up %s to %szSwitching %s %s to %s%s©r˜   r™   rš   r›   )r—   rž   ©r™   rš   r›   )rœ   rŸ   éÿÿÿÿ)rŠ   rl   rm   Úexistsr“   r3   Úis_repository_directoryrh   r‘   rS   rU   Ú	repo_nameÚtitler	   r–   r   Úinfor•   rT   r+   r   ÚsysÚexitr
   r   ÚshutilÚmover”   )	r)   r’   ru   r‰   Úrev_displayÚexisting_urlÚpromptÚresponseÚdest_dirr   r   r    Úobtain£  s€    	


  




zVersionControl.obtainc             C   s&   t j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.
        )ru   N)rl   rm   r¡   r
   r¯   )r)   rZ   ru   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.
        N)rk   )rV   rZ   r   r   r    rh     s    zVersionControl.get_remote_urlc             C   s   t ‚dS )zR
        Return the current commit id of the files at the given location.
        N)rk   )rV   rZ   r   r   r    rf     s    zVersionControl.get_revisionTÚraisec	       
      C   st   | j g| }yt|||||||| j|d�	S  tk
rn }	 z*|	jtjkr\td| j | j f ƒ‚n‚ W Y dd}	~	X 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ÚspinnerzCCannot find command %r - do you have %r installed and in your PATH?N)r+   r   r¶   ÚOSErrorÚerrnoÚENOENTr   )
rV   ÚcmdÚshow_stdoutÚcwdr²   r³   r´   rµ   r·   Úer   r   r    Úrun_command  s    zVersionControl.run_commandc             C   s,   t jd|| j| jƒ tjjtjj|| jƒƒS )zL
        Return whether a directory path is a repository directory.
        zChecking in %s for %s (%s)...)rS   rU   rL   r+   rl   rm   r¡   Újoin)rV   rm   r   r   r    r¢   C  s    z&VersionControl.is_repository_directoryc             C   s
   | j |ƒ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¢   )rV   rZ   r   r   r    rY   M  s    z VersionControl.controls_location)NN)TNr±   NNNN)$r#   r$   r%   r+   rL   r£   rC   r¶   r-   Úclassmethodrc   re   rg   rj   Ústaticmethodr/   r4   rt   rv   ry   r…   rˆ   rŠ   rŽ   r‘   r“   r”   r•   r–   r¯   r°   rh   rf   r¿   r¢   rY   r   r   r   r    ra   Æ   sN   

				]
      
ra   )N)1r6   Ú
__future__r   r¹   Úloggingrl   r¨   r¦   Úpip._vendorr   Zpip._vendor.six.moves.urllibr   r@   Zpip._internal.exceptionsr   Zpip._internal.utils.miscr   r   r   r	   r
   Zpip._internal.utils.typingr   Útypingr   r   r   r   r   r   r   r   r   Zpip._internal.utils.uir   ÚstrZAuthInfoÚ__all__Ú	getLoggerr#   rS   r!   Ú	Exceptionr"   Úobjectr&   r8   r   ra   r   r   r   r    Ú<module>   s.   ,

GG