B
    Kkd~C  ã               @   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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 $e%¡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)ƒZ-dS )z)Handles all VCS (version control) supporté    )Úabsolute_importN)Úparse)Ú
BadCommand)Údisplay_pathÚ
backup_dirÚcall_subprocessÚrmtreeÚask_path_exists)ÚMYPY_CHECK_RUNNING)	ÚAnyÚDictÚIterableÚListÚMappingÚOptionalÚTextÚTupleÚType)ÚSpinnerInterfaceÚvcsc               @   s   e Zd ZdS )ÚRemoteNotFoundErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú;/tmp/pip-install-gxxfd9b7/pip/pip/_internal/vcs/__init__.pyr       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:
          vcs: a VersionControl object.
          rev: the name of the revision to install.
          extra_args: a list of extra options.
        N)Ú
extra_argsÚrevr   )Úselfr   r   r   r   r   r   Ú__init__-   s
    zRevOptions.__init__c             C   s   d  | jj| j¡S )Nz<RevOptions {}: rev={!r}>)Úformatr   Únamer   )r   r   r   r   Ú__repr__<   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_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)r%   r   Úget_base_rev_argsr   )r   Úargsr   r   r   r   Úto_argsG   s    
zRevOptions.to_argsc             C   s   | j s
dS d | j ¡S )NÚ z (to revision {}))r   r!   )r   r   r   r   Ú
to_displayT   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_new[   s    zRevOptions.make_new)NN)r   r   r   Ú__doc__r    r#   Úpropertyr%   r(   r*   r,   r   r   r   r   r   $   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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Úgetattrr6   Úsuperr/   r    )r   )Ú	__class__r   r   r    j   s    zVcsSupport.__init__c             C   s
   | j  ¡ S )N)Ú	_registryÚ__iter__)r   r   r   r   r?   t   s    zVcsSupport.__iter__c             C   s   t | j ¡ ƒS )N)Úlistr>   Úvalues)r   r   r   r   Úbackendsw   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>)rB   )r   r   r   r   Údirnames|   s    zVcsSupport.dirnamesc             C   s$   g }x| j D ]}| |j¡ qW |S )N)rB   r9   r:   )r   r:   rE   r   r   r   Úall_schemes   s    zVcsSupport.all_schemesc             C   sF   t |dƒst d|j¡ d S |j| jkrB|| j|j< t d|j¡ d S )Nr"   zCannot register VCS %szRegistered VCS backend: %s)ÚhasattrÚloggerÚwarningr   r"   r>   Údebug)r   Úclsr   r   r   Úregister‰   s    
zVcsSupport.registerNc             C   s<   || j kr| j |= n$|| j  ¡ kr.| j |j= n
t d¡ d S )Nz0Cannot unregister because no class or name given)r>   rA   r"   rJ   rK   )r   rM   r"   r   r   r   Ú
unregister’   s
    

zVcsSupport.unregisterc             C   s6   x0| j  ¡ D ]"}| |¡rt d||j¡ |S qW dS )z—
        Return the type of the version control backend if found at given
        location, e.g. vcs.get_backend_type('/path/to/vcs/checkout')
        zDetermine that %s uses VCS: %sN)r>   rA   Úcontrols_locationrJ   rL   r"   )r   ÚlocationÚvc_typer   r   r   Úget_backend_type›   s    

zVcsSupport.get_backend_typec             C   s    |  ¡ }|| jkr| j| S d S )N)Úlowerr>   )r   r"   r   r   r   Úget_backend¨   s    

zVcsSupport.get_backend)NN)r   r   r   r>   r:   r    r?   r.   rB   rG   rH   rN   rO   rS   rU   Ú__classcell__r   r   )r=   r   r/   f   s   
	
	r/   c                   s  e Zd ZdZdZdZdZdZdZd4‡ fdd„	Z	dd„ Z
d5dd	„Zed
d„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zed&d'„ ƒZed(d)„ ƒZed*d+„ ƒZed6d.d/„ƒZed0d1„ ƒZed2d3„ ƒZ ‡  Z!S )7ÚVersionControlr)   r   Nc                s   || _ tt| ƒj||Ž d S )N)Úurlr<   rW   r    )r   rX   r'   Úkwargs)r=   r   r   r    ½   s    zVersionControl.__init__c             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   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   )r   r   r   r   r   r   r+   Ê   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)
        )ÚosÚpathÚ
splitdriveÚ
startswithÚsepÚbool)rM   Ú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
        N)rZ   )r   rQ   r   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)).
        )NNr   )r   ÚnetlocÚschemer   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ú@r)   )r7   ÚurlsplitÚ
ValueErrorr!   Úsplitrh   ÚrsplitÚ
urlunsplit)	r   rX   rg   rf   r\   Ú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   )r   ÚusernameÚpasswordr   r   r   Úmake_rev_args  s    zVersionControl.make_rev_argsc             C   s:   |   |¡\}}}|\}}|  ||¡}| 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   )rt   rw   r+   )r   rX   r   rs   ru   rv   r   Ú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.
        ú/)r7   ÚunquoteÚrstrip)r   rX   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}   )r   Ú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.
        N)rZ   )r   ÚdestrX   rx   r   r   r   Ú	fetch_new2  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)rZ   )r   r   rX   rx   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)rZ   )r   r   rX   rx   r   r   r   ÚupdateF  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)rZ   )r   r   r"   r   r   r   Úis_commit_id_equalO  s    z!VersionControl.is_commit_id_equalc       	      C   sÎ  |   | j¡\}}tj |¡s.|  |||¡ dS | ¡ }|  |¡rÒ|  |¡}|  	||¡r²t
 d| j ¡ t|ƒ|¡ |  ||j¡s¤t
 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ršt|ƒ}t
 dt|ƒ|¡ t ||¡ |  |||¡ dS |dkrÊt
 d| jt|ƒ||¡ |  |||¡ dS )zÊ
        Install or update in editable mode the package represented by this
        VersionControl object.

        Args:
          dest: the repository directory in which to install or update.
        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   rj   Úaéÿÿÿÿrˆ   zDeleting %sr‰   zBacking up %s to %sr†   zSwitching %s %s to %s%s)ry   rX   r[   r\   Úexistsr‚   r*   Úis_repository_directoryÚget_remote_urlr€   rJ   rL   Ú	repo_nameÚtitler   r…   r   Úinfor„   rK   r"   r	   ÚsysÚexitr   r   ÚshutilÚmoverƒ   )	r   r   rX   rx   Úrev_displayÚexisting_urlÚpromptÚresponseÚdest_dirr   r   r   ÚobtainY  s|    	







zVersionControl.obtainc             C   s"   t j |¡rt|ƒ |  |¡ dS )zq
        Clean up current location and download the url repository
        (and vcs infos) into location
        N)r[   r\   rŒ   r   r›   )r   rQ   r   r   r   Úunpack¶  s    zVersionControl.unpackc             C   s   t ‚dS )zê
        Return a string representing the requirement needed to
        redownload the files currently present in location, something
        like:
          {repository_url}@{revision}#egg={project_name}-{version_identifier}
        N)rZ   )rM   rQ   Úproject_namer   r   r   Úget_src_requirementÀ  s    z"VersionControl.get_src_requirementc             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)rZ   )rM   rQ   r   r   r   rŽ   Ê  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)rZ   )rM   rQ   r   r   r   Úget_revisionÔ  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 d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ÚspinnerzCCannot find command %r - do you have %r installed and in your PATH?N)r"   r   r¥   ÚOSErrorÚerrnoÚENOENTr   )
rM   ÚcmdÚshow_stdoutÚcwdr¡   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)...)rJ   rL   rC   r"   r[   r\   rŒ   Újoin)rM   r\   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   )rM   rQ   r   r   r   rP     s    z VersionControl.controls_location)N)NN)TNr    NNNN)"r   r   r   r"   rC   r   r:   r¥   r$   r    r&   r+   Úclassmethodrd   re   rh   rt   rw   ry   r}   r€   r‚   rƒ   r„   r…   r›   rœ   rž   rŽ   rŸ   r®   r   rP   rV   r   r   )r=   r   rW   ³   sH   	

		
]


      
rW   ).r-   Ú
__future__r   r¨   Úloggingr[   r”   r’   Úpip._vendor.six.moves.urllibr   r7   Úpip._internal.exceptionsr   Úpip._internal.utils.miscr   r   r   r   r	   Úpip._internal.utils.typingr
   Útypingr   r   r   r   r   r   r   r   r   Úpip._internal.utils.uir   ÚstrZAuthInfoÚ__all__Ú	getLoggerr   rJ   Ú	Exceptionr   Úobjectr   r/   r   rW   r   r   r   r   Ú<module>   s*   ,
BJ