michael@0: #!/usr/bin/env python michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: michael@0: # Usage: check_source_count.py SEARCH_TERM COUNT ERROR_LOCATION REPLACEMENT [FILES...] michael@0: # Checks that FILES contains exactly COUNT matches of SEARCH_TERM. If it does michael@0: # not, an error message is printed, quoting ERROR_LOCATION, which should michael@0: # probably be the filename and line number of the erroneous call to michael@0: # check_source_count.py. michael@0: from __future__ import print_function michael@0: import sys michael@0: import os michael@0: import re michael@0: michael@0: search_string = sys.argv[1] michael@0: expected_count = int(sys.argv[2]) michael@0: error_location = sys.argv[3] michael@0: replacement = sys.argv[4] michael@0: files = sys.argv[5:] michael@0: michael@0: details = {} michael@0: michael@0: count = 0 michael@0: for f in files: michael@0: text = file(f).read() michael@0: match = re.findall(search_string, text) michael@0: if match: michael@0: num = len(match) michael@0: count += num michael@0: details[f] = num michael@0: michael@0: if count == expected_count: michael@0: print("TEST-PASS | check_source_count.py {0} | {1}" michael@0: .format(search_string, expected_count)) michael@0: michael@0: else: michael@0: print("TEST-UNEXPECTED-FAIL | check_source_count.py {0} | " michael@0: .format(search_string), michael@0: end='') michael@0: if count < expected_count: michael@0: print("There are fewer occurrences of /{0}/ than expected. " michael@0: "This may mean that you have removed some, but forgotten to " michael@0: "account for it {1}.".format(search_string, error_location)) michael@0: else: michael@0: print("There are more occurrences of /{0}/ than expected. We're trying " michael@0: "to prevent an increase in the number of {1}'s, using {2} if " michael@0: "possible. If it is unavoidable, you should update the expected " michael@0: "count {3}.".format(search_string, search_string, replacement, michael@0: error_location)) michael@0: michael@0: print("Expected: {0}; found: {1}".format(expected_count, count)) michael@0: for k in sorted(details): michael@0: print("Found {0} occurences in {1}".format(details[k],k)) michael@0: sys.exit(-1) michael@0: