Back to index

enigmail  1.4.3
check_source_count.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Usage: check_source_count.py SEARCH_TERM COUNT ERROR_LOCATION REPLACEMENT [FILES...]
00004 #   Checks that FILES contains exactly COUNT matches of SEARCH_TERM. If it does
00005 #   not, an error message is printed, quoting ERROR_LOCATION, which should
00006 #   probably be the filename and line number of the erroneous call to
00007 #   check_source_count.py.
00008 
00009 import sys
00010 import os
00011 import re
00012 
00013 search_string = sys.argv[1]
00014 expected_count = int(sys.argv[2])
00015 error_location = sys.argv[3]
00016 replacement = sys.argv[4]
00017 files = sys.argv[5:]
00018 
00019 details = {}
00020 
00021 count = 0
00022 for f in files:
00023     text = file(f).read()
00024     match = re.findall(search_string, text)
00025     if match:
00026         num = len(match)
00027         count += num
00028         details[f] = num
00029 
00030 if count == expected_count:
00031     print "TEST-PASS | check_source_count.py %s | %d" % (search_string, expected_count)
00032 
00033 else:
00034     print "TEST-UNEXPECTED-FAIL | check_source_count.py %s | " % (search_string),
00035     if count < expected_count:
00036         print "There are fewer occurrences of /%s/ than expected. This may mean that you have removed some, but forgotten to account for it %s." % (search_string, error_location)
00037     else:
00038         print "There are more occurrences of /%s/ than expected. We're trying to prevent an increase in the number of %s's, using %s if possible. If it in unavoidable, you should update the expected count %s." % (search_string, search_string, replacement, error_location)
00039 
00040     print "Expected: %d; found: %d" % (expected_count, count)
00041     for k in sorted(details):
00042         print "Found %d occurences in %s" % (details[k],k)
00043     sys.exit(-1)
00044