Phillip Smyth pushed to branch mac_fixes at BuildStream / buildstream
Commits:
-
d7a1041e
by knownexus at 2018-09-07T13:02:25Z
1 changed file:
Changes:
... | ... | @@ -35,6 +35,7 @@ import tempfile |
35 | 35 |
import itertools
|
36 | 36 |
import functools
|
37 | 37 |
from contextlib import contextmanager
|
38 |
+from stat import S_ISDIR
|
|
38 | 39 |
|
39 | 40 |
import psutil
|
40 | 41 |
|
... | ... | @@ -328,26 +329,28 @@ def safe_remove(path): |
328 | 329 |
Raises:
|
329 | 330 |
UtilError: In the case of unexpected system call failures
|
330 | 331 |
"""
|
331 |
- if os.path.lexists(path):
|
|
332 |
+ # return True if path does not exist
|
|
333 |
+ if lexists(path):
|
|
334 |
+ # Check if path is a directory
|
|
335 |
+ if not S_ISDIR(os.lstat(path).st_mode):
|
|
336 |
+ # If path is not a directory, try to unlink
|
|
337 |
+ try:
|
|
338 |
+ os.unlink(path)
|
|
339 |
+ return True
|
|
340 |
+ except OSError as e:
|
|
341 |
+ raise UtilError("Failed to remove '{}': {}"
|
|
342 |
+ .format(path, e))
|
|
332 | 343 |
|
333 |
- # Try to remove anything that is in the way, but issue
|
|
334 |
- # a warning instead if it removes a non empty directory
|
|
344 |
+ # If path is a directory, try to remove
|
|
335 | 345 |
try:
|
336 |
- os.unlink(path)
|
|
346 |
+ os.rmdir(path)
|
|
337 | 347 |
except OSError as e:
|
338 |
- if e.errno != errno.EISDIR:
|
|
348 |
+ if e.errno == errno.ENOTEMPTY:
|
|
349 |
+ return False
|
|
350 |
+ else:
|
|
339 | 351 |
raise UtilError("Failed to remove '{}': {}"
|
340 | 352 |
.format(path, e))
|
341 | 353 |
|
342 |
- try:
|
|
343 |
- os.rmdir(path)
|
|
344 |
- except OSError as e:
|
|
345 |
- if e.errno == errno.ENOTEMPTY:
|
|
346 |
- return False
|
|
347 |
- else:
|
|
348 |
- raise UtilError("Failed to remove '{}': {}"
|
|
349 |
- .format(path, e))
|
|
350 |
- |
|
351 | 354 |
return True
|
352 | 355 |
|
353 | 356 |
|