[Notes] [Git][BuildStream/buildstream][mac_fixes] Stopped safe_remove attempting to unlink dirs



Title: GitLab

Phillip Smyth pushed to branch mac_fixes at BuildStream / buildstream

Commits:

1 changed file:

Changes:

  • buildstream/utils.py
    ... ... @@ -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,27 @@ 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
    -
    
    333
    -        # Try to remove anything that is in the way, but issue
    
    334
    -        # a warning instead if it removes a non empty directory
    
    332
    +    if lexists(path):
    
    333
    +        if not S_ISDIR(os.lstat(path).st_mode):
    
    334
    +    
    
    335
    +            # Try to remove anything that is in the way, but issue
    
    336
    +            # a warning instead if it removes a non empty directory
    
    337
    +            try:
    
    338
    +                os.unlink(path)
    
    339
    +                return True
    
    340
    +            except OSError as e:
    
    341
    +                raise UtilError("Failed to remove '{}': {}"
    
    342
    +                                .format(path, e))
    
    343
    +    
    
    335 344
             try:
    
    336
    -            os.unlink(path)
    
    345
    +            os.rmdir(path)
    
    337 346
             except OSError as e:
    
    338
    -            if e.errno != errno.EISDIR:
    
    347
    +            if e.errno == errno.ENOTEMPTY:
    
    348
    +                return False
    
    349
    +            else:
    
    339 350
                     raise UtilError("Failed to remove '{}': {}"
    
    340 351
                                     .format(path, e))
    
    341
    -
    
    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
    -
    
    352
    +    
    
    351 353
         return True
    
    352 354
     
    
    353 355
     
    



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]