diff options
Diffstat (limited to 'mesonbuild/utils/posix.py')
-rw-r--r-- | mesonbuild/utils/posix.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/mesonbuild/utils/posix.py b/mesonbuild/utils/posix.py index e8387ba..a601dee 100644 --- a/mesonbuild/utils/posix.py +++ b/mesonbuild/utils/posix.py @@ -10,23 +10,33 @@ import fcntl import typing as T from .core import MesonException -from .platform import BuildDirLock as BuildDirLockBase +from .platform import DirectoryLockBase, DirectoryLockAction -__all__ = ['BuildDirLock'] +__all__ = ['DirectoryLock', 'DirectoryLockAction'] -class BuildDirLock(BuildDirLockBase): +class DirectoryLock(DirectoryLockBase): def __enter__(self) -> None: - self.lockfile = open(self.lockfilename, 'w', encoding='utf-8') + self.lockfile = open(self.lockpath, 'w+', encoding='utf-8') try: - fcntl.flock(self.lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB) - except (BlockingIOError, PermissionError): + flags = fcntl.LOCK_EX + if self.action != DirectoryLockAction.WAIT: + flags = flags | fcntl.LOCK_NB + fcntl.flock(self.lockfile, flags) + except BlockingIOError: self.lockfile.close() - raise MesonException('Some other Meson process is already using this build directory. Exiting.') + if self.action == DirectoryLockAction.IGNORE: + return + raise MesonException(self.err) + except PermissionError: + self.lockfile.close() + raise MesonException(self.err) except OSError as e: self.lockfile.close() - raise MesonException(f'Failed to lock the build directory: {e.strerror}') + raise MesonException(f'Failed to lock directory {self.lockpath}: {e.strerror}') def __exit__(self, *args: T.Any) -> None: + if self.lockfile is None or self.lockfile.closed: + return fcntl.flock(self.lockfile, fcntl.LOCK_UN) self.lockfile.close() |