-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbt_ep.py
298 lines (244 loc) · 9.62 KB
/
fbt_ep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/env python3
import json
import logging
import multiprocessing
import os
import pathlib
import signal
import subprocess
import sys
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import ClassVar
logging.basicConfig(level=logging.INFO)
logging.getLogger().handlers[0].setFormatter(
logging.Formatter("%(levelname)s: %(message)s")
)
@dataclass
class PathConfig:
work_dir: pathlib.Path
repo_dir: pathlib.Path
class LayerDeployment(ABC):
def __init__(self, layer: "FbtRepoLayer", path_config: PathConfig):
self.layer = layer
self.path_config = path_config
self.path = self._build_path()
@abstractmethod
def ensure(self) -> None:
pass
@abstractmethod
def _build_path(self) -> pathlib.Path:
pass
@dataclass
class FbtRepoLayer:
class SourceType(Enum):
# Managed using external git repo. Requires "url" and "ref"
GIT = "git"
# Managed using git submodule in the main repo. Requires "path"
SUBMODULE = "submodule"
# Managed using a catalog. Requires "name" and "version"
CATALOG = "catalog"
name: str
source: dict
@staticmethod
def from_dict(d) -> "FbtRepoLayer":
return FbtRepoLayer(
name=d["name"],
source=d["source"],
)
def get_layer_deployment(self, path_config: PathConfig) -> LayerDeployment:
def validate_keys(d, keys):
for key in keys:
if key not in d:
raise Exception(f"fbtng: missing '{key}' in {d}")
if self.source["type"] == FbtRepoLayer.SourceType.GIT.value:
validate_keys(self.source, ["url", "ref"])
return GitLayerDeployment(self, path_config)
if self.source["type"] == FbtRepoLayer.SourceType.SUBMODULE.value:
validate_keys(self.source, ["path"])
return SubmoduleLayerDeployment(self, path_config)
if self.source["type"] == FbtRepoLayer.SourceType.CATALOG.value:
validate_keys(self.source, ["name", "version"])
return CatalogLayerDeployment(self, path_config)
raise Exception(f"fbtng: unsupported source type {self.source['type']}")
class GitLayerDeployment(LayerDeployment):
def ensure(self):
# Check if the layer is already cloned and its version is correct
layer_path = self.path # TODO: remove
if not layer_path.exists():
logging.info(f"fbtng: fetching {self.layer.name}...")
subprocess.run(
["git", "clone", self.layer.source["url"], layer_path]
).check_returncode()
# Check if there are local changes - emit a warning if there are
if subprocess.run(["git", "diff", "--quiet"], cwd=layer_path).returncode != 0:
logging.warning(
f"fbtng: {self.layer.name} has local changes, not updating, using as-is"
)
return
# Check if the layer is at the correct version
try:
subprocess.run(
["git", "checkout", self.layer.source["ref"]], cwd=layer_path
).check_returncode()
except subprocess.CalledProcessError:
# Fetch tags and try again
subprocess.run(
["git", "fetch", "--tags", "--prune"], cwd=layer_path
).check_returncode()
subprocess.run(
["git", "checkout", self.layer.source["ref"]], cwd=layer_path
).check_returncode()
# Update submodules
subprocess.run(
[
"git",
"submodule",
"update",
"--init",
"--recursive",
"--jobs",
str(multiprocessing.cpu_count()),
],
cwd=layer_path,
).check_returncode()
def _build_path(self) -> pathlib.Path:
return self.path_config.work_dir / self.layer.name
class SubmoduleLayerDeployment(LayerDeployment):
def ensure(self):
# Check if the layer is already cloned
layer_path = self.path
if not layer_path.exists():
raise Exception(f"fbtng: expecting {self.layer.name} to be a submodule")
# Check if there are local changes - emit a warning if there are
if subprocess.run(["git", "diff", "--quiet"], cwd=layer_path).returncode != 0:
logging.warning(f"fbtng: {self.layer.name} has local changes")
# No need to update the submodule, it's managed by the main repo
def _build_path(self) -> pathlib.Path:
return self.path_config.repo_dir / self.layer.source["path"]
class CatalogLayerDeployment(LayerDeployment):
def ensure(self):
raise NotImplementedError("CatalogLayerDeployment is not implemented yet")
def _build_path(self) -> pathlib.Path:
return self.path_config.work_dir / self.layer.name
@dataclass
class RepoConfig:
MIN_SUPPORTED_VERSION: ClassVar[int] = 1
MAX_SUPPORTED_VERSION: ClassVar[int] = 1
version: int
layers: list[FbtRepoLayer]
@staticmethod
def from_dict(d) -> "RepoConfig":
config = RepoConfig(
version=d["version"],
layers=[FbtRepoLayer.from_dict(layer) for layer in d["layers"]],
)
if (
config.version < RepoConfig.MIN_SUPPORTED_VERSION
or config.version > RepoConfig.MAX_SUPPORTED_VERSION
):
raise Exception(
f"fbtng: unsupported fbtng.json version {config.version}, "
f"supported versions are {RepoConfig.MIN_SUPPORTED_VERSION} to "
f"{RepoConfig.MAX_SUPPORTED_VERSION}"
)
return config
@dataclass
class FbtEnvConfig:
no_sync: bool = False
toolchain_path: str = ""
verbose: bool = False
shallow_submodules: bool = False
@staticmethod
def from_env(env) -> "FbtEnvConfig":
true_values = {"1", "true", "True", "yes", "Yes"}
return FbtEnvConfig(
no_sync=env.get("FBT_NO_SYNC") in true_values,
toolchain_path=env.get("FBT_TOOLCHAIN_PATH"),
verbose=env.get("FBT_VERBOSE") in true_values,
shallow_submodules=env.get("FBT_GIT_SUBMODULE_SHALLOW") in true_values,
)
class FbtNG:
DEFAULT_SCONS_ARGS = ["--warn=target-not-built"]
WORK_DIR_NAME = ".fbt"
CONFIG_FILE_NAME = "fbt-project.json"
FBTNG_LAYER_NAME = "fbtng"
def __init__(self, repo_dir: str, env: dict):
self.path_config = PathConfig(
work_dir=pathlib.Path(repo_dir) / self.WORK_DIR_NAME,
repo_dir=pathlib.Path(repo_dir),
)
self.env_config = FbtEnvConfig.from_env(env)
self._load_config()
def _load_config(self):
with open(self.path_config.repo_dir / self.CONFIG_FILE_NAME) as f:
self.config = RepoConfig.from_dict(json.load(f))
if self.env_config.verbose:
logging.getLogger().setLevel(logging.DEBUG)
logging.debug(f"fbtng: {self.config}")
fbtng_component = next(
(c for c in self.config.layers if c.name == self.FBTNG_LAYER_NAME),
None,
)
if not (fbtng_component):
raise Exception("fbtng component not found in fbtng.json")
self.fbtng = fbtng_component.get_layer_deployment(self.path_config)
self.layers = [
c.get_layer_deployment(self.path_config)
for c in self.config.layers
if c.name != self.FBTNG_LAYER_NAME
]
def _update_git_submodules(self, git_repo: pathlib.Path):
# Disabled for now in favor of shell script-based solution
return
if not os.path.exists(git_repo / ".git"):
logging.error(f"fbtng: {git_repo} is not a git repository")
git_cmd = ["git", "submodule", "update", "--init", "--recursive"]
if self.env_config.verbose:
git_cmd.extend(["--verbose"])
if self.env_config.shallow_submodules:
git_cmd.extend(["--depth", "1"])
git_cmd.extend(["--jobs", str(multiprocessing.cpu_count())])
subprocess.run(git_cmd, cwd=git_repo).check_returncode()
def _ensure_repo_layers(self):
self._update_git_submodules(self.path_config.repo_dir)
for component in [self.fbtng, *self.layers]:
component.ensure()
def run(self, args: list[str]):
self._ensure_repo_layers()
if sys.platform == "win32":
python = "python"
else:
python = "python3"
cmdline = [python, "-m", "SCons", *self.DEFAULT_SCONS_ARGS]
if not self.env_config.verbose:
cmdline.append("-Q")
cmdline.extend(["-C", self.fbtng.path])
for layer in self.layers:
cmdline.extend(["-Y", layer.path])
cmdline.extend(["-Y", self.path_config.repo_dir])
cmdline.extend(args)
# Past this point, we're going to run SCons, so we need to ignore SIGINT
signal.signal(signal.SIGINT, signal.SIG_IGN)
logging.debug(cmdline)
return subprocess.run(cmdline).returncode
def main():
# Check that git is installed
if subprocess.run(["git", "--version"], stdout=subprocess.DEVNULL).returncode != 0:
logging.error("git is not installed")
return 1
fbtng = FbtNG(os.getcwd(), os.environ)
sys.exit(fbtng.run(sys.argv[1:]))
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
logging.warning("Interrupted by user")
sys.exit(1)
except subprocess.CalledProcessError as e:
logging.error(f"Command failed with exit code {e.returncode}")
sys.exit(e.returncode)
except Exception as e:
logging.error(e)
sys.exit(1)