annotate env/lib/python3.9/site-packages/filelock.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
1 # This is free and unencumbered software released into the public domain.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
2 #
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
3 # Anyone is free to copy, modify, publish, use, compile, sell, or
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
4 # distribute this software, either in source code form or as a compiled
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
5 # binary, for any purpose, commercial or non-commercial, and by any
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
6 # means.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
7 #
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
8 # In jurisdictions that recognize copyright laws, the author or authors
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
9 # of this software dedicate any and all copyright interest in the
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
10 # software to the public domain. We make this dedication for the benefit
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
11 # of the public at large and to the detriment of our heirs and
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
12 # successors. We intend this dedication to be an overt act of
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
13 # relinquishment in perpetuity of all present and future rights to this
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
14 # software under copyright law.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
15 #
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
19 # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
20 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
21 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
22 # OTHER DEALINGS IN THE SOFTWARE.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
23 #
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
24 # For more information, please refer to <http://unlicense.org>
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
25
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
26 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
27 A platform independent file lock that supports the with-statement.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
28 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
29
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
30
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
31 # Modules
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
32 # ------------------------------------------------
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
33 import logging
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
34 import os
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
35 import threading
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
36 import time
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
37 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
38 import warnings
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
39 except ImportError:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
40 warnings = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
41
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
42 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
43 import msvcrt
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
44 except ImportError:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
45 msvcrt = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
46
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
47 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
48 import fcntl
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
49 except ImportError:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
50 fcntl = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
51
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
52
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
53 # Backward compatibility
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
54 # ------------------------------------------------
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
55 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
56 TimeoutError
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
57 except NameError:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
58 TimeoutError = OSError
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
59
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
60
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
61 # Data
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
62 # ------------------------------------------------
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
63 __all__ = [
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
64 "Timeout",
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
65 "BaseFileLock",
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
66 "WindowsFileLock",
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
67 "UnixFileLock",
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
68 "SoftFileLock",
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
69 "FileLock"
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
70 ]
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
71
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
72 __version__ = "3.0.12"
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
73
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
74
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
75 _logger = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
76 def logger():
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
77 """Returns the logger instance used in this module."""
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
78 global _logger
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
79 _logger = _logger or logging.getLogger(__name__)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
80 return _logger
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
81
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
82
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
83 # Exceptions
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
84 # ------------------------------------------------
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
85 class Timeout(TimeoutError):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
86 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
87 Raised when the lock could not be acquired in *timeout*
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
88 seconds.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
89 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
90
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
91 def __init__(self, lock_file):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
92 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
93 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
94 #: The path of the file lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
95 self.lock_file = lock_file
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
96 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
97
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
98 def __str__(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
99 temp = "The file lock '{}' could not be acquired."\
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
100 .format(self.lock_file)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
101 return temp
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
102
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
103
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
104 # Classes
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
105 # ------------------------------------------------
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
106
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
107 # This is a helper class which is returned by :meth:`BaseFileLock.acquire`
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
108 # and wraps the lock to make sure __enter__ is not called twice when entering
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
109 # the with statement.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
110 # If we would simply return *self*, the lock would be acquired again
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
111 # in the *__enter__* method of the BaseFileLock, but not released again
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
112 # automatically.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
113 #
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
114 # :seealso: issue #37 (memory leak)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
115 class _Acquire_ReturnProxy(object):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
116
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
117 def __init__(self, lock):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
118 self.lock = lock
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
119 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
120
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
121 def __enter__(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
122 return self.lock
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
123
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
124 def __exit__(self, exc_type, exc_value, traceback):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
125 self.lock.release()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
126 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
127
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
128
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
129 class BaseFileLock(object):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
130 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
131 Implements the base class of a file lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
132 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
133
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
134 def __init__(self, lock_file, timeout = -1):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
135 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
136 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
137 # The path to the lock file.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
138 self._lock_file = lock_file
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
139
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
140 # The file descriptor for the *_lock_file* as it is returned by the
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
141 # os.open() function.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
142 # This file lock is only NOT None, if the object currently holds the
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
143 # lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
144 self._lock_file_fd = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
145
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
146 # The default timeout value.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
147 self.timeout = timeout
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
148
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
149 # We use this lock primarily for the lock counter.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
150 self._thread_lock = threading.Lock()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
151
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
152 # The lock counter is used for implementing the nested locking
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
153 # mechanism. Whenever the lock is acquired, the counter is increased and
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
154 # the lock is only released, when this value is 0 again.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
155 self._lock_counter = 0
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
156 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
157
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
158 @property
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
159 def lock_file(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
160 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
161 The path to the lock file.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
162 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
163 return self._lock_file
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
164
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
165 @property
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
166 def timeout(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
167 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
168 You can set a default timeout for the filelock. It will be used as
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
169 fallback value in the acquire method, if no timeout value (*None*) is
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
170 given.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
171
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
172 If you want to disable the timeout, set it to a negative value.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
173
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
174 A timeout of 0 means, that there is exactly one attempt to acquire the
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
175 file lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
176
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
177 .. versionadded:: 2.0.0
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
178 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
179 return self._timeout
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
180
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
181 @timeout.setter
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
182 def timeout(self, value):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
183 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
184 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
185 self._timeout = float(value)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
186 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
187
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
188 # Platform dependent locking
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
189 # --------------------------------------------
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
190
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
191 def _acquire(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
192 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
193 Platform dependent. If the file lock could be
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
194 acquired, self._lock_file_fd holds the file descriptor
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
195 of the lock file.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
196 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
197 raise NotImplementedError()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
198
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
199 def _release(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
200 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
201 Releases the lock and sets self._lock_file_fd to None.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
202 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
203 raise NotImplementedError()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
204
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
205 # Platform independent methods
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
206 # --------------------------------------------
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
207
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
208 @property
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
209 def is_locked(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
210 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
211 True, if the object holds the file lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
212
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
213 .. versionchanged:: 2.0.0
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
214
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
215 This was previously a method and is now a property.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
216 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
217 return self._lock_file_fd is not None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
218
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
219 def acquire(self, timeout=None, poll_intervall=0.05):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
220 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
221 Acquires the file lock or fails with a :exc:`Timeout` error.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
222
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
223 .. code-block:: python
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
224
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
225 # You can use this method in the context manager (recommended)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
226 with lock.acquire():
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
227 pass
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
228
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
229 # Or use an equivalent try-finally construct:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
230 lock.acquire()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
231 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
232 pass
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
233 finally:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
234 lock.release()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
235
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
236 :arg float timeout:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
237 The maximum time waited for the file lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
238 If ``timeout < 0``, there is no timeout and this method will
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
239 block until the lock could be acquired.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
240 If ``timeout`` is None, the default :attr:`~timeout` is used.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
241
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
242 :arg float poll_intervall:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
243 We check once in *poll_intervall* seconds if we can acquire the
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
244 file lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
245
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
246 :raises Timeout:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
247 if the lock could not be acquired in *timeout* seconds.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
248
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
249 .. versionchanged:: 2.0.0
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
250
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
251 This method returns now a *proxy* object instead of *self*,
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
252 so that it can be used in a with statement without side effects.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
253 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
254 # Use the default timeout, if no timeout is provided.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
255 if timeout is None:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
256 timeout = self.timeout
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
257
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
258 # Increment the number right at the beginning.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
259 # We can still undo it, if something fails.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
260 with self._thread_lock:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
261 self._lock_counter += 1
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
262
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
263 lock_id = id(self)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
264 lock_filename = self._lock_file
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
265 start_time = time.time()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
266 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
267 while True:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
268 with self._thread_lock:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
269 if not self.is_locked:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
270 logger().debug('Attempting to acquire lock %s on %s', lock_id, lock_filename)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
271 self._acquire()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
272
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
273 if self.is_locked:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
274 logger().info('Lock %s acquired on %s', lock_id, lock_filename)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
275 break
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
276 elif timeout >= 0 and time.time() - start_time > timeout:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
277 logger().debug('Timeout on acquiring lock %s on %s', lock_id, lock_filename)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
278 raise Timeout(self._lock_file)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
279 else:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
280 logger().debug(
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
281 'Lock %s not acquired on %s, waiting %s seconds ...',
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
282 lock_id, lock_filename, poll_intervall
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
283 )
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
284 time.sleep(poll_intervall)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
285 except:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
286 # Something did go wrong, so decrement the counter.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
287 with self._thread_lock:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
288 self._lock_counter = max(0, self._lock_counter - 1)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
289
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
290 raise
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
291 return _Acquire_ReturnProxy(lock = self)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
292
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
293 def release(self, force = False):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
294 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
295 Releases the file lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
296
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
297 Please note, that the lock is only completly released, if the lock
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
298 counter is 0.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
299
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
300 Also note, that the lock file itself is not automatically deleted.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
301
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
302 :arg bool force:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
303 If true, the lock counter is ignored and the lock is released in
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
304 every case.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
305 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
306 with self._thread_lock:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
307
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
308 if self.is_locked:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
309 self._lock_counter -= 1
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
310
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
311 if self._lock_counter == 0 or force:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
312 lock_id = id(self)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
313 lock_filename = self._lock_file
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
314
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
315 logger().debug('Attempting to release lock %s on %s', lock_id, lock_filename)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
316 self._release()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
317 self._lock_counter = 0
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
318 logger().info('Lock %s released on %s', lock_id, lock_filename)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
319
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
320 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
321
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
322 def __enter__(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
323 self.acquire()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
324 return self
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
325
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
326 def __exit__(self, exc_type, exc_value, traceback):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
327 self.release()
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
328 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
329
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
330 def __del__(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
331 self.release(force = True)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
332 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
333
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
334
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
335 # Windows locking mechanism
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
336 # ~~~~~~~~~~~~~~~~~~~~~~~~~
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
337
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
338 class WindowsFileLock(BaseFileLock):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
339 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
340 Uses the :func:`msvcrt.locking` function to hard lock the lock file on
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
341 windows systems.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
342 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
343
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
344 def _acquire(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
345 open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
346
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
347 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
348 fd = os.open(self._lock_file, open_mode)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
349 except OSError:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
350 pass
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
351 else:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
352 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
353 msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
354 except (IOError, OSError):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
355 os.close(fd)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
356 else:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
357 self._lock_file_fd = fd
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
358 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
359
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
360 def _release(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
361 fd = self._lock_file_fd
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
362 self._lock_file_fd = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
363 msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
364 os.close(fd)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
365
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
366 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
367 os.remove(self._lock_file)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
368 # Probably another instance of the application
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
369 # that acquired the file lock.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
370 except OSError:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
371 pass
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
372 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
373
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
374 # Unix locking mechanism
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
375 # ~~~~~~~~~~~~~~~~~~~~~~
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
376
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
377 class UnixFileLock(BaseFileLock):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
378 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
379 Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
380 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
381
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
382 def _acquire(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
383 open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
384 fd = os.open(self._lock_file, open_mode)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
385
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
386 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
387 fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
388 except (IOError, OSError):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
389 os.close(fd)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
390 else:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
391 self._lock_file_fd = fd
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
392 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
393
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
394 def _release(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
395 # Do not remove the lockfile:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
396 #
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
397 # https://github.com/benediktschmitt/py-filelock/issues/31
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
398 # https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
399 fd = self._lock_file_fd
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
400 self._lock_file_fd = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
401 fcntl.flock(fd, fcntl.LOCK_UN)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
402 os.close(fd)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
403 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
404
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
405 # Soft lock
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
406 # ~~~~~~~~~
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
407
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
408 class SoftFileLock(BaseFileLock):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
409 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
410 Simply watches the existence of the lock file.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
411 """
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
412
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
413 def _acquire(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
414 open_mode = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
415 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
416 fd = os.open(self._lock_file, open_mode)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
417 except (IOError, OSError):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
418 pass
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
419 else:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
420 self._lock_file_fd = fd
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
421 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
422
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
423 def _release(self):
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
424 os.close(self._lock_file_fd)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
425 self._lock_file_fd = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
426
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
427 try:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
428 os.remove(self._lock_file)
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
429 # The file is already deleted and that's what we want.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
430 except OSError:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
431 pass
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
432 return None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
433
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
434
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
435 # Platform filelock
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
436 # ~~~~~~~~~~~~~~~~~
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
437
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
438 #: Alias for the lock, which should be used for the current platform. On
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
439 #: Windows, this is an alias for :class:`WindowsFileLock`, on Unix for
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
440 #: :class:`UnixFileLock` and otherwise for :class:`SoftFileLock`.
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
441 FileLock = None
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
442
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
443 if msvcrt:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
444 FileLock = WindowsFileLock
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
445 elif fcntl:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
446 FileLock = UnixFileLock
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
447 else:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
448 FileLock = SoftFileLock
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
449
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
450 if warnings is not None:
4f3585e2f14b "planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
shellac
parents:
diff changeset
451 warnings.warn("only soft file lock is available")