flockfile 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
flockfile/__init__.py ADDED
@@ -0,0 +1,66 @@
1
+ import fcntl
2
+ import os
3
+
4
+ try:
5
+ from ._version import version as __version__
6
+ except ImportError: # _version.py is generated by setuptools-scm at build time
7
+ __version__ = "0.0.0+unknown"
8
+
9
+
10
+ class FileNotLocked(Exception):
11
+ pass
12
+
13
+
14
+ class FlockFile:
15
+ def check_lock(self):
16
+ """Returns true if locked, false if not"""
17
+
18
+ if hasattr(self.lockfd, "read"):
19
+ # is a file type object, return whether or not it is open
20
+ return not self.lockfd.closed
21
+
22
+ else:
23
+ return False
24
+
25
+ def unlock(self):
26
+ """unlock and delete the file"""
27
+
28
+ self.lockfd.close()
29
+ self.lockfd = None
30
+
31
+ if self.delete_on_unlock:
32
+ os.remove(self.lock_file)
33
+
34
+ def lock(self):
35
+ """create and lock the file"""
36
+
37
+ # if it's a directory, crash
38
+ if os.path.isdir(self.lock_file):
39
+ raise RuntimeError("The lock file path is a directory.")
40
+
41
+ # touch the file if it doesn't exist.
42
+ if not os.path.isfile(self.lock_file):
43
+ open(self.lock_file, "a").close()
44
+
45
+ try:
46
+ self.lockfd = open(self.lock_file, "w")
47
+ fcntl.flock(self.lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
48
+ except IOError:
49
+ raise FileNotLocked("Unable to obtain lock on {}".format(self.lock_file))
50
+
51
+ def __init__(self, file_name, lock_dir="/tmp", delete_on_unlock=True):
52
+
53
+ self.lock_file = os.path.join(lock_dir, file_name)
54
+ self.lockfd = None
55
+ self.delete_on_unlock = delete_on_unlock
56
+
57
+ def __enter__(self):
58
+ """acquire the lock on entering a 'with' block"""
59
+
60
+ self.lock()
61
+ return self
62
+
63
+ def __exit__(self, exc_type, exc_value, traceback):
64
+ """release the lock on leaving a 'with' block"""
65
+
66
+ self.unlock()
flockfile/_version.py ADDED
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '0.2.0'
22
+ __version_tuple__ = version_tuple = (0, 2, 0)
23
+
24
+ __commit_id__ = commit_id = 'g9aa2e5e4b'
@@ -0,0 +1,74 @@
1
+ Metadata-Version: 2.4
2
+ Name: flockfile
3
+ Version: 0.2.0
4
+ Summary: A simple lock file class based on file locking.
5
+ Author-email: Ryan Veach <rveach@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://gitlab.com/rveach/flockfile
8
+ Project-URL: Source, https://gitlab.com/rveach/flockfile
9
+ Project-URL: Tracker, https://gitlab.com/rveach/flockfile/issues
10
+ Keywords: Lock,Flock
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: POSIX
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Dynamic: license-file
24
+
25
+ # flockfile
26
+
27
+ Just a simple python lock file class using file locks.
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ pip install flockfile
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ ```python
38
+ from flockfile import FlockFile, FileNotLocked
39
+
40
+ # init the class using a file name
41
+ # optionally, the directory can be set to something other than /tmp
42
+ lock = FlockFile("myscriptname", lock_dir="/tmp")
43
+
44
+
45
+ # lock the file
46
+ lock.lock()
47
+ # if this is unsuccessful, exception FileNotLocked will be raised.
48
+
49
+ # the following class method can be used to check lock status
50
+ assert lock.check_lock()
51
+
52
+ # unlock and delete file
53
+ lock.unlock()
54
+ ```
55
+
56
+ `FlockFile` can also be used as a context manager. The lock is acquired on entry
57
+ (raising `FileNotLocked` if it cannot be obtained) and released on exit:
58
+
59
+ ```python
60
+ from flockfile import FlockFile
61
+
62
+ with FlockFile("myscriptname", lock_dir="/tmp"):
63
+ # the lock is held here
64
+ ...
65
+ # the lock is released and the file deleted here
66
+ ```
67
+
68
+ ## Questions or Issues?
69
+
70
+ Please report as a Gitlab issue: https://gitlab.com/rveach/flockfile/issues
71
+
72
+ ## A note on the use of AI
73
+
74
+ This library was initally created by hand and uploaded to PyPI in 2019. A lot has changed, so I've used AI to add a context manager and modernize the toolset. All commits using AI have been tagged as such.
@@ -0,0 +1,9 @@
1
+ flockfile/__init__.py,sha256=Oikk0D2HQfFHsdOHR9GOCzUFjHIshRye6hteqD4geqU,1785
2
+ flockfile/_version.py,sha256=BGaan9phX84Hg6IlmJ3DbSotRDOdun6tT3UzeOGU88U,528
3
+ flockfile-0.2.0.dist-info/licenses/LICENSE,sha256=RZFVoVXbutbnAJdDAfcEyGKeztds2UAKMw55D8t3GXo,1067
4
+ flockfile-0.2.0.dist-info/METADATA,sha256=jLzVQnoOYTMBFuZDpq8SjzzjTH_Xbb0H1kflyKEKefI,2162
5
+ flockfile-0.2.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
6
+ flockfile-0.2.0.dist-info/scm_file_list.json,sha256=UyFCRzwp0pHNmWsjrOfxu_YX7fJWA2wHGdQr2Sjye1o,237
7
+ flockfile-0.2.0.dist-info/scm_version.json,sha256=fz6CYWSUmv_TkTl1MZURcMwZ3g-K8oYMr5aCqvhb30g,160
8
+ flockfile-0.2.0.dist-info/top_level.txt,sha256=C-TDR3EtjR1bB4ns5cB8sCp4PknEHr5Qs2KXlvaIkB4,10
9
+ flockfile-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Ryan Veach
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,14 @@
1
+ {
2
+ "files": [
3
+ ".gitignore",
4
+ ".gitlab-ci.yml",
5
+ ".pre-commit-config.yaml",
6
+ "README.md",
7
+ "CLAUDE.md",
8
+ "uv.lock",
9
+ "pyproject.toml",
10
+ "LICENSE",
11
+ "tests/test_flockfile.py",
12
+ "flockfile/__init__.py"
13
+ ]
14
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "tag": "0.2.0",
3
+ "distance": 0,
4
+ "node": "g9aa2e5e4b5df80aebafcd45ef50a390e9d1ceed8",
5
+ "dirty": false,
6
+ "branch": "HEAD",
7
+ "node_date": "2026-07-16"
8
+ }
@@ -0,0 +1 @@
1
+ flockfile