def openlock(filename, operation, wait=True):
    """
    Returns a file-like object that gets a fnctl() lock.

    `operation` should be one of LOCK_SH or LOCK_EX for shared or
    exclusive locks.

    If `wait` is False, then openlock() will not block on trying to
    acquire the lock.
    """
    f = os.fdopen(os.open(filename, os.O_RDWR | os.O_CREAT, 0666), "r+")
    if not wait:
        operation |= LOCK_NB
    try:
        lockf(f.fileno(), operation)
    except __HOLE__, err:
        if not wait and err.errno in (EACCES, EAGAIN):
            from django.core.management.base import CommandError
            raise CommandError("Could not acquire lock on '%s' held by %s." %
                               (filename, f.readline().strip()))
        raise
    print >>f, "%s:%d" % (socket.gethostname(), os.getpid())
    f.truncate()
    f.flush()
    return f
dataset/ETHPy150Open e-loue/django-lean/django_lean/lockfile.py/openlock