Skip to content

Commit 166fd3e

Browse files
pdgendtnashif
authored andcommitted
scripts: west_commands: build: Fix linter issues
Fix issues reported by ruff. Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
1 parent 2dc9c2b commit 166fd3e

File tree

2 files changed

+45
-61
lines changed

2 files changed

+45
-61
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,14 +1078,6 @@
10781078
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation-union
10791079
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
10801080
]
1081-
"./scripts/west_commands/build.py" = [
1082-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
1083-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
1084-
"SIM105", # https://docs.astral.sh/ruff/rules/suppressible-exception
1085-
"UP008", # https://docs.astral.sh/ruff/rules/super-call-with-parameters
1086-
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
1087-
"UP032", # https://docs.astral.sh/ruff/rules/f-string
1088-
]
10891081
"./scripts/west_commands/build_helpers.py" = [
10901082
"E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file
10911083
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports

scripts/west_commands/build.py

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import argparse
6+
import contextlib
67
import os
78
import pathlib
89
import shlex
910
import sys
10-
import yaml
1111

12+
import yaml
13+
from build_helpers import FIND_BUILD_DIR_DESCRIPTION, find_build_dir, is_zephyr_build, load_domains
1214
from west.commands import Verbosity
1315
from west.configuration import config
1416
from west.util import west_topdir
1517
from west.version import __version__
16-
from zcmake import DEFAULT_CMAKE_GENERATOR, run_cmake, run_build, CMakeCache
17-
from build_helpers import is_zephyr_build, find_build_dir, load_domains, \
18-
FIND_BUILD_DIR_DESCRIPTION
19-
18+
from zcmake import DEFAULT_CMAKE_GENERATOR, CMakeCache, run_build, run_cmake
2019
from zephyr_ext_common import Forceable
2120

2221
_ARG_SEPARATOR = '--'
@@ -69,7 +68,7 @@ def __call__(self, parser, namespace, values, option_string=None):
6968
class Build(Forceable):
7069

7170
def __init__(self):
72-
super(Build, self).__init__(
71+
super().__init__(
7372
'build',
7473
# Keep this in sync with the string in west-commands.yml.
7574
'compile a Zephyr application',
@@ -189,7 +188,7 @@ def do_add_parser(self, parser_adder):
189188
def do_run(self, args, remainder):
190189
self.args = args # Avoid having to pass them around
191190
self.config_board = config_get('board', None)
192-
self.dbg('args: {} remainder: {}'.format(args, remainder),
191+
self.dbg(f'args: {args} remainder: {remainder}',
193192
level=Verbosity.DBG_EXTREME)
194193
# Store legacy -s option locally
195194
source_dir = self.args.source_dir
@@ -211,11 +210,10 @@ def do_run(self, args, remainder):
211210

212211
if source_dir:
213212
if self.args.source_dir:
214-
self.die("source directory specified twice:({} and {})".format(
215-
source_dir, self.args.source_dir))
213+
self.die(
214+
f"source directory specified twice:({source_dir} and {self.args.source_dir})")
216215
self.args.source_dir = source_dir
217-
self.dbg('source_dir: {} cmake_opts: {}'.format(self.args.source_dir,
218-
self.args.cmake_opts),
216+
self.dbg(f'source_dir: {self.args.source_dir} cmake_opts: {self.args.cmake_opts}',
219217
level=Verbosity.DBG_EXTREME)
220218
self._sanity_precheck()
221219
self._setup_build_dir()
@@ -227,13 +225,11 @@ def do_run(self, args, remainder):
227225
pristine = config_get('pristine', 'never')
228226
if pristine not in ['auto', 'always', 'never']:
229227
self.wrn(
230-
'treating unknown build.pristine value "{}" as "never"'.
231-
format(pristine))
228+
f'treating unknown build.pristine value "{pristine}" as "never"')
232229
pristine = 'never'
233230
self.auto_pristine = pristine == 'auto'
234231

235-
self.dbg('pristine: {} auto_pristine: {}'.format(pristine,
236-
self.auto_pristine),
232+
self.dbg(f'pristine: {pristine} auto_pristine: {self.auto_pristine}',
237233
level=Verbosity.DBG_MORE)
238234
if is_zephyr_build(self.build_dir):
239235
if pristine == 'always':
@@ -322,7 +318,7 @@ def _parse_test_item(self, test_item):
322318
if not os.path.exists(yf):
323319
continue
324320
found_test_metadata = True
325-
with open(yf, 'r') as stream:
321+
with open(yf) as stream:
326322
try:
327323
y = yaml.safe_load(stream)
328324
except yaml.YAMLError as exc:
@@ -369,17 +365,21 @@ def _parse_test_item(self, test_item):
369365
# conditional configs (xxx:yyy:CONFIG_FOO=bar)
370366
# are not supported by 'west build'
371367
self.wrn('"west build" does not support '
372-
'conditional config "{}". Add "-D{}" '
368+
f'conditional config "{arg}". Add "-D{arg[colon+1:]}" '
373369
'to the supplied CMake arguments if '
374-
'desired.'.format(arg, arg[colon+1:]))
370+
'desired.')
375371
continue
376372
args.append("-D{}".format(arg.replace('"', '\"')))
377373
elif data == 'extra_args':
378374
# Retain quotes around config options
379375
config_options = [arg for arg in arg_list if arg.startswith("CONFIG_")]
380-
non_config_options = [arg for arg in arg_list if not arg.startswith("CONFIG_")]
376+
non_config_options = [
377+
arg for arg in arg_list if not arg.startswith("CONFIG_")
378+
]
381379
args = ["-D{}".format(a.replace('"', '\"')) for a in config_options]
382-
args.extend(["-D{}".format(arg.replace('"', '')) for arg in non_config_options])
380+
args.extend([
381+
"-D{}".format(arg.replace('"', '')) for arg in non_config_options
382+
])
383383
elif data == 'extra_conf_files':
384384
extra_conf_files.extend(arg_list)
385385
continue
@@ -429,16 +429,14 @@ def _sanity_precheck(self):
429429
if app:
430430
self.check_force(
431431
os.path.isdir(app),
432-
'source directory {} does not exist'.format(app))
432+
f'source directory {app} does not exist')
433433
self.check_force(
434434
'CMakeLists.txt' in os.listdir(app),
435-
"{} doesn't contain a CMakeLists.txt".format(app))
435+
f"{app} doesn't contain a CMakeLists.txt")
436436

437437
def _update_cache(self):
438-
try:
438+
with contextlib.suppress(FileNotFoundError):
439439
self.cmake_cache = CMakeCache.from_build_dir(self.build_dir)
440-
except FileNotFoundError:
441-
pass
442440

443441
def _setup_build_dir(self):
444442
# Initialize build_dir and created_build_dir attributes.
@@ -456,8 +454,7 @@ def _setup_build_dir(self):
456454

457455
if os.path.exists(build_dir):
458456
if not os.path.isdir(build_dir):
459-
self.die('build directory {} exists and is not a directory'.
460-
format(build_dir))
457+
self.die(f'build directory {build_dir} exists and is not a directory')
461458
else:
462459
os.makedirs(build_dir, exist_ok=False)
463460
self.created_build_dir = True
@@ -494,23 +491,20 @@ def _find_source_dir(self):
494491
def _sanity_check_source_dir(self):
495492
if self.source_dir == self.build_dir:
496493
# There's no forcing this.
497-
self.die('source and build directory {} cannot be the same; '
498-
'use --build-dir {} to specify a build directory'.
499-
format(self.source_dir, self.build_dir))
494+
self.die(f'source and build directory {self.source_dir} cannot be the same; '
495+
f'use --build-dir {self.build_dir} to specify a build directory')
500496

501497
srcrel = os.path.relpath(self.source_dir)
502498
self.check_force(
503499
not is_zephyr_build(self.source_dir),
504-
'it looks like {srcrel} is a build directory: '
505-
'did you mean --build-dir {srcrel} instead?'.
506-
format(srcrel=srcrel))
500+
f'it looks like {srcrel} is a build directory: '
501+
f'did you mean --build-dir {srcrel} instead?')
507502
self.check_force(
508503
'CMakeLists.txt' in os.listdir(self.source_dir),
509-
'source directory "{srcrel}" does not contain '
504+
f'source directory "{srcrel}" does not contain '
510505
'a CMakeLists.txt; is this really what you '
511506
'want to build? (Use -s SOURCE_DIR to specify '
512-
'the application source directory)'.
513-
format(srcrel=srcrel))
507+
'the application source directory)')
514508

515509
def _sanity_check(self):
516510
# Sanity check the build configuration.
@@ -548,10 +542,9 @@ def _sanity_check(self):
548542

549543
self.check_force(
550544
not apps_mismatched or self.auto_pristine,
551-
'Build directory "{}" is for application "{}", but source '
552-
'directory "{}" was specified; please clean it, use --pristine, '
553-
'or use --build-dir to set another build directory'.
554-
format(self.build_dir, cached_abs, source_abs))
545+
f'Build directory "{self.build_dir}" is for application "{cached_abs}", but source '
546+
f'directory "{source_abs}" was specified; please clean it, use --pristine, '
547+
'or use --build-dir to set another build directory')
555548

556549
if apps_mismatched:
557550
self.run_cmake = True # If they insist, we need to re-run cmake.
@@ -577,10 +570,10 @@ def _sanity_check(self):
577570
self.args.board != cached_board)
578571
self.check_force(
579572
not boards_mismatched or self.auto_pristine,
580-
'Build directory {} targets board {}, but board {} was specified. '
573+
f'Build directory {self.build_dir} targets board {cached_board}, '
574+
'but board {self.args.board} was specified. '
581575
'(Clean the directory, use --pristine, or use --build-dir to '
582-
'specify a different one.)'.
583-
format(self.build_dir, cached_board, self.args.board))
576+
'specify a different one.)')
584577

585578
if self.auto_pristine and (apps_mismatched or boards_mismatched):
586579
self._run_pristine()
@@ -610,7 +603,7 @@ def _run_cmake(self, board, origin, cmake_opts):
610603
self._banner('generating a build system')
611604

612605
if board is not None and origin != 'CMakeCache.txt':
613-
cmake_opts = ['-DBOARD={}'.format(board)]
606+
cmake_opts = [f'-DBOARD={board}']
614607
else:
615608
cmake_opts = []
616609
if self.args.cmake_opts:
@@ -633,28 +626,27 @@ def _run_cmake(self, board, origin, cmake_opts):
633626

634627
config_sysbuild = config_getboolean('sysbuild', False)
635628
if self.args.sysbuild or (config_sysbuild and not self.args.no_sysbuild):
636-
cmake_opts.extend(['-S{}'.format(SYSBUILD_PROJ_DIR),
637-
'-DAPP_DIR:PATH={}'.format(self.source_dir)])
629+
cmake_opts.extend([f'-S{SYSBUILD_PROJ_DIR}',
630+
f'-DAPP_DIR:PATH={self.source_dir}'])
638631
else:
639632
# self.args.no_sysbuild == True or config sysbuild False
640-
cmake_opts.extend(['-S{}'.format(self.source_dir)])
633+
cmake_opts.extend([f'-S{self.source_dir}'])
641634

642635
# Invoke CMake from the current working directory using the
643636
# -S and -B options (officially introduced in CMake 3.13.0).
644637
# This is important because users expect invocations like this
645638
# to Just Work:
646639
#
647640
# west build -- -DOVERLAY_CONFIG=relative-path.conf
648-
final_cmake_args = ['-DWEST_PYTHON={}'.format(pathlib.Path(sys.executable).as_posix()),
649-
'-B{}'.format(self.build_dir),
650-
'-G{}'.format(config_get('generator',
651-
DEFAULT_CMAKE_GENERATOR))]
641+
final_cmake_args = [f'-DWEST_PYTHON={pathlib.Path(sys.executable).as_posix()}',
642+
f'-B{self.build_dir}',
643+
f'-G{config_get("generator", DEFAULT_CMAKE_GENERATOR)}']
652644
if cmake_opts:
653645
final_cmake_args.extend(cmake_opts)
654646
run_cmake(final_cmake_args, dry_run=self.args.dry_run)
655647

656648
def _run_pristine(self):
657-
self._banner('making build dir {} pristine'.format(self.build_dir))
649+
self._banner(f'making build dir {self.build_dir} pristine')
658650
if not is_zephyr_build(self.build_dir):
659651
self.die('Refusing to run pristine on a folder that is not a '
660652
'Zephyr build system')
@@ -671,7 +663,7 @@ def _run_pristine(self):
671663

672664
def _run_build(self, target, domain):
673665
if target:
674-
self._banner('running target {}'.format(target))
666+
self._banner(f'running target {target}')
675667
elif self.run_cmake:
676668
self._banner('building application')
677669
extra_args = ['--target', target] if target else []

0 commit comments

Comments
 (0)