
from __future__ import absolute_import
from __future__ import division

import errno
import os
import fcntl
from . import filecontrol


class LockError(Exception):
    """ Raised when failed to get lock """


def lock(filename):
    try:
        lockfile = os.path.join(filename)
        fd = os.open(lockfile, os.O_RDWR | os.O_CREAT)
        filecontrol.set_close_on_exec(fd)

        fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except EnvironmentError as e:
        if e.errno == errno.EWOULDBLOCK:
            raise LockError("Instance already running")
        else:
            raise LockError(str(e))
