1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009 Evan Fosmark
4 # Copyright (c) 2014 kaliko <kaliko@azylum.org>
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.
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.
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/>.
21 # https://github.com/dmfrey/FileLock
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
35 class FileLockException(Exception):
36 """FileLock Exception"""
40 """ A plain file lock whit context-manager"""
42 def __init__(self, file_name, timeout=10, delay=.05):
45 Setup timeout and the delay.
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
56 """Acquire the lock, if possible.
58 start_time = time.time()
61 self.filedsc = os.open(self.lockfile,
62 os.O_CREAT|os.O_EXCL|os.O_RDWR)
64 except OSError as err:
65 if err.errno != errno.EEXIST:
67 if (time.time() - start_time) >= self.timeout:
68 raise FileLockException('Timeout occured.')
69 time.sleep(self.delay)
76 os.close(self.filedsc)
77 os.unlink(self.lockfile)
78 self.is_locked = False
81 """start of the with statement.
83 if not self.is_locked:
87 def __exit__(self, type, value, traceback):
88 """end of the with statement