Skip to content

Commit

Permalink
Update pylockfile to 0.12.2
Browse files Browse the repository at this point in the history
  • Loading branch information
medariox committed Jun 27, 2016
1 parent b234884 commit 3d8702c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
24 changes: 18 additions & 6 deletions lib/lockfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@

from __future__ import absolute_import

import sys
import socket
import functools
import os
import socket
import threading
import time
import urllib
import warnings
import functools

# Work with PEP8 and non-PEP8 versions of threading module.
if not hasattr(threading, "current_thread"):
Expand All @@ -73,6 +70,7 @@
'LinkFileLock', 'MkdirFileLock', 'SQLiteFileLock',
'LockBase', 'locked']


class Error(Exception):
"""
Base class for other exceptions.
Expand All @@ -84,6 +82,7 @@ class Error(Exception):
"""
pass


class LockError(Error):
"""
Base class for error arising from attempts to acquire the lock.
Expand All @@ -95,6 +94,7 @@ class LockError(Error):
"""
pass


class LockTimeout(LockError):
"""Raised when lock creation fails within a user-defined period of time.
Expand All @@ -105,6 +105,7 @@ class LockTimeout(LockError):
"""
pass


class AlreadyLocked(LockError):
"""Some other thread/process is locking the file.
Expand All @@ -115,6 +116,7 @@ class AlreadyLocked(LockError):
"""
pass


class LockFailed(LockError):
"""Lock file creation failed for some other reason.
Expand All @@ -125,6 +127,7 @@ class LockFailed(LockError):
"""
pass


class UnlockError(Error):
"""
Base class for errors arising from attempts to release the lock.
Expand All @@ -136,6 +139,7 @@ class UnlockError(Error):
"""
pass


class NotLocked(UnlockError):
"""Raised when an attempt is made to unlock an unlocked file.
Expand All @@ -146,6 +150,7 @@ class NotLocked(UnlockError):
"""
pass


class NotMyLock(UnlockError):
"""Raised when an attempt is made to unlock a file someone else locked.
Expand All @@ -156,6 +161,7 @@ class NotMyLock(UnlockError):
"""
pass


class _SharedBase(object):
def __init__(self, path):
self.path = path
Expand Down Expand Up @@ -200,6 +206,7 @@ def __exit__(self, *_exc):
def __repr__(self):
return "<%s: %r>" % (self.__class__.__name__, self.path)


class LockBase(_SharedBase):
"""Base class for platform-specific lock classes."""
def __init__(self, path, threaded=True, timeout=None):
Expand Down Expand Up @@ -257,6 +264,7 @@ def __repr__(self):
return "<%s: %r -- %r>" % (self.__class__.__name__, self.unique_name,
self.path)


def _fl_helper(cls, mod, *args, **kwds):
warnings.warn("Import from %s module instead of lockfile package" % mod,
DeprecationWarning, stacklevel=2)
Expand All @@ -270,6 +278,7 @@ def _fl_helper(cls, mod, *args, **kwds):
kwds["threaded"] = True
return cls(*args, **kwds)


def LinkFileLock(*args, **kwds):
"""Factory function provided for backwards compatibility.
Expand All @@ -280,6 +289,7 @@ def LinkFileLock(*args, **kwds):
return _fl_helper(linklockfile.LinkLockFile, "lockfile.linklockfile",
*args, **kwds)


def MkdirFileLock(*args, **kwds):
"""Factory function provided for backwards compatibility.
Expand All @@ -290,6 +300,7 @@ def MkdirFileLock(*args, **kwds):
return _fl_helper(mkdirlockfile.MkdirLockFile, "lockfile.mkdirlockfile",
*args, **kwds)


def SQLiteFileLock(*args, **kwds):
"""Factory function provided for backwards compatibility.
Expand All @@ -300,6 +311,7 @@ def SQLiteFileLock(*args, **kwds):
return _fl_helper(sqlitelockfile.SQLiteLockFile, "lockfile.sqlitelockfile",
*args, **kwds)


def locked(path, timeout=None):
"""Decorator which enables locks for decorated function.
Expand All @@ -324,6 +336,7 @@ def wrapper(*args, **kwargs):
return wrapper
return decor


if hasattr(os, "link"):
from . import linklockfile as _llf
LockFile = _llf.LinkLockFile
Expand All @@ -332,4 +345,3 @@ def wrapper(*args, **kwargs):
LockFile = _mlf.MkdirLockFile

FileLock = LockFile

4 changes: 2 additions & 2 deletions lib/lockfile/linklockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from . import (LockBase, LockFailed, NotLocked, NotMyLock, LockTimeout,
AlreadyLocked)


class LinkLockFile(LockBase):
"""Lock access to a file using atomic property of link(2).
Expand Down Expand Up @@ -46,7 +47,7 @@ def acquire(self, timeout=None):
else:
raise AlreadyLocked("%s is already locked" %
self.path)
time.sleep(timeout is not None and timeout/10 or 0.1)
time.sleep(timeout is not None and timeout / 10 or 0.1)
else:
# Link creation succeeded. We're good to go.
return
Expand All @@ -70,4 +71,3 @@ def i_am_locking(self):
def break_lock(self):
if os.path.exists(self.lock_file):
os.unlink(self.lock_file)

9 changes: 5 additions & 4 deletions lib/lockfile/mkdirlockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from . import (LockBase, LockFailed, NotLocked, NotMyLock, LockTimeout,
AlreadyLocked)


class MkdirLockFile(LockBase):
"""Lock file by creating a directory."""
def __init__(self, path, threaded=True, timeout=None):
Expand All @@ -18,10 +19,10 @@ def __init__(self, path, threaded=True, timeout=None):
LockBase.__init__(self, path, threaded, timeout)
# Lock file itself is a directory. Place the unique file name into
# it.
self.unique_name = os.path.join(self.lock_file,
"%s.%s%s" % (self.hostname,
self.tname,
self.pid))
self.unique_name = os.path.join(self.lock_file,
"%s.%s%s" % (self.hostname,
self.tname,
self.pid))

def acquire(self, timeout=None):
timeout = timeout if timeout is not None else self.timeout
Expand Down
15 changes: 6 additions & 9 deletions lib/lockfile/pidlockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

from __future__ import absolute_import

import os
import sys
import errno
import os
import time

from . import (LockBase, AlreadyLocked, LockFailed, NotLocked, NotMyLock,
Expand All @@ -38,8 +37,6 @@ def __init__(self, path, threaded=False, timeout=None):
# pid lockfiles don't support threaded operation, so always force
# False as the threaded arg.
LockBase.__init__(self, path, False, timeout)
dirname = os.path.dirname(self.lock_file)
basename = os.path.split(self.path)[-1]
self.unique_name = self.path

def read_pid(self):
Expand Down Expand Up @@ -89,7 +86,7 @@ def acquire(self, timeout=None):
else:
raise AlreadyLocked("%s is already locked" %
self.path)
time.sleep(timeout is not None and timeout/10 or 0.1)
time.sleep(timeout is not None and timeout / 10 or 0.1)
else:
raise LockFailed("failed to create %s" % self.path)
else:
Expand Down Expand Up @@ -117,6 +114,7 @@ def break_lock(self):
"""
remove_existing_pidfile(self.path)


def read_pid_from_pidfile(pidfile_path):
""" Read the PID recorded in the named PID file.
Expand All @@ -132,10 +130,10 @@ def read_pid_from_pidfile(pidfile_path):
pass
else:
# According to the FHS 2.3 section on PID files in /var/run:
#
#
# The file must consist of the process identifier in
# ASCII-encoded decimal, followed by a newline character.
#
#
# Programs that read PID files should be somewhat flexible
# in what they accept; i.e., they should ignore extra
# whitespace, leading zeroes, absence of the trailing
Expand Down Expand Up @@ -171,8 +169,7 @@ def write_pid_to_pidfile(pidfile_path):
# would contain three characters: two, five, and newline.

pid = os.getpid()
line = "%(pid)d\n" % vars()
pidfile.write(line)
pidfile.write("%s\n" % pid)
pidfile.close()


Expand Down
7 changes: 4 additions & 3 deletions lib/lockfile/sqlitelockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from . import LockBase, NotLocked, NotMyLock, LockTimeout, AlreadyLocked


class SQLiteLockFile(LockBase):
"Demonstrate SQL-based locking."

Expand All @@ -34,7 +35,7 @@ def __init__(self, path, threaded=True, timeout=None):

import sqlite3
self.connection = sqlite3.connect(SQLiteLockFile.testdb)

c = self.connection.cursor()
try:
c.execute("create table locks"
Expand Down Expand Up @@ -97,7 +98,7 @@ def acquire(self, timeout=None):
if len(rows) == 1:
# We're the locker, so go home.
return

# Maybe we should wait a bit longer.
if timeout is not None and time.time() > end_time:
if timeout > 0:
Expand Down Expand Up @@ -130,7 +131,7 @@ def _who_is_locking(self):
" where lock_file = ?",
(self.lock_file,))
return cursor.fetchone()[0]

def is_locked(self):
cursor = self.connection.cursor()
cursor.execute("select * from locks"
Expand Down
19 changes: 10 additions & 9 deletions lib/lockfile/symlinklockfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import absolute_import

import time
import os
import time

from . import (LockBase, LockFailed, NotLocked, NotMyLock, LockTimeout,
from . import (LockBase, NotLocked, NotMyLock, LockTimeout,
AlreadyLocked)


class SymlinkLockFile(LockBase):
"""Lock access to a file using symlink(2)."""

Expand All @@ -17,10 +18,10 @@ def __init__(self, path, threaded=True, timeout=None):

def acquire(self, timeout=None):
# Hopefully unnecessary for symlink.
#try:
# open(self.unique_name, "wb").close()
#except IOError:
# raise LockFailed("failed to create %s" % self.unique_name)
# try:
# open(self.unique_name, "wb").close()
# except IOError:
# raise LockFailed("failed to create %s" % self.unique_name)
timeout = timeout if timeout is not None else self.timeout
end_time = time.time()
if timeout is not None and timeout > 0:
Expand All @@ -45,7 +46,7 @@ def acquire(self, timeout=None):
else:
raise AlreadyLocked("%s is already locked" %
self.path)
time.sleep(timeout/10 if timeout is not None else 0.1)
time.sleep(timeout / 10 if timeout is not None else 0.1)
else:
# Link creation succeeded. We're good to go.
return
Expand All @@ -61,8 +62,8 @@ def is_locked(self):
return os.path.islink(self.lock_file)

def i_am_locking(self):
return os.path.islink(self.lock_file) and \
os.readlink(self.lock_file) == self.unique_name
return (os.path.islink(self.lock_file)
and os.readlink(self.lock_file) == self.unique_name)

def break_lock(self):
if os.path.islink(self.lock_file): # exists && link
Expand Down

0 comments on commit 3d8702c

Please sign in to comment.