From 1c8ff4a51f37598bd05dc13f2a30ec6b2504165c Mon Sep 17 00:00:00 2001 From: Maciej Sikorski Date: Tue, 13 Sep 2022 23:30:54 +0200 Subject: [PATCH] [generator] download_file() now allows URLs with "file" protocol (#3382) * [generator] download_file() now allows URLs with "file" protocol download_file(url) function now allows URLs beginning with "file://" and pointing to a local path. Signed-off-by: Maciej Sikorski * Update tools/python/maps_generator/utils/file.py Co-authored-by: Alexander Borsuk <170263+biodranik@users.noreply.github.com> Signed-off-by: Maciej Sikorski * [generator] remove unnecessary try-except Signed-off-by: Maciej Sikorski * [generator] Update to when to call os.path.expanduser Signed-off-by: Maciej Sikorski * Formatting changed Signed-off-by: Maciej Sikorski Signed-off-by: Maciej Sikorski Co-authored-by: Alexander Borsuk <170263+biodranik@users.noreply.github.com> --- tools/python/maps_generator/utils/file.py | 21 +++++++++++++++++++ .../var/etc/map_generator.ini.default | 1 + 2 files changed, 22 insertions(+) diff --git a/tools/python/maps_generator/utils/file.py b/tools/python/maps_generator/utils/file.py index f97bcfae2a..368abe57d6 100644 --- a/tools/python/maps_generator/utils/file.py +++ b/tools/python/maps_generator/utils/file.py @@ -13,6 +13,7 @@ from typing import Optional from urllib.parse import unquote from urllib.parse import urljoin from urllib.parse import urlparse +from urllib.request import url2pathname import requests from bs4 import BeautifulSoup @@ -24,6 +25,20 @@ from maps_generator.utils.md5 import md5_ext logger = logging.getLogger("maps_generator") +def is_file_uri(url: AnyStr) -> bool: + return urlparse(url).scheme == "file" + +def file_uri_to_path(url : AnyStr) -> AnyStr: + file_uri = urlparse(url) + file_path = file_uri.path + + # URI is something like "file://~/..." + if file_uri.netloc == '~': + file_path = f'~{file_uri.path}' + return os.path.expanduser(file_path) + + return file_path + def is_executable(fpath: AnyStr) -> bool: return os.path.isfile(fpath) and os.access(fpath, os.X_OK) @@ -48,6 +63,12 @@ def download_file(url: AnyStr, name: AnyStr, download_if_exists: bool = True): logger.info(f"File {name} already exists.") return + if is_file_uri(url): + # url uses 'file://' scheme + copy_overwrite(file_uri_to_path(url), name) + logger.info(f"File {name} was copied from {url}.") + return + tmp_name = f"{name}__" os.makedirs(os.path.dirname(tmp_name), exist_ok=True) with requests.Session() as session: diff --git a/tools/python/maps_generator/var/etc/map_generator.ini.default b/tools/python/maps_generator/var/etc/map_generator.ini.default index fdca7b6499..3c9f420288 100644 --- a/tools/python/maps_generator/var/etc/map_generator.ini.default +++ b/tools/python/maps_generator/var/etc/map_generator.ini.default @@ -46,6 +46,7 @@ DIFF_VERSION_DEPTH: 2 [External] # Note: If you want to set a directory name you have to add "/" to the end of url. +# In each field where you need to specify a URL, you can specify the path to the file system using file:///path/to/file # The url to the planet file. # PLANET_URL: