• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7If 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
16extract_edits.py takes input that is concatenated from multiple tool invocations
17and 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    ...
29extract_edits.py would emit the following output:
30    <edit1>
31    <edit2>
32    <yet another edit1>
33    <yet another edit2>
34
35This python script is mainly needed on Windows.
36On 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
44import sys
45
46
47def main():
48  # TODO(dcheng): extract_edits.py should normalize paths. Doing this in
49  # apply_edits.py is too late, as a common use case is to apply edits from many
50  # different platforms.
51  unique_lines = set()
52  inside_marker_lines = False
53  for line in sys.stdin:
54    line = line.rstrip("\n\r")
55    if line == '==== BEGIN EDITS ====':
56      inside_marker_lines = True
57      continue
58    if line == '==== END EDITS ====':
59      inside_marker_lines = False
60      continue
61    if inside_marker_lines and line not in unique_lines:
62      unique_lines.add(line)
63      print line
64  return 0
65
66
67if __name__ == '__main__':
68  sys.exit(main())
69