Bug 1905865 - [devtools] Update webidl-pure-allowlist.js. r=devtools-reviewers,ochameau.
[gecko.git] / python / mozboot / mozboot / mozconfig.py
bloba1ae4c8523cdbad53b3df3215f3b3cec5a496473
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import filecmp
6 import os
7 from pathlib import Path
8 from typing import Union
10 MOZ_MYCONFIG_ERROR = """
11 The MOZ_MYCONFIG environment variable to define the location of mozconfigs
12 is deprecated. If you wish to define the mozconfig path via an environment
13 variable, use MOZCONFIG instead.
14 """.strip()
16 MOZCONFIG_LEGACY_PATH_ERROR = """
17 You currently have a mozconfig at %s. This implicit location is no longer
18 supported. Please move it to %s/.mozconfig or set an explicit path
19 via the $MOZCONFIG environment variable.
20 """.strip()
22 DEFAULT_TOPSRCDIR_PATHS = (".mozconfig", "mozconfig")
23 DEPRECATED_TOPSRCDIR_PATHS = ("mozconfig.sh", "myconfig.sh")
24 DEPRECATED_HOME_PATHS = (".mozconfig", ".mozconfig.sh", ".mozmyconfig.sh")
27 class MozconfigFindException(Exception):
28 """Raised when a mozconfig location is not defined properly."""
31 class MozconfigBuilder(object):
32 def __init__(self):
33 self._lines = []
35 def append(self, block):
36 self._lines.extend([line.strip() for line in block.split("\n") if line.strip()])
38 def generate(self):
39 return "".join(line + "\n" for line in self._lines)
42 def find_mozconfig(topsrcdir: Union[str, Path], env=os.environ):
43 """Find the active mozconfig file for the current environment.
45 This emulates the logic in mozconfig-find.
47 1) If ENV[MOZCONFIG] is set, use that
48 2) If $TOPSRCDIR/mozconfig or $TOPSRCDIR/.mozconfig exists, use it.
49 3) If both exist or if there are legacy locations detected, error out.
51 The absolute path to the found mozconfig will be returned on success.
52 None will be returned if no mozconfig could be found. A
53 MozconfigFindException will be raised if there is a bad state,
54 including conditions from #3 above.
55 """
56 topsrcdir = Path(topsrcdir)
58 # Check for legacy methods first.
59 if "MOZ_MYCONFIG" in env:
60 raise MozconfigFindException(MOZ_MYCONFIG_ERROR)
62 env_path = env.get("MOZCONFIG", None) or None
64 if env_path is not None:
65 env_path = Path(env_path)
67 if env_path is not None:
68 if not env_path.is_absolute():
69 potential_roots = [topsrcdir, Path.cwd()]
70 # Attempt to eliminate duplicates for e.g.
71 # self.topsrcdir == Path.cwd().
72 potential_roots_strings = set(str(p.resolve()) for p in potential_roots)
73 existing = [
74 root
75 for root in potential_roots_strings
76 if (Path(root) / env_path).exists()
78 if len(existing) > 1:
79 # There are multiple files, but we might have a setup like:
81 # somedirectory/
82 # srcdir/
83 # objdir/
85 # MOZCONFIG=../srcdir/some/path/to/mozconfig
87 # and be configuring from the objdir. So even though we
88 # have multiple existing files, they are actually the same
89 # file.
90 mozconfigs = [root / env_path for root in existing]
91 if not all(
92 map(
93 lambda p1, p2: filecmp.cmp(p1, p2, shallow=False),
94 mozconfigs[:-1],
95 mozconfigs[1:],
98 raise MozconfigFindException(
99 "MOZCONFIG environment variable refers to a path that "
100 + "exists in more than one of "
101 + ", ".join(potential_roots_strings)
102 + ". Remove all but one."
104 elif not existing:
105 raise MozconfigFindException(
106 "MOZCONFIG environment variable refers to a path that "
107 + "does not exist in any of "
108 + ", ".join(potential_roots_strings)
111 env_path = existing[0] / env_path
112 elif not env_path.exists(): # non-relative path
113 raise MozconfigFindException(
114 "MOZCONFIG environment variable refers to a path that "
115 f"does not exist: {env_path}"
118 if not env_path.is_file():
119 raise MozconfigFindException(
120 "MOZCONFIG environment variable refers to a " f"non-file: {env_path}"
123 srcdir_paths = [topsrcdir / p for p in DEFAULT_TOPSRCDIR_PATHS]
124 existing = [p for p in srcdir_paths if p.is_file()]
126 if env_path is None and len(existing) > 1:
127 raise MozconfigFindException(
128 "Multiple default mozconfig files "
129 "present. Remove all but one. " + ", ".join(str(p) for p in existing)
132 path = None
134 if env_path is not None:
135 path = env_path
136 elif len(existing):
137 assert len(existing) == 1
138 path = existing[0]
140 if path is not None:
141 return Path.cwd() / path
143 deprecated_paths = [topsrcdir / s for s in DEPRECATED_TOPSRCDIR_PATHS]
145 home = env.get("HOME", None)
146 if home is not None:
147 home = Path(home)
148 deprecated_paths.extend([home / s for s in DEPRECATED_HOME_PATHS])
150 for path in deprecated_paths:
151 if path.exists():
152 raise MozconfigFindException(
153 MOZCONFIG_LEGACY_PATH_ERROR % (path, topsrcdir)
156 return None