1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/config/check_source_count.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,57 @@ 1.4 +#!/usr/bin/env python 1.5 +# This Source Code Form is subject to the terms of the Mozilla Public 1.6 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.8 + 1.9 + 1.10 +# Usage: check_source_count.py SEARCH_TERM COUNT ERROR_LOCATION REPLACEMENT [FILES...] 1.11 +# Checks that FILES contains exactly COUNT matches of SEARCH_TERM. If it does 1.12 +# not, an error message is printed, quoting ERROR_LOCATION, which should 1.13 +# probably be the filename and line number of the erroneous call to 1.14 +# check_source_count.py. 1.15 +from __future__ import print_function 1.16 +import sys 1.17 +import os 1.18 +import re 1.19 + 1.20 +search_string = sys.argv[1] 1.21 +expected_count = int(sys.argv[2]) 1.22 +error_location = sys.argv[3] 1.23 +replacement = sys.argv[4] 1.24 +files = sys.argv[5:] 1.25 + 1.26 +details = {} 1.27 + 1.28 +count = 0 1.29 +for f in files: 1.30 + text = file(f).read() 1.31 + match = re.findall(search_string, text) 1.32 + if match: 1.33 + num = len(match) 1.34 + count += num 1.35 + details[f] = num 1.36 + 1.37 +if count == expected_count: 1.38 + print("TEST-PASS | check_source_count.py {0} | {1}" 1.39 + .format(search_string, expected_count)) 1.40 + 1.41 +else: 1.42 + print("TEST-UNEXPECTED-FAIL | check_source_count.py {0} | " 1.43 + .format(search_string), 1.44 + end='') 1.45 + if count < expected_count: 1.46 + print("There are fewer occurrences of /{0}/ than expected. " 1.47 + "This may mean that you have removed some, but forgotten to " 1.48 + "account for it {1}.".format(search_string, error_location)) 1.49 + else: 1.50 + print("There are more occurrences of /{0}/ than expected. We're trying " 1.51 + "to prevent an increase in the number of {1}'s, using {2} if " 1.52 + "possible. If it is unavoidable, you should update the expected " 1.53 + "count {3}.".format(search_string, search_string, replacement, 1.54 + error_location)) 1.55 + 1.56 + print("Expected: {0}; found: {1}".format(expected_count, count)) 1.57 + for k in sorted(details): 1.58 + print("Found {0} occurences in {1}".format(details[k],k)) 1.59 + sys.exit(-1) 1.60 +