Bug 1735777 [wpt PR 31236] - webrtc wpt: fix invalid unicode indexOf method call...
[gecko.git] / config / check_source_count.py
blob33f26acd393fe41482d75044d191dd2d21ba79bf
1 #!/usr/bin/env python
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 # Usage: check_source_count.py SEARCH_TERM COUNT ERROR_LOCATION REPLACEMENT [FILES...]
8 # Checks that FILES contains exactly COUNT matches of SEARCH_TERM. If it does
9 # not, an error message is printed, quoting ERROR_LOCATION, which should
10 # probably be the filename and line number of the erroneous call to
11 # check_source_count.py.
12 from __future__ import absolute_import
13 from __future__ import print_function
14 import sys
15 import re
17 search_string = sys.argv[1]
18 expected_count = int(sys.argv[2])
19 error_location = sys.argv[3]
20 replacement = sys.argv[4]
21 files = sys.argv[5:]
23 details = {}
25 count = 0
26 for f in files:
27 text = file(f).read()
28 match = re.findall(search_string, text)
29 if match:
30 num = len(match)
31 count += num
32 details[f] = num
34 if count == expected_count:
35 print(
36 "TEST-PASS | check_source_count.py {0} | {1}".format(
37 search_string, expected_count
41 else:
42 print(
43 "TEST-UNEXPECTED-FAIL | check_source_count.py {0} | ".format(search_string),
44 end="",
46 if count < expected_count:
47 print(
48 "There are fewer occurrences of /{0}/ than expected. "
49 "This may mean that you have removed some, but forgotten to "
50 "account for it {1}.".format(search_string, error_location)
52 else:
53 print(
54 "There are more occurrences of /{0}/ than expected. We're trying "
55 "to prevent an increase in the number of {1}'s, using {2} if "
56 "possible. If it is unavoidable, you should update the expected "
57 "count {3}.".format(
58 search_string, search_string, replacement, error_location
62 print("Expected: {0}; found: {1}".format(expected_count, count))
63 for k in sorted(details):
64 print("Found {0} occurences in {1}".format(details[k], k))
65 sys.exit(-1)