diff options
Diffstat (limited to 'buildstream/utils.py')
-rw-r--r-- | buildstream/utils.py | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py index 60211f35b..1e04a31ed 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): |