Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Windows: Download Osmosis into in Osmosis and not into subfolder, example Osmosis/osmosis-0.49.2 #261

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion wahoomc/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions wahoomc/file_directory_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions wahoomc/setup_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
"""
Expand Down
Loading