11import json
2+ import logging
23import os
34import re
5+ import subprocess
46import sys
57from collections import defaultdict
68from subprocess import check_output
@@ -32,16 +34,28 @@ def valid_l10n_mappings():
3234 return mapping
3335
3436
35- def add_selected_mappings (mappings ):
37+ def distribute_mappings_evenly (mappings , version ):
3638 """
3739 Write the selected mappings to the output file.
3840
3941 Args:
4042 mappings (dict): A dictionary of mappings, where the keys are sites and the values are sets of regions.
43+ version (int): The beta_version of the beta.
4144 """
42- for site , regions in mappings .items ():
43- with open (OUTPUT_FILE , "a+" ) as f :
44- f .write (f"{ site } { ' ' .join (regions )} \n " )
45+ if not mappings :
46+ return {}
47+ # sort the mappings by the length of the regions per site
48+ mappings = dict (sorted (mappings .items (), key = lambda val : len (val [1 ]), reverse = True ))
49+ # place the mappings into 3 containers evenly according to the load
50+ loads = [0 , 0 , 0 ]
51+ containers = [defaultdict (set ) for _ in range (3 )]
52+ for key , value in mappings .items ():
53+ min_idx = loads .index (min (loads ))
54+ containers [min_idx ][key ] = value
55+ loads [min_idx ] += len (value )
56+ # get container index according to beta beta_version
57+ run_idx = version % 3
58+ return containers [run_idx ]
4559
4660
4761def process_changed_file (f , selected_mappings ):
@@ -53,7 +67,6 @@ def process_changed_file(f, selected_mappings):
5367 selected_mappings: the selected mappings dictionary (updated in place).
5468 """
5569 split = f .split (SLASH )
56-
5770 if f .startswith (os .path .join ("l10n_CM" , "sites" )) or f .startswith (
5871 os .path .join ("l10n_CM" , "constants" )
5972 ):
@@ -73,6 +86,23 @@ def process_changed_file(f, selected_mappings):
7386 selected_mappings [site ].add (region )
7487
7588
89+ def save_mappings (selected_container ):
90+ """
91+ Save the selected mappings to the output file.
92+
93+ Args:
94+ selected_container: the selected mappings container.
95+ """
96+ if not selected_container :
97+ return
98+ current_running_mappings = [
99+ f"{ key } { ' ' .join (value )} \n " for key , value in selected_container .items ()
100+ ]
101+ logging .warning (f"Running the mappings:\n { '' .join (current_running_mappings )} " )
102+ with open (OUTPUT_FILE , "a+" ) as f :
103+ f .writelines (current_running_mappings )
104+
105+
76106if __name__ == "__main__" :
77107 if os .path .exists (".env" ):
78108 with open (".env" ) as fh :
@@ -83,11 +113,27 @@ def process_changed_file(f, selected_mappings):
83113 os .environ ["MANUAL" ] = "true"
84114 with open (OUTPUT_FILE , "w" ) as file :
85115 pass # File is created or cleared
116+ try :
117+ beta_version = int (
118+ (
119+ subprocess .check_output (
120+ [sys .executable , "./collect_executables.py" , "-n" ]
121+ )
122+ .strip ()
123+ .decode ()
124+ )
125+ .split ("-" )[0 ]
126+ .split ("." )[1 ]
127+ .split ("b" )[1 ]
128+ )
129+ except ValueError :
130+ # failsafe beta_version
131+ beta_version = 0
86132 l10n_mappings = valid_l10n_mappings ()
87133 sample_mappings = {k : v for k , v in l10n_mappings .items () if k .startswith ("demo" )}
88134 if os .environ .get ("TESTRAIL_REPORT" ) or os .environ .get ("MANUAL" ):
89135 # Run all tests if this is a scheduled beta or a manual run
90- add_selected_mappings ( l10n_mappings )
136+ save_mappings ( distribute_mappings_evenly ( l10n_mappings , beta_version ) )
91137 sys .exit (0 )
92138
93139 re_set_all = [
@@ -148,5 +194,5 @@ def process_changed_file(f, selected_mappings):
148194 selected_mappings |= sample_mappings
149195 break
150196
151- add_selected_mappings ( selected_mappings )
197+ save_mappings ( distribute_mappings_evenly ( selected_mappings , beta_version ) )
152198 sys .exit (0 )
0 commit comments