def path_to_url(path): """ 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 normalize(self): parsed = urllib_parse.urlsplit(self.url) if parsed.scheme == 'file': path = urllib_request.url2pathname(parsed.path) path = normalize_path(path) path = urllib_request.pathname2url(path) self.url = urllib_parse.urlunsplit( (parsed.scheme, parsed.netloc, path, parsed.query, parsed.fragment))
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 _clean_file_url_path(part): # type: (str) -> str """ Clean the first part of a URL path that corresponds to a local filesystem path (i.e. the first part after splitting on "@" characters). """ # We unquote prior to quoting to make sure nothing is double quoted. # Also, on Windows the path part might contain a drive letter which # should not be quoted. On Linux where drive letters do not # exist, the colon should be quoted. We rely on urllib.request # to do the right thing here. return urllib_request.pathname2url(urllib_request.url2pathname(part))
def test_get_html_page_directory_append_index(tmpdir): """`_get_html_page()` should append "index.html" to a directory URL. """ dirpath = tmpdir.mkdir("something") dir_url = "file:///{}".format( urllib_request.pathname2url(dirpath).lstrip("/"), ) session = mock.Mock(PipSession) with mock.patch("pip._internal.collector._get_html_response") as mock_func: _get_html_page(Link(dir_url), session=session) assert mock_func.mock_calls == [ mock.call( "{}/index.html".format(dir_url.rstrip("/")), session=session, ), ]
def test_get_html_page_directory_append_index(tmpdir): """`_get_html_page()` should append "index.html" to a directory URL. """ dirpath = tmpdir.mkdir("something") dir_url = "file:///{}".format( urllib_request.pathname2url(dirpath).lstrip("/"), ) session = mock.Mock(PipSession) with mock.patch("pip._internal.index._get_html_response") as mock_func: _get_html_page(Link(dir_url), session=session) assert mock_func.mock_calls == [ mock.call( "{}/index.html".format(dir_url.rstrip("/")), session=session, ), ]
def test_get_html_page_directory_append_index(tmpdir): """`_get_html_page()` should append "index.html" to a directory URL. """ dirpath = tmpdir.mkdir("something") dir_url = "file:///{}".format( urllib_request.pathname2url(dirpath).lstrip("/"), ) expected_url = "{}/index.html".format(dir_url.rstrip("/")) session = mock.Mock(PipSession) fake_response = make_fake_html_response(expected_url) with mock.patch("pip._internal.collector._get_html_response") as mock_func: mock_func.return_value = fake_response actual = _get_html_page(Link(dir_url), session=session) assert mock_func.mock_calls == [ mock.call(expected_url, session=session), ], 'actual calls: {}'.format(mock_func.mock_calls) assert actual.content == fake_response.content assert actual.encoding is None assert actual.url == expected_url
def _clean_link(url): # type: (str) -> str """Makes sure a link is fully encoded. That is, if a ' ' shows up in the link, it will be rewritten to %20 (while not over-quoting % or other characters).""" # Split the URL into parts according to the general structure # `scheme://netloc/path;parameters?query#fragment`. Note that the # `netloc` can be empty and the URI will then refer to a local # filesystem path. result = urllib_parse.urlparse(url) # In both cases below we unquote prior to quoting to make sure # nothing is double quoted. if result.netloc == "": # On Windows the path part might contain a drive letter which # should not be quoted. On Linux where drive letters do not # exist, the colon should be quoted. We rely on urllib.request # to do the right thing here. path = urllib_request.pathname2url( urllib_request.url2pathname(result.path)) else: path = urllib_parse.quote(urllib_parse.unquote(result.path)) return urllib_parse.urlunparse(result._replace(path=path))
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)
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)
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)
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)