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

project.py: Add new 'joblib' dependency to parallelize update #713

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies = [
"pykwalify",
"setuptools",
"packaging",
"joblib",
]

[project.license]
Expand Down
41 changes: 35 additions & 6 deletions src/west/app/project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018, 2019 Nordic Semiconductor ASA

Check warning on line 1 in src/west/app/project.py

View workflow job for this annotation

GitHub Actions / Check file src/west/app/project.py

File format check failed

Run 'ruff format src/west/app/project.py'
# Copyright 2018, 2019 Foundries.io
#
# SPDX-License-Identifier: Apache-2.0
Expand Down Expand Up @@ -36,6 +36,12 @@
)
from west.manifest import is_group as is_project_group

JOBLIB_PRESENT = True
try:
from joblib import Parallel, delayed
except ModuleNotFoundError:
JOBLIB_PRESENT = False

#
# Project-related or multi-repo commands, like "init", "update",
# "diff", etc.
Expand Down Expand Up @@ -952,6 +958,12 @@
parser.add_argument('--stats', action='store_true',
help='''print performance statistics for
update operations''')
if JOBLIB_PRESENT:
parser.add_argument('-j', '--jobs', nargs='?', const=-1,
default=1, type=int, action='store',
help='''Use multiple jobs to parallelize the update process.
Pass -1 to use all available jobs.
''')

group = parser.add_argument_group(
title='local project clone caches',
Expand Down Expand Up @@ -1087,18 +1099,26 @@
import_flags=ImportFlag.FORCE_PROJECTS)

failed = []
for project in self.manifest.projects:

def project_update(project):
if (isinstance(project, ManifestProject) or
project.name in self.updated):
continue
return
try:
if not self.project_is_active(project):
self.dbg(f'{project.name}: skipping inactive project')
continue
self.update(project)
return
self.updated.add(project.name)
self.update(project)
except subprocess.CalledProcessError:
failed.append(project)

if not JOBLIB_PRESENT or self.args.jobs == 1:
for project in self.manifest.projects:
project_update(project)
else:
Parallel(n_jobs=self.args.jobs, require='sharedmem')(
delayed(project_update)(project) for project in self.manifest.projects)
self._handle_failed(self.args, failed)

def update_importer(self, project, path):
Expand Down Expand Up @@ -1159,13 +1179,22 @@
projects = self._projects(self.args.projects)

failed = []
for project in projects:

def project_update_some(project):
if isinstance(project, ManifestProject):
continue
return
try:
self.update(project)
except subprocess.CalledProcessError:
failed.append(project)

if not JOBLIB_PRESENT or self.args.jobs == 1:
for project in projects:
project_update_some(project)
else:
Parallel(n_jobs=self.args.jobs, require='sharedmem')(
delayed(project_update_some)(project) for project in projects)

self._handle_failed(self.args, failed)

def toplevel_projects(self):
Expand Down
19 changes: 10 additions & 9 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020, Nordic Semiconductor ASA

Check warning on line 1 in tests/test_project.py

View workflow job for this annotation

GitHub Actions / Check file tests/test_project.py

File format check failed

Run 'ruff format tests/test_project.py'

import collections
import os
Expand Down Expand Up @@ -447,14 +447,14 @@

assert re.search('west-commands', cmd('grep -- -- -commands'))


def test_update_projects(west_init_tmpdir):
@pytest.mark.parametrize("options", ["", "-j 1", "-j 2", "-j"])
Copy link

@pdunaj pdunaj Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is -j indented? should not be -j -1?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree seems so from here: parser.add_argument('-j', '--jobs', nargs='?', const=-1, that -1 should be passed

def test_update_projects(options, west_init_tmpdir):
# Test the 'west update' command. It calls through to the same backend
# functions that are used for automatic updates and 'west init'
# reinitialization.

# create local repositories
cmd('update')
cmd('update ' + options)

# Add commits to the local repos.
ur = update_helper(west_init_tmpdir)
Expand Down Expand Up @@ -644,7 +644,8 @@
assert modified_files.strip() == "M CODEOWNERS", \
'local zephyr change not preserved'

def test_update_some_with_imports(repos_tmpdir):
@pytest.mark.parametrize("options", ["", "-j 1", "-j 2", "-j -1"])
def test_update_some_with_imports(options, repos_tmpdir):
# 'west update project1 project2' should work fine even when
# imports are used, as long as the relevant projects are all
# defined in the manifest repository.
Expand Down Expand Up @@ -685,19 +686,19 @@
# Updating unknown projects should fail as always.

with pytest.raises(subprocess.CalledProcessError):
cmd('update unknown-project', cwd=ws)
cmd(f'update {options} unknown-project', cwd=ws)

# Updating a list of projects when some are resolved via project
# imports must fail.

with pytest.raises(subprocess.CalledProcessError):
cmd('update Kconfiglib net-tools', cwd=ws)
cmd(f'update {options} Kconfiglib net-tools', cwd=ws)

# Updates of projects defined in the manifest repository or all
# projects must succeed, and behave the same as if no imports
# existed.

cmd('update net-tools', cwd=ws)
cmd(f'update {options} net-tools', cwd=ws)
with pytest.raises(ManifestImportFailed):
Manifest.from_topdir(topdir=ws)
manifest = Manifest.from_topdir(topdir=ws,
Expand All @@ -708,10 +709,10 @@
assert net_tools_project.is_cloned()
assert not zephyr_project.is_cloned()

cmd('update zephyr', cwd=ws)
cmd(f'update {options} zephyr', cwd=ws)
assert zephyr_project.is_cloned()

cmd('update', cwd=ws)
cmd(f'update {options}', cwd=ws)
manifest = Manifest.from_topdir(topdir=ws)
assert manifest.get_projects(['Kconfiglib'])[0].is_cloned()

Expand Down
Loading