|
| 1 | +from . import _version |
| 2 | +__version__ = _version.get_versions()['version'] |
| 3 | + |
| 4 | +from filelock import FileLock, Timeout |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import tempfile |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +# Creating the locks directly in the /tmp dir on linux causes |
| 11 | +# many problems since the `/tmp` dir has the sticky bit enabled. |
| 12 | +# The sticky bit stops an other user from deleting your file. |
| 13 | +# It seems that this stops some other user from opening a file for |
| 14 | +# |
| 15 | +# We tried to use `SoftFileLock` and while it worked, |
| 16 | +# SoftFileLocks did not automatically get deleted upon the crash |
| 17 | +# or force quit of a python console. |
| 18 | +# This made them really problematic when closing python quickly. |
| 19 | +# |
| 20 | +# Therefore, we store all our locks in a subdirectory |
| 21 | +# On linux we set the group of this MultiUserFileLock to |
| 22 | +# the desired group so that multiple people |
| 23 | +if os.name == 'nt': |
| 24 | + # Windows has a pretty bad per use /tmp directory |
| 25 | + # We therefore use the public directory, and create a tempdir in there |
| 26 | + tmpdir = Path(os.environ.get('public', r'C:\Users\Public')) / 'tmp' |
| 27 | +else: |
| 28 | + tmpdir = tempfile.gettempdir() |
| 29 | + |
| 30 | + |
| 31 | +class MultiUserFileLock(FileLock): |
| 32 | + def __init__(self, *args, user=None, group=None, chmod=0o666, **kwargs): |
| 33 | + if os.name == 'nt': |
| 34 | + self._user = None |
| 35 | + self._group = None |
| 36 | + else: |
| 37 | + self._user = user |
| 38 | + self._group = group |
| 39 | + self._chmod = chmod |
| 40 | + # Will create a ._lock_file object |
| 41 | + # but will not create the files on the file system |
| 42 | + super().__init__(*args, **kwargs) |
| 43 | + self._lock_file_path = Path(self._lock_file) |
| 44 | + parent = self._lock_file_path.parent |
| 45 | + # Even though the "other write" permissions are enabled |
| 46 | + # it seems that the operating systems disables that for the /tmp dir |
| 47 | + parent.mkdir(mode=0o777, parents=True, exist_ok=True) |
| 48 | + |
| 49 | + if self._group is not None and parent.group() != self._group: |
| 50 | + shutil.chown(parent, group=self._group) |
| 51 | + |
| 52 | + # Changing the owner in the tmp directory is hard.. |
| 53 | + if self._user is not None and parent.owner() != self._user: |
| 54 | + shutil.chown(parent, user=self._user) |
| 55 | + |
| 56 | + def acquire(self, *args, **kwargs): |
| 57 | + super().acquire(*args, **kwargs) |
| 58 | + # once the lock has been acquired, we are more guaranteed that the |
| 59 | + # _lock_file exists |
| 60 | + if self._chmod: |
| 61 | + desired_permissions = self._chmod |
| 62 | + current_permissions = self._lock_file_path.stat().st_mode |
| 63 | + |
| 64 | + missing_permissions = ( |
| 65 | + current_permissions & desired_permissions |
| 66 | + ) ^ desired_permissions |
| 67 | + |
| 68 | + # changing permissions can be tricky, so only change them |
| 69 | + # if we need to |
| 70 | + if missing_permissions != 0: |
| 71 | + # Make sure the parent directory can be written to by others |
| 72 | + self._lock_file_path.chmod(desired_permissions) |
| 73 | + if self._group is not None: |
| 74 | + if self._lock_file_path.group() != self._group: |
| 75 | + shutil.chown(self._lock_file_path, group=self._group) |
| 76 | + if self._user is not None: |
| 77 | + if self._lock_file_path.owner() != self._user: |
| 78 | + shutil.chown(self._lock_file_path, user=self._user) |
| 79 | + |
| 80 | + |
| 81 | +__all__ = [ |
| 82 | + 'MultiUserFileLock', |
| 83 | + 'Timeout', |
| 84 | +] |
0 commit comments