diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:04:21 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:04:21 +0000 |
commit | 8a754e0858d922e955e71b253c139e071ecec432 (patch) | |
tree | 527d16e74bfd1840c85efd675fdecad056c54107 /hacking/build_library/build_ansible/change_detection.py | |
parent | Initial commit. (diff) | |
download | ansible-core-8a754e0858d922e955e71b253c139e071ecec432.tar.xz ansible-core-8a754e0858d922e955e71b253c139e071ecec432.zip |
Adding upstream version 2.14.3.upstream/2.14.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | hacking/build_library/build_ansible/change_detection.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/hacking/build_library/build_ansible/change_detection.py b/hacking/build_library/build_ansible/change_detection.py new file mode 100644 index 0000000..22e21d3 --- /dev/null +++ b/hacking/build_library/build_ansible/change_detection.py @@ -0,0 +1,33 @@ +# Copyright: (c) 2018, 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 + + +def update_file_if_different(filename, b_data): + """ + Replace file content only if content is different. + + This preserves timestamps in case the file content has not changed. It performs multiple + operations on the file so it is not atomic and may be slower than simply writing to the file. + + :arg filename: The filename to write to + :b_data: Byte string containing the data to write to the file + """ + try: + with open(filename, 'rb') as f: + b_data_old = f.read() + except IOError as e: + if e.errno != 2: + raise + # File did not exist, set b_data_old to a sentinel value so that + # b_data gets written to the filename + b_data_old = None + + if b_data_old != b_data: + with open(filename, 'wb') as f: + f.write(b_data) + return True + + return False |