tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

check_source_count.py (1906B)


      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 
      6 
      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 import re
     13 import sys
     14 
     15 search_string = sys.argv[1]
     16 expected_count = int(sys.argv[2])
     17 error_location = sys.argv[3]
     18 replacement = sys.argv[4]
     19 files = sys.argv[5:]
     20 
     21 details = {}
     22 
     23 count = 0
     24 for f in files:
     25    text = file(f).read()
     26    match = re.findall(search_string, text)
     27    if match:
     28        num = len(match)
     29        count += num
     30        details[f] = num
     31 
     32 if count == expected_count:
     33    print(f"TEST-PASS | check_source_count.py {search_string} | {expected_count}")
     34 
     35 else:
     36    print(
     37        f"TEST-UNEXPECTED-FAIL | check_source_count.py {search_string} | ",
     38        end="",
     39    )
     40    if count < expected_count:
     41        print(
     42            f"There are fewer occurrences of /{search_string}/ than expected. "
     43            "This may mean that you have removed some, but forgotten to "
     44            f"account for it {error_location}."
     45        )
     46    else:
     47        print(
     48            f"There are more occurrences of /{search_string}/ than expected. We're trying "
     49            f"to prevent an increase in the number of {search_string}'s, using {replacement} if "
     50            "possible. If it is unavoidable, you should update the expected "
     51            f"count {error_location}."
     52        )
     53 
     54    print(f"Expected: {expected_count}; found: {count}")
     55    for k in sorted(details):
     56        print(f"Found {details[k]} occurences in {k}")
     57    sys.exit(-1)