compare_torque_output.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. # Copyright 2020 the V8 project authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """
  6. Compare two folders and print any differences between files to both a
  7. results file and stderr.
  8. Specifically we use this to compare the output of Torque generator for
  9. both x86 and x64 (-m32) toolchains.
  10. """
  11. import difflib
  12. import filecmp
  13. import itertools
  14. import os
  15. import sys
  16. assert len(sys.argv) > 3
  17. folder1 = sys.argv[1]
  18. folder2 = sys.argv[2]
  19. results_file_name = sys.argv[3]
  20. with open(results_file_name, "w") as results_file:
  21. def write(line):
  22. # Print line to both results file and stderr
  23. sys.stderr.write(line)
  24. results_file.write(line)
  25. def has_one_sided_diff(dcmp, side, side_list):
  26. # Check that we do not have files only on one side of the comparison
  27. if side_list:
  28. write("Some files exist only in %s\n" % side)
  29. for fl in side_list:
  30. write(fl)
  31. return side_list
  32. def has_content_diff(dcmp):
  33. # Check that we do not have content differences in the common files
  34. _, diffs, _ = filecmp.cmpfiles(
  35. dcmp.left, dcmp.right,
  36. dcmp.common_files, shallow=False)
  37. if diffs:
  38. write("Found content differences between %s and %s\n" %
  39. (dcmp.left, dcmp.right))
  40. for name in diffs:
  41. write("File diff %s\n" % name)
  42. left_file = os.path.join(dcmp.left, name)
  43. right_file = os.path.join(dcmp.right, name)
  44. with open(left_file) as f1, open(right_file) as f2:
  45. diff = difflib.unified_diff(
  46. f1.readlines(), f2.readlines(),
  47. dcmp.left, dcmp.right)
  48. for l in itertools.islice(diff, 100):
  49. write(l)
  50. write("\n\n")
  51. return diffs
  52. dcmp = filecmp.dircmp(folder1, folder2)
  53. has_diffs = has_one_sided_diff(dcmp, dcmp.left, dcmp.left_only) \
  54. or has_one_sided_diff(dcmp, dcmp.right, dcmp.right_only) \
  55. or has_content_diff(dcmp)
  56. if has_diffs:
  57. sys.exit(1)