diff options
author | Patrik Teivonen <patrik.teivonen@qt.io> | 2023-01-02 15:29:58 +0200 |
---|---|---|
committer | Patrik Teivonen <patrik.teivonen@qt.io> | 2023-01-03 07:12:17 +0000 |
commit | ec146a460e908c53f5e5c16902b1db2595d68f7f (patch) | |
tree | cef801769f6da188a766f3bb8dff58390d631514 | |
parent | 4e3ab8c3ce2973850a9bbcafb0636c386db97a45 (diff) |
bldinstallercommon.py: Fix uri_exists() for file:// uris
Use urllib.requests.url2pathname to convert local file uris to file
system paths before checking from the file system.
Change-Id: I32271c00a4c66da1df852b0745932ff58c9e60f2
Reviewed-by: Antti Kokko <antti.kokko@qt.io>
-rw-r--r-- | packaging-tools/bldinstallercommon.py | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/packaging-tools/bldinstallercommon.py b/packaging-tools/bldinstallercommon.py index 5f21ec420..0d212ae85 100644 --- a/packaging-tools/bldinstallercommon.py +++ b/packaging-tools/bldinstallercommon.py @@ -46,7 +46,7 @@ from traceback import print_exc from types import TracebackType from typing import Any, Callable, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse -from urllib.request import urlcleanup, urlretrieve +from urllib.request import url2pathname, urlcleanup, urlretrieve import requests @@ -65,6 +65,29 @@ log = init_logger(__name__, debug_mode=False) MAX_DEBUG_PRINT_LENGTH = 10000 +def file_uri_to_path(uri: str) -> Path: + """ + Convert file:// uris and string paths to pathlib path + + Examples: + # unix + file:///home/qt/foo%20bar -> Path("/home/qt/foo bar") + # windows + file:///c:/users/qt/foo%20bar -> Path("c:/users/qt/foo bar") + # string path + c:\\users\\qt\\foo\\bar -> Path("c:\\users\\qt\\foo\\bar") + + Args: + uri: A string containing a file:// uri or path + + Returns: + A pathlib.Path object + """ + if uri.startswith("file://"): + uri = url2pathname(uri.replace("file://", "")) + return Path(uri) + + def uri_exists(uri: str) -> bool: """ Check URI and return whether the location exists, log the errors if any @@ -77,9 +100,10 @@ def uri_exists(uri: str) -> bool: Returns: True if the file exists at the given URI location, otherwise False """ - # check first if the url points to file on local file system - if Path(uri.replace("file://", "")).resolve().is_file(): - return True + # check first if the url points to file on local file system (i.e. not http) + if not uri.startswith(("http://", "https://")): + # convert file URI to pathname + return file_uri_to_path(uri).resolve().is_file() try: with requests.head(uri, timeout=30, stream=True) as res: res.raise_for_status() |