Skip to content

Commit

Permalink
app: project: Improve forall
Browse files Browse the repository at this point in the history
* Add environment variables when running forall for each project.
* Add a "quiet" argument to prevent printing the banner
* Add a "cwd" argument to execute the command in different directory

Signed-off-by: Pieter De Gendt <[email protected]>
  • Loading branch information
pdgendt committed Jul 22, 2024
1 parent 155b242 commit 64b8fba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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

0 comments on commit 64b8fba

Please sign in to comment.