1
1
import json
2
+ import logging
2
3
import os
3
4
import re
5
+ import subprocess
4
6
import sys
5
7
from collections import defaultdict
6
8
from subprocess import check_output
@@ -32,16 +34,28 @@ def valid_l10n_mappings():
32
34
return mapping
33
35
34
36
35
- def add_selected_mappings (mappings ):
37
+ def distribute_mappings_evenly (mappings , version ):
36
38
"""
37
39
Write the selected mappings to the output file.
38
40
39
41
Args:
40
42
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.
41
44
"""
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 ]
45
59
46
60
47
61
def process_changed_file (f , selected_mappings ):
@@ -53,7 +67,6 @@ def process_changed_file(f, selected_mappings):
53
67
selected_mappings: the selected mappings dictionary (updated in place).
54
68
"""
55
69
split = f .split (SLASH )
56
-
57
70
if f .startswith (os .path .join ("l10n_CM" , "sites" )) or f .startswith (
58
71
os .path .join ("l10n_CM" , "constants" )
59
72
):
@@ -73,6 +86,23 @@ def process_changed_file(f, selected_mappings):
73
86
selected_mappings [site ].add (region )
74
87
75
88
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
+
76
106
if __name__ == "__main__" :
77
107
if os .path .exists (".env" ):
78
108
with open (".env" ) as fh :
@@ -83,11 +113,27 @@ def process_changed_file(f, selected_mappings):
83
113
os .environ ["MANUAL" ] = "true"
84
114
with open (OUTPUT_FILE , "w" ) as file :
85
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
86
132
l10n_mappings = valid_l10n_mappings ()
87
133
sample_mappings = {k : v for k , v in l10n_mappings .items () if k .startswith ("demo" )}
88
134
if os .environ .get ("TESTRAIL_REPORT" ) or os .environ .get ("MANUAL" ):
89
135
# 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 ) )
91
137
sys .exit (0 )
92
138
93
139
re_set_all = [
@@ -148,5 +194,5 @@ def process_changed_file(f, selected_mappings):
148
194
selected_mappings |= sample_mappings
149
195
break
150
196
151
- add_selected_mappings ( selected_mappings )
197
+ save_mappings ( distribute_mappings_evenly ( selected_mappings , beta_version ) )
152
198
sys .exit (0 )
0 commit comments