png.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2012 Balazs Ankes (bank@inf.u-szeged.hu) University of Szeged
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. """Supports checking WebKit style in png files."""
  24. import os
  25. import re
  26. from webkitpy.common import checksvnconfigfile
  27. from webkitpy.common import read_checksum_from_png
  28. from webkitpy.common.system.systemhost import SystemHost
  29. from webkitpy.common.checkout.scm.detection import SCMDetector
  30. class PNGChecker(object):
  31. """Check svn:mime-type for checking style"""
  32. categories = set(['image/png'])
  33. def __init__(self, file_path, handle_style_error, scm=None, host=None):
  34. self._file_path = file_path
  35. self._handle_style_error = handle_style_error
  36. self._host = host or SystemHost()
  37. self._fs = self._host.filesystem
  38. self._detector = scm or SCMDetector(self._fs, self._host.executive).detect_scm_system(self._fs.getcwd())
  39. def check(self, inline=None):
  40. errorstr = ""
  41. config_file_path = ""
  42. detection = self._detector.display_name()
  43. if self._fs.exists(self._file_path) and self._file_path.endswith("-expected.png"):
  44. with self._fs.open_binary_file_for_reading(self._file_path) as filehandle:
  45. if not read_checksum_from_png.read_checksum(filehandle):
  46. self._handle_style_error(0, 'image/png', 5, "Image lacks a checksum. Generate pngs using run-webkit-tests to ensure they have a checksum.")
  47. if detection == "git":
  48. (file_missing, autoprop_missing, png_missing) = checksvnconfigfile.check(self._host, self._fs)
  49. config_file_path = checksvnconfigfile.config_file_path(self._host, self._fs)
  50. if file_missing:
  51. self._handle_style_error(0, 'image/png', 5, "There is no SVN config file. (%s)" % config_file_path)
  52. elif autoprop_missing and png_missing:
  53. self._handle_style_error(0, 'image/png', 5, checksvnconfigfile.errorstr_autoprop(config_file_path) + checksvnconfigfile.errorstr_png(config_file_path))
  54. elif autoprop_missing:
  55. self._handle_style_error(0, 'image/png', 5, checksvnconfigfile.errorstr_autoprop(config_file_path))
  56. elif png_missing:
  57. self._handle_style_error(0, 'image/png', 5, checksvnconfigfile.errorstr_png(config_file_path))
  58. elif detection == "svn":
  59. prop_get = self._detector.propget("svn:mime-type", self._file_path)
  60. if prop_get != "image/png":
  61. errorstr = "Set the svn:mime-type property (svn propset svn:mime-type image/png %s)." % self._file_path
  62. self._handle_style_error(0, 'image/png', 5, errorstr)