提交 5bc94f33 编写于 作者: J Jeremy Zafran

Move remove_auth_from_url and test from Subversion class to pip._internal.utils.misc

上级 e1abd238
...@@ -24,6 +24,7 @@ from pip._vendor import pkg_resources ...@@ -24,6 +24,7 @@ from pip._vendor import pkg_resources
from pip._vendor.retrying import retry # type: ignore from pip._vendor.retrying import retry # type: ignore
from pip._vendor.six import PY2 from pip._vendor.six import PY2
from pip._vendor.six.moves import input from pip._vendor.six.moves import input
from pip._vendor.six.moves.urllib import parse as urllib_parse
from pip._internal.compat import console_to_str, expanduser, stdlib_pkgs from pip._internal.compat import console_to_str, expanduser, stdlib_pkgs
from pip._internal.exceptions import InstallationError from pip._internal.exceptions import InstallationError
...@@ -47,7 +48,7 @@ __all__ = ['rmtree', 'display_path', 'backup_dir', ...@@ -47,7 +48,7 @@ __all__ = ['rmtree', 'display_path', 'backup_dir',
'unzip_file', 'untar_file', 'unpack_file', 'call_subprocess', 'unzip_file', 'untar_file', 'unpack_file', 'call_subprocess',
'captured_stdout', 'ensure_dir', 'captured_stdout', 'ensure_dir',
'ARCHIVE_EXTENSIONS', 'SUPPORTED_EXTENSIONS', 'ARCHIVE_EXTENSIONS', 'SUPPORTED_EXTENSIONS',
'get_installed_version'] 'get_installed_version', 'remove_auth_from_url']
logger = std_logging.getLogger(__name__) logger = std_logging.getLogger(__name__)
...@@ -849,3 +850,21 @@ def enum(*sequential, **named): ...@@ -849,3 +850,21 @@ def enum(*sequential, **named):
reverse = {value: key for key, value in enums.items()} reverse = {value: key for key, value in enums.items()}
enums['reverse_mapping'] = reverse enums['reverse_mapping'] = reverse
return type('Enum', (), enums) return type('Enum', (), enums)
def remove_auth_from_url(url):
# Return a copy of url with 'username:password@' removed.
# username/pass params are passed to subversion through flags
# and are not recognized in the url.
# parsed url
purl = urllib_parse.urlsplit(url)
stripped_netloc = \
purl.netloc.split('@')[-1]
# stripped url
url_pieces = (
purl.scheme, stripped_netloc, purl.path, purl.query, purl.fragment
)
surl = urllib_parse.urlunsplit(url_pieces)
return surl
...@@ -8,7 +8,7 @@ from pip._vendor.six.moves.urllib import parse as urllib_parse ...@@ -8,7 +8,7 @@ from pip._vendor.six.moves.urllib import parse as urllib_parse
from pip._internal.index import Link from pip._internal.index import Link
from pip._internal.utils.logging import indent_log from pip._internal.utils.logging import indent_log
from pip._internal.utils.misc import display_path, rmtree from pip._internal.utils.misc import display_path, remove_auth_from_url, rmtree
from pip._internal.vcs import VersionControl, vcs from pip._internal.vcs import VersionControl, vcs
_svn_xml_url_re = re.compile('url="([^"]+)"') _svn_xml_url_re = re.compile('url="([^"]+)"')
...@@ -63,7 +63,7 @@ class Subversion(VersionControl): ...@@ -63,7 +63,7 @@ class Subversion(VersionControl):
"""Export the svn repository at the url to the destination location""" """Export the svn repository at the url to the destination location"""
url, rev = self.get_url_rev() url, rev = self.get_url_rev()
rev_options = get_rev_options(self, url, rev) rev_options = get_rev_options(self, url, rev)
url = self.remove_auth_from_url(url) url = remove_auth_from_url(url)
logger.info('Exporting svn repository %s to %s', url, location) logger.info('Exporting svn repository %s to %s', url, location)
with indent_log(): with indent_log():
if os.path.exists(location): if os.path.exists(location):
...@@ -84,7 +84,7 @@ class Subversion(VersionControl): ...@@ -84,7 +84,7 @@ class Subversion(VersionControl):
def obtain(self, dest): def obtain(self, dest):
url, rev = self.get_url_rev() url, rev = self.get_url_rev()
rev_options = get_rev_options(self, url, rev) rev_options = get_rev_options(self, url, rev)
url = self.remove_auth_from_url(url) url = remove_auth_from_url(url)
if self.check_destination(dest, url, rev_options): if self.check_destination(dest, url, rev_options):
rev_display = rev_options.to_display() rev_display = rev_options.to_display()
logger.info( logger.info(
...@@ -221,24 +221,6 @@ class Subversion(VersionControl): ...@@ -221,24 +221,6 @@ class Subversion(VersionControl):
"""Always assume the versions don't match""" """Always assume the versions don't match"""
return False return False
@staticmethod
def remove_auth_from_url(url):
# Return a copy of url with 'username:password@' removed.
# username/pass params are passed to subversion through flags
# and are not recognized in the url.
# parsed url
purl = urllib_parse.urlsplit(url)
stripped_netloc = \
purl.netloc.split('@')[-1]
# stripped url
url_pieces = (
purl.scheme, stripped_netloc, purl.path, purl.query, purl.fragment
)
surl = urllib_parse.urlunsplit(url_pieces)
return surl
def get_rev_options(vcs, url, rev): def get_rev_options(vcs, url, rev):
""" """
......
...@@ -24,7 +24,8 @@ from pip._internal.utils.glibc import check_glibc_version ...@@ -24,7 +24,8 @@ from pip._internal.utils.glibc import check_glibc_version
from pip._internal.utils.hashes import Hashes, MissingHashes from pip._internal.utils.hashes import Hashes, MissingHashes
from pip._internal.utils.misc import ( from pip._internal.utils.misc import (
call_subprocess, egg_link_path, ensure_dir, get_installed_distributions, call_subprocess, egg_link_path, ensure_dir, get_installed_distributions,
get_prog, normalize_path, rmtree, untar_file, unzip_file, get_prog, normalize_path, remove_auth_from_url, rmtree, untar_file,
unzip_file,
) )
from pip._internal.utils.packaging import check_dist_requires_python from pip._internal.utils.packaging import check_dist_requires_python
from pip._internal.utils.temp_dir import TempDirectory from pip._internal.utils.temp_dir import TempDirectory
...@@ -624,3 +625,29 @@ def test_call_subprocess_works_okay_when_just_given_nothing(): ...@@ -624,3 +625,29 @@ def test_call_subprocess_works_okay_when_just_given_nothing():
def test_call_subprocess_closes_stdin(): def test_call_subprocess_closes_stdin():
with pytest.raises(InstallationError): with pytest.raises(InstallationError):
call_subprocess([sys.executable, '-c', 'input()']) call_subprocess([sys.executable, '-c', 'input()'])
def test_remove_auth_from_url():
# Check that the url is doctored appropriately to remove auth elements
# from the url
svn_auth_url = 'https://user:pass@svnrepo.org/svn/project/tags/v0.2'
expected_url = 'https://svnrepo.org/svn/project/tags/v0.2'
url = remove_auth_from_url(svn_auth_url)
assert url == expected_url
# Check that this doesn't impact urls without authentication'
svn_noauth_url = 'https://svnrepo.org/svn/project/tags/v0.2'
expected_url = svn_noauth_url
url = remove_auth_from_url(svn_noauth_url)
assert url == expected_url
# Check that links to specific revisions are handled properly
svn_rev_url = 'https://user:pass@svnrepo.org/svn/project/trunk@8181'
expected_url = 'https://svnrepo.org/svn/project/trunk@8181'
url = remove_auth_from_url(svn_rev_url)
assert url == expected_url
svn_rev_url = 'https://svnrepo.org/svn/project/trunk@8181'
expected_url = 'https://svnrepo.org/svn/project/trunk@8181'
url = remove_auth_from_url(svn_rev_url)
assert url == expected_url
...@@ -173,32 +173,6 @@ def test_bazaar_simple_urls(): ...@@ -173,32 +173,6 @@ def test_bazaar_simple_urls():
) )
def test_subversion_remove_auth_from_url():
# Check that the url is doctored appropriately to remove auth elements
# from the url
svn_auth_url = 'https://user:pass@svnrepo.org/svn/project/tags/v0.2'
expected_url = 'https://svnrepo.org/svn/project/tags/v0.2'
url = Subversion.remove_auth_from_url(svn_auth_url)
assert url == expected_url
# Check that this doesn't impact urls without authentication'
svn_noauth_url = 'https://svnrepo.org/svn/project/tags/v0.2'
expected_url = svn_noauth_url
url = Subversion.remove_auth_from_url(svn_noauth_url)
assert url == expected_url
# Check that links to specific revisions are handled properly
svn_rev_url = 'https://user:pass@svnrepo.org/svn/project/trunk@8181'
expected_url = 'https://svnrepo.org/svn/project/trunk@8181'
url = Subversion.remove_auth_from_url(svn_rev_url)
assert url == expected_url
svn_rev_url = 'https://svnrepo.org/svn/project/trunk@8181'
expected_url = 'https://svnrepo.org/svn/project/trunk@8181'
url = Subversion.remove_auth_from_url(svn_rev_url)
assert url == expected_url
def test_get_git_version(): def test_get_git_version():
git_version = Git().get_git_version() git_version = Git().get_git_version()
assert git_version >= parse_version('1.0.0') assert git_version >= parse_version('1.0.0')
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册