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

Delete previous pio core install directories #39

Merged
merged 2 commits into from
Dec 19, 2023
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
11 changes: 2 additions & 9 deletions OATFWGUI/gui_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import zipfile
import json
import shutil
import os
import stat
from typing import List, Optional
from pathlib import Path

Expand All @@ -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('')

Expand Down Expand Up @@ -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:
Expand Down
14 changes: 13 additions & 1 deletion OATFWGUI/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions OATFWGUI/misc_utils.py
Original file line number Diff line number Diff line change
@@ -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)