1"""
2A script that replaces an old file with a new one, only if the contents
3actually changed.  If not, the new file is simply deleted.
4
5This avoids wholesale rebuilds when a code (re)generation phase does not
6actually change the in-tree generated code.
7"""
8
9import os
10import sys
11
12
13def 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
24if __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])
29