|
| 1 | +import json |
| 2 | +import os |
| 3 | +import re |
| 4 | +import sys |
| 5 | +from collections import defaultdict |
| 6 | +from subprocess import check_output |
| 7 | + |
| 8 | +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 9 | +SLASH = "/" if "/" in SCRIPT_DIR else "\\" |
| 10 | +CI_MARK = "@pytest.mark.ci" |
| 11 | +HEADED_MARK = "@pytest.mark.headed" |
| 12 | +OUTPUT_FILE = "selected_l10n_mappings" |
| 13 | + |
| 14 | + |
| 15 | +def valid_l10n_mappings(): |
| 16 | + """ |
| 17 | + Get a dictionary of valid l10n mappings by going through the region files. |
| 18 | +
|
| 19 | + Returns: |
| 20 | + The dictionary of valid l10n mappings. |
| 21 | + """ |
| 22 | + mapping = defaultdict(set) |
| 23 | + region_paths = [d for d in os.listdir("./l10n_CM/region/")] |
| 24 | + for region_path in region_paths: |
| 25 | + if region_path != "Unified.json": |
| 26 | + region = region_path.split(".")[0] |
| 27 | + with open(f"./l10n_CM/region/{region_path}", "r+") as f: |
| 28 | + region_file = json.load(f) |
| 29 | + if region_file.get("sites"): |
| 30 | + for site in region_file.get("sites"): |
| 31 | + mapping[site].add(region) |
| 32 | + return mapping |
| 33 | + |
| 34 | + |
| 35 | +def add_selected_mappings(mappings): |
| 36 | + """ |
| 37 | + Write the selected mappings to the output file. |
| 38 | +
|
| 39 | + Args: |
| 40 | + mappings (dict): A dictionary of mappings, where the keys are sites and the values are sets of regions. |
| 41 | + """ |
| 42 | + for site, regions in mappings.items(): |
| 43 | + with open(OUTPUT_FILE, "a+") as f: |
| 44 | + f.write(f"{site} {' '.join(regions)}\n") |
| 45 | + |
| 46 | + |
| 47 | +def process_changed_file(f, selected_mappings): |
| 48 | + """ |
| 49 | + process the changed file to add the site/region mappings. |
| 50 | +
|
| 51 | + Args: |
| 52 | + f: the changed file. |
| 53 | + selected_mappings: the selected mappings dictionary (updated in place). |
| 54 | + """ |
| 55 | + split = f.split(SLASH) |
| 56 | + if f.startswith("l10n_CM/sites/") or f.startswith("l10n_CM/constants/"): |
| 57 | + # if constants or sites are changed, add a single site/region mapping entry. |
| 58 | + site = split[2] |
| 59 | + region = split[3] |
| 60 | + region_path = os.path.join("l10n_CM", "region", f"{region}.json") |
| 61 | + # make sure the region mapping file exists before adding the mapping |
| 62 | + if os.path.exists(region_path): |
| 63 | + selected_mappings[site].add(region) |
| 64 | + elif f.startswith("l10n_CM/region/"): |
| 65 | + # if a region file is changed, add the region to each site mapping. |
| 66 | + region = split[-1].split(".")[0] |
| 67 | + with open(f, "r+") as f: |
| 68 | + region_file = json.load(f) |
| 69 | + for site in region_file.get("sites", []): |
| 70 | + selected_mappings[site].add(region) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + if os.path.exists(".env"): |
| 75 | + with open(".env") as fh: |
| 76 | + contents = fh.read() |
| 77 | + if "TESTRAIL_REPORT='true'" in contents: |
| 78 | + os.environ["TESTRAIL_REPORT"] = "true" |
| 79 | + if "RUN_ALL='true'" in contents: |
| 80 | + os.environ["MANUAL"] = "true" |
| 81 | + with open(OUTPUT_FILE, "w") as file: |
| 82 | + pass # File is created or cleared |
| 83 | + l10n_mappings = valid_l10n_mappings() |
| 84 | + if os.environ.get("TESTRAIL_REPORT") or os.environ.get("MANUAL"): |
| 85 | + # Run all tests if this is a scheduled beta or a manual run |
| 86 | + add_selected_mappings(l10n_mappings) |
| 87 | + sys.exit(0) |
| 88 | + |
| 89 | + re_set_all = [ |
| 90 | + r"l10n_CM/Unified/test_.*\.py", |
| 91 | + r"l10n_CM/Unified/conftest\.py", |
| 92 | + r"l10n_CM/conftest\.py", |
| 93 | + r"modules/page_object_prefs\.py", |
| 94 | + r"modules/data/about_prefs\.components\.json", |
| 95 | + r"modules/page_object_autofill\.py", |
| 96 | + r"modules/data/address_fill\.components\.json", |
| 97 | + r"modules/data/credit_card_fill\.components\.json", |
| 98 | + r"modules/browser_object_autofill_popup\.py", |
| 99 | + r"modules/data/autofill_popup\.components\.json", |
| 100 | + ] |
| 101 | + re_set_select = [ |
| 102 | + r"l10n_CM/constants/.*/.*/.*\.json", |
| 103 | + r"l10n_CM/sites/.*/.*/.*\.html", |
| 104 | + r"l10n_CM/region/.*\.json", |
| 105 | + ] |
| 106 | + |
| 107 | + re_set_all = set( |
| 108 | + [ |
| 109 | + re.compile(val.replace("/", r"\\")) if SLASH == "\\" else re.compile(val) |
| 110 | + for val in re_set_all |
| 111 | + ] |
| 112 | + ) |
| 113 | + re_set_select = set( |
| 114 | + [ |
| 115 | + re.compile(val.replace("/", r"\\")) if SLASH == "\\" else re.compile(val) |
| 116 | + for val in re_set_select |
| 117 | + ] |
| 118 | + ) |
| 119 | + |
| 120 | + run_list = [] |
| 121 | + check_output(["git", "fetch", "--quiet", "--depth=1", "origin", "main"]) |
| 122 | + committed_files = ( |
| 123 | + check_output(["git", "--no-pager", "diff", "--name-only", "origin/main"]) |
| 124 | + .decode() |
| 125 | + .replace("/", SLASH) |
| 126 | + .splitlines() |
| 127 | + ) |
| 128 | + main_conftest = "conftest.py" |
| 129 | + base_page = os.path.join("modules", "page_base.py") |
| 130 | + |
| 131 | + if main_conftest in committed_files or base_page in committed_files: |
| 132 | + # Run all the tests for all mappings if main conftest or basepage changed |
| 133 | + add_selected_mappings(l10n_mappings) |
| 134 | + sys.exit(0) |
| 135 | + |
| 136 | + # Run all the tests for all mappings if any core l10n model, component, conftest, or tests are changed. |
| 137 | + selected_mappings = defaultdict(set) |
| 138 | + # by default, have the demo page run for US |
| 139 | + selected_mappings["demo"] = {"US"} |
| 140 | + for f in committed_files: |
| 141 | + for re_val in re_set_all: |
| 142 | + if re_val.match(f): |
| 143 | + add_selected_mappings(l10n_mappings) |
| 144 | + sys.exit(0) |
| 145 | + # check if constants, sites or region directory files were changed or added. |
| 146 | + # if so, add the site/region mappings. |
| 147 | + for re_val in re_set_select: |
| 148 | + if re_val.match(f): |
| 149 | + process_changed_file(f, selected_mappings) |
| 150 | + |
| 151 | + add_selected_mappings(selected_mappings) |
| 152 | + sys.exit(0) |
0 commit comments