ansible/hacking/build_library/build_ansible/change_detection.py
Toshio Kuratomi 019d078a5a
Move common build code from _build_helpers (#55986)
We have some common code used by several docs scripts.  Migrate that
into the build-only shared code repository.

* Move lib/ansible/utils/_build_helpers.py to the directory for common
  build code
* Migrate docs/bin/dump_config.py to a build-ansible subcommand
* Migrate dump_keywords to the build-ansible framework
  * Make the script more maintainable by using functions and good
    variable names
  * Port to Python3 idioms
  * Fix bug so that private attributes will be undocumented
* Move generate_man to a build-ansible subcommand
* Port plugin_formatter to a build-ansible subcommand
* Rework command_plugins so that docs scripts can target Python-3.4+ and
  releng-only subcommands can use more recent versions of Python.
  The architecture is now that command_plugins/* need to be importable
  on Python-3.4.  The init_parsers() method needs to run on Python-3.4.
  But the main() method can utilize features of more recent Python as
  long as it fits within those parameters.
* Update docs build requirements

Port the plugin_formatter to build-ansible framework
2019-07-16 12:19:01 -07:00

41 lines
1.3 KiB
Python

# 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
"""
This file contains common code for building ansible. If you want to use code from here at runtime,
it needs to be moved out of this file and the implementation looked over to figure out whether API
should be changed before being made public.
"""
import os.path
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