Debian Codegraph @

build/compare-mozconfig/compare-mozconfigs.py

raw
1 #!/usr/bin/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 # originally from https://hg.mozilla.org/build/tools/file/4ab9c1a4e05b/scripts/release/compare-mozconfigs.py # NOQA: E501
7
8 import difflib
9 import logging
10 import os
11 import unittest
12
13 import mozunit
14
15 here = os.path.abspath(os.path.dirname(__file__))
16
17 FAILURE_CODE = 1
18 SUCCESS_CODE = 0
19
20 PLATFORMS = (
21 "linux32",
22 "linux64",
23 "macosx64",
24 "win32",
25 "win64",
26 "win64-aarch64",
27 )
28
29 log = logging.getLogger(__name__)
30
31
32 class ConfigError(Exception):
33 pass
34
35
36 def readConfig(configfile):
37 c = {}
38 with open(configfile) as config:
39 exec(config.read(), c)
40 return c["allowlist"]
41
42
43 def verify_mozconfigs(
44 mozconfig_pair, nightly_mozconfig_pair, platform, mozconfigAllowlist
45 ):
46 """Compares mozconfig to nightly_mozconfig and compare to an optional
47 allowlist of known differences. mozconfig_pair and nightly_mozconfig_pair
48 are pairs containing the mozconfig's identifier and the list of lines in
49 the mozconfig."""
50
51 # unpack the pairs to get the names, the names are just for
52 # identifying the mozconfigs when logging the error messages
53 mozconfig_name, mozconfig_lines = mozconfig_pair
54 nightly_mozconfig_name, nightly_mozconfig_lines = nightly_mozconfig_pair
55
56 if not mozconfig_lines or not nightly_mozconfig_lines:
57 log.info("Missing mozconfigs to compare for %s" % platform)
58 return False
59
60 success = True
61
62 diff_instance = difflib.Differ()
63 diff_result = diff_instance.compare(mozconfig_lines, nightly_mozconfig_lines)
64 diff_list = list(diff_result)
65
66 for line in diff_list:
67 clean_line = line[1:].strip()
68 if (line[0] == "-" or line[0] == "+") and len(clean_line) > 1:
69 # skip comment lines
70 if clean_line.startswith("#"):
71 continue
72 # compare to allowlist
73 message = ""
74 if line[0] == "-":
75 # handle lines that move around in diff
76 if "+" + line[1:] in diff_list:
77 continue
78 if platform in mozconfigAllowlist.get("release", {}):
79 if clean_line in mozconfigAllowlist["release"][platform]:
80 continue
81 elif line[0] == "+":
82 if "-" + line[1:] in diff_list:
83 continue
84 if platform in mozconfigAllowlist.get("nightly", {}):
85 if clean_line in mozconfigAllowlist["nightly"][platform]:
86 continue
87 else:
88 log.warning(
89 "%s not in %s %s!"
90 % (
91 clean_line,
92 platform,
93 mozconfigAllowlist["nightly"][platform],
94 )
95 )
96 else:
97 log.error("Skipping line %s!" % line)
98 continue
99 message = "found in %s but not in %s: %s"
100 if line[0] == "-":
101 log.error(
102 message % (mozconfig_name, nightly_mozconfig_name, clean_line)
103 )
104 else:
105 log.error(
106 message % (nightly_mozconfig_name, mozconfig_name, clean_line)
107 )
108 success = False
109 return success
110
111
112 def get_mozconfig(path):
113 """Consumes a path and returns a list of lines from the mozconfig file."""
114 with open(path) as fh:
115 return fh.readlines()
116
117
118 def compare(topsrcdir):
119 app = os.path.join(topsrcdir, "browser")
120 allowlist = readConfig(os.path.join(app, "config", "mozconfigs", "allowlist"))
121
122 success = True
123
124 def normalize_lines(lines):
125 return {l.strip() for l in lines}
126
127 for platform in PLATFORMS:
128 log.info("Comparing platform %s" % platform)
129
130 mozconfigs_path = os.path.join(app, "config", "mozconfigs", platform)
131
132 nightly_path = os.path.join(mozconfigs_path, "nightly")
133 beta_path = os.path.join(mozconfigs_path, "beta")
134 release_path = os.path.join(mozconfigs_path, "release")
135
136 nightly_lines = get_mozconfig(nightly_path)
137 beta_lines = get_mozconfig(beta_path)
138 release_lines = get_mozconfig(release_path)
139
140 # Validate that entries in allowlist['nightly'][platform] are actually
141 # present.
142 allowlist_normalized = normalize_lines(allowlist["nightly"].get(platform, []))
143 nightly_normalized = normalize_lines(nightly_lines)
144
145 for line in sorted(allowlist_normalized - nightly_normalized):
146 log.error("extra line in nightly allowlist: %s" % line)
147 success = False
148
149 log.info("Comparing beta and nightly mozconfigs")
150 passed = verify_mozconfigs(
151 (beta_path, beta_lines), (nightly_path, nightly_lines), platform, allowlist
152 )
153
154 if not passed:
155 success = False
156
157 log.info("Comparing release and nightly mozconfigs")
158 passed = verify_mozconfigs(
159 (release_path, release_lines),
160 (nightly_path, nightly_lines),
161 platform,
162 allowlist,
163 )
164 if not passed:
165 success = False
166
167 return success
168
169
170 class TestCompareMozconfigs(unittest.TestCase):
171 def test_compare_mozconfigs(self):
172 topsrcdir = os.path.abspath(os.path.join(here, "..", ".."))
173 self.assertTrue(compare(topsrcdir))
174
175
176 if __name__ == "__main__":
177 logging.basicConfig(level=logging.INFO)
178 mozunit.main()