Merge "Make lock_file_prefix optional"
This commit is contained in:
commit
ddae590a28
|
@ -135,7 +135,7 @@ else:
|
|||
_semaphores = weakref.WeakValueDictionary()
|
||||
|
||||
|
||||
def synchronized(name, lock_file_prefix, external=False, lock_path=None):
|
||||
def synchronized(name, lock_file_prefix=None, external=False, lock_path=None):
|
||||
"""Synchronization decorator.
|
||||
|
||||
Decorating a method like so::
|
||||
|
@ -159,8 +159,7 @@ def synchronized(name, lock_file_prefix, external=False, lock_path=None):
|
|||
This way only one of either foo or bar can be executing at a time.
|
||||
|
||||
:param lock_file_prefix: The lock_file_prefix argument is used to provide
|
||||
lock files on disk with a meaningful prefix. The prefix should end with a
|
||||
hyphen ('-') if specified.
|
||||
lock files on disk with a meaningful prefix.
|
||||
|
||||
:param external: The external keyword argument denotes whether this lock
|
||||
should work across multiple processes. This means that if two different
|
||||
|
@ -214,10 +213,17 @@ def synchronized(name, lock_file_prefix, external=False, lock_path=None):
|
|||
if not os.path.exists(local_lock_path):
|
||||
fileutils.ensure_tree(local_lock_path)
|
||||
|
||||
def add_prefix(name, prefix):
|
||||
if not prefix:
|
||||
return name
|
||||
sep = '' if prefix.endswith('-') else '-'
|
||||
return '%s%s%s' % (prefix, sep, name)
|
||||
|
||||
# NOTE(mikal): the lock name cannot contain directory
|
||||
# separators
|
||||
safe_name = name.replace(os.sep, '_')
|
||||
lock_file_name = '%s%s' % (lock_file_prefix, safe_name)
|
||||
lock_file_name = add_prefix(name.replace(os.sep, '_'),
|
||||
lock_file_prefix)
|
||||
|
||||
lock_file_path = os.path.join(local_lock_path,
|
||||
lock_file_name)
|
||||
|
||||
|
@ -273,7 +279,7 @@ def synchronized_with_prefix(lock_file_prefix):
|
|||
...
|
||||
|
||||
The lock_file_prefix argument is used to provide lock files on disk with a
|
||||
meaningful prefix. The prefix should end with a hyphen ('-') if specified.
|
||||
meaningful prefix.
|
||||
"""
|
||||
|
||||
return functools.partial(synchronized, lock_file_prefix=lock_file_prefix)
|
||||
|
|
|
@ -211,3 +211,31 @@ class LockTestCase(utils.BaseTestCase):
|
|||
self.config(lock_path=lock_dir)
|
||||
|
||||
self.assertTrue(bar(lock_dir, lock_pfix, lock_name))
|
||||
|
||||
def test_synchronized_without_prefix(self):
|
||||
lock_dir = tempfile.mkdtemp()
|
||||
|
||||
@lockutils.synchronized('lock', external=True, lock_path=lock_dir)
|
||||
def test_without_prefix():
|
||||
path = os.path.join(lock_dir, "lock")
|
||||
self.assertTrue(os.path.exists(path))
|
||||
|
||||
try:
|
||||
test_without_prefix()
|
||||
finally:
|
||||
if os.path.exists(lock_dir):
|
||||
shutil.rmtree(lock_dir, ignore_errors=True)
|
||||
|
||||
def test_synchronized_prefix_without_hypen(self):
|
||||
lock_dir = tempfile.mkdtemp()
|
||||
|
||||
@lockutils.synchronized('lock', 'hypen', True, lock_dir)
|
||||
def test_without_hypen():
|
||||
path = os.path.join(lock_dir, "hypen-lock")
|
||||
self.assertTrue(os.path.exists(path))
|
||||
|
||||
try:
|
||||
test_without_hypen()
|
||||
finally:
|
||||
if os.path.exists(lock_dir):
|
||||
shutil.rmtree(lock_dir, ignore_errors=True)
|
||||
|
|
Loading…
Reference in New Issue