Martí Bolívar | 808dd9e | 2020-10-12 11:02:46 -0700 | [diff] [blame] | 1 | # Copyright (c) 2020 Nordic Semiconductor ASA |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | """ |
| 5 | Helper functions used by gen_kconfig_rest.py and gen_devicetree_rest.py. |
| 6 | """ |
| 7 | |
| 8 | import errno |
| 9 | |
| 10 | def write_if_updated(path, s): |
| 11 | """ |
| 12 | Writes 's' as the contents of <out_dir>/<filename>, but only if it |
| 13 | differs from the current contents of the file. This avoids unnecessary |
| 14 | timestamp updates, which trigger documentation rebuilds. |
| 15 | |
| 16 | Returns True if the file was updated, False otherwise. |
| 17 | """ |
| 18 | |
| 19 | try: |
| 20 | with open(path, "r", encoding="utf-8") as f: |
| 21 | if s == f.read(): |
| 22 | return False |
| 23 | except OSError as e: |
| 24 | if e.errno != errno.ENOENT: |
| 25 | raise |
| 26 | |
| 27 | with open(path, "w", encoding="utf-8") as f: |
| 28 | f.write(s) |
| 29 | return True |