Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app: project: Improve forall #721

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/west/app/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,18 @@ def __init__(self):
Runs a shell (on a Unix OS) or batch (on Windows) command
within the repository of each of the specified PROJECTs.

The following variables are set when running your command:
WEST_PROJECT_NAME
WEST_PROJECT_PATH
WEST_PROJECT_ABSPATH (depends on topdir)
WEST_PROJECT_REVISION
WEST_PROJECT_URL
WEST_PROJECT_REMOTE

Use proper escaping, for example:

west forall -c "echo \\$WEST_PROJECT_NAME"

If the command has multiple words, you must quote the -c
option to prevent the shell from splitting it up. Since
the command is run through the shell, you can use
Expand All @@ -1615,6 +1627,9 @@ def do_add_parser(self, parser_adder):
epilog=ACTIVE_CLONED_PROJECTS_HELP)
parser.add_argument('-c', dest='subcommand', metavar='COMMAND',
required=True)
parser.add_argument('-C', dest='cwd',
help='''run commands from this directory;
defaults to each project's paths if omitted''')
parser.add_argument('-a', '--all', action='store_true',
help='include inactive projects'),
parser.add_argument('-g', '--group', dest='groups',
Expand All @@ -1631,13 +1646,24 @@ def do_add_parser(self, parser_adder):
def do_run(self, args, user_args):
failed = []
group_set = set(args.groups)
env = os.environ.copy()
for project in self._cloned_projects(args, only_active=not args.all):
if group_set and not group_set.intersection(set(project.groups)):
continue

env["WEST_PROJECT_NAME"] = project.name
env["WEST_PROJECT_PATH"] = project.path
env["WEST_PROJECT_ABSPATH"] = project.abspath if project.abspath else ''
env["WEST_PROJECT_REVISION"] = project.revision
env["WEST_PROJECT_URL"] = project.url
env["WEST_PROJECT_REMOTE"] = project.remote_name

cwd = args.cwd if args.cwd else project.abspath

self.banner(
f'running "{args.subcommand}" in {project.name_and_path}:')
rc = subprocess.Popen(args.subcommand, shell=True,
cwd=project.abspath).wait()
rc = subprocess.Popen(args.subcommand, shell=True, env=env,
cwd=cwd).wait()
if rc:
failed.append(project)
self._handle_failed(args, failed)
Expand Down
1 change: 1 addition & 0 deletions src/west/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@ def __init__(self, path: Optional[PathType] = None,
self.url: str = ''
self.submodules = False
self.revision: str = 'HEAD'
self.remote_name: str = ''
self.clone_depth: Optional[int] = None
self.groups = []
self.userdata: Optional[Any] = userdata
Expand Down
12 changes: 11 additions & 1 deletion tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from west.manifest import ImportFlag as MIF
from conftest import create_branch, create_workspace, create_repo, \
add_commit, add_tag, check_output, cmd, GIT, rev_parse, \
check_proj_consistency
check_proj_consistency, WINDOWS

assert 'TOXTEMPDIR' in os.environ, "you must run these tests using tox"

Expand Down Expand Up @@ -378,6 +378,16 @@ def test_forall(west_init_tmpdir):
'=== running "echo foo" in net-tools (net-tools):',
'foo']

# Use environment variables

env_var = "%WEST_PROJECT_NAME%" if WINDOWS else "$WEST_PROJECT_NAME"

assert cmd(f'forall -c "echo {env_var}"').splitlines() == [
f'=== running "echo {env_var}" in manifest (zephyr):',
'manifest',
f'=== running "echo {env_var}" in net-tools (net-tools):',
'net-tools']

cmd('update Kconfiglib')
assert cmd('forall -c "echo foo"').splitlines() == [
'=== running "echo foo" in manifest (zephyr):',
Expand Down
Loading