compare_snapshots.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import argparse
  9. import pickle
  10. import sys
  11. """This is a command-line entry point which, when given two snapshots created using make_snapshot.py
  12. compares them and reports on the differences. It returns 0 if and only if there are no differences.
  13. To use it in a scripting environment instead of a CLI, invoke do_compare(filename1, filename2)
  14. Or do it manually using FolderSnapshot.CompareSnapshots and then enumerate_changes
  15. """
  16. ignore_patterns = []
  17. from snapshot_folder.snapshot_folder import FolderSnapshot, SnapshotComparison
  18. def do_compare(filename1, filename2):
  19. """Given two filenames, returns the diffs as a list of tuples [(type, file)]"""
  20. snap1 = pickle.load(open(filename1, 'rb'))
  21. snap2 = pickle.load(open(filename2, 'rb'))
  22. comparison = FolderSnapshot.CompareSnapshots(snap1, snap2)
  23. changes = [change_entry for change_entry in comparison.enumerate_changes()]
  24. return changes
  25. def init_parser():
  26. """Prepares the command line parser"""
  27. parser = argparse.ArgumentParser()
  28. parser.description = "Compares two snapshots previously saved, outputs the changes. Exit code is 0 if no diff, 1 otherwise."
  29. parser.add_argument('first', default='.', help='first file to use')
  30. parser.add_argument('second', default='.', help='second file to use')
  31. return parser
  32. def main():
  33. parser = init_parser()
  34. args = parser.parse_args()
  35. changes = do_compare(args.first, args.second)
  36. for change in changes:
  37. print(f"Change detected: {change}")
  38. if changes:
  39. return 1
  40. return 0
  41. if __name__ == '__main__':
  42. sys.exit(main())