Skip to content

Commit

Permalink
Move shutil.rmtree() dance to new function delete_directory(), in new…
Browse files Browse the repository at this point in the history
… file misc_utils.py
  • Loading branch information
julianneswinoga committed Dec 19, 2023
1 parent 84b26a4 commit d4ed760
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
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
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)

0 comments on commit d4ed760

Please sign in to comment.