summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2015-10-22 12:49:02 +0200
committerVictor Stinner <vstinner@redhat.com>2015-10-22 12:55:15 +0200
commit728a7d556954a2a705e58bc6eca7857332d4940e (patch)
treeae2e68b47649c50494d37f28849793f4ae7d45c8
parent4a7a20d91c44d2be6396be5fc64d27b0b5afa4e2 (diff)
downloadlockfile-728a7d556954a2a705e58bc6eca7857332d4940e.tar.gz
Fix flake8 warnings
* Remove unused imports * Add empty lines to respect the PEP8 * Sort imports * Replace "timeout/10" with "timeout / 10" (add spaces) * Fix style of comments * Remove trailing spaces * test_lockfile.py: add "# noqa" on the sqlite3 import, the import is used to decide if we skip the test or not * tox.ini: enable all warnings except F841 (which will be fixed later) Change-Id: I8218785f5cea23532357ec1756a3ab2255880d74
-rw-r--r--lockfile/__init__.py24
-rw-r--r--lockfile/linklockfile.py4
-rw-r--r--lockfile/mkdirlockfile.py9
-rw-r--r--lockfile/pidlockfile.py13
-rw-r--r--lockfile/sqlitelockfile.py7
-rw-r--r--lockfile/symlinklockfile.py19
-rw-r--r--test/compliancetest.py65
-rw-r--r--test/test_lockfile.py13
-rw-r--r--tox.ini2
9 files changed, 88 insertions, 68 deletions
diff --git a/lockfile/__init__.py b/lockfile/__init__.py
index 04b2350..a6f44a5 100644
--- a/lockfile/__init__.py
+++ b/lockfile/__init__.py
@@ -53,14 +53,11 @@ Exceptions:
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"):
@@ -73,6 +70,7 @@ __all__ = ['Error', 'LockError', 'LockTimeout', 'AlreadyLocked',
'LinkFileLock', 'MkdirFileLock', 'SQLiteFileLock',
'LockBase', 'locked']
+
class Error(Exception):
"""
Base class for other exceptions.
@@ -84,6 +82,7 @@ class Error(Exception):
"""
pass
+
class LockError(Error):
"""
Base class for error arising from attempts to acquire the lock.
@@ -95,6 +94,7 @@ class LockError(Error):
"""
pass
+
class LockTimeout(LockError):
"""Raised when lock creation fails within a user-defined period of time.
@@ -105,6 +105,7 @@ class LockTimeout(LockError):
"""
pass
+
class AlreadyLocked(LockError):
"""Some other thread/process is locking the file.
@@ -115,6 +116,7 @@ class AlreadyLocked(LockError):
"""
pass
+
class LockFailed(LockError):
"""Lock file creation failed for some other reason.
@@ -125,6 +127,7 @@ class LockFailed(LockError):
"""
pass
+
class UnlockError(Error):
"""
Base class for errors arising from attempts to release the lock.
@@ -136,6 +139,7 @@ class UnlockError(Error):
"""
pass
+
class NotLocked(UnlockError):
"""Raised when an attempt is made to unlock an unlocked file.
@@ -146,6 +150,7 @@ class NotLocked(UnlockError):
"""
pass
+
class NotMyLock(UnlockError):
"""Raised when an attempt is made to unlock a file someone else locked.
@@ -156,6 +161,7 @@ class NotMyLock(UnlockError):
"""
pass
+
class _SharedBase(object):
def __init__(self, path):
self.path = path
@@ -200,6 +206,7 @@ class _SharedBase(object):
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):
@@ -257,6 +264,7 @@ class LockBase(_SharedBase):
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)
@@ -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.
@@ -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.
@@ -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.
@@ -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.
@@ -324,6 +336,7 @@ def locked(path, timeout=None):
return wrapper
return decor
+
if hasattr(os, "link"):
from . import linklockfile as _llf
LockFile = _llf.LinkLockFile
@@ -332,4 +345,3 @@ else:
LockFile = _mlf.MkdirLockFile
FileLock = LockFile
-
diff --git a/lockfile/linklockfile.py b/lockfile/linklockfile.py
index ab5e1f2..2ca9be0 100644
--- a/lockfile/linklockfile.py
+++ b/lockfile/linklockfile.py
@@ -6,6 +6,7 @@ import os
from . import (LockBase, LockFailed, NotLocked, NotMyLock, LockTimeout,
AlreadyLocked)
+
class LinkLockFile(LockBase):
"""Lock access to a file using atomic property of link(2).
@@ -46,7 +47,7 @@ class LinkLockFile(LockBase):
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
@@ -70,4 +71,3 @@ class LinkLockFile(LockBase):
def break_lock(self):
if os.path.exists(self.lock_file):
os.unlink(self.lock_file)
-
diff --git a/lockfile/mkdirlockfile.py b/lockfile/mkdirlockfile.py
index caf462e..05a8c96 100644
--- a/lockfile/mkdirlockfile.py
+++ b/lockfile/mkdirlockfile.py
@@ -8,6 +8,7 @@ import errno
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):
@@ -18,10 +19,10 @@ class MkdirLockFile(LockBase):
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
diff --git a/lockfile/pidlockfile.py b/lockfile/pidlockfile.py
index c4c8a39..fbf7d55 100644
--- a/lockfile/pidlockfile.py
+++ b/lockfile/pidlockfile.py
@@ -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,
@@ -89,7 +88,7 @@ class PIDLockFile(LockBase):
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:
@@ -117,6 +116,7 @@ class PIDLockFile(LockBase):
"""
remove_existing_pidfile(self.path)
+
def read_pid_from_pidfile(pidfile_path):
""" Read the PID recorded in the named PID file.
@@ -132,10 +132,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
@@ -171,8 +171,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()
diff --git a/lockfile/sqlitelockfile.py b/lockfile/sqlitelockfile.py
index 734ce03..f997e24 100644
--- a/lockfile/sqlitelockfile.py
+++ b/lockfile/sqlitelockfile.py
@@ -10,6 +10,7 @@ except NameError:
from . import LockBase, NotLocked, NotMyLock, LockTimeout, AlreadyLocked
+
class SQLiteLockFile(LockBase):
"Demonstrate SQL-based locking."
@@ -34,7 +35,7 @@ class SQLiteLockFile(LockBase):
import sqlite3
self.connection = sqlite3.connect(SQLiteLockFile.testdb)
-
+
c = self.connection.cursor()
try:
c.execute("create table locks"
@@ -97,7 +98,7 @@ class SQLiteLockFile(LockBase):
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:
@@ -130,7 +131,7 @@ class SQLiteLockFile(LockBase):
" where lock_file = ?",
(self.lock_file,))
return cursor.fetchone()[0]
-
+
def is_locked(self):
cursor = self.connection.cursor()
cursor.execute("select * from locks"
diff --git a/lockfile/symlinklockfile.py b/lockfile/symlinklockfile.py
index b5dd71f..23b41f5 100644
--- a/lockfile/symlinklockfile.py
+++ b/lockfile/symlinklockfile.py
@@ -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)."""
@@ -17,10 +18,10 @@ class SymlinkLockFile(LockBase):
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:
@@ -45,7 +46,7 @@ class SymlinkLockFile(LockBase):
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
@@ -61,8 +62,8 @@ class SymlinkLockFile(LockBase):
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
diff --git a/test/compliancetest.py b/test/compliancetest.py
index e0258b1..bf4e59c 100644
--- a/test/compliancetest.py
+++ b/test/compliancetest.py
@@ -4,6 +4,7 @@ import shutil
import lockfile
+
class ComplianceTest(object):
def __init__(self):
self.saved_class = lockfile.LockFile
@@ -38,8 +39,8 @@ class ComplianceTest(object):
lock.release()
assert not lock.is_locked()
-## def test_acquire_basic_threaded(self):
-## self._test_acquire_helper(True)
+# def test_acquire_basic_threaded(self):
+# self._test_acquire_helper(True)
def test_acquire_basic_unthreaded(self):
self._test_acquire_helper(False)
@@ -79,11 +80,11 @@ class ComplianceTest(object):
e2.set() # tell thread t to release lock
t.join()
-## def test_acquire_no_timeout_threaded(self):
-## self._test_acquire_no_timeout_helper(True)
+# def test_acquire_no_timeout_threaded(self):
+# self._test_acquire_no_timeout_helper(True)
-## def test_acquire_no_timeout_unthreaded(self):
-## self._test_acquire_no_timeout_helper(False)
+# def test_acquire_no_timeout_unthreaded(self):
+# self._test_acquire_no_timeout_helper(False)
def _test_acquire_timeout_helper(self, tbool):
# Timeout test
@@ -150,27 +151,27 @@ class ComplianceTest(object):
else:
raise AssertionError('erroneously unlocked file')
-## def test_release_basic_threaded(self):
-## self._test_release_basic_helper(True)
+# def test_release_basic_threaded(self):
+# self._test_release_basic_helper(True)
def test_release_basic_unthreaded(self):
self._test_release_basic_helper(False)
-## def test_release_from_thread(self):
-## e1, e2 = threading.Event(), threading.Event()
-## t = _in_thread(self._lock_wait_unlock, e1, e2)
-## e1.wait()
-## lock2 = lockfile.LockFile(self._testfile(), threaded=False)
-## assert not lock2.i_am_locking()
-## try:
-## lock2.release()
-## except lockfile.NotMyLock:
-## pass
-## else:
-## raise AssertionError('erroneously unlocked a file locked'
-## ' by another thread.')
-## e2.set()
-## t.join()
+# def test_release_from_thread(self):
+# e1, e2 = threading.Event(), threading.Event()
+# t = _in_thread(self._lock_wait_unlock, e1, e2)
+# e1.wait()
+# lock2 = lockfile.LockFile(self._testfile(), threaded=False)
+# assert not lock2.i_am_locking()
+# try:
+# lock2.release()
+# except lockfile.NotMyLock:
+# pass
+# else:
+# raise AssertionError('erroneously unlocked a file locked'
+# ' by another thread.')
+# e2.set()
+# t.join()
def _test_is_locked_helper(self, tbool):
lock = lockfile.LockFile(self._testfile(), threaded=tbool)
@@ -179,14 +180,14 @@ class ComplianceTest(object):
lock.release()
assert not lock.is_locked(), "still locked after release!"
-## def test_is_locked_threaded(self):
-## self._test_is_locked_helper(True)
+# def test_is_locked_threaded(self):
+# self._test_is_locked_helper(True)
def test_is_locked_unthreaded(self):
self._test_is_locked_helper(False)
-## def test_i_am_locking_threaded(self):
-## self._test_i_am_locking_helper(True)
+# def test_i_am_locking_threaded(self):
+# self._test_i_am_locking_helper(True)
def test_i_am_locking_unthreaded(self):
self._test_i_am_locking_helper(False)
@@ -219,8 +220,8 @@ class ComplianceTest(object):
else:
raise AssertionError('break lock failed')
-## def test_break_lock_threaded(self):
-## self._test_break_lock_helper(True)
+# def test_break_lock_threaded(self):
+# self._test_break_lock_helper(True)
def test_break_lock_unthreaded(self):
self._test_break_lock_helper(False)
@@ -230,8 +231,8 @@ class ComplianceTest(object):
l = lockfile.LockFile(self._testfile())
l.acquire()
try:
- event1.set() # we're in,
- event2.wait() # wait for boss's permission to leave
+ event1.set() # we're in,
+ event2.wait() # wait for boss's permission to leave
finally:
l.release()
@@ -250,6 +251,7 @@ class ComplianceTest(object):
return a + b
assert func(4, 3) == 7
+
def _in_thread(func, *args, **kwargs):
"""Execute func(*args, **kwargs) after dt seconds. Helper for tests."""
def _f():
@@ -258,4 +260,3 @@ def _in_thread(func, *args, **kwargs):
t.setDaemon(True)
t.start()
return t
-
diff --git a/test/test_lockfile.py b/test/test_lockfile.py
index e1f4f72..ca1ed06 100644
--- a/test/test_lockfile.py
+++ b/test/test_lockfile.py
@@ -1,36 +1,41 @@
-import sys
-
import lockfile.linklockfile
import lockfile.mkdirlockfile
import lockfile.pidlockfile
import lockfile.symlinklockfile
from compliancetest import ComplianceTest
-
+
+
class TestLinkLockFile(ComplianceTest):
class_to_test = lockfile.linklockfile.LinkLockFile
+
class TestSymlinkLockFile(ComplianceTest):
class_to_test = lockfile.symlinklockfile.SymlinkLockFile
+
class TestMkdirLockFile(ComplianceTest):
class_to_test = lockfile.mkdirlockfile.MkdirLockFile
+
class TestPIDLockFile(ComplianceTest):
class_to_test = lockfile.pidlockfile.PIDLockFile
+
# Check backwards compatibility
class TestLinkFileLock(ComplianceTest):
class_to_test = lockfile.LinkFileLock
+
class TestMkdirFileLock(ComplianceTest):
class_to_test = lockfile.MkdirFileLock
try:
- import sqlite3
+ import sqlite3 # noqa
except ImportError:
pass
else:
import lockfile.sqlitelockfile
+
class TestSQLiteLockFile(ComplianceTest):
class_to_test = lockfile.sqlitelockfile.SQLiteLockFile
diff --git a/tox.ini b/tox.ini
index df64d02..70350fe 100644
--- a/tox.ini
+++ b/tox.ini
@@ -24,6 +24,6 @@ commands =
nosetests --with-coverage --cover-erase --cover-package=lockfile --cover-inclusive []
[flake8]
-ignore = E121,E123,E128,E221,E226,E261,E265,E301,E302,E713,F401,F841,W291,W293,W391
+ignore = F841
exclude=.venv,.git,.tox,dist,doc
show-source = True