diff options
author | Martin Krizek <martin.krizek@gmail.com> | 2023-04-27 16:04:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-27 16:04:09 +0200 |
commit | ed749cf0a02306e82cbd95f01f05697c77fc30b4 (patch) | |
tree | 72374f2e528b98056ffd02b162973546a23162b8 | |
parent | 8754d8e1e1ea7a2b931d028ce0fc45084254fa5b (diff) | |
download | ansible-ed749cf0a02306e82cbd95f01f05697c77fc30b4.tar.gz |
Remove deprecated FileLock class (#80438)
-rw-r--r-- | changelogs/fragments/remove-deprecated-filelock-class.yml | 2 | ||||
-rw-r--r-- | lib/ansible/module_utils/common/file.py | 100 | ||||
-rw-r--r-- | test/sanity/ignore.txt | 1 |
3 files changed, 2 insertions, 101 deletions
diff --git a/changelogs/fragments/remove-deprecated-filelock-class.yml b/changelogs/fragments/remove-deprecated-filelock-class.yml new file mode 100644 index 0000000000..fba516040e --- /dev/null +++ b/changelogs/fragments/remove-deprecated-filelock-class.yml @@ -0,0 +1,2 @@ +removed_features: + - Remove deprecated ``FileLock`` class diff --git a/lib/ansible/module_utils/common/file.py b/lib/ansible/module_utils/common/file.py index a7638238fb..72b0d2cf0f 100644 --- a/lib/ansible/module_utils/common/file.py +++ b/lib/ansible/module_utils/common/file.py @@ -7,12 +7,6 @@ __metaclass__ = type import os import stat import re -import time -import fcntl -import sys - -from contextlib import contextmanager -from ansible.module_utils.common.warnings import deprecate try: import selinux # pylint: disable=unused-import @@ -102,97 +96,3 @@ def get_file_arg_spec(): attributes=dict(aliases=['attr']), ) return arg_spec - - -class LockTimeout(Exception): - pass - - -class FileLock: - ''' - Currently FileLock is implemented via fcntl.flock on a lock file, however this - behaviour may change in the future. Avoid mixing lock types fcntl.flock, - fcntl.lockf and module_utils.common.file.FileLock as it will certainly cause - unwanted and/or unexpected behaviour - ''' - def __init__(self): - deprecate("FileLock is not reliable and has never been used in core for that reason. There is no current alternative that works across POSIX targets", - version='2.16') - self.lockfd = None - - @contextmanager - def lock_file(self, path, tmpdir, lock_timeout=None): - ''' - Context for lock acquisition - ''' - try: - self.set_lock(path, tmpdir, lock_timeout) - yield - finally: - self.unlock() - - def set_lock(self, path, tmpdir, lock_timeout=None): - ''' - Create a lock file based on path with flock to prevent other processes - using given path. - Please note that currently file locking only works when it's executed by - the same user, I.E single user scenarios - - :kw path: Path (file) to lock - :kw tmpdir: Path where to place the temporary .lock file - :kw lock_timeout: - Wait n seconds for lock acquisition, fail if timeout is reached. - 0 = Do not wait, fail if lock cannot be acquired immediately, - Default is None, wait indefinitely until lock is released. - :returns: True - ''' - lock_path = os.path.join(tmpdir, 'ansible-{0}.lock'.format(os.path.basename(path))) - l_wait = 0.1 - r_exception = IOError - if sys.version_info[0] == 3: - r_exception = BlockingIOError - - self.lockfd = open(lock_path, 'w') - - if lock_timeout <= 0: - fcntl.flock(self.lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB) - os.chmod(lock_path, stat.S_IWRITE | stat.S_IREAD) - return True - - if lock_timeout: - e_secs = 0 - while e_secs < lock_timeout: - try: - fcntl.flock(self.lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB) - os.chmod(lock_path, stat.S_IWRITE | stat.S_IREAD) - return True - except r_exception: - time.sleep(l_wait) - e_secs += l_wait - continue - - self.lockfd.close() - raise LockTimeout('{0} sec'.format(lock_timeout)) - - fcntl.flock(self.lockfd, fcntl.LOCK_EX) - os.chmod(lock_path, stat.S_IWRITE | stat.S_IREAD) - - return True - - def unlock(self): - ''' - Make sure lock file is available for everyone and Unlock the file descriptor - locked by set_lock - - :returns: True - ''' - if not self.lockfd: - return True - - try: - fcntl.flock(self.lockfd, fcntl.LOCK_UN) - self.lockfd.close() - except ValueError: # file wasn't opened, let context manager fail gracefully - pass - - return True diff --git a/test/sanity/ignore.txt b/test/sanity/ignore.txt index 9bd2379616..63a31a2ba1 100644 --- a/test/sanity/ignore.txt +++ b/test/sanity/ignore.txt @@ -244,4 +244,3 @@ test/units/utils/collection_loader/fixtures/collections_masked/ansible_collectio test/units/utils/collection_loader/test_collection_loader.py pylint:undefined-variable # magic runtime local var splatting lib/ansible/playbook/helpers.py pylint:ansible-deprecated-version lib/ansible/playbook/included_file.py pylint:ansible-deprecated-version -lib/ansible/module_utils/common/file.py pylint:ansible-deprecated-version |