From 1a84168f8cf1186b773e52a378bf7fe848a77db4 Mon Sep 17 00:00:00 2001 From: Mark Harfouche Date: Thu, 5 Nov 2026 11:22:19 -0500 Subject: [PATCH 1/2] improve subdirectories --- multiuserfilelock/__init__.py | 19 ++++++++++++++++--- multiuserfilelock/tests/test_acquire.py | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/multiuserfilelock/__init__.py b/multiuserfilelock/__init__.py index 5229a7e..4f2247c 100644 --- a/multiuserfilelock/__init__.py +++ b/multiuserfilelock/__init__.py @@ -48,9 +48,7 @@ def __init__(self, *args, user=None, group=None, chmod=0o666, **kwargs): self._filelock = FileLock(*args, **kwargs) self._lock_file_path = Path(self._filelock.lock_file) parent = self._lock_file_path.parent - # Even though the "other write" permissions are enabled - # it seems that the operating systems disables that for the /tmp dir - parent.mkdir(mode=0o777, parents=True, exist_ok=True) + self._make_directories(parent) if self._group is not None and parent.group() != self._group: shutil.chown(parent, group=self._group) @@ -59,6 +57,21 @@ def __init__(self, *args, user=None, group=None, chmod=0o666, **kwargs): if self._user is not None and parent.owner() != self._user: shutil.chown(parent, user=self._user) + def _make_directories(self, directory): + if directory.is_dir(): + return + self._make_directories(directory.parent) + try: + directory.mkdir(mode=0o777) + except FileExistsError: + return + if os.name != 'nt': + directory.chmod(0o777) + if self._group is not None: + shutil.chown(directory, group=self._group) + if self._user is not None: + shutil.chown(directory, user=self._user) + def acquire(self, *args, **kwargs): self._filelock.acquire(*args, **kwargs) # once the lock has been acquired, we are more guaranteed that the diff --git a/multiuserfilelock/tests/test_acquire.py b/multiuserfilelock/tests/test_acquire.py index 6d5ebe7..b5273a4 100644 --- a/multiuserfilelock/tests/test_acquire.py +++ b/multiuserfilelock/tests/test_acquire.py @@ -1,3 +1,5 @@ +import os +import grp from multiuserfilelock import MultiUserFileLock, Timeout from threading import Thread from queue import Queue @@ -63,3 +65,22 @@ def thread_lock_release(lock): assert not lock_thread2.is_alive() lock2 = results.get(timeout=1) assert lock2.is_locked + + +@pytest.mark.skipif(os.name == 'nt', reason='no unix groups') +def test_nested_directories_get_group_and_mode(tmp_path): + groups = [g for g in os.getgroups() if g != tmp_path.stat().st_gid] + if not groups: + pytest.skip('needs membership in a second group') + group = grp.getgrgid(groups[0]).gr_name + lock_filename = tmp_path / 'a' / 'b' / 'test.lock' + old_umask = os.umask(0o022) + try: + lock = MultiUserFileLock(lock_filename, group=group) + finally: + os.umask(old_umask) + lock.acquire() + lock.release() + for directory in [tmp_path / 'a', tmp_path / 'a' / 'b']: + assert directory.group() == group + assert directory.stat().st_mode & 0o777 == 0o777 From 0724f85d7539c00641263f415fe3015f8ed5c69b Mon Sep 17 00:00:00 2001 From: Mark Harfouche Date: Thu, 5 Nov 2026 11:50:29 -0500 Subject: [PATCH 2/2] improve subdirectories --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe090f7..42915c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.1.0 (2026/11/05) + +- Create every missing parent directory of a lock with the requested group, + owner and mode `0o777`, so that other users can create locks in nested + directories. +- Require Python 3.11 or newer. + ## 0.0.10 (2024/12/31) - Address incompatibility with FileLock 3.15.3 causing the timeout parameter to