Skip to content

Commit

Permalink
Avoid failures when trying to remove missing directories
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed Jun 4, 2024
1 parent a775d87 commit a7c1f72
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ffpuppet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,5 @@ def main(argv: Optional[List[str]] = None) -> None: # pylint: disable=missing-d
if ffp.reason == Reason.ALERT or args.save_all:
LOG.info("Browser logs available here '%s'", logs.resolve())
else:
rmtree(logs)
rmtree(logs, ignore_errors=True)
ffp.clean_up()
9 changes: 6 additions & 3 deletions src/ffpuppet/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def __init__(
for cert in cert_files:
self._install_cert(cert, certutil_find(browser_bin))
except Exception:
rmtree(self.path, onerror=onerror)
if self.path.exists():
rmtree(self.path, onerror=onerror)
raise

def __enter__(self) -> "Profile":
Expand Down Expand Up @@ -228,9 +229,11 @@ def remove(self, ignore_errors: bool = True) -> None:
None
"""
if self.path:
LOG.debug("removing profile")
try:
rmtree(self.path, onerror=onerror)
# check if path exists to properly support "onerror"
if self.path.exists():
LOG.debug("removing profile")
rmtree(self.path, onerror=onerror)
except OSError:
LOG.error("Failed to remove profile '%s'", self.path)
# skip raising here instead of passing ignore_errors to rmtree
Expand Down
11 changes: 7 additions & 4 deletions src/ffpuppet/puppet_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ def clean_up(self, ignore_errors: bool = False) -> None:
"""
if not self.closed:
self.close()
if self.path is not None and self.path.is_dir():
for attempt in range(2):
if self.path is not None:
# try multiple attempts to remove data
for attempt in reversed(range(2)):
try:
rmtree(self.path, ignore_errors=ignore_errors, onerror=onerror)
# check if path exists to properly support "onerror"
if self.path.exists():
rmtree(self.path, ignore_errors=ignore_errors, onerror=onerror)
except OSError:
if attempt > 0:
if attempt == 0:
warn_open(self.path)
raise
continue
Expand Down

0 comments on commit a7c1f72

Please sign in to comment.