1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Move path_to_url() from download.py to misc.py.

This commit is contained in:
Chris Jerdonek 2019-05-26 21:53:59 -07:00
parent 30855ff9b7
commit 2d450936c6
4 changed files with 33 additions and 32 deletions

View file

@ -37,8 +37,8 @@ from pip._internal.utils.glibc import libc_ver
from pip._internal.utils.misc import ( from pip._internal.utils.misc import (
ARCHIVE_EXTENSIONS, ask, ask_input, ask_password, ask_path_exists, ARCHIVE_EXTENSIONS, ask, ask_input, ask_password, ask_path_exists,
backup_dir, consume, display_path, format_size, get_installed_version, backup_dir, consume, display_path, format_size, get_installed_version,
remove_auth_from_url, rmtree, split_auth_netloc_from_url, splitext, path_to_url, remove_auth_from_url, rmtree, split_auth_netloc_from_url,
unpack_file, splitext, unpack_file,
) )
from pip._internal.utils.temp_dir import TempDirectory from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.utils.typing import MYPY_CHECK_RUNNING from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@ -693,17 +693,6 @@ def url_to_path(url):
return path return path
def path_to_url(path):
# type: (Union[str, Text]) -> str
"""
Convert a path to a file: URL. The path will be made absolute and have
quoted path parts.
"""
path = os.path.normpath(os.path.abspath(path))
url = urllib_parse.urljoin('file:', urllib_request.pathname2url(path))
return url
def is_archive_file(name): def is_archive_file(name):
# type: (str) -> bool # type: (str) -> bool
"""Return True if `name` is a considered as an archive file.""" """Return True if `name` is a considered as an archive file."""

View file

@ -25,6 +25,7 @@ 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, shlex_quote from pip._vendor.six.moves import input, shlex_quote
from pip._vendor.six.moves.urllib import parse as urllib_parse from pip._vendor.six.moves.urllib import parse as urllib_parse
from pip._vendor.six.moves.urllib import request as urllib_request
from pip._vendor.six.moves.urllib.parse import unquote as urllib_unquote from pip._vendor.six.moves.urllib.parse import unquote as urllib_unquote
from pip._internal.exceptions import CommandError, InstallationError from pip._internal.exceptions import CommandError, InstallationError
@ -931,6 +932,17 @@ def enum(*sequential, **named):
return type('Enum', (), enums) return type('Enum', (), enums)
def path_to_url(path):
# type: (Union[str, Text]) -> str
"""
Convert a path to a file: URL. The path will be made absolute and have
quoted path parts.
"""
path = os.path.normpath(os.path.abspath(path))
url = urllib_parse.urljoin('file:', urllib_request.pathname2url(path))
return url
def split_auth_from_netloc(netloc): def split_auth_from_netloc(netloc):
""" """
Parse out and remove the auth information from a netloc. Parse out and remove the auth information from a netloc.

View file

@ -8,7 +8,6 @@ from tempfile import mkdtemp
import pytest import pytest
from mock import Mock, patch from mock import Mock, patch
from pip._vendor.six.moves.urllib import request as urllib_request
import pip import pip
from pip._internal.download import ( from pip._internal.download import (
@ -199,22 +198,6 @@ def test_unpack_http_url_bad_downloaded_checksum(mock_unpack_file):
rmtree(download_dir) rmtree(download_dir)
@pytest.mark.skipif("sys.platform == 'win32'")
def test_path_to_url_unix():
assert path_to_url('/tmp/file') == 'file:///tmp/file'
path = os.path.join(os.getcwd(), 'file')
assert path_to_url('file') == 'file://' + urllib_request.pathname2url(path)
@pytest.mark.skipif("sys.platform != 'win32'")
def test_path_to_url_win():
assert path_to_url('c:/tmp/file') == 'file:///C:/tmp/file'
assert path_to_url('c:\\tmp\\file') == 'file:///C:/tmp/file'
assert path_to_url(r'\\unc\as\path') == 'file://unc/as/path'
path = os.path.join(os.getcwd(), 'file')
assert path_to_url('file') == 'file:' + urllib_request.pathname2url(path)
@pytest.mark.parametrize("url,win_expected,non_win_expected", [ @pytest.mark.parametrize("url,win_expected,non_win_expected", [
('file:tmp', 'tmp', 'tmp'), ('file:tmp', 'tmp', 'tmp'),
('file:c:/path/to/file', r'C:\path\to\file', 'c:/path/to/file'), ('file:c:/path/to/file', r'C:\path\to\file', 'c:/path/to/file'),

View file

@ -18,6 +18,7 @@ from logging import DEBUG, ERROR, INFO, WARNING
import pytest import pytest
from mock import Mock, patch from mock import Mock, patch
from pip._vendor.six.moves.urllib import request as urllib_request
from pip._internal.exceptions import ( from pip._internal.exceptions import (
HashMismatch, HashMissing, InstallationError, HashMismatch, HashMissing, InstallationError,
@ -27,8 +28,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, format_command_args, call_subprocess, egg_link_path, ensure_dir, format_command_args,
get_installed_distributions, get_prog, normalize_path, redact_netloc, get_installed_distributions, get_prog, normalize_path, path_to_url,
redact_password_from_url, remove_auth_from_url, rmtree, redact_netloc, redact_password_from_url, remove_auth_from_url, rmtree,
split_auth_from_netloc, split_auth_netloc_from_url, untar_file, unzip_file, split_auth_from_netloc, split_auth_netloc_from_url, untar_file, unzip_file,
) )
from pip._internal.utils.temp_dir import AdjacentTempDirectory, TempDirectory from pip._internal.utils.temp_dir import AdjacentTempDirectory, TempDirectory
@ -964,6 +965,22 @@ class TestCallSubprocess(object):
) )
@pytest.mark.skipif("sys.platform == 'win32'")
def test_path_to_url_unix():
assert path_to_url('/tmp/file') == 'file:///tmp/file'
path = os.path.join(os.getcwd(), 'file')
assert path_to_url('file') == 'file://' + urllib_request.pathname2url(path)
@pytest.mark.skipif("sys.platform != 'win32'")
def test_path_to_url_win():
assert path_to_url('c:/tmp/file') == 'file:///C:/tmp/file'
assert path_to_url('c:\\tmp\\file') == 'file:///C:/tmp/file'
assert path_to_url(r'\\unc\as\path') == 'file://unc/as/path'
path = os.path.join(os.getcwd(), 'file')
assert path_to_url('file') == 'file:' + urllib_request.pathname2url(path)
@pytest.mark.parametrize('netloc, expected', [ @pytest.mark.parametrize('netloc, expected', [
# Test a basic case. # Test a basic case.
('example.com', ('example.com', (None, None))), ('example.com', ('example.com', (None, None))),