From 913d12d1248c50348543646db63abf2b2cf061e3 Mon Sep 17 00:00:00 2001 From: "Benjamin K." <53038537+treee111@users.noreply.github.com> Date: Wed, 16 Oct 2024 12:35:27 +0200 Subject: [PATCH] [FIX] Windows: Download Osmosis into in `Osmosis` and not into subfolder, example `Osmosis/osmosis-0.49.2` (#261) * [FIX] Windows: when unzipping Osmosis download, don't unzip version dir - do directly unzip into Osmosis directory and not in a subdirectory named like the Osmosis version, i.e. 'osmosis-0.49.2' * delete tooling_win files due to not working Osmosis v0.49.2 on Windows * delete folders as well as files * adjust comments --- wahoomc/downloader.py | 26 +++++++++++++++++++++++++- wahoomc/file_directory_functions.py | 20 ++++++++++++++++++++ wahoomc/setup_functions.py | 15 +++++++++++++-- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/wahoomc/downloader.py b/wahoomc/downloader.py index e762c15d..1716ab49 100644 --- a/wahoomc/downloader.py +++ b/wahoomc/downloader.py @@ -61,7 +61,10 @@ def download_file(target_filepath, url, target_dir=""): target_path = USER_DL_DIR # unpack it - unzip(dl_file_path, target_path) + if os.path.basename(target_dir) == 'Osmosis': + unzip_ignore_first_dir(dl_file_path, target_path) + else: + unzip(dl_file_path, target_path) os.remove(dl_file_path) else: @@ -194,6 +197,27 @@ def unzip(source_filename, dest_dir): zip_ref.extractall(dest_dir) +def unzip_ignore_first_dir(source_filename, dest_dir): + """ + unzip the given file into the given directory without the first directory. + made because of Osmosis was unzipped to Osmosis/osmosis-0.49.2 + """ + first_dir_processed = False + with zipfile.ZipFile(source_filename) as zip_file: + for zip_info in zip_file.infolist(): + if zip_info.is_dir() and not first_dir_processed: + # ignore first dir in zip. for osmosis, this is 'osmosis-0.49.2' as of 14.10.2024 + first_dir_processed = True + continue + + # cut out first part of the dir. for osmosis, this is 'osmosis-0.49.2' as of 14.10.2024 + dir_without_first_part = zip_info.filename.split('/', 1)[1] + + # set name where to save to the newly created dir name + zip_info.filename = dir_without_first_part + zip_file.extract(zip_info, dest_dir) + + class Downloader: """ This is the class to check and download maps / artifacts" diff --git a/wahoomc/file_directory_functions.py b/wahoomc/file_directory_functions.py index 0979cda0..29fae38a 100644 --- a/wahoomc/file_directory_functions.py +++ b/wahoomc/file_directory_functions.py @@ -131,6 +131,26 @@ def delete_o5m_pbf_files_in_folder(folder): pass +def delete_everything_in_folder(folder): + """ + delete all files and directories of given folder + """ + files_and_folders = list(os.listdir(folder)) # [f for f in os.listdir(folder)] + + for file in files_and_folders: + try: + file_or_dir = os.path.join(folder, file) + # file or dir? + if os.path.isfile(file_or_dir): + # delete file + os.remove(file_or_dir) + else: + # delete directory if exists. copytree fails if dir exists already + shutil.rmtree(file_or_dir) + except OSError: + pass + + def copy_or_move_files_and_folder(from_path, to_path, delete_from_dir=False): """ copy content from source directory to destination directory diff --git a/wahoomc/setup_functions.py b/wahoomc/setup_functions.py index c7fe5d84..ad735ed7 100644 --- a/wahoomc/setup_functions.py +++ b/wahoomc/setup_functions.py @@ -13,12 +13,12 @@ import pkg_resources # import custom python packages -from wahoomc.file_directory_functions import write_json_file_generic, \ +from wahoomc.file_directory_functions import delete_everything_in_folder, write_json_file_generic, \ read_json_file_generic, copy_or_move_files_and_folder from wahoomc.constants_functions import get_tooling_win_path, get_absolute_dir_user_or_repo from wahoomc.downloader import get_latest_pypi_version -from wahoomc.constants import GEOFABRIK_PATH, USER_WAHOO_MC +from wahoomc.constants import GEOFABRIK_PATH, USER_TOOLING_WIN_DIR, USER_WAHOO_MC from wahoomc.constants import USER_DL_DIR from wahoomc.constants import USER_MAPS_DIR from wahoomc.constants import USER_OUTPUT_DIR @@ -47,6 +47,17 @@ def adjustments_due_to_breaking_changes(): """ version_last_run = read_version_last_run() # pylint: disable=unused-variable + # Osmosis in v.0.49.2 seams not to be working on WINDOWS since the upgrade to v0.49.2 + # - due to the path into it was downloaded, 'tooling_win/Osmosis/osmosis-0.49.2' + # and not 'tooling_win/Osmosis' + # - to cleanup, tooling_win/ dir files and folders are deleted here. + if (version_last_run is None or \ + pkg_resources.parse_version(VERSION) <= pkg_resources.parse_version('4.2.1')) and \ + platform.system() == "Windows": + log.info( + 'Last run was with version %s, deleting Windows Tooling files of %s directory due to possible bad files.', version_last_run, USER_TOOLING_WIN_DIR) + delete_everything_in_folder(USER_TOOLING_WIN_DIR) + def check_installation_of_required_programs(): """