PRESUBMIT.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 The LibYuv Project Authors. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style license
  5. # that can be found in the LICENSE file in the root of the source
  6. # tree. An additional intellectual property rights grant can be found
  7. # in the file PATENTS. All contributing project authors may
  8. # be found in the AUTHORS file in the root of the source tree.
  9. """
  10. Copied from Chrome's src/tools/valgrind/memcheck/PRESUBMIT.py
  11. See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
  12. for more details on the presubmit API built into gcl.
  13. """
  14. import os
  15. import re
  16. import sys
  17. def CheckChange(input_api, output_api):
  18. """Checks the memcheck suppressions files for bad data."""
  19. # Add the path to the Chrome valgrind dir to the import path:
  20. tools_vg_path = os.path.join(input_api.PresubmitLocalPath(), '..', '..', '..',
  21. 'tools', 'valgrind')
  22. sys.path.append(tools_vg_path)
  23. import suppressions
  24. sup_regex = re.compile('suppressions.*\.txt$')
  25. suppressions = {}
  26. errors = []
  27. check_for_memcheck = False
  28. # skip_next_line has 3 possible values:
  29. # - False: don't skip the next line.
  30. # - 'skip_suppression_name': the next line is a suppression name, skip.
  31. # - 'skip_param': the next line is a system call parameter error, skip.
  32. skip_next_line = False
  33. for f in filter(lambda x: sup_regex.search(x.LocalPath()),
  34. input_api.AffectedFiles()):
  35. for line, line_num in zip(f.NewContents(),
  36. xrange(1, len(f.NewContents()) + 1)):
  37. line = line.lstrip()
  38. if line.startswith('#') or not line:
  39. continue
  40. if skip_next_line:
  41. if skip_next_line == 'skip_suppression_name':
  42. if 'insert_a_suppression_name_here' in line:
  43. errors.append('"insert_a_suppression_name_here" is not a valid '
  44. 'suppression name')
  45. if suppressions.has_key(line):
  46. if f.LocalPath() == suppressions[line][1]:
  47. errors.append('suppression with name "%s" at %s line %s '
  48. 'has already been defined at line %s' %
  49. (line, f.LocalPath(), line_num,
  50. suppressions[line][1]))
  51. else:
  52. errors.append('suppression with name "%s" at %s line %s '
  53. 'has already been defined at %s line %s' %
  54. (line, f.LocalPath(), line_num,
  55. suppressions[line][0], suppressions[line][1]))
  56. else:
  57. suppressions[line] = (f, line_num)
  58. check_for_memcheck = True;
  59. skip_next_line = False
  60. continue
  61. if check_for_memcheck:
  62. if not line.startswith('Memcheck:'):
  63. errors.append('"%s" should be "Memcheck:..." in %s line %s' %
  64. (line, f.LocalPath(), line_num))
  65. check_for_memcheck = False;
  66. if line == '{':
  67. skip_next_line = 'skip_suppression_name'
  68. continue
  69. if line == "Memcheck:Param":
  70. skip_next_line = 'skip_param'
  71. continue
  72. if (line.startswith('fun:') or line.startswith('obj:') or
  73. line.startswith('Memcheck:') or line == '}' or
  74. line == '...'):
  75. continue
  76. errors.append('"%s" is probably wrong: %s line %s' % (line, f.LocalPath(),
  77. line_num))
  78. if errors:
  79. return [output_api.PresubmitError('\n'.join(errors))]
  80. return []
  81. def CheckChangeOnUpload(input_api, output_api):
  82. return CheckChange(input_api, output_api)
  83. def CheckChangeOnCommit(input_api, output_api):
  84. return CheckChange(input_api, output_api)
  85. def GetPreferredTrySlaves():
  86. # We don't have any memcheck slaves yet, so there's no use for this method.
  87. # When we have, the slave name(s) should be put into this list.
  88. return []