diff options
| author | Flaper Fesp <flaper87@gmail.com> | 2013-06-10 14:25:58 +0200 |
|---|---|---|
| committer | Flaper Fesp <flaper87@gmail.com> | 2013-07-15 17:32:44 +0200 |
| commit | 15c17fb1d04bf9fa3094e86fd7c38c4c8efad43c (patch) | |
| tree | 94757c4d2a652fbaf8958b02ac7a3bec8baea178 /openstack | |
| parent | ca0ca29e3f04ced20f62c33ad8b5bf7492775824 (diff) | |
| download | oslo-15c17fb1d04bf9fa3094e86fd7c38c4c8efad43c.tar.gz oslo-15c17fb1d04bf9fa3094e86fd7c38c4c8efad43c.tar.xz oslo-15c17fb1d04bf9fa3094e86fd7c38c4c8efad43c.zip | |
Make lock_file_prefix optional
Currently, the lock_file_prefix says it should end with an hypen if
specified. It seems that it was intended to be optional. Since that
behavior makes sense, this patch changes the synchronized's signature
and makes lock_file_prefix optional.
With this patch, the prefix will not be required to end with an hypen
anymore, such sign will be added to the prefix if specified and if not
already present.
This patch doesn't change synchronized's behavior at all, which means it
doesn't break backward compatibility.
Change-Id: I8841da1e8d105aac4a86b247228a9c935e00afc5
Diffstat (limited to 'openstack')
| -rw-r--r-- | openstack/common/lockutils.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/openstack/common/lockutils.py b/openstack/common/lockutils.py index be362f8..ad8c0e5 100644 --- a/openstack/common/lockutils.py +++ b/openstack/common/lockutils.py @@ -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) |
