Antoine Pitrou | b091bec | 2017-09-20 23:57:56 +0200 | [diff] [blame] | 1 | """ |
| 2 | A script that replaces an old file with a new one, only if the contents |
| 3 | actually changed. If not, the new file is simply deleted. |
| 4 | |
| 5 | This avoids wholesale rebuilds when a code (re)generation phase does not |
| 6 | actually change the in-tree generated code. |
| 7 | """ |
| 8 | |
| 9 | import os |
| 10 | import sys |
| 11 | |
| 12 | |
| 13 | def main(old_path, new_path): |
| 14 | with open(old_path, 'rb') as f: |
| 15 | old_contents = f.read() |
| 16 | with open(new_path, 'rb') as f: |
| 17 | new_contents = f.read() |
| 18 | if old_contents != new_contents: |
| 19 | os.replace(new_path, old_path) |
| 20 | else: |
| 21 | os.unlink(new_path) |
| 22 | |
| 23 | |
| 24 | if __name__ == '__main__': |
| 25 | if len(sys.argv) != 3: |
| 26 | print("Usage: %s <path to be updated> <path with new contents>" % (sys.argv[0],)) |
| 27 | sys.exit(1) |
| 28 | main(sys.argv[1], sys.argv[2]) |