Skip to content

Commit

Permalink
Merge pull request #1 from ramonaoptics/first_push
Browse files Browse the repository at this point in the history
First push
  • Loading branch information
hmaarrfk authored Oct 30, 2021
2 parents d745ac4 + 7350cc7 commit 795c302
Show file tree
Hide file tree
Showing 14 changed files with 2,961 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
multiuserfilelock/_version.py export-subst
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.egg-info
.pymon
.tox
dist
build
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: python
python:
- 3.9
- 3.8
- 3.7
install:
- pip install -U tox-travis
script: tox
deploy:
provider: pypi
user: __token__
distributions: sdist bdist_wheel
on:
tags: true
python: 3.7
all_branches: true
password:
secure: YOZIZf+k4u6OuWTDrgl/GOPhK7jEaQiS2iSXCnGUFCcuFL0eKVqxqJQFnI4LUYVUZKyEPLPyESKsvjlj33O3MnetDdh6Jr249kvAK9y/VVBIIIk0XqC2sDqxSj6h7dP33dXfwfmCAMbrR3xMVUUoL0uaTMbQHebhDGG/8yLMxAdnYKXpdTkVeP0vg4Ar2BCXnsR1lbAPW1Co+lSMPiLltIIf3HpFqsEH0C1Nnsbl4nhg5xwsz8Rv1lNXQkfCmlvzJ9RVssF0vQOf5D+VOg0ABKGzWM69Yh3cZjLTpge2Oy8FHJzCmlepa5tH7BlbHYImgSREiOPplUTBiQZdju6X8+QZJathJBFRKClPEJjPfXxGZkcmv+vHWYfEq1bpJxezw3zOprpzdf2S809MolIP3veW4WVaXABhItebKAISZLY1o8OL9Q6UywGnhzg7HJ04bZuVTe0b7WGZJabvmksv4MrEH1SRHKkoVQXJV1DkTBf5dOp7uvE3EAMwZ1msJ9K+4qKavArYc8Hwj6WjytQCB6uY5Z1kDlxU1sHLDhzlj+oPcDzqxt5RBRiJ1cusHCBq/n03o1UdvhvLifE+A0OGS1EHRlYRaloQsJkIlZSPWGR2M7ruEgcrltdgrNbaKbR6lNYjzVGknCp8QC7DifVAFwOZ6hwBcmzwCm58cyrT9Vk=
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include versioneer.py
include multiuserfilelock/_version.py
include LICENSE
84 changes: 84 additions & 0 deletions multiuserfilelock/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from . import _version
__version__ = _version.get_versions()['version']

from filelock import FileLock, Timeout
import os
import shutil
import tempfile
from pathlib import Path

# Creating the locks directly in the /tmp dir on linux causes
# many problems since the `/tmp` dir has the sticky bit enabled.
# The sticky bit stops an other user from deleting your file.
# It seems that this stops some other user from opening a file for
#
# We tried to use `SoftFileLock` and while it worked,
# SoftFileLocks did not automatically get deleted upon the crash
# or force quit of a python console.
# This made them really problematic when closing python quickly.
#
# Therefore, we store all our locks in a subdirectory
# On linux we set the group of this MultiUserFileLock to
# the desired group so that multiple people
if os.name == 'nt':
# Windows has a pretty bad per use /tmp directory
# We therefore use the public directory, and create a tempdir in there
tmpdir = Path(os.environ.get('public', r'C:\Users\Public')) / 'tmp'
else:
tmpdir = tempfile.gettempdir()


class MultiUserFileLock(FileLock):
def __init__(self, *args, user=None, group=None, chmod=0o666, **kwargs):
if os.name == 'nt':
self._user = None
self._group = None
else:
self._user = user
self._group = group
self._chmod = chmod
# Will create a ._lock_file object
# but will not create the files on the file system
super().__init__(*args, **kwargs)
self._lock_file_path = Path(self._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)

if self._group is not None and parent.group() != self._group:
shutil.chown(parent, group=self._group)

# Changing the owner in the tmp directory is hard..
if self._user is not None and parent.owner() != self._user:
shutil.chown(parent, user=self._user)

def acquire(self, *args, **kwargs):
super().acquire(*args, **kwargs)
# once the lock has been acquired, we are more guaranteed that the
# _lock_file exists
if self._chmod:
desired_permissions = self._chmod
current_permissions = self._lock_file_path.stat().st_mode

missing_permissions = (
current_permissions & desired_permissions
) ^ desired_permissions

# changing permissions can be tricky, so only change them
# if we need to
if missing_permissions != 0:
# Make sure the parent directory can be written to by others
self._lock_file_path.chmod(desired_permissions)
if self._group is not None:
if self._lock_file_path.group() != self._group:
shutil.chown(self._lock_file_path, group=self._group)
if self._user is not None:
if self._lock_file_path.owner() != self._user:
shutil.chown(self._lock_file_path, user=self._user)


__all__ = [
'MultiUserFileLock',
'Timeout',
]
Loading

0 comments on commit 795c302

Please sign in to comment.