-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfetch_update.py
More file actions
90 lines (73 loc) · 3.09 KB
/
dfetch_update.py
File metadata and controls
90 lines (73 loc) · 3.09 KB
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
"""West extension command: dfetch-update
Runs ``dfetch update`` on the workspace-root dfetch.yaml using dfetch's own
command-line entry point, then reports which fetched projects provide a
Zephyr module (i.e. contain a ``zephyr/module.yml``).
This replicates the module-discovery logic of ``dfetch/cmake/DfetchModules.cmake``
so the same information is available at fetch time without a CMake invocation.
"""
import argparse
import os
import sys
from pathlib import Path
import yaml
from west import log
from west.commands import WestCommand
class DfetchUpdate(WestCommand):
def __init__(self):
super().__init__(
name="dfetch-update",
help="fetch projects listed in the workspace dfetch.yaml",
description=(
"Run ``dfetch update`` on the workspace-root dfetch.yaml, "
"then discover which fetched projects contain a Zephyr module."
),
accepts_unknown_args=True, # forward unrecognised flags to dfetch
)
def do_add_parser(self, parser_adder):
parser = parser_adder.add_parser(
self.name,
help=self.help,
description=self.description,
)
# Mirror dfetch update's own argument definitions so --help output
# stays in sync with dfetch without duplicating the help strings here.
from dfetch.commands.update import Update as _Update
_tmp_sub = argparse.ArgumentParser().add_subparsers()
_Update.create_menu(_tmp_sub)
for action in _tmp_sub.choices["update"]._actions:
if not isinstance(action, argparse._HelpAction):
parser._add_action(action)
return parser
def do_run(self, args, unknown_args):
topdir = Path(self.topdir)
manifest_file = topdir / "dfetch.yaml"
if not manifest_file.exists():
log.die(f"dfetch.yaml not found at {manifest_file}")
# Reconstruct the explicit dfetch flags from the parsed namespace,
# then append any unrecognised flags for forward compatibility.
extra = []
if args.force:
extra.append("--force")
if args.no_recommendations:
extra.append("--no-recommendations")
extra.extend(args.projects)
extra.extend(unknown_args)
self._run_dfetch_update(topdir, extra)
# ------------------------------------------------------------------
# helpers
# ------------------------------------------------------------------
def _run_dfetch_update(self, topdir, extra_args=None):
"""Invoke dfetch's own CLI entry point with ``update`` in *topdir*."""
from dfetch.__main__ import main as dfetch_main
saved_argv = sys.argv[:]
saved_cwd = os.getcwd()
try:
os.chdir(topdir)
sys.argv = ["dfetch", "update"] + (extra_args or [])
dfetch_main()
except SystemExit as exc:
if exc.code not in (None, 0):
log.die(f"dfetch update failed (exit code {exc.code})")
finally:
sys.argv = saved_argv
os.chdir(saved_cwd)