1# Copyright (c) 2011 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""
6See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
7for more details on the presubmit API built into gcl.
8"""
9
10import re
11
12def CheckChange(input_api, output_api):
13  """Checks the heapcheck suppressions files for bad data."""
14  sup_regex = re.compile('suppressions.*\.txt$')
15  suppressions = {}
16  errors = []
17  check_for_heapcheck = False
18  skip_next_line = False
19  for f in filter(lambda x: sup_regex.search(x.LocalPath()),
20                  input_api.AffectedFiles()):
21    for line, line_num in zip(f.NewContents(),
22                              xrange(1, len(f.NewContents()) + 1)):
23      line = line.lstrip()
24      if line.startswith('#') or not line:
25        continue
26
27      if skip_next_line:
28        if 'insert_a_suppression_name_here' in line:
29          errors.append('"insert_a_suppression_name_here" is not a valid '
30                        'suppression name')
31        if suppressions.has_key(line):
32          errors.append('suppression with name "%s" at %s line %s has already '
33                        'been defined at line %s' % (line, f.LocalPath(),
34                                                     line_num,
35                                                     suppressions[line][1]))
36        else:
37          suppressions[line] = (f, line_num)
38          check_for_heapcheck = True
39        skip_next_line = False
40        continue
41      if check_for_heapcheck:
42        if not line == 'Heapcheck:Leak':
43          errors.append('"%s" should be "Heapcheck:Leak" in %s line %s' %
44                        (line, f.LocalPath(), line_num))
45        check_for_heapcheck = False;
46      if line == '{':
47        skip_next_line = True
48        continue
49      if (line.startswith('fun:') or line.startswith('obj:') or
50          line == 'Heapcheck:Leak' or line == '}' or
51          line == '...'):
52        continue
53      errors.append('"%s" is probably wrong: %s line %s' % (line, f.LocalPath(),
54                                                            line_num))
55  if errors:
56    return [output_api.PresubmitError('\n'.join(errors))]
57  return []
58
59def CheckChangeOnUpload(input_api, output_api):
60  return CheckChange(input_api, output_api)
61
62def CheckChangeOnCommit(input_api, output_api):
63  return CheckChange(input_api, output_api)
64