You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by no...@apache.org on 2020/12/29 12:55:21 UTC
[buildstream] 07/09: utils.py: reworked safe_remove
This is an automated email from the ASF dual-hosted git repository.
not-in-ldap pushed a commit to branch workspace_list_error_message
in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 3a1e27af54dd47aa54c071a32ceebe58bb21193d
Author: knownexus <ph...@codethink.co.uk>
AuthorDate: Mon Sep 3 16:22:35 2018 +0100
utils.py: reworked safe_remove
non-Linux platforms don't return EISDIR when attempting to unlink a directory
Stopped safe_remove attempting to unlink dirs
Previously safe_remove would attempt to unlink a path
Before attempting to remove it if it was a dir
Now it checks for a dir before that step
---
buildstream/utils.py | 37 ++++++++++++++++++-------------------
1 file changed, 18 insertions(+), 19 deletions(-)
diff --git a/buildstream/utils.py b/buildstream/utils.py
index 60211f3..1e04a31 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -35,6 +35,7 @@ import tempfile
import itertools
import functools
from contextlib import contextmanager
+from stat import S_ISDIR
import psutil
@@ -328,27 +329,25 @@ def safe_remove(path):
Raises:
UtilError: In the case of unexpected system call failures
"""
- if os.path.lexists(path):
-
- # Try to remove anything that is in the way, but issue
- # a warning instead if it removes a non empty directory
- try:
+ try:
+ if S_ISDIR(os.lstat(path).st_mode):
+ os.rmdir(path)
+ else:
os.unlink(path)
- except OSError as e:
- if e.errno != errno.EISDIR:
- raise UtilError("Failed to remove '{}': {}"
- .format(path, e))
-
- try:
- os.rmdir(path)
- except OSError as e:
- if e.errno == errno.ENOTEMPTY:
- return False
- else:
- raise UtilError("Failed to remove '{}': {}"
- .format(path, e))
- return True
+ # File removed/unlinked successfully
+ return True
+
+ except OSError as e:
+ if e.errno == errno.ENOTEMPTY:
+ # Path is non-empty directory
+ return False
+ elif e.errno == errno.ENOENT:
+ # Path does not exist
+ return True
+
+ raise UtilError("Failed to remove '{}': {}"
+ .format(path, e))
def copy_files(src, dest, *, files=None, ignore_missing=False, report_written=False):