5
5
import shutil
6
6
import tempfile
7
7
import os
8
- import pathlib
8
+ from pathlib import Path
9
9
import json
10
10
from jinja2 import Environment , FileSystemLoader
11
11
import difflib
16
16
parser .add_argument (
17
17
"--cli" ,
18
18
"-x" ,
19
- type = pathlib . Path ,
19
+ type = Path ,
20
20
required = False ,
21
21
default = shutil .which ("arduino-cli" ),
22
22
help = "path to arduino-cli" ,
30
30
)
31
31
output_args = parser .add_mutually_exclusive_group (required = True )
32
32
output_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"
34
34
)
35
35
output_args .add_argument (
36
36
"--sketch" ,
37
37
"-s" ,
38
- type = pathlib . Path ,
38
+ type = Path ,
39
39
help = "output file (CMake) filled given a sketch folder" ,
40
40
)
41
41
62
62
exit (- 1 )
63
63
64
64
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
+
65
72
def arduino (* args ):
66
73
# return (out.stdout, logfile)
67
74
# raises an exception if the command fails
@@ -76,16 +83,23 @@ def arduino(*args):
76
83
return (out , logfile )
77
84
78
85
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
+
79
93
def get_log (fname ):
80
94
with open (fname ) as file :
81
95
for line in file :
82
96
yield json .loads (line )
83
97
84
98
85
- def get_boards (boardstxt ):
99
+ def get_boards ():
86
100
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 )
89
103
del families ["menu" ]
90
104
91
105
boards = set ()
@@ -97,7 +111,7 @@ def get_boards(boardstxt):
97
111
98
112
_ , logf = arduino ("lib" , "list" )
99
113
100
- allboards = get_boards (pathlib . Path ( __file__ ). parent . parent . parent / "boards.txt" )
114
+ allboards = get_boards ()
101
115
102
116
103
117
if shargs .board and shargs .board not in allboards :
@@ -119,11 +133,11 @@ def get_boards(boardstxt):
119
133
libpaths = dict ()
120
134
for line in get_log (logf ):
121
135
if line ["msg" ] == "Adding libraries dir" :
122
- libpaths [line ["location" ]] = pathlib . Path (line ["dir" ])
136
+ libpaths [line ["location" ]] = Path (line ["dir" ])
123
137
124
138
# platform lib path is already known, obviously, since that's where this script resides
125
139
if "user" in libpaths .keys ():
126
- userlibs = pathlib . Path (libpaths ["user" ])
140
+ userlibs = Path (libpaths ["user" ])
127
141
if userlibs .exists ():
128
142
userlibs = userlibs .resolve ()
129
143
libs = [u .name for u in userlibs .iterdir () if u .is_dir ()]
@@ -137,7 +151,7 @@ def get_boards(boardstxt):
137
151
)
138
152
libs = list ()
139
153
else :
140
- userlibs = pathlib . Path .home () / "Arduino/libraries"
154
+ userlibs = Path .home () / "Arduino/libraries"
141
155
print (
142
156
f"""No user library path found through arduino-cli (falling back to { userlibs } ).
143
157
This has likely to do with your arduino-cli configuration.
@@ -147,22 +161,13 @@ def get_boards(boardstxt):
147
161
)
148
162
libs = list ()
149
163
150
- corepath = pathlib .Path (__file__ ).parent .parent .parent .resolve ()
151
-
152
164
j2_env = Environment (
153
165
loader = FileSystemLoader (str (corepath / "cmake" / "templates" )),
154
166
trim_blocks = True ,
155
167
lstrip_blocks = True ,
156
168
)
157
169
cmake_template = j2_env .get_template ("easy_cmake.cmake" )
158
170
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
-
166
171
if shargs .sketch :
167
172
SOURCEFILE_EXTS = (".c" , ".cpp" , ".S" , ".ino" )
168
173
sources = {
@@ -180,19 +185,15 @@ def get_boards(boardstxt):
180
185
tgtname = ""
181
186
sources = set ()
182
187
183
- scriptname = pathlib .Path (__file__ )
184
- if scriptname .is_relative_to (userhome ):
185
- scriptname = "~/" + str (scriptname .relative_to (userhome ))
186
-
187
188
with open (shargs .output or shargs .sketch / "CMakeLists.txt" , "w" ) as out :
188
189
out .write (
189
190
cmake_template .render (
190
- corepath = str (corepath ).replace (
191
+ corepath = userhome_process (corepath ).replace (
191
192
"\\ " , "\\ \\ "
192
193
), # escape backslashes for CMake
193
- userlibs = str (userlibs ).replace ("\\ " , "\\ \\ " ),
194
+ userlibs = userhome_process (userlibs ).replace ("\\ " , "\\ \\ " ),
194
195
libs = libs ,
195
- scriptfile = scriptname ,
196
+ scriptfile = userhome_process ( scriptname ) ,
196
197
tgtname = tgtname ,
197
198
scrcfiles = sources ,
198
199
boardname = shargs .board ,
0 commit comments