11import argparse
2- import glob
32import re
4- import os
3+ from pathlib import Path
54from stm32common import createFolder , deleteFolder , genSTM32List
65
7- script_path = os .path .dirname (os .path .abspath (__file__ ))
8- home = os .path .expanduser ("~" )
6+ script_path = Path (__file__ ).parent .resolve ()
97# Base path
10- core_path = os . path . abspath ( os . path . join ( script_path , ".." , ".." ))
8+ core_path = script_path . parent . parent
119SrcWrapper_path = ""
1210HALDrivers_path = ""
1311CMSIS_Device_ST_path = ""
1412CMSIS_DSP_lib_path = ""
1513
1614# CMSIS outside of the core. Can be updated by arg
17- CMSIS_path = os .path .abspath (
18- os .path .join (core_path , ".." , "ArduinoModule-CMSIS" , "CMSIS_5" )
19- )
15+ CMSIS_path = core_path .parent / "ArduinoModule-CMSIS" / "CMSIS_5"
2016CMSIS_DSPSrc_path = ""
2117
2218# Out sources files
@@ -50,33 +46,27 @@ def checkConfig(arg_core, arg_cmsis):
5046 global LLoutInc_path
5147
5248 if arg_core is not None :
53- core_path = arg_core
54- CMSIS_path = os .path .abspath (
55- os .path .join (core_path , ".." , "ArduinoModule-CMSIS" , "CMSIS_5" )
56- )
49+ core_path = Path (arg_core ).resolve ()
50+ CMSIS_path = core_path .parent / "ArduinoModule-CMSIS" / "CMSIS_5"
5751
58- if not os . path . isdir ( core_path ):
52+ if not core_path . is_dir ( ):
5953 print ("Could not find " + core_path )
6054 exit (1 )
6155
62- SrcWrapper_path = os .path .join (core_path , "libraries" , "SrcWrapper" )
63- HALDrivers_path = os .path .join (core_path , "system" , "Drivers" )
64- CMSIS_Device_ST_path = os .path .join (
65- core_path , "system" , "Drivers" , "CMSIS" , "Device" , "ST"
66- )
67- CMSIS_DSP_lib_path = os .path .join (core_path , "libraries" , "CMSIS_DSP" )
68- CMSIS_DSP_outSrc_path = os .path .join (CMSIS_DSP_lib_path , "src" )
69- CMSIS_Startupfile = os .path .join (
70- core_path , "cores" , "arduino" , "stm32" , "stm32_def_build.h"
71- )
56+ SrcWrapper_path = core_path / "libraries" / "SrcWrapper"
57+ HALDrivers_path = core_path / "system" / "Drivers"
58+ CMSIS_Device_ST_path = core_path / "system" / "Drivers" / "CMSIS" / "Device" / "ST"
59+ CMSIS_DSP_lib_path = core_path / "libraries" / "CMSIS_DSP"
60+ CMSIS_DSP_outSrc_path = CMSIS_DSP_lib_path / "src"
61+ CMSIS_Startupfile = core_path / "cores" / "arduino" / "stm32" / "stm32_def_build.h"
7262
73- HALoutSrc_path = os . path . join ( SrcWrapper_path , "src" , "HAL" )
74- LLoutSrc_path = os . path . join ( SrcWrapper_path , "src" , "LL" )
75- LLoutInc_path = os . path . join ( core_path , "cores" , "arduino" , "stm32" , "LL" )
63+ HALoutSrc_path = SrcWrapper_path / "src" / "HAL"
64+ LLoutSrc_path = SrcWrapper_path / "src" / "LL"
65+ LLoutInc_path = core_path / "cores" / "arduino" / "stm32" / "LL"
7666
7767 if arg_cmsis is not None :
78- CMSIS_path = arg_cmsis
79- CMSIS_DSPSrc_path = os . path . join ( CMSIS_path , "CMSIS" , "DSP" , "Source" )
68+ CMSIS_path = Path ( arg_cmsis ). resolve ()
69+ CMSIS_DSPSrc_path = CMSIS_path / "CMSIS" / "DSP" / "Source"
8070
8171
8272# Add some pragma to ll header files to avoid several warnings
@@ -98,18 +88,7 @@ def print_LL_header(open_file, name):
9888
9989
10090def printCMSISStartup (log ):
101- filelist = sorted (
102- glob .glob (
103- os .path .join (
104- CMSIS_Device_ST_path ,
105- "STM32*" ,
106- "Source" ,
107- "Templates" ,
108- "gcc" ,
109- "startup_*.s" ,
110- )
111- )
112- )
91+ filelist = sorted (CMSIS_Device_ST_path .glob ("**/startup_*.s" ))
11392 if len (filelist ):
11493 if log :
11594 print ("Number of startup files: %i" % len (filelist ))
@@ -123,7 +102,7 @@ def printCMSISStartup(log):
123102"""
124103 )
125104 # File name
126- fn = os . path . basename (filelist .pop (0 ))
105+ fn = (filelist .pop (0 )). name
127106 valueline = re .split ("_|\\ ." , fn )
128107 upper = valueline [1 ].upper ().replace ("X" , "x" )
129108 out_file .write (
@@ -136,7 +115,7 @@ def printCMSISStartup(log):
136115 if len (filelist ):
137116 for fp in filelist :
138117 # File name
139- fn = os . path . basename ( fp )
118+ fn = fp . name
140119 valueline = re .split ("_|\\ ." , fn )
141120 if "stm32mp15" in valueline [1 ] and not valueline [1 ].endswith ("xx" ):
142121 valueline [1 ] += "xx"
@@ -178,48 +157,48 @@ def wrap(arg_core, arg_cmsis, log):
178157 createFolder (LLoutSrc_path )
179158 deleteFolder (LLoutInc_path )
180159 createFolder (LLoutInc_path )
181- if os . path . isfile ( CMSIS_Startupfile ):
182- os . remove ( CMSIS_Startupfile )
160+ if CMSIS_Startupfile . is_file ( ):
161+ CMSIS_Startupfile . unlink ( )
183162 full_ll_list = []
184163 # Search all files for each series
185164 for serie in stm32_series :
186- src = os . path . join ( HALDrivers_path , "STM32" + serie + "xx_HAL_Driver" , "Src" )
187- inc = os . path . join ( HALDrivers_path , "STM32" + serie + "xx_HAL_Driver" , "Inc" )
165+ src = HALDrivers_path / ( "STM32" + serie + "xx_HAL_Driver" ) / "Src"
166+ inc = HALDrivers_path / ( "STM32" + serie + "xx_HAL_Driver" ) / "Inc"
188167
189- if os . path . exists (src ):
168+ if src . exists ():
190169 if log :
191170 print ("Generating for " + serie + "..." )
192171 lower = serie .lower ()
193172 # Generate stm32yyxx_[hal|ll]*.c file
194- filelist = glob .glob (os . path . join ( src , "stm32" + lower + "xx_*.c" ) )
173+ filelist = src .glob ("stm32" + lower + "xx_*.c" )
195174 for fp in filelist :
196- if "_template" in fp :
175+ # File name
176+ fn = fp .name
177+ if "_template" in fn :
197178 continue
198179 outp = HALoutSrc_path
199- # File name
200- fn = os .path .basename (fp )
201180 if "_ll_" in fn :
202181 outp = LLoutSrc_path
203182 # Compute generic file name with path
204- gp = os . path . join ( outp , fn .replace (lower , "yy" ) )
183+ gp = outp / fn .replace (lower , "yy" )
205184 out_file = open (gp , "a" , newline = "\n " )
206185 # Amend file name under serie switch
207186 out_file .write ("#ifdef STM32" + serie + "xx\n " )
208187 out_file .write (' #include "' + fn + '"\n ' )
209188 out_file .write ("#endif\n " )
210189 out_file .close ()
211190 # Generate stm32yyxx_ll_*.h file
212- filelist = glob .glob (os . path . join ( inc , "stm32" + lower + "xx_ll_*.h" ) )
191+ filelist = inc .glob ("stm32" + lower + "xx_ll_*.h" )
213192 for fp in filelist :
214193 outp = LLoutInc_path
215194 # File name
216- fn = os . path . basename ( fp )
195+ fn = fp . name
217196 # Compute generic file name
218197 gn = fn .replace (lower , "yy" )
219198 # with path
220- gp = os . path . join ( outp , gn )
199+ gp = outp / gn
221200 out_file = open (gp , "a" , newline = "\n " )
222- if os . path . getsize ( gp ) == 0 :
201+ if gp . stat (). st_size == 0 :
223202 print_LL_header (out_file , gn )
224203 # Amend full LL header file
225204 full_ll_list .append (gn )
@@ -232,7 +211,7 @@ def wrap(arg_core, arg_cmsis, log):
232211 print ("done" )
233212
234213 # Filter full LL header file
235- full_ll_file = open (os . path . join ( LLoutInc_path , all_LL_file ) , "w" , newline = "\n " )
214+ full_ll_file = open (LLoutInc_path / all_LL_file , "w" , newline = "\n " )
236215 print_LL_header (full_ll_file , all_LL_file )
237216 full_ll_file .write ("/* Include Low Layers drivers */\n " )
238217 full_ll_list = sorted (set (full_ll_list ))
@@ -241,10 +220,10 @@ def wrap(arg_core, arg_cmsis, log):
241220 full_ll_file .close ()
242221
243222 # Search all LL header files to end guard
244- filelist = glob .glob (os . path . join ( LLoutInc_path , "stm32yyxx_ll*.h" ) )
223+ filelist = LLoutInc_path .glob ("stm32yyxx_ll*.h" )
245224 for fp in filelist :
246225 out_file = open (fp , "a" , newline = "\n " )
247- upper = os . path . basename ( fp ) .upper ().replace ("." , "_" )
226+ upper = fp . name .upper ().replace ("." , "_" )
248227 out_file .write ("#pragma GCC diagnostic pop\n " )
249228 out_file .write ("#endif /* _" + upper + "_ */\n " )
250229 out_file .close ()
@@ -253,23 +232,23 @@ def wrap(arg_core, arg_cmsis, log):
253232 printCMSISStartup (log )
254233
255234 # CMSIS DSP C source file
256- if not os . path . isdir ( CMSIS_path ):
257- print ("Could not find " + CMSIS_path )
235+ if not CMSIS_path . is_dir ( ):
236+ print ("Could not find {}" ). format ( CMSIS_path )
258237 print ("CMSIS DSP generation skipped." )
259238 else :
260239 # Delete all subfolders
261- deleteFolder (os . path . join ( CMSIS_DSP_outSrc_path , "*" ) )
240+ deleteFolder (CMSIS_DSP_outSrc_path / "*" )
262241 dirlist = []
263- for root , dirs , files in os . walk ( CMSIS_DSPSrc_path ):
264- for file in files :
265- if file .endswith (".c" ):
266- dirlist .append (root . replace ( CMSIS_DSPSrc_path , "" )[ 1 :] )
242+ for path_object in CMSIS_DSPSrc_path . glob ( "**/*" ):
243+ if path_object . is_file () :
244+ if path_object . name .endswith (".c" ):
245+ dirlist .append (path_object . parent . name )
267246 dirlist = sorted (set (dirlist ))
268247 for dn in dirlist :
269- fdn = os . path . join ( CMSIS_DSP_outSrc_path , dn )
270- if not os . path . isdir ( fdn ):
248+ fdn = CMSIS_DSP_outSrc_path / dn
249+ if not fdn . is_dir ( ):
271250 createFolder (fdn )
272- out_file = open (os . path . join ( fdn , dn + ".c" ), "w" , newline = "\n " )
251+ out_file = open (fdn / ( dn + ".c" ), "w" , newline = "\n " )
273252 out_file .write ('#include "../Source/{0}/{0}.c"\n ' .format (dn ))
274253 out_file .close ()
275254 return 0
@@ -284,13 +263,13 @@ def wrap(arg_core, arg_cmsis, log):
284263 "-c" ,
285264 "--core" ,
286265 metavar = "core_path" ,
287- help = "Root path of the STM32 core. Default: " + core_path ,
266+ help = "Root path of the STM32 core. Default: {}" . format ( core_path ) ,
288267 )
289268 wrapparser .add_argument (
290269 "-s" ,
291270 "--cmsis" ,
292271 metavar = "cmsis_path" ,
293- help = "Root path of the CMSIS. Default: " + CMSIS_path ,
272+ help = "Root path of the CMSIS. Default: {}" . format ( CMSIS_path ) ,
294273 )
295274
296275 wrapargs = wrapparser .parse_args ()
0 commit comments