4
4
5
5
'''Sphinx extensions related to managing Zephyr applications.'''
6
6
7
- from docutils import nodes
8
- from docutils .parsers .rst import Directive
9
- from docutils .parsers .rst import directives
10
7
from pathlib import Path
11
8
9
+ from docutils import nodes
10
+ from docutils .parsers .rst import Directive , directives
12
11
13
12
ZEPHYR_BASE = Path (__file__ ).parents [3 ]
14
13
@@ -79,8 +78,7 @@ def run(self):
79
78
flash_args = self .options .get ('flash-args' , None )
80
79
81
80
if tool not in self .TOOLS :
82
- raise self .error ('Unknown tool {}; choose from: {}' .format (
83
- tool , self .TOOLS ))
81
+ raise self .error (f'Unknown tool { tool } ; choose from: { self .TOOLS } ' )
84
82
85
83
if app and zephyr_app :
86
84
raise self .error ('Both app and zephyr-app options were given.' )
@@ -92,25 +90,22 @@ def run(self):
92
90
raise self .error ('build-dir-fmt is only supported for the west build tool.' )
93
91
94
92
if generator not in self .GENERATORS :
95
- raise self .error ('Unknown generator {}; choose from: {}' .format (
96
- generator , self .GENERATORS ))
93
+ raise self .error (f'Unknown generator { generator } ; choose from: { self .GENERATORS } ' )
97
94
98
95
if host_os not in self .HOST_OS :
99
- raise self .error ('Unknown host-os {}; choose from: {}' .format (
100
- host_os , self .HOST_OS ))
96
+ raise self .error (f'Unknown host-os { host_os } ; choose from: { self .HOST_OS } ' )
101
97
102
98
if compact and skip_config :
103
99
raise self .error ('Both compact and maybe-skip-config options were given.' )
104
100
105
- if zephyr_app :
106
- # as folks might use "<...>" notation to indicate a variable portion of the path, we
107
- # deliberately don't check for the validity of such paths.
108
- if not any ([x in zephyr_app for x in ["<" , ">" ]]):
109
- app_path = ZEPHYR_BASE / zephyr_app
110
- if not app_path .is_dir ():
111
- raise self .error (
112
- f"zephyr-app: { zephyr_app } is not a valid folder in the zephyr tree."
113
- )
101
+ # as folks might use "<...>" notation to indicate a variable portion of the path, we
102
+ # deliberately don't check for the validity of such paths.
103
+ if zephyr_app and not any ([x in zephyr_app for x in ["<" , ">" ]]):
104
+ app_path = ZEPHYR_BASE / zephyr_app
105
+ if not app_path .is_dir ():
106
+ raise self .error (
107
+ f"zephyr-app: { zephyr_app } is not a valid folder in the zephyr tree."
108
+ )
114
109
115
110
app = app or zephyr_app
116
111
in_tree = self .IN_TREE_STR if zephyr_app else None
@@ -168,7 +163,7 @@ def run(self):
168
163
if tool_comment :
169
164
paragraph = nodes .paragraph ()
170
165
paragraph += nodes .Text (tool_comment .format (
171
- 'CMake and {}' . format ( generator ) ))
166
+ f 'CMake and { generator } ' ))
172
167
content .append (paragraph )
173
168
content .append (self ._lit_block (c ))
174
169
else :
@@ -208,30 +203,30 @@ def _generate_west(self, **kwargs):
208
203
# west always defaults to ninja
209
204
gen_arg = ' -G\' Unix Makefiles\' ' if generator == 'make' else ''
210
205
cmake_args = gen_arg + self ._cmake_args (** kwargs )
211
- cmake_args = ' --{}' . format ( cmake_args ) if cmake_args != '' else ''
206
+ cmake_args = f ' --{ cmake_args } ' if cmake_args != '' else ''
212
207
build_args = "" .join (f" -o { b } " for b in build_args ) if build_args else ""
213
- west_args = ' {}' . format ( west_args ) if west_args else ''
214
- flash_args = ' {}' . format ( flash_args ) if flash_args else ''
208
+ west_args = f ' { west_args } ' if west_args else ''
209
+ flash_args = f ' { flash_args } ' if flash_args else ''
215
210
snippet_args = '' .join (f' -S { s } ' for s in snippets ) if snippets else ''
216
211
shield_args = '' .join (f' --shield { s } ' for s in shield ) if shield else ''
217
212
# ignore zephyr_app since west needs to run within
218
213
# the installation. Instead rely on relative path.
219
- src = ' {}' . format ( app ) if app and not cd_into else ''
214
+ src = f ' { app } ' if app and not cd_into else ''
220
215
221
216
if build_dir_fmt is None :
222
- dst = ' -d {}' . format ( build_dir ) if build_dir != 'build' else ''
217
+ dst = f ' -d { build_dir } ' if build_dir != 'build' else ''
223
218
build_dst = dst
224
219
else :
225
220
app_name = app .split ('/' )[- 1 ]
226
221
build_dir_formatted = build_dir_fmt .format (app = app_name , board = board , source_dir = app )
227
- dst = ' -d {}' . format ( build_dir_formatted )
222
+ dst = f ' -d { build_dir_formatted } '
228
223
build_dst = ''
229
224
230
225
if in_tree and not compact :
231
226
content .append (in_tree )
232
227
233
228
if cd_into and app :
234
- content .append ('cd {}' . format ( app ) )
229
+ content .append (f 'cd { app } ' )
235
230
236
231
# We always have to run west build.
237
232
#
@@ -252,48 +247,49 @@ def _generate_west(self, **kwargs):
252
247
# etc. commands can use the signed file which must be created
253
248
# in this step.
254
249
if 'sign' in goals :
255
- content .append ('west sign{}' . format ( dst ) )
250
+ content .append (f 'west sign{ dst } ' )
256
251
257
252
for goal in goals :
258
253
if goal in {'build' , 'sign' }:
259
254
continue
260
255
elif goal == 'flash' :
261
- content .append ('west flash{}{}' . format ( flash_args , dst ) )
256
+ content .append (f 'west flash{ flash_args } { dst } ' )
262
257
elif goal == 'debug' :
263
- content .append ('west debug{}' . format ( dst ) )
258
+ content .append (f 'west debug{ dst } ' )
264
259
elif goal == 'debugserver' :
265
- content .append ('west debugserver{}' . format ( dst ) )
260
+ content .append (f 'west debugserver{ dst } ' )
266
261
elif goal == 'attach' :
267
- content .append ('west attach{}' . format ( dst ) )
262
+ content .append (f 'west attach{ dst } ' )
268
263
else :
269
- content .append ('west build -t {}{}' . format ( goal , dst ) )
264
+ content .append (f 'west build -t { goal } { dst } ' )
270
265
271
266
return content
272
267
273
268
@staticmethod
274
269
def _mkdir (mkdir , build_dir , host_os , skip_config ):
275
270
content = []
276
271
if skip_config :
277
- content .append ("# If you already made a build directory ({}) and ran cmake, just 'cd {}' instead." .format (build_dir , build_dir )) # noqa: E501
272
+ content .append (f"# If you already made a build directory ({ build_dir } ) and ran cmake, "
273
+ f"just 'cd { build_dir } ' instead." )
278
274
if host_os == 'all' :
279
- content .append ('mkdir {} && cd {}' . format ( build_dir , build_dir ) )
275
+ content .append (f 'mkdir { build_dir } && cd { build_dir } ' )
280
276
if host_os == "unix" :
281
- content .append ('{ } {} && cd {}' . format ( mkdir , build_dir , build_dir ) )
277
+ content .append (f' { mkdir } { build_dir } && cd { build_dir } ' )
282
278
elif host_os == "win" :
283
279
build_dir = build_dir .replace ('/' , '\\ ' )
284
- content .append ('mkdir {} & cd {}' . format ( build_dir , build_dir ) )
280
+ content .append (f 'mkdir { build_dir } & cd { build_dir } ' )
285
281
return content
286
282
287
283
@staticmethod
288
284
def _cmake_args (** kwargs ):
289
285
board = kwargs ['board' ]
290
286
conf = kwargs ['conf' ]
291
287
gen_args = kwargs ['gen_args' ]
292
- board_arg = ' -DBOARD={}' . format ( board ) if board else ''
293
- conf_arg = ' -DCONF_FILE={}' . format ( conf ) if conf else ''
294
- gen_args = ' {}' . format ( gen_args ) if gen_args else ''
288
+ board_arg = f ' -DBOARD={ board } ' if board else ''
289
+ conf_arg = f ' -DCONF_FILE={ conf } ' if conf else ''
290
+ gen_args = f ' { gen_args } ' if gen_args else ''
295
291
296
- return '{}{}{}' . format ( board_arg , conf_arg , gen_args )
292
+ return f' { board_arg } { conf_arg } { gen_args } '
297
293
298
294
def _cd_into (self , mkdir , ** kwargs ):
299
295
app = kwargs ['app' ]
@@ -319,13 +315,13 @@ def _cd_into(self, mkdir, **kwargs):
319
315
if os_comment :
320
316
content .append (os_comment .format ('Linux/macOS' ))
321
317
if app :
322
- content .append ('cd {}' . format ( app ) )
318
+ content .append (f 'cd { app } ' )
323
319
elif host == "win" :
324
320
if os_comment :
325
321
content .append (os_comment .format ('Windows' ))
326
322
if app :
327
323
backslashified = app .replace ('/' , '\\ ' )
328
- content .append ('cd {}' . format ( backslashified ) )
324
+ content .append (f 'cd { backslashified } ' )
329
325
if mkdir :
330
326
content .extend (self ._mkdir (mkdir , build_dir , host , skip_config ))
331
327
if not compact :
@@ -359,39 +355,36 @@ def _generate_cmake(self, **kwargs):
359
355
cmake_build_dir = ''
360
356
tool_build_dir = ''
361
357
else :
362
- source_dir = ' {}' . format ( app ) if app else ' .'
363
- cmake_build_dir = ' -B{}' . format ( build_dir )
364
- tool_build_dir = ' -C{}' . format ( build_dir )
358
+ source_dir = f ' { app } ' if app else ' .'
359
+ cmake_build_dir = f ' -B{ build_dir } '
360
+ tool_build_dir = f ' -C{ build_dir } '
365
361
366
362
# Now generate the actual cmake and make/ninja commands
367
363
gen_arg = ' -GNinja' if generator == 'ninja' else ''
368
- build_args = ' {}' . format ( build_args ) if build_args else ''
364
+ build_args = f ' { build_args } ' if build_args else ''
369
365
snippet_args = ' -DSNIPPET="{}"' .format (';' .join (snippets )) if snippets else ''
370
366
shield_args = ' -DSHIELD="{}"' .format (';' .join (shield )) if shield else ''
371
367
cmake_args = self ._cmake_args (** kwargs )
372
368
373
369
if not compact :
374
370
if not cd_into and skip_config :
375
- content .append ("# If you already ran cmake with -B{}, you " \
376
- "can skip this step and run {} directly." .
377
- format (build_dir , generator )) # noqa: E501
371
+ content .append (f'# If you already ran cmake with -B{ build_dir } , you '
372
+ f'can skip this step and run { generator } directly.' )
378
373
else :
379
- content .append ('# Use cmake to configure a {}-based build' \
380
- 'system:' . format ( generator . capitalize ())) # noqa: E501
374
+ content .append (f '# Use cmake to configure a { generator . capitalize () } -based build'
375
+ 'system:' )
381
376
382
- content .append ('cmake{}{}{}{}{}{}' .format (cmake_build_dir , gen_arg ,
383
- cmake_args , snippet_args , shield_args , source_dir ))
377
+ content .append (f'cmake{ cmake_build_dir } { gen_arg } { cmake_args } { snippet_args } { shield_args } { source_dir } ' )
384
378
if not compact :
385
379
content .extend (['' ,
386
380
'# Now run the build tool on the generated build system:' ])
387
381
388
382
if 'build' in goals :
389
- content .append ('{}{}{}' .format (generator , tool_build_dir ,
390
- build_args ))
383
+ content .append (f'{ generator } { tool_build_dir } { build_args } ' )
391
384
for goal in goals :
392
385
if goal == 'build' :
393
386
continue
394
- content .append ('{}{ } {}' . format ( generator , tool_build_dir , goal ) )
387
+ content .append (f' { generator } { tool_build_dir } { goal } ' )
395
388
396
389
return content
397
390
0 commit comments