# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details on the presubmit API built into gcl.
"""

def CheckChange(input_api, output_api):
  """Checks the memcheck suppressions files for bad data."""
  suppressions = {}
  errors = []
  skip_next_line = False
  func_re = input_api.re.compile('[a-z_.]+\(.+\)$')
  for f, line_num, line in input_api.RightHandSideLines(lambda x:
      x.LocalPath().endswith('.txt')):
    line = line.lstrip()
    if line.startswith('#') or not line:
      continue

    if skip_next_line:
      if suppressions.has_key(line):
        errors.append('suppression with name "%s" at %s line %s has already '
                      'been defined at line %s' % (line, f.LocalPath(),
                                                   line_num,
                                                   suppressions[line][1]))
      else:
        suppressions[line] = (f, line_num)
      skip_next_line = False
      continue
    if line == '{':
      skip_next_line = True
      continue
    if (line.startswith('fun:') or line.startswith('obj:') or
        line == 'Heapcheck:Leak' or line == '}' or
        line == '...'):
      continue
    if func_re.match(line):
      continue
    errors.append('"%s" is probably wrong: %s line %s' % (line, f.LocalPath(),
                                                          line_num))
  if errors:
    return [output_api.PresubmitError('\n'.join(errors))]
  return []

def CheckChangeOnUpload(input_api, output_api):
  return CheckChange(input_api, output_api)

def CheckChangeOnCommit(input_api, output_api):
  return CheckChange(input_api, output_api)

@@ -12,17 +12,17 @@
  (c) 201
-0
+1
  The Chr
@@ -359,11 +359,12 @@
 the 
-mem
+heap
 chec
@@ -467,64 +467,123 @@
 %0A  f
-unc_re = input_api.re.compile('%5Ba-z_.%5D+%5C(.+%5C)$')%0A
+or f in filter(lambda x: x.LocalPath().endswith('.txt'),%0A                  input_api.AffectedFiles()):%0A 
   
+ 
 for 
-f
+line
 , li
@@ -592,96 +592,104 @@
 _num
-, line in input_api.RightHandSideLines(lambda x:%0A      x.LocalPath().endswith('.txt'
+ in zip(f.NewContents(),%0A                              xrange(1, len(f.NewContents()) + 1
 )):%0A
+%0A  
     
@@ -709,16 +709,18 @@
 strip()%0A
+  
     if l
@@ -750,32 +750,34 @@
 not line:%0A      
+  
 continue%0A%0A    if
@@ -770,16 +770,18 @@
 ntinue%0A%0A
+  
     if s
@@ -797,24 +797,26 @@
 line:%0A      
+ 
+ 
 if suppressi
@@ -830,24 +830,26 @@
 _key(line):%0A
+  
         erro
@@ -914,16 +914,18 @@
 ready '%0A
+  
         
@@ -1031,32 +1031,34 @@
                 
+  
 line_num,%0A      
@@ -1051,16 +1051,18 @@
 ne_num,%0A
+  
         
@@ -1138,14 +1138,18 @@
     
+  
 else:%0A
+  
     
@@ -1187,16 +1187,18 @@
 ne_num)%0A
+  
       sk
@@ -1216,32 +1216,34 @@
 e = False%0A      
+  
 continue%0A    if 
@@ -1231,24 +1231,26 @@
    continue%0A
+  
     if line 
@@ -1259,24 +1259,26 @@
  '%7B':%0A      
+ 
+ 
 skip_next_li
@@ -1289,24 +1289,26 @@
  True%0A      
+  
 continue%0A   
@@ -1300,24 +1300,26 @@
    continue%0A
+  
     if (line
@@ -1368,32 +1368,34 @@
 j:') or%0A        
+  
 line == 'Heapche
@@ -1421,32 +1421,34 @@
  '%7D' or%0A        
+  
 line == '...'):%0A
@@ -1445,32 +1445,34 @@
 = '...'):%0A      
+  
 continue%0A    if 
@@ -1470,49 +1470,8 @@
 e%0A  
-  if func_re.match(line):%0A      continue%0A
     
@@ -1537,32 +1537,34 @@
  f.LocalPath(),%0A
+  
                 

Fix the heapcheck presubmit script to parse the whole suppression file instead of diffs.