diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-05 16:16:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-05 16:16:49 +0000 |
commit | 48e387c5c12026a567eb7b293a3a590241c0cecb (patch) | |
tree | 80f2573be2d7d534b8ac4d2a852fe43f7ac35324 /lib/ansible/modules/find.py | |
parent | Releasing progress-linux version 2.16.6-1~progress7.99u1. (diff) | |
download | ansible-core-48e387c5c12026a567eb7b293a3a590241c0cecb.tar.xz ansible-core-48e387c5c12026a567eb7b293a3a590241c0cecb.zip |
Merging upstream version 2.17.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/ansible/modules/find.py')
-rw-r--r-- | lib/ansible/modules/find.py | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/lib/ansible/modules/find.py b/lib/ansible/modules/find.py index 0251224..5e8e36a 100644 --- a/lib/ansible/modules/find.py +++ b/lib/ansible/modules/find.py @@ -6,8 +6,7 @@ # Copyright: (c) 2017, Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import absolute_import, division, print_function -__metaclass__ = type +from __future__ import annotations DOCUMENTATION = r''' @@ -150,6 +149,11 @@ options: - Default is unlimited depth. type: int version_added: "2.6" + encoding: + description: + - When doing a C(contains) search, determine the encoding of the files to be searched. + type: str + version_added: "2.17" extends_documentation_fragment: action_common_attributes attributes: check_mode: @@ -339,11 +343,12 @@ def sizefilter(st, size): return False -def contentfilter(fsname, pattern, read_whole_file=False): +def contentfilter(fsname, pattern, encoding, read_whole_file=False): """ Filter files which contain the given expression :arg fsname: Filename to scan for lines matching a pattern :arg pattern: Pattern to look for inside of line + :arg encoding: Encoding of the file to be scanned :arg read_whole_file: If true, the whole file is read into memory before the regex is applied against it. Otherwise, the regex is applied line-by-line. :rtype: bool :returns: True if one of the lines in fsname matches the pattern. Otherwise False @@ -354,7 +359,7 @@ def contentfilter(fsname, pattern, read_whole_file=False): prog = re.compile(pattern) try: - with open(fsname) as f: + with open(fsname, encoding=encoding) as f: if read_whole_file: return bool(prog.search(f.read())) @@ -362,6 +367,13 @@ def contentfilter(fsname, pattern, read_whole_file=False): if prog.match(line): return True + except LookupError as e: + raise e + except UnicodeDecodeError as e: + if encoding is None: + encoding = 'None (default determined by the Python built-in function "open")' + msg = f'Failed to read the file {fsname} due to an encoding error. current encoding: {encoding}' + raise Exception(msg) from e except Exception: pass @@ -455,6 +467,7 @@ def main(): depth=dict(type='int'), mode=dict(type='raw'), exact_mode=dict(type='bool', default=True), + encoding=dict(type='str') ), supports_check_mode=True, ) @@ -567,7 +580,7 @@ def main(): if (pfilter(fsobj, params['patterns'], params['excludes'], params['use_regex']) and agefilter(st, now, age, params['age_stamp']) and sizefilter(st, size) and - contentfilter(fsname, params['contains'], params['read_whole_file']) and + contentfilter(fsname, params['contains'], params['encoding'], params['read_whole_file']) and mode_filter(st, params['mode'], params['exact_mode'], module)): r.update(statinfo(st)) |