diff --git a/OATFWGUI/gui_logic.py b/OATFWGUI/gui_logic.py index 1070dfb..6921711 100644 --- a/OATFWGUI/gui_logic.py +++ b/OATFWGUI/gui_logic.py @@ -4,8 +4,6 @@ import zipfile import json import shutil -import os -import stat from typing import List, Optional from pathlib import Path @@ -21,6 +19,7 @@ from external_processes import external_processes, get_install_dir from gui_state import LogicState, PioEnv, FWVersion from anon_usage_data import AnonStatsDialog, create_anon_stats, upload_anon_stats +from misc_utils import delete_directory log = logging.getLogger('') @@ -66,17 +65,11 @@ def download_fw(zip_url: str) -> Path: def extract_fw(zipfile_name: Path) -> Path: - def remove_readonly(func, path, excinfo): - # Windows has a problem with deleting some git files - log.debug(f'Problem removing {path}, attempting to make writable') - os.chmod(path, stat.S_IWRITE) - func(path) - # For Windows path length reasons, keep the firmware folder name short fw_dir = Path(get_install_dir(), 'OATFW') if fw_dir.exists(): log.info(f'Removing previously downloaded FW from {fw_dir}') - shutil.rmtree(fw_dir, onerror=remove_readonly) + delete_directory(fw_dir) log.info(f'Extracting FW from {zipfile_name}') with zipfile.ZipFile(zipfile_name, 'r') as zip_ref: diff --git a/OATFWGUI/main.py b/OATFWGUI/main.py index b7b4607..b36adf1 100755 --- a/OATFWGUI/main.py +++ b/OATFWGUI/main.py @@ -24,6 +24,7 @@ from platform_check import get_platform, PlatformEnum from external_processes import external_processes, add_external_process, get_install_dir from anon_usage_data import create_anon_stats +from misc_utils import delete_directory parser = argparse.ArgumentParser(usage='Graphical way to build and load OAT Firmware') parser.add_argument('--no-gui', action='store_true', @@ -61,10 +62,21 @@ def setup_environment(): # Putting the platformio core directory in a temporary folder is only needed because # Windows doesn't support long path names... :/ - pio_core_dir = Path(tempfile.gettempdir(), f'.pioOATFWGUI{__version__}') + tempdir_path = Path(tempfile.gettempdir()) + pio_prefix_str = '.pioOATFWGUI' + pio_core_dir = Path(tempdir_path, f'{pio_prefix_str}{__version__}') log.info(f'Setting PLATFORMIO_CORE_DIR to {pio_core_dir}') os.environ['PLATFORMIO_CORE_DIR'] = str(pio_core_dir) + log.debug('Checking for previous OATFWGUI pio core installs...') + for temp_path in tempdir_path.iterdir(): + is_dir = temp_path.is_dir() + is_oatfwgui_core_dir = pio_prefix_str in temp_path.name + not_current_core_dir = temp_path.name != pio_core_dir + if is_dir and is_oatfwgui_core_dir and not_current_core_dir: + log.info(f'Removing other pio core directory:{temp_path.name}') + delete_directory(temp_path) + python_interpreter_path = Path(sys.executable) log.debug(f'Python interpreter: {python_interpreter_path}') python_interpreter_dir = python_interpreter_path.parent diff --git a/OATFWGUI/misc_utils.py b/OATFWGUI/misc_utils.py new file mode 100644 index 0000000..1e682ce --- /dev/null +++ b/OATFWGUI/misc_utils.py @@ -0,0 +1,18 @@ +import os +import stat +import shutil +import logging +from pathlib import Path +from typing import Callable + +log = logging.getLogger('') + + +def delete_directory(dir_to_delete: Path): + def remove_readonly(func: Callable, path, excinfo): + # Windows has a problem with deleting some git files + log.debug(f'Problem removing {path}, attempting to make writable') + os.chmod(path, stat.S_IWRITE) + func(path) + + shutil.rmtree(dir_to_delete, onerror=remove_readonly)