3 igW@sdZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZddlmZddlmZmZddlmZmZmZmZmZmZmZm Z ddl!m"Z"m#Z#m$Z$ddl%m&Z&erdd lm'Z'd gZ(ej)e*Z+eee,ee,fZ-e,e.d d d Z/de,e,e,ee,e,dddZ0e,e,ee,dddZ1Gddde2Z3Gddde2Z4GdddZ5GdddZ6e6Z7GdddZ8dS)z)Handles all VCS (version control) supportN) TYPE_CHECKINGAnyDictIterableIteratorListMappingOptionalTupleTypeUnion)SpinnerInterface) BadCommandInstallationError) HiddenTextask_path_exists backup_dir display_pathhide_url hide_valueis_installable_dirrmtree) CommandArgscall_subprocess make_command)get_url_scheme)Literalvcs)namereturncCs*t|}|dkrdS|ddddgtjkS)z3 Return true if the name looks like a URL. NFhttphttpsfileftp)rr all_schemes)rschemer&?/tmp/pip-build-88gy_88q/pip/pip/_internal/vcs/versioncontrol.pyis_url4sr()repo_urlrev project_namesubdirrcCs6|jdd}|d|d|}|r2|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=)replace)r)r*r+r,egg_project_namereqr&r&r'make_vcs_requirement_url>s r3)location repo_rootrcCsZ|}x4t|s8|}tjj|}||krtjd|dSqWtjj||rLdStjj||S)z Find the the Python project's root by searching up the filesystem from `location`. Return the path to project root relative to `repo_root`. Return None if the project root is `repo_root`, or cannot be found. zOCould not find a Python project for directory %s (tried all parent directories)N)rospathdirnameloggerwarningsamefilerelpath)r4r5 orig_location last_locationr&r&r'(find_path_to_project_root_from_repo_rootPs   r?c@s eZdZdS)RemoteNotFoundErrorN)__name__ __module__ __qualname__r&r&r&r'r@msr@cs"eZdZedfdd ZZS)RemoteNotValidError)urlcstj|||_dS)N)super__init__rE)selfrE) __class__r&r'rGrs zRemoteNotValidError.__init__)rArBrCstrrG __classcell__r&r&)rIr'rDqsrDc@seZdZdZdedeeeeddddZeddd Z e eedd d Z edd d Z edddZ eddddZdS) RevOptionsz Encapsulates a VCS-specific revision to install, along with any VCS install options. Instances of this class should be treated as if immutable. NVersionControl)vc_classr* extra_argsrcCs(|dkr g}||_||_||_d|_dS)z Args: vc_class: a VersionControl subclass. rev: the name of the revision to install. extra_args: a list of extra options. N)rOr*rNZ branch_name)rHrNr*rOr&r&r'rGs zRevOptions.__init__)rcCsd|jjd|jdS)Nz )rNrr*)rHr&r&r'__repr__szRevOptions.__repr__cCs|jdkr|jjS|jS)N)r*rNdefault_arg_rev)rHr&r&r'arg_revs zRevOptions.arg_revcCs0g}|j}|dk r"||jj|7}||j7}|S)z< Return the VCS-specific command arguments. N)rSrNget_base_rev_argsrO)rHargsr*r&r&r'to_argss  zRevOptions.to_argscCs|js dSd|jdS)Nz (to revision ))r*)rHr&r&r' to_displayszRevOptions.to_display)r*rcCs|jj||jdS)z Make a copy of the current instance, but with a new rev. Args: rev: the name of the revision for the new object. )rO)rNmake_rev_optionsrO)rHr*r&r&r'make_newszRevOptions.make_new)NN)rArBrC__doc__r r rJrrGrQpropertyrSrVrYr[r&r&r&r'rLws rLcseZdZUiZeedfddddddgZdd fd d Zeed d d Z e e dd ddZ e e ed ddZ e e ed ddZedddddZeddddZeeddddZeeddddZeeddd d!ZZS)" VcsSupportrMsshgithgbzrsftpsvnN)rcstjjj|jtjdS)N)urllibparse uses_netlocextendschemesrFrG)rH)rIr&r'rGszVcsSupport.__init__cCs |jjS)N) _registry__iter__)rHr&r&r'rkszVcsSupport.__iter__cCst|jjS)N)listrjvalues)rHr&r&r'backendsszVcsSupport.backendscCsdd|jDS)NcSsg|] }|jqSr&)r8).0backendr&r&r' sz'VcsSupport.dirnames..)rn)rHr&r&r'dirnamesszVcsSupport.dirnamescCs$g}x|jD]}|j|jq W|S)N)rnrhri)rHrirpr&r&r'r$s zVcsSupport.all_schemes)clsrcCsHt|dstjd|jdS|j|jkrD||j|j<tjd|jdS)NrzCannot register VCS %szRegistered VCS backend: %s)hasattrr9r:rArrjdebug)rHrsr&r&r'registers   zVcsSupport.register)rrcCs||jkr|j|=dS)N)rj)rHrr&r&r' unregisters zVcsSupport.unregister)r4rcCs\i}x:|jjD],}|j|}|s$qtjd||j|||<qW|sHdSt|td}||S)zv Return a VersionControl object if a repository of that type is found at the given directory. zDetermine that %s uses VCS: %sN)key)rjrmget_repository_rootr9rurmaxlen)rHr4Z vcs_backends vcs_backendZ repo_pathZinner_most_repo_pathr&r&r'get_backend_for_dirs   zVcsSupport.get_backend_for_dir)r%rcCs&x |jjD]}||jkr |Sq WdS)z9 Return a VersionControl object or None. N)rjrmri)rHr%r|r&r&r'get_backend_for_schemes z!VcsSupport.get_backend_for_schemecCs|j}|jj|S)z9 Return a VersionControl object or None. )lowerrjget)rHrr&r&r' get_backendszVcsSupport.get_backend)rArBrCrjrrJrirGrrkr]rrnrrr$r rvrwr r}r~rrKr&r&)rIr'r^s  r^c@seZdZUdZdZdZfZeedffZ eedf dZ e e e ee dddZe ee eddd Ze eed d d Ze eeed ddZeeeedddZeee dddZe dLe ee eedddZe ee dddZe eeeeee ee effdddZe eeee eefdd d!Zee ee eed"d#d$Zeeeefdd%d&Zeeedd'd(Z e eee d)d*d+Z!eeedd,d-d.Z"eeedd,d/d0Z#eeedd,d1d2Z$e ee ee d3d4d5Z%eedd6d7d8Z&eedd9d:d;Z'e eeddd?Z)e dMe*eeefe e edCe e+e,e ee e-ee.fe e/e e edD dEdFZ0e ee dGdHdIZ1e ee eddJdKZ2dS)NrMrW.N) remote_urlrcCs|jj|jd S)z Return whether the vcs prefix (e.g. "git+") should be added to a repository's remote url when used in a requirement. :)r startswithr)rsrr&r&r'should_add_vcs_url_prefixsz(VersionControl.should_add_vcs_url_prefix)r4rcCsdS)z Return the path to Python project root, relative to the repo root. Return None if the project root is in the repo root. Nr&)rsr4r&r&r'get_subdirectoryszVersionControl.get_subdirectory)repo_dirrcCs |j|S)zR Return the revision string that should be used in a requirement. ) get_revision)rsrr&r&r'get_requirement_revision'sz'VersionControl.get_requirement_revision)rr+rcCsL|j|}|j|r$|jd|}|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} +)r,)get_remote_urlrrrrr3)rsrr+r)revisionr,r2r&r&r'get_src_requirement.s    z"VersionControl.get_src_requirement)r*rcCstdS)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'rTFsz VersionControl.get_base_rev_args)rEdestrcCsdS)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&)rHrErr&r&r'is_immutable_rev_checkoutPs z(VersionControl.is_immutable_rev_checkout)r*rOrcCst|||dS)z Return a RevOptions object. Args: rev: the name of a revision to install. extra_args: a list of extra options. )rO)rL)rsr*rOr&r&r'rZ]s zVersionControl.make_rev_options)reporcCs&tjj|\}}|jtjjp$t|S)zs posix absolute paths start with os.path.sep, win32 ones start with drive (like c:\folder) )r6r7 splitdriversepbool)rsrdrivetailr&r&r'_is_local_repositoryjsz#VersionControl._is_local_repository)netlocr%rcCs|dfS)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&)rsrr%r&r&r'get_netloc_and_authssz"VersionControl.get_netloc_and_auth)rErc Cstjj|\}}}}}d|kr,tdj||jddd}|j||\}}d}d|krz|jdd\}}|sztdj|tjj ||||df}|||fS)z Parse the repository URL to use, and return the URL, revision, and auth info to use. Returns: (url, rev, (username, password)). rzvSorry, {!r} is a malformed VCS url. The format is +://, e.g. svn+http://myrepo/svn/MyApp#egg=MyAppNr/zyThe URL {!r} has an empty revision (after @) which is not supported. Include a revision after @ or remove @ from the URL.rW) rerfurlsplit ValueErrorformatsplitrrsplitr urlunsplit) rsrEr%rr7queryfrag user_passr*r&r&r'get_url_rev_and_auths z#VersionControl.get_url_rev_and_auth)usernamepasswordrcCsgS)zM Return the RevOptions "extra arguments" to use in obtain(). r&)rrr&r&r' make_rev_argsszVersionControl.make_rev_argsc CsT|j|j\}}}|\}}d}|dk r.t|}|j||}|j||d} t|| fS)zq Return the URL and RevOptions object to use in obtain(), as a tuple (url, rev_options). N)rO)rsecretrrrZr) rHrEZ secret_urlr*rrZsecret_passwordrrO rev_optionsr&r&r'get_url_rev_optionss z"VersionControl.get_url_rev_optionscCstjj|jdS)zi Normalize a URL for comparison by unquoting it and removing any trailing slash. /)rerfunquoterstrip)rEr&r&r' normalize_urlszVersionControl.normalize_url)url1url2rcCs|j||j|kS)zV Compare two repo URLs for identity, ignoring incidental differences. )r)rsrrr&r&r' compare_urlsszVersionControl.compare_urls)rrErrcCstdS)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)r)rHrrErr&r&r' fetch_news zVersionControl.fetch_newcCstdS)z} Switch the repo at ``dest`` to point to ``URL``. Args: rev_options: a RevOptions object. N)r)rHrrErr&r&r'switchszVersionControl.switchcCstdS)z Update an already-existing repo to the given ``rev_options``. Args: rev_options: a RevOptions object. N)r)rHrrErr&r&r'updateszVersionControl.update)rrrcCstdS)z Return whether the id of the current commit equals the given name. Args: dest: the repository directory. name: a string name. N)r)rsrrr&r&r'is_commit_id_equals z!VersionControl.is_commit_id_equal)rrErc Cs|j|\}}tjj|s,|j|||dS|j}|j|r|j|}|j||j rt j d|j j t|||j||jst jdt||j ||j|||n t jddSt jd|j|j t||d}nt jd ||j|j d}t jd |j|tdj|d|d}|dkr&tjd|d krZt jdt|t||j|||dS|d krt|}t jdt||tj|||j|||dS|dkrt jd|j t||||j|||dS)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)zUpdating %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 siwbz0Directory %s already exists, and is not a %s %s.(i)gnore, (w)ipe, (b)ackup z+The plan is to install the %s repository %szWhat to do? {}rraz Deleting %szBacking up %s to %szSwitching %s %s to %s%srrrr)rrrrr)rr)rr6r7existsrrYis_repository_directoryrrrr9ru repo_nametitlerrr*inforr:rrrsysexitrrshutilmover) rHrrEr rev_display existing_urlpromptresponsedest_dirr&r&r'obtainsz          zVersionControl.obtain)r4rErcCs&tjj|rt||j||ddS)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. )rEN)r6r7rrr)rHr4rEr&r&r'unpackMs zVersionControl.unpackcCstdS)z Return the url used at location Raises RemoteNotFoundError if the repository does not have a remote url configured. N)r)rsr4r&r&r'rXszVersionControl.get_remote_urlcCstdS)zR Return the current commit id of the files at the given location. N)r)rsr4r&r&r'rbszVersionControl.get_revisionTraiseFz"Literal["raise", "warn", "ignore"]) cmd show_stdoutcwd on_returncodeextra_ok_returncodes command_desc extra_environspinnerlog_failed_cmd stdout_onlyrc Cst|jf|}y t||||||||j|| | d Stk r^td|jd|jdYn&tk rtd|jdYnXdS)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 )rrrr unset_environrrrzCannot find command z - do you have z installed and in your PATH?zNo permission to execute z - install it locally, globally (ask admin), or check your PATH. See possible solutions at https://pip.pypa.io/en/latest/reference/pip_freeze/#fixing-permission-denied.N)rrrrFileNotFoundErrorrPermissionError) rsrrrrrrrrrrr&r&r' run_commandis(zVersionControl.run_command)r7rcCs,tjd||j|jtjjtjj||jS)zL Return whether a directory path is a repository directory. zChecking in %s for %s (%s)...)r9rur8rr6r7rjoin)rsr7r&r&r'rsz&VersionControl.is_repository_directorycCs|j|r|SdS)ay Return the "root" (top-level) directory controlled by the vcs, or `None` if the directory is not in any. 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. N)r)rsr4r&r&r'rys z"VersionControl.get_repository_root)NN) TNrNNNNTF)3rArBrCrr8rrir rJrrRr classmethodrrrrr staticmethodrrTrrrLrZrrAuthInforrrrrrrrrrrrrrr rintrrr rrryr&r&r&r'rM sz     (      X  B)rM)N)9r\loggingr6rr urllib.parsereZtypingrrrrrrrr r r r Zpip._internal.cli.spinnersr Zpip._internal.exceptionsrrZpip._internal.utils.miscrrrrrrrrZpip._internal.utils.subprocessrrrZpip._internal.utils.urlsrr__all__ getLoggerrAr9rJrrr(r3r? Exceptionr@rDrLr^rrMr&r&r&r's64 (     CP