Skip to content

Commit

Permalink
Fix upgraded package not being cleared
Browse files Browse the repository at this point in the history
This commit fixes out-dated files in root directory of unpacked packages
not being cleared due to skipped files in sub-directories causing ENOTEMPTY
exception, when removing top-level directories.

Example:

/dir1/dir2/file    <- if this is ignored and /dir1/ only contains dirs
/plugin.py         <- this wouldn't be deleted as /dir1 was not added
                      to ignored_dirs and removing fails as not being empty

Thus ignore non-empty directories.
  • Loading branch information
deathaxe committed Apr 5, 2024
1 parent 226bd52 commit 90d7850
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions package_control/clear_directory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import errno
import os
import stat
import sys
Expand Down Expand Up @@ -53,21 +54,13 @@ def clear_directory(directory, ignored_files=None, ignore_errors=True):
trash_dir = sys_path.trash_path()
os.makedirs(trash_dir, exist_ok=True)

ignored_dirs = set()
was_exception = False

for root, dirs, files in os.walk(directory, topdown=False):
try:
for d in dirs:
path = os.path.join(root, d)
if path not in ignored_dirs:
os.rmdir(path)

for f in files:
path = os.path.normcase(os.path.join(root, f))
if ignored_files and path in ignored_files:
# also ignore parent folder from being removed
ignored_dirs.add(root)
continue

try:
Expand All @@ -85,7 +78,15 @@ def clear_directory(directory, ignored_files=None, ignore_errors=True):
)
os.rename(path, trash_path)

except OSError:
for d in dirs:
try:
os.rmdir(os.path.join(root, d))
except OSError as e:
if e.errno == errno.ENOTEMPTY:
continue
raise

except OSError as e:
if not ignore_errors:
raise
was_exception = True
Expand Down

0 comments on commit 90d7850

Please sign in to comment.