def parse_uri(uri, cwd=None):
"""Parse the repository location provided and attempt to transform it
into a valid repository URI.
'cwd' is the working directory to use to turn paths into an absolute
path. If not provided, the current working directory is used.
"""
if uri.find("://") == -1 and not uri.startswith("file:/"):
# Convert the file path to a URI.
if not cwd:
uri = os.path.abspath(uri)
elif not os.path.isabs(uri):
uri = os.path.normpath(os.path.join(cwd, uri))
uri = urlunparse(("file", "",
pathname2url(uri), "", "", ""))
scheme, netloc, path, params, query, fragment = \
urlparse(uri, "file", allow_fragments=0)
scheme = scheme.lower()
if scheme == "file":
# During urlunparsing below, ensure that the path starts with
# only one '/' character, if any are present.
if path.startswith("/"):
path = "/" + path.lstrip("/")
# Rebuild the URI with the sanitized components.
return urlunparse((scheme, netloc, path, params,
query, fragment))
评论列表
文章目录