OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2017 The Chromium Authors. All rights reserved. | 2 # Copyright 2017 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Fix header files missing in GN. | 6 """Fix header files missing in GN. |
7 | 7 |
8 This script takes the missing header files from check_gn_headers.py, and | 8 This script takes the missing header files from check_gn_headers.py, and |
9 try to fix them by adding them to the GN files. | 9 try to fix them by adding them to the GN files. |
10 Manual cleaning up is likely required afterwards. | 10 Manual cleaning up is likely required afterwards. |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 continue | 125 continue |
126 | 126 |
127 if len(matched) < 1: | 127 if len(matched) < 1: |
128 continue | 128 continue |
129 print ' ', gnfile, rel | 129 print ' ', gnfile, rel |
130 index = matched[0] | 130 index = matched[0] |
131 lines.insert(index + 1, '"%s",' % rel) | 131 lines.insert(index + 1, '"%s",' % rel) |
132 open(gnfile, 'w').write('\n'.join(lines) + '\n') | 132 open(gnfile, 'w').write('\n'.join(lines) + '\n') |
133 | 133 |
134 | 134 |
| 135 def RemoveHeader(headers, skip_ambiguous=True): |
| 136 """Remove non-existing headers in GN files. |
| 137 |
| 138 When skip_ambiguous is True, skip if multiple matches are found. |
| 139 """ |
| 140 edits = {} |
| 141 unhandled = [] |
| 142 for filename in headers: |
| 143 filename = filename.strip() |
| 144 if not (filename.endswith('.h') or filename.endswith('.hh')): |
| 145 continue |
| 146 basename = os.path.basename(filename) |
| 147 print filename |
| 148 out, returncode = GitGrep('(/|")' + basename + '"') |
| 149 if returncode != 0 or not out: |
| 150 unhandled.append(filename) |
| 151 print ' Not found' |
| 152 continue |
| 153 |
| 154 grep_lines = out.splitlines() |
| 155 matches = [] |
| 156 for line in grep_lines: |
| 157 gnfile, linenr, contents = line.split(':') |
| 158 print ' ', gnfile, linenr, contents |
| 159 linenr = int(linenr) |
| 160 lines = open(gnfile).read().splitlines() |
| 161 assert contents in lines[linenr - 1] |
| 162 matches.append((gnfile, linenr, contents)) |
| 163 |
| 164 if len(matches) == 0: |
| 165 continue |
| 166 if len(matches) > 1: |
| 167 print '\n[WARNING] Ambiguous matching for', filename |
| 168 for i in enumerate(matches, 1): |
| 169 print '%d: %s' % (i[0], i[1]) |
| 170 print |
| 171 if skip_ambiguous: |
| 172 continue |
| 173 |
| 174 picked = raw_input('Pick the matches ("2,3" for multiple): ') |
| 175 try: |
| 176 matches = [matches[int(i) - 1] for i in picked.split(',')] |
| 177 except (ValueError, IndexError): |
| 178 continue |
| 179 |
| 180 for match in matches: |
| 181 gnfile, linenr, contents = match |
| 182 print ' ', gnfile, linenr, contents |
| 183 edits.setdefault(gnfile, set()).add(linenr) |
| 184 |
| 185 for gnfile in edits: |
| 186 lines = open(gnfile).read().splitlines() |
| 187 for l in sorted(edits[gnfile], reverse=True): |
| 188 lines.pop(l - 1) |
| 189 open(gnfile, 'w').write('\n'.join(lines) + '\n') |
| 190 |
| 191 return unhandled |
| 192 |
| 193 |
135 def main(): | 194 def main(): |
136 parser = argparse.ArgumentParser() | 195 parser = argparse.ArgumentParser() |
137 parser.add_argument('input_file', | 196 parser.add_argument('input_file', help="missing or non-existing headers, " |
138 help="missing headers, output of check_gn_headers.py") | 197 "output of check_gn_headers.py") |
139 parser.add_argument('--prefix', | 198 parser.add_argument('--prefix', |
140 help="only handle path name with this prefix") | 199 help="only handle path name with this prefix") |
| 200 parser.add_argument('--remove', action='store_true', |
| 201 help="treat input_file as non-existing headers") |
141 | 202 |
142 args, _extras = parser.parse_known_args() | 203 args, _extras = parser.parse_known_args() |
143 | 204 |
144 headers = open(args.input_file).readlines() | 205 headers = open(args.input_file).readlines() |
145 | 206 |
146 if args.prefix: | 207 if args.prefix: |
147 headers = [i for i in headers if i.startswith(args.prefix)] | 208 headers = [i for i in headers if i.startswith(args.prefix)] |
148 | 209 |
149 unhandled = AddHeadersNextToCC(headers) | 210 if args.remove: |
150 AddHeadersToSources(unhandled) | 211 RemoveHeader(headers, False) |
| 212 else: |
| 213 unhandled = AddHeadersNextToCC(headers) |
| 214 AddHeadersToSources(unhandled) |
151 | 215 |
152 | 216 |
153 if __name__ == '__main__': | 217 if __name__ == '__main__': |
154 sys.exit(main()) | 218 sys.exit(main()) |
OLD | NEW |