@@ -35,48 +35,28 @@ def valid_l10n_mappings():
35
35
return mapping
36
36
37
37
38
- def add_selected_mappings (mappings ):
38
+ def distribute_mappings_evenly (mappings , version ):
39
39
"""
40
40
Write the selected mappings to the output file.
41
41
42
42
Args:
43
43
mappings (dict): A dictionary of mappings, where the keys are sites and the values are sets of regions.
44
+ version (int): The beta_version of the beta.
44
45
"""
45
46
if not mappings :
46
47
return
47
48
# sort the mappings by the length of the regions per site
48
49
mappings = dict (sorted (mappings .items (), key = lambda val : len (val [1 ]), reverse = True ))
49
50
# place the mappings into 3 containers evenly according to the load
50
51
loads = [0 , 0 , 0 ]
51
- containers = [[] for _ in range (3 )]
52
+ containers = [defaultdict ( set ) for _ in range (3 )]
52
53
for key , value in mappings .items ():
53
54
min_idx = loads .index (min (loads ))
54
- containers [min_idx ]. append ( f" { key } { ' ' . join ( value ) } " )
55
+ containers [min_idx ][ key ] = value
55
56
loads [min_idx ] += len (value )
56
- # shuffle the containers so each run for a beta is random.
57
- random .shuffle (containers )
58
- try :
59
- version = int (
60
- (
61
- subprocess .check_output (
62
- [sys .executable , "./collect_executables.py" , "-n" ]
63
- )
64
- .strip ()
65
- .decode ()
66
- )
67
- .split ("-" )[0 ]
68
- .split ("." )[1 ]
69
- .split ("b" )[1 ]
70
- )
71
- except ValueError :
72
- # failsafe version
73
- version = 0
74
- # get container index according to beta version
57
+ # get container index according to beta beta_version
75
58
run_idx = version % 3
76
- current_running_mappings = "\n " .join (containers [run_idx ])
77
- logging .warning (f"Running the mappings:\n { current_running_mappings } " )
78
- with open (OUTPUT_FILE , "a+" ) as f :
79
- f .write (f"{ current_running_mappings } \n " )
59
+ return containers [run_idx ]
80
60
81
61
82
62
def process_changed_file (f , selected_mappings ):
@@ -107,6 +87,22 @@ def process_changed_file(f, selected_mappings):
107
87
selected_mappings [site ].add (region )
108
88
109
89
90
+ def save_mappings (selected_container ):
91
+ """
92
+ Save the selected mappings to the output file.
93
+
94
+ Args:
95
+ selected_container: the selected mappings container.
96
+ """
97
+
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
+
110
106
if __name__ == "__main__" :
111
107
if os .path .exists (".env" ):
112
108
with open (".env" ) as fh :
@@ -117,11 +113,27 @@ def process_changed_file(f, selected_mappings):
117
113
os .environ ["MANUAL" ] = "true"
118
114
with open (OUTPUT_FILE , "w" ) as file :
119
115
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
120
132
l10n_mappings = valid_l10n_mappings ()
121
133
sample_mappings = {k : v for k , v in l10n_mappings .items () if k .startswith ("demo" )}
122
134
if os .environ .get ("TESTRAIL_REPORT" ) or os .environ .get ("MANUAL" ):
123
135
# Run all tests if this is a scheduled beta or a manual run
124
- add_selected_mappings ( l10n_mappings )
136
+ save_mappings ( distribute_mappings_evenly ( l10n_mappings , beta_version ) )
125
137
sys .exit (0 )
126
138
127
139
re_set_all = [
@@ -182,5 +194,5 @@ def process_changed_file(f, selected_mappings):
182
194
selected_mappings |= sample_mappings
183
195
break
184
196
185
- add_selected_mappings ( selected_mappings )
197
+ save_mappings ( distribute_mappings_evenly ( selected_mappings , beta_version ) )
186
198
sys .exit (0 )
0 commit comments