Skip to content

Commit

Permalink
[FEATURE] Check for required programs at wahooMapsCreator start (#127)
Browse files Browse the repository at this point in the history
* implement check for required programs for macOS

- incl. unittests

* move get_tooling_win_path to constants_functions

* implement check for required programs for Windows

- incl. unittests

* adjustments after testing on Windows. Extension needed

* verbose output for osmium processing

* catch error if no file exists in plugins directory

* format terminal output when checking for required programs

* move get_tag_wahoo_xml_path to constants_functions

to be identical with changes from eab0345

* Revert "verbose output for osmium processing"

This reverts commit a700f68.
It is not working as intended on macOS // not outputting something. Most likely because of the "subprocess.Popen" stlye of calling the command

* Bump to version v2.1.0a15
  • Loading branch information
treee111 committed Oct 30, 2022
1 parent efacbcc commit bcb2dc0
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 69 deletions.
29 changes: 27 additions & 2 deletions tests/test_constants.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
tests for the downloader file
"""
import os
import unittest
from mock import patch


from wahoomc.constants_functions import translate_country_input_to_geofabrik
from wahoomc.constants_functions import translate_tags_to_keep
from wahoomc.constants_functions import translate_country_input_to_geofabrik, translate_tags_to_keep, \
get_tag_wahoo_xml_path, TagWahooXmlNotFoundError
from wahoomc.constants import RESOURCES_DIR


class TestTranslateCountries(unittest.TestCase):
Expand Down Expand Up @@ -176,5 +178,28 @@ def test_translate_name_tags_to_keep_full_win(self):
self.assertEqual(names_tags_win, transl_tags)


class TestTagWahooXML(unittest.TestCase):
"""
tests for tag-wahoo xml file
"""

def test_not_existing_tag_wahoo_xml(self):
"""
check if a non-existing tag-wahoo xml file issues an exception
"""
with self.assertRaises(TagWahooXmlNotFoundError):
get_tag_wahoo_xml_path("not_existing.xml")

def test_existing_tag_wahoo_xml(self):
"""
check if the correct path of an existing tag-wahoo xml file is returned
"""

expected_path = os.path.join(
RESOURCES_DIR, "tag_wahoo_adjusted", "tag-wahoo-poi.xml")
self.assertEqual(get_tag_wahoo_xml_path(
"tag-wahoo-poi.xml"), expected_path)


if __name__ == '__main__':
unittest.main()
36 changes: 0 additions & 36 deletions tests/test_file_directory.py

This file was deleted.

57 changes: 57 additions & 0 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
tests for setup functions
"""
import unittest
import platform
import os

# import custom python packages
from wahoomc.setup_functions import is_program_installed, is_map_writer_plugin_installed
from wahoomc.constants_functions import get_tooling_win_path


class TestSetup(unittest.TestCase):
"""
tests for the required non-python programs of the python module
"""

def test_installed_programs_mac(self):
"""
tests, if the mac-relevant programs are installed
"""
if platform.system() != "Windows":
self.check_installation_of_program("java")

self.check_installation_of_program("osmium")
self.check_installation_of_program("osmosis")

result = is_map_writer_plugin_installed()
self.assertTrue(result)

def test_installed_programs_windows(self):
"""
tests, if the mac-relevant programs are installed
"""
if platform.system() == "Windows":
self.check_installation_of_program("java")

self.assertTrue(os.path.exists(get_tooling_win_path(
['Osmosis', 'bin', 'osmosis.bat'])))
self.assertTrue(os.path.exists(
get_tooling_win_path(['osmconvert.exe'])))
self.assertTrue(os.path.exists(
get_tooling_win_path(['osmfilter.exe'])))
self.assertTrue(os.path.exists(get_tooling_win_path(['7za.exe'])))

def check_installation_of_program(self, program):
"""
tests, if a given program is installed
"""

result = is_program_installed(program)

self.assertTrue(result)


if __name__ == '__main__':
unittest.main()
26 changes: 26 additions & 0 deletions wahoomc/constants_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
# import custom python packages
from wahoomc import constants
from wahoomc.constants import RESOURCES_DIR
from wahoomc.constants import TOOLING_WIN_DIR

log = logging.getLogger('main-logger')


class TagWahooXmlNotFoundError(Exception):
"""Raised when the specified tag-wahoo xml file does not exist"""


def get_region_of_country(county):
"""
returns the region / continent of a given country
Expand Down Expand Up @@ -148,3 +153,24 @@ def transl_tag_value(sys_platform, separator, tag, value):
to_append = tag

return to_append


def get_tooling_win_path(path_in_tooling_win):
"""
return path to a tooling in the tooling_win directory and the given path
"""
return os.path.join(TOOLING_WIN_DIR, *path_in_tooling_win)


def get_tag_wahoo_xml_path(tag_wahoo_xml):
"""
return path to tag-wahoo xml file if the file exists
"""

path_tag_wahoo_xml = os.path.join(
RESOURCES_DIR, "tag_wahoo_adjusted", tag_wahoo_xml)

if os.path.exists(path_tag_wahoo_xml):
return path_tag_wahoo_xml

raise TagWahooXmlNotFoundError
27 changes: 0 additions & 27 deletions wahoomc/file_directory_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@
import requests

# import custom python packages
from wahoomc.constants import TOOLING_WIN_DIR
from wahoomc.constants import RESOURCES_DIR

log = logging.getLogger('main-logger')


class TagWahooXmlNotFoundError(Exception):
"""Raised when the specified tag-wahoo xml file does not exist"""


def unzip(source_filename, dest_dir):
"""
unzip the given file into the given directory
Expand Down Expand Up @@ -165,24 +159,3 @@ def get_filenames_of_jsons_in_folder(folder):
json_files.extend([filename])

return json_files


def get_tooling_win_path(path_in_tooling_win):
"""
return path to a tooling in the tooling_win directory and the given path
"""
return os.path.join(TOOLING_WIN_DIR, *path_in_tooling_win)


def get_tag_wahoo_xml_path(tag_wahoo_xml):
"""
return path to tag-wahoo xml file if the file exists
"""

path_tag_wahoo_xml = os.path.join(
RESOURCES_DIR, "tag_wahoo_adjusted", tag_wahoo_xml)

if os.path.exists(path_tag_wahoo_xml):
return path_tag_wahoo_xml

raise TagWahooXmlNotFoundError
3 changes: 3 additions & 0 deletions wahoomc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from wahoomc.input import process_call_of_the_tool
from wahoomc.setup_functions import initialize_work_directories
from wahoomc.setup_functions import move_old_content_into_new_dirs
from wahoomc.setup_functions import check_installation_of_required_programs
from wahoomc.osm_maps_functions import OsmMaps
from wahoomc.osm_maps_functions import OsmData

Expand All @@ -27,6 +28,8 @@ def run():
logging.basicConfig(format='%(levelname)s:%(message)s',
level=logging.INFO)

check_installation_of_required_programs()

# handle GUI and CLI processing via one function and different cli-calls
o_input_data = process_call_of_the_tool()

Expand Down
8 changes: 4 additions & 4 deletions wahoomc/osm_maps_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import logging

# import custom python packages
from wahoomc.file_directory_functions import get_tooling_win_path, read_json_file, \
get_folders_in_folder, get_filenames_of_jsons_in_folder, create_empty_directories, \
get_tag_wahoo_xml_path, TagWahooXmlNotFoundError
from wahoomc.constants_functions import get_path_to_static_tile_json, translate_tags_to_keep
from wahoomc.file_directory_functions import read_json_file, \
get_folders_in_folder, get_filenames_of_jsons_in_folder, create_empty_directories
from wahoomc.constants_functions import get_path_to_static_tile_json, translate_tags_to_keep, \
get_tooling_win_path, get_tag_wahoo_xml_path, TagWahooXmlNotFoundError

from wahoomc.constants import USER_WAHOO_MC
from wahoomc.constants import USER_OUTPUT_DIR
Expand Down
81 changes: 81 additions & 0 deletions wahoomc/setup_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
# import official python packages
import os
import logging
import platform
import shutil
from pathlib import Path
import sys

# import custom python packages
from wahoomc.file_directory_functions import move_content
from wahoomc.constants_functions import get_tooling_win_path
from wahoomc.constants import USER_WAHOO_MC
from wahoomc.constants import USER_DL_DIR
from wahoomc.constants import USER_MAPS_DIR
Expand Down Expand Up @@ -38,3 +43,79 @@ def move_old_content_into_new_dirs():
"""
move_content('wahooMapsCreator_download', USER_DL_DIR)
move_content('wahooMapsCreator_output', USER_OUTPUT_DIR)


def check_installation_of_required_programs():
"""
check if required programs are installed
"""
text_to_docu = "\nPlease refer to the Quickstart Guide of wahooMapsCreator for instructions:\n- https://github.com/treee111/wahooMapsCreator/blob/develop/docs/QUICKSTART_ANACONDA.md \
\nor create an issue:\n- https://github.com/treee111/wahooMapsCreator/issues"

if not is_program_installed("java"):
sys.exit(
f"Java is not installed. {text_to_docu}")

if platform.system() == "Windows":
if not os.path.exists(get_tooling_win_path(
['Osmosis', 'bin', 'osmosis.bat'])):
sys.exit(
f"Osmosis is not available. {text_to_docu}")

if not os.path.exists(get_tooling_win_path(['osmconvert.exe'])):
sys.exit(
f"osmconvert is not available. {text_to_docu}")

if not os.path.exists(get_tooling_win_path(['osmfilter.exe'])):
sys.exit(
f"osmfilter is not available. {text_to_docu}")

if not os.path.exists(get_tooling_win_path(['7za.exe'])):
sys.exit(
f"7za is not available. {text_to_docu}")

else:
if not is_program_installed("osmium"):
sys.exit(
f"osmium-tool is not installed. {text_to_docu}")

if not is_program_installed("osmosis"):
sys.exit(
f"Osmosis is not installed. {text_to_docu}")

if not is_map_writer_plugin_installed():
sys.exit(
f"mapsforge-map-writer plugin is not installed. {text_to_docu}")


def is_program_installed(program):
"""
check if a given program is installed
"""
if shutil.which(program) is not None:
return True

return False


def is_map_writer_plugin_installed():
"""
tests, if the mapwriter plugin is in the correct location
Example filename for the map-writer-plugin
mapsforge-map-writer-master-20210527.154736-408-jar-with-dependencies.jar
downloaded on 01.10.2022: mapsforge-map-writer-0.18.0-jar-with-dependencies.jar
"""
map_writer_path = os.path.join(
str(Path.home()), '.openstreetmap', 'osmosis', 'plugins')

# test if the file is there
try:
for file in os.listdir(map_writer_path):
if "mapsforge-map-writer" in file:
return True
# if there is no file in the plugins directory
except FileNotFoundError:
pass

return False

0 comments on commit bcb2dc0

Please sign in to comment.