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