image_diff.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. # Copyright (C) 2010 Gabor Rapcsanyi <rgabor@inf.u-szeged.hu>, University of Szeged
  3. # Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the Google name nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """WebKit implementations of the Port interface."""
  31. import logging
  32. import re
  33. import time
  34. from webkitpy.port import server_process
  35. _log = logging.getLogger(__name__)
  36. class ImageDiffer(object):
  37. def __init__(self, port):
  38. self._port = port
  39. self._tolerance = None
  40. self._process = None
  41. def diff_image(self, expected_contents, actual_contents, tolerance):
  42. if tolerance != self._tolerance:
  43. self.stop()
  44. try:
  45. assert(expected_contents)
  46. assert(actual_contents)
  47. assert(tolerance is not None)
  48. if not self._process:
  49. self._start(tolerance)
  50. # Note that although we are handed 'old', 'new', ImageDiff wants 'new', 'old'.
  51. self._process.write('Content-Length: %d\n%sContent-Length: %d\n%s' % (
  52. len(actual_contents), actual_contents,
  53. len(expected_contents), expected_contents))
  54. return self._read()
  55. except IOError as exception:
  56. return (None, 0, "Failed to compute an image diff: %s" % str(exception))
  57. def _start(self, tolerance):
  58. command = [self._port._path_to_image_diff(), '--tolerance', str(tolerance)]
  59. environment = self._port.setup_environ_for_server('ImageDiff')
  60. self._process = self._port._server_process_constructor(self._port, 'ImageDiff', command, environment)
  61. self._process.start()
  62. self._tolerance = tolerance
  63. def _read(self):
  64. deadline = time.time() + 2.0
  65. output = None
  66. output_image = ""
  67. while not self._process.timed_out and not self._process.has_crashed():
  68. output = self._process.read_stdout_line(deadline)
  69. if self._process.timed_out or self._process.has_crashed() or not output:
  70. break
  71. if output.startswith('diff'): # This is the last line ImageDiff prints.
  72. break
  73. if output.startswith('Content-Length'):
  74. m = re.match('Content-Length: (\d+)', output)
  75. content_length = int(m.group(1))
  76. output_image = self._process.read_stdout(deadline, content_length)
  77. output = self._process.read_stdout_line(deadline)
  78. break
  79. stderr = self._process.pop_all_buffered_stderr()
  80. err_str = ''
  81. if stderr:
  82. err_str += "ImageDiff produced stderr output:\n" + stderr
  83. if self._process.timed_out:
  84. err_str += "ImageDiff timed out\n"
  85. if self._process.has_crashed():
  86. err_str += "ImageDiff crashed\n"
  87. # FIXME: There is no need to shut down the ImageDiff server after every diff.
  88. self._process.stop()
  89. diff_percent = 0
  90. if output and output.startswith('diff'):
  91. m = re.match('diff: (.+)% (passed|failed)', output)
  92. if m.group(2) == 'passed':
  93. return (None, 0, None)
  94. diff_percent = float(m.group(1))
  95. return (output_image, diff_percent, err_str or None)
  96. def stop(self):
  97. if self._process:
  98. self._process.stop()
  99. self._process = None