check_source_count.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/.
  5. # Usage: check_source_count.py SEARCH_TERM COUNT ERROR_LOCATION REPLACEMENT [FILES...]
  6. # Checks that FILES contains exactly COUNT matches of SEARCH_TERM. If it does
  7. # not, an error message is printed, quoting ERROR_LOCATION, which should
  8. # probably be the filename and line number of the erroneous call to
  9. # check_source_count.py.
  10. from __future__ import print_function
  11. import sys
  12. import os
  13. import re
  14. search_string = sys.argv[1]
  15. expected_count = int(sys.argv[2])
  16. error_location = sys.argv[3]
  17. replacement = sys.argv[4]
  18. files = sys.argv[5:]
  19. details = {}
  20. count = 0
  21. for f in files:
  22. text = file(f).read()
  23. match = re.findall(search_string, text)
  24. if match:
  25. num = len(match)
  26. count += num
  27. details[f] = num
  28. if count == expected_count:
  29. print("TEST-PASS | check_source_count.py {0} | {1}"
  30. .format(search_string, expected_count))
  31. else:
  32. print("TEST-UNEXPECTED-FAIL | check_source_count.py {0} | "
  33. .format(search_string),
  34. end='')
  35. if count < expected_count:
  36. print("There are fewer occurrences of /{0}/ than expected. "
  37. "This may mean that you have removed some, but forgotten to "
  38. "account for it {1}.".format(search_string, error_location))
  39. else:
  40. print("There are more occurrences of /{0}/ than expected. We're trying "
  41. "to prevent an increase in the number of {1}'s, using {2} if "
  42. "possible. If it is unavoidable, you should update the expected "
  43. "count {3}.".format(search_string, search_string, replacement,
  44. error_location))
  45. print("Expected: {0}; found: {1}".format(expected_count, count))
  46. for k in sorted(details):
  47. print("Found {0} occurences in {1}".format(details[k],k))
  48. sys.exit(-1)