]> kaliko git repositories - mpd-sima.git/blob - sima/utils/filelock.py
Some refactoring around Exceptions
[mpd-sima.git] / sima / utils / filelock.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 Evan Fosmark
4 # Copyright (c) 2014 kaliko <kaliko@azylum.org>
5 #
6 #   This program is free software: you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation, either version 3 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #
20
21 # https://github.com/dmfrey/FileLock
22 """
23 Plain file lock to une in context:
24     >>> with FileLock('/path/to/file/to/write'):
25     >>>     # a lock file is maintain within the scope of this context:
26     >>>     # /path/to/file/to/write.lock
27     >>>     ... # process file writing
28 """
29
30 import errno
31 import os
32 import time
33
34
35 class FileLockException(Exception):
36     """FileLock Exception"""
37
38
39 class FileLock:
40     """ A plain file lock whit context-manager"""
41
42     def __init__(self, file_name, timeout=10, delay=.05):
43         """
44         Setup file lock.
45         Setup timeout and the delay.
46         """
47         self.filedsc = None
48         self.is_locked = False
49         dirname = os.path.dirname(file_name)
50         self.lockfile = os.path.join(dirname, '{0}.lock'.format(file_name))
51         self.file_name = file_name
52         self.timeout = timeout
53         self.delay = delay
54
55     def acquire(self):
56         """Acquire the lock, if possible.
57         """
58         start_time = time.time()
59         while True:
60             try:
61                 self.filedsc = os.open(self.lockfile,
62                                        os.O_CREAT | os.O_EXCL | os.O_RDWR)
63                 break
64             except OSError as err:
65                 if err.errno != errno.EEXIST:
66                     raise
67                 if (time.time() - start_time) >= self.timeout:
68                     raise FileLockException('Timeout occured.') from err
69                 time.sleep(self.delay)
70         self.is_locked = True
71
72     def release(self):
73         """Release the lock.
74         """
75         if self.is_locked:
76             os.close(self.filedsc)
77             os.unlink(self.lockfile)
78             self.is_locked = False
79
80     def __enter__(self):
81         """start of the with statement.
82         """
83         if not self.is_locked:
84             self.acquire()
85         return self
86
87     def __exit__(self, type, value, traceback):
88         """end of the with statement
89         """
90         if self.is_locked:
91             self.release()
92
93     def __del__(self):
94         """Cleanup
95         """
96         self.release()