55import shutil
66import tempfile
77import os
8- import pathlib
8+ from pathlib import Path
99import json
1010from jinja2 import Environment , FileSystemLoader
1111import difflib
1616parser .add_argument (
1717 "--cli" ,
1818 "-x" ,
19- type = pathlib . Path ,
19+ type = Path ,
2020 required = False ,
2121 default = shutil .which ("arduino-cli" ),
2222 help = "path to arduino-cli" ,
3030)
3131output_args = parser .add_mutually_exclusive_group (required = True )
3232output_args .add_argument (
33- "--output" , "-o" , type = pathlib . Path , help = "output file (CMake) with placeholders"
33+ "--output" , "-o" , type = Path , help = "output file (CMake) with placeholders"
3434)
3535output_args .add_argument (
3636 "--sketch" ,
3737 "-s" ,
38- type = pathlib . Path ,
38+ type = Path ,
3939 help = "output file (CMake) filled given a sketch folder" ,
4040)
4141
6262 exit (- 1 )
6363
6464
65+ # Path management
66+ scriptname = Path (__file__ ).resolve ()
67+ corepath = scriptname .parent .parent .parent .resolve ()
68+ boards_txt = corepath / "boards.txt"
69+ userhome = Path .home ()
70+
71+
6572def arduino (* args ):
6673 # return (out.stdout, logfile)
6774 # raises an exception if the command fails
@@ -76,16 +83,23 @@ def arduino(*args):
7683 return (out , logfile )
7784
7885
86+ def userhome_process (path ):
87+ lpath = str (path )
88+ if path .is_relative_to (userhome ):
89+ lpath = f"~/{ str (path .relative_to (userhome ))} "
90+ return lpath
91+
92+
7993def get_log (fname ):
8094 with open (fname ) as file :
8195 for line in file :
8296 yield json .loads (line )
8397
8498
85- def get_boards (boardstxt ):
99+ def get_boards ():
86100
87- # we "reject" everything because we don't care about the values, only the keys
88- families = parse_file (boardstxt , lambda x : True )
101+ # "reject" everything because we don't care about the values, only the keys
102+ families = parse_file (boards_txt , lambda x : True )
89103 del families ["menu" ]
90104
91105 boards = set ()
@@ -97,7 +111,7 @@ def get_boards(boardstxt):
97111
98112_ , logf = arduino ("lib" , "list" )
99113
100- allboards = get_boards (pathlib . Path ( __file__ ). parent . parent . parent / "boards.txt" )
114+ allboards = get_boards ()
101115
102116
103117if shargs .board and shargs .board not in allboards :
@@ -119,11 +133,11 @@ def get_boards(boardstxt):
119133libpaths = dict ()
120134for line in get_log (logf ):
121135 if line ["msg" ] == "Adding libraries dir" :
122- libpaths [line ["location" ]] = pathlib . Path (line ["dir" ])
136+ libpaths [line ["location" ]] = Path (line ["dir" ])
123137
124138# platform lib path is already known, obviously, since that's where this script resides
125139if "user" in libpaths .keys ():
126- userlibs = pathlib . Path (libpaths ["user" ])
140+ userlibs = Path (libpaths ["user" ])
127141 if userlibs .exists ():
128142 userlibs = userlibs .resolve ()
129143 libs = [u .name for u in userlibs .iterdir () if u .is_dir ()]
@@ -137,7 +151,7 @@ def get_boards(boardstxt):
137151 )
138152 libs = list ()
139153else :
140- userlibs = pathlib . Path .home () / "Arduino/libraries"
154+ userlibs = Path .home () / "Arduino/libraries"
141155 print (
142156 f"""No user library path found through arduino-cli (falling back to { userlibs } ).
143157 This has likely to do with your arduino-cli configuration.
@@ -147,22 +161,13 @@ def get_boards(boardstxt):
147161 )
148162 libs = list ()
149163
150- corepath = pathlib .Path (__file__ ).parent .parent .parent .resolve ()
151-
152164j2_env = Environment (
153165 loader = FileSystemLoader (str (corepath / "cmake" / "templates" )),
154166 trim_blocks = True ,
155167 lstrip_blocks = True ,
156168)
157169cmake_template = j2_env .get_template ("easy_cmake.cmake" )
158170
159-
160- userhome = pathlib .Path .home ()
161- if userlibs .is_relative_to (userhome ):
162- userlibs = "~/" + str (userlibs .relative_to (userhome ))
163- if corepath .is_relative_to (userhome ):
164- corepath = "~/" + str (corepath .relative_to (userhome ))
165-
166171if shargs .sketch :
167172 SOURCEFILE_EXTS = (".c" , ".cpp" , ".S" , ".ino" )
168173 sources = {
@@ -180,19 +185,15 @@ def get_boards(boardstxt):
180185 tgtname = ""
181186 sources = set ()
182187
183- scriptname = pathlib .Path (__file__ )
184- if scriptname .is_relative_to (userhome ):
185- scriptname = "~/" + str (scriptname .relative_to (userhome ))
186-
187188with open (shargs .output or shargs .sketch / "CMakeLists.txt" , "w" ) as out :
188189 out .write (
189190 cmake_template .render (
190- corepath = str (corepath ).replace (
191+ corepath = userhome_process (corepath ).replace (
191192 "\\ " , "\\ \\ "
192193 ), # escape backslashes for CMake
193- userlibs = str (userlibs ).replace ("\\ " , "\\ \\ " ),
194+ userlibs = userhome_process (userlibs ).replace ("\\ " , "\\ \\ " ),
194195 libs = libs ,
195- scriptfile = scriptname ,
196+ scriptfile = userhome_process ( scriptname ) ,
196197 tgtname = tgtname ,
197198 scrcfiles = sources ,
198199 boardname = shargs .board ,
0 commit comments