Skip to content

Commit 913d12d

Browse files
authored
[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
1 parent e5d19bd commit 913d12d

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

wahoomc/downloader.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ def download_file(target_filepath, url, target_dir=""):
6161
target_path = USER_DL_DIR
6262

6363
# unpack it
64-
unzip(dl_file_path, target_path)
64+
if os.path.basename(target_dir) == 'Osmosis':
65+
unzip_ignore_first_dir(dl_file_path, target_path)
66+
else:
67+
unzip(dl_file_path, target_path)
6568

6669
os.remove(dl_file_path)
6770
else:
@@ -194,6 +197,27 @@ def unzip(source_filename, dest_dir):
194197
zip_ref.extractall(dest_dir)
195198

196199

200+
def unzip_ignore_first_dir(source_filename, dest_dir):
201+
"""
202+
unzip the given file into the given directory without the first directory.
203+
made because of Osmosis was unzipped to Osmosis/osmosis-0.49.2
204+
"""
205+
first_dir_processed = False
206+
with zipfile.ZipFile(source_filename) as zip_file:
207+
for zip_info in zip_file.infolist():
208+
if zip_info.is_dir() and not first_dir_processed:
209+
# ignore first dir in zip. for osmosis, this is 'osmosis-0.49.2' as of 14.10.2024
210+
first_dir_processed = True
211+
continue
212+
213+
# cut out first part of the dir. for osmosis, this is 'osmosis-0.49.2' as of 14.10.2024
214+
dir_without_first_part = zip_info.filename.split('/', 1)[1]
215+
216+
# set name where to save to the newly created dir name
217+
zip_info.filename = dir_without_first_part
218+
zip_file.extract(zip_info, dest_dir)
219+
220+
197221
class Downloader:
198222
"""
199223
This is the class to check and download maps / artifacts"

wahoomc/file_directory_functions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ def delete_o5m_pbf_files_in_folder(folder):
131131
pass
132132

133133

134+
def delete_everything_in_folder(folder):
135+
"""
136+
delete all files and directories of given folder
137+
"""
138+
files_and_folders = list(os.listdir(folder)) # [f for f in os.listdir(folder)]
139+
140+
for file in files_and_folders:
141+
try:
142+
file_or_dir = os.path.join(folder, file)
143+
# file or dir?
144+
if os.path.isfile(file_or_dir):
145+
# delete file
146+
os.remove(file_or_dir)
147+
else:
148+
# delete directory if exists. copytree fails if dir exists already
149+
shutil.rmtree(file_or_dir)
150+
except OSError:
151+
pass
152+
153+
134154
def copy_or_move_files_and_folder(from_path, to_path, delete_from_dir=False):
135155
"""
136156
copy content from source directory to destination directory

wahoomc/setup_functions.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import pkg_resources
1414

1515
# import custom python packages
16-
from wahoomc.file_directory_functions import write_json_file_generic, \
16+
from wahoomc.file_directory_functions import delete_everything_in_folder, write_json_file_generic, \
1717
read_json_file_generic, copy_or_move_files_and_folder
1818
from wahoomc.constants_functions import get_tooling_win_path, get_absolute_dir_user_or_repo
1919
from wahoomc.downloader import get_latest_pypi_version
2020

21-
from wahoomc.constants import GEOFABRIK_PATH, USER_WAHOO_MC
21+
from wahoomc.constants import GEOFABRIK_PATH, USER_TOOLING_WIN_DIR, USER_WAHOO_MC
2222
from wahoomc.constants import USER_DL_DIR
2323
from wahoomc.constants import USER_MAPS_DIR
2424
from wahoomc.constants import USER_OUTPUT_DIR
@@ -47,6 +47,17 @@ def adjustments_due_to_breaking_changes():
4747
"""
4848
version_last_run = read_version_last_run() # pylint: disable=unused-variable
4949

50+
# Osmosis in v.0.49.2 seams not to be working on WINDOWS since the upgrade to v0.49.2
51+
# - due to the path into it was downloaded, 'tooling_win/Osmosis/osmosis-0.49.2'
52+
# and not 'tooling_win/Osmosis'
53+
# - to cleanup, tooling_win/ dir files and folders are deleted here.
54+
if (version_last_run is None or \
55+
pkg_resources.parse_version(VERSION) <= pkg_resources.parse_version('4.2.1')) and \
56+
platform.system() == "Windows":
57+
log.info(
58+
'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)
59+
delete_everything_in_folder(USER_TOOLING_WIN_DIR)
60+
5061

5162
def check_installation_of_required_programs():
5263
"""

0 commit comments

Comments
 (0)