Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/env python
      2 # Copyright (c) 2016 The Chromium Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 """Script to extract edits from clang tool output.
      6 
      7 If a clang tool emits edits, then the edits should look like this:
      8     ...
      9     ==== BEGIN EDITS ====
     10     <edit1>
     11     <edit2>
     12     ...
     13     ==== END EDITS ====
     14     ...
     15 
     16 extract_edits.py takes input that is concatenated from multiple tool invocations
     17 and extract just the edits.  In other words, given the following input:
     18     ...
     19     ==== BEGIN EDITS ====
     20     <edit1>
     21     <edit2>
     22     ==== END EDITS ====
     23     ...
     24     ==== BEGIN EDITS ====
     25     <yet another edit1>
     26     <yet another edit2>
     27     ==== END EDITS ====
     28     ...
     29 extract_edits.py would emit the following output:
     30     <edit1>
     31     <edit2>
     32     <yet another edit1>
     33     <yet another edit2>
     34 
     35 This python script is mainly needed on Windows.
     36 On unix this script can be replaced with running sed as follows:
     37 
     38     $ cat run_tool.debug.out \
     39         | sed '/^==== BEGIN EDITS ====$/,/^==== END EDITS ====$/{//!b};d'
     40         | sort | uniq
     41 """
     42 
     43 
     44 import sys
     45 
     46 
     47 def main():
     48   unique_lines = set()
     49   inside_marker_lines = False
     50   for line in sys.stdin:
     51     line = line.rstrip("\n\r")
     52     if line == '==== BEGIN EDITS ====':
     53       inside_marker_lines = True
     54       continue
     55     if line == '==== END EDITS ====':
     56       inside_marker_lines = False
     57       continue
     58     if inside_marker_lines and line not in unique_lines:
     59       unique_lines.add(line)
     60       print line
     61   return 0
     62 
     63 
     64 if __name__ == '__main__':
     65   sys.exit(main())
     66