X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Futils%2Ffilelock.py;fp=sima%2Futils%2Ffilelock.py;h=8f7065f7a4ff24af923ee779b6a46eead8775a9b;hb=71500abd7ef16784d027a8a20aa28b06e8a13a4f;hp=0000000000000000000000000000000000000000;hpb=bed2018b162e3493dd5a5a7fe5433af2005a7d0a;p=mpd-sima.git diff --git a/sima/utils/filelock.py b/sima/utils/filelock.py new file mode 100644 index 0000000..8f7065f --- /dev/null +++ b/sima/utils/filelock.py @@ -0,0 +1,95 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2009 Evan Fosmark +# Copyright (c) 2014 Jack Kaliko +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# + +# https://github.com/dmfrey/FileLock +""" +Plain file lock to une in context: + >>> with FileLock('/path/to/file/to/write'): + >>> # a lock file is maintain within the scope of this context: + >>> # /path/to/file/to/write.lock + >>> ... # process file writing +""" + +import errno +import os +import time + +class FileLockException(Exception): + """FileLock Exception""" + pass + +class FileLock: + """ A plain file lock whit context-manager""" + + def __init__(self, file_name, timeout=10, delay=.05): + """ + Setup file lock. + Setup timeout and the delay. + """ + self.filedsc = None + self.is_locked = False + dirname = os.path.dirname(file_name) + self.lockfile = os.path.join(dirname, '{0}.lock'.format(file_name)) + self.file_name = file_name + self.timeout = timeout + self.delay = delay + + def acquire(self): + """Acquire the lock, if possible. + """ + start_time = time.time() + while True: + try: + self.filedsc = os.open(self.lockfile, + os.O_CREAT|os.O_EXCL|os.O_RDWR) + break + except OSError as err: + if err.errno != errno.EEXIST: + raise + if (time.time() - start_time) >= self.timeout: + raise FileLockException('Timeout occured.') + time.sleep(self.delay) + self.is_locked = True + + def release(self): + """Release the lock. + """ + if self.is_locked: + os.close(self.filedsc) + os.unlink(self.lockfile) + self.is_locked = False + + def __enter__(self): + """start of the with statement. + """ + if not self.is_locked: + self.acquire() + return self + + def __exit__(self, type, value, traceback): + """end of the with statement + """ + if self.is_locked: + self.release() + + def __del__(self): + """Cleanup + """ + self.release()