| 
 | 1 | +"""A cross-process lock based on exclusive creation of a given file name"""  | 
 | 2 | +import os  | 
 | 3 | +import sys  | 
 | 4 | +import errno  | 
 | 5 | +import time  | 
 | 6 | +import logging  | 
 | 7 | + | 
 | 8 | + | 
 | 9 | +logger = logging.getLogger(__name__)  | 
 | 10 | + | 
 | 11 | + | 
 | 12 | +class LockError(RuntimeError):  | 
 | 13 | +    """It will be raised when unable to obtain a lock"""  | 
 | 14 | + | 
 | 15 | + | 
 | 16 | +class CrossPlatLock(object):  | 
 | 17 | +    """This implementation relies only on ``open(..., 'x')``"""  | 
 | 18 | +    def __init__(self, lockfile_path):  | 
 | 19 | +        self._lockpath = lockfile_path  | 
 | 20 | + | 
 | 21 | +    def __enter__(self):  | 
 | 22 | +        self._create_lock_file('{} {}'.format(  | 
 | 23 | +            os.getpid(),  | 
 | 24 | +            sys.argv[0],  | 
 | 25 | +            ).encode('utf-8'))  # pylint: disable=consider-using-f-string  | 
 | 26 | +        return self  | 
 | 27 | + | 
 | 28 | +    def _create_lock_file(self, content):  | 
 | 29 | +        timeout = 5  | 
 | 30 | +        check_interval = 0.25  | 
 | 31 | +        current_time = getattr(time, "monotonic", time.time)  | 
 | 32 | +        timeout_end = current_time() + timeout  | 
 | 33 | +        while timeout_end > current_time():  | 
 | 34 | +            try:  | 
 | 35 | +                with open(self._lockpath, 'xb') as lock_file:  # pylint: disable=unspecified-encoding  | 
 | 36 | +                    lock_file.write(content)  | 
 | 37 | +                return None  # Happy path  | 
 | 38 | +            except ValueError:  # This needs to be the first clause, for Python 2 to hit it  | 
 | 39 | +                raise LockError("Python 2 does not support atomic creation of file")  | 
 | 40 | +            except FileExistsError:  # Only Python 3 will reach this clause  | 
 | 41 | +                logger.debug(  | 
 | 42 | +                    "Process %d found existing lock file, will retry after %f second",  | 
 | 43 | +                    os.getpid(), check_interval)  | 
 | 44 | +                time.sleep(check_interval)  | 
 | 45 | +        raise LockError(  | 
 | 46 | +            "Unable to obtain lock, despite trying for {} second(s). "  | 
 | 47 | +            "You may want to manually remove the stale lock file {}".format(  | 
 | 48 | +                timeout,  | 
 | 49 | +                self._lockpath,  | 
 | 50 | +            ))  | 
 | 51 | + | 
 | 52 | +    def __exit__(self, *args):  | 
 | 53 | +        try:  | 
 | 54 | +            os.remove(self._lockpath)  | 
 | 55 | +        except OSError as ex:  # pylint: disable=invalid-name  | 
 | 56 | +            if ex.errno in (errno.ENOENT, errno.EACCES):  | 
 | 57 | +                # Probably another process has raced this one  | 
 | 58 | +                # and ended up clearing or locking the file for itself.  | 
 | 59 | +                logger.debug("Unable to remove lock file")  | 
 | 60 | +            else:  | 
 | 61 | +                raise  | 
 | 62 | + | 
0 commit comments