update_file.py 762 B

12345678910111213141516171819202122232425262728
  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. This avoids wholesale rebuilds when a code (re)generation phase does not
  5. actually change the in-tree generated code.
  6. """
  7. import os
  8. import sys
  9. def main(old_path, new_path):
  10. with open(old_path, 'rb') as f:
  11. old_contents = f.read()
  12. with open(new_path, 'rb') as f:
  13. new_contents = f.read()
  14. if old_contents != new_contents:
  15. os.replace(new_path, old_path)
  16. else:
  17. os.unlink(new_path)
  18. if __name__ == '__main__':
  19. if len(sys.argv) != 3:
  20. print("Usage: %s <path to be updated> <path with new contents>" % (sys.argv[0],))
  21. sys.exit(1)
  22. main(sys.argv[1], sys.argv[2])