Skip to content

Commit cadd233

Browse files
committed
Allow dict in addition to the other types of src_paths
The dict allows the user of the exporter api to specify the result directory of particular groups of scanned dirs. This will be used by the online exporters to spoof everything being in the same directory when they are not. It may also be used by tests, if they would like to export something that looks exactly like a normal project.
1 parent 6686411 commit cadd233

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

tools/project_api.py

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from tools.build_api import prepare_toolchain
1313
from tools.build_api import scan_resources
1414
from tools.export import EXPORTERS
15+
from tools.toolchains import Resources
1516

1617

1718
def get_exporter_toolchain(ide):
@@ -49,13 +50,16 @@ def subtract_basepath(resources, export_path):
4950
'lib_dirs']
5051
for key in keys:
5152
vals = getattr(resources, key)
52-
if type(vals) is set:
53+
if isinstance(vals, set):
5354
vals = list(vals)
54-
if type(vals) is list:
55+
if isinstance(vals, list):
5556
new_vals = []
5657
for val in vals:
5758
new_vals.append(rewrite_basepath(val, resources, export_path))
58-
setattr(resources, key, new_vals)
59+
if isinstance(getattr(resources, key), set):
60+
setattr(resources, key, set(new_vals))
61+
else:
62+
setattr(resources, key, new_vals)
5963
elif vals:
6064
setattr(resources, key, rewrite_basepath(vals, resources,
6165
export_path))
@@ -85,7 +89,7 @@ def prepare_project(src_paths, export_path, target, ide,
8589
_, toolchain_name = get_exporter_toolchain(ide)
8690

8791
# Pass all params to the unified prepare_resources()
88-
toolchain = prepare_toolchain(src_paths, export_path, target,
92+
toolchain = prepare_toolchain(src_paths, target,
8993
toolchain_name, macros=macros,
9094
options=options, clean=clean, jobs=jobs,
9195
notify=notify, silent=silent, verbose=verbose,
@@ -111,7 +115,7 @@ def prepare_project(src_paths, export_path, target, ide,
111115

112116

113117
def generate_project_files(resources, export_path, target, name, toolchain, ide,
114-
macros=None):
118+
macros=None):
115119
"""Generate the project files for a project
116120
117121
Positional arguments:
@@ -148,16 +152,16 @@ def zip_export(file_name, prefix, resources, project_files):
148152
with zipfile.ZipFile(file_name, "w") as zip_file:
149153
for prj_file in project_files:
150154
zip_file.write(prj_file, join(prefix, basename(prj_file)))
151-
for source in resources.headers + resources.s_sources + \
152-
resources.c_sources + resources.cpp_sources + \
153-
resources.libraries + resources.hex_files + \
154-
[resources.linker_script] + resources.bin_files \
155-
+ resources.objects + resources.json_files:
156-
if source:
157-
zip_file.write(source,
158-
join(prefix,
159-
relpath(source,
160-
resources.file_basepath[source])))
155+
for loc, res in resources.iteritems():
156+
for source in \
157+
res.headers + res.s_sources + res.c_sources + res.cpp_sources +\
158+
res.libraries + res.hex_files + [res.linker_script] +\
159+
res.bin_files + res.objects + res.json_files:
160+
if source:
161+
zip_file.write(source,
162+
join(prefix, loc,
163+
relpath(source,
164+
res.file_basepath[source])))
161165

162166

163167
def export_project(src_paths, export_path, target, ide,
@@ -194,11 +198,19 @@ def export_project(src_paths, export_path, target, ide,
194198
"""
195199

196200
# Convert src_path to a list if needed
197-
if type(src_paths) != type([]):
198-
src_paths = [src_paths]
199-
# Extend src_paths wiht libraries_paths
201+
if isinstance(src_paths, dict):
202+
paths = sum(src_paths.values(), [])
203+
elif isinstance(src_paths, list):
204+
paths = src_paths[:]
205+
else:
206+
paths = [src_paths]
207+
208+
# Extend src_paths wit libraries_paths
200209
if libraries_paths is not None:
201-
src_paths.extend(libraries_paths)
210+
paths.extend(libraries_paths)
211+
212+
if not isinstance(src_paths, dict):
213+
src_paths = {"": paths}
202214

203215
# Export Directory
204216
if exists(export_path) and clean:
@@ -209,7 +221,7 @@ def export_project(src_paths, export_path, target, ide,
209221
_, toolchain_name = get_exporter_toolchain(ide)
210222

211223
# Pass all params to the unified prepare_resources()
212-
toolchain = prepare_toolchain(src_paths, target, toolchain_name,
224+
toolchain = prepare_toolchain(paths, target, toolchain_name,
213225
macros=macros, options=options, clean=clean,
214226
jobs=jobs, notify=notify, silent=silent,
215227
verbose=verbose, extra_verbose=extra_verbose,
@@ -219,17 +231,23 @@ def export_project(src_paths, export_path, target, ide,
219231
name = basename(normpath(abspath(src_paths[0])))
220232

221233
# Call unified scan_resources
222-
resources = scan_resources(src_paths, toolchain, inc_dirs=inc_dirs)
234+
resource_dict = {loc: scan_resources(path, toolchain, inc_dirs=inc_dirs)
235+
for loc, path in src_paths.iteritems()}
236+
resources = Resources()
223237
toolchain.build_dir = export_path
224238
config_header = toolchain.get_config_header()
225239
resources.headers.append(config_header)
226240
resources.file_basepath[config_header] = dirname(config_header)
227-
temp = copy.deepcopy(resources)
228241

229242
if zip_proj:
230243
subtract_basepath(resources, export_path)
244+
for loc, res in resource_dict.iteritems():
245+
temp = copy.deepcopy(res)
246+
subtract_basepath(temp, join(export_path, loc))
247+
resources.add(temp)
231248
else:
232-
resources.relative_to(export_path)
249+
for _, res in resource_dict.iteritems():
250+
resources.add(res)
233251

234252
# Change linker script if specified
235253
if linker_script is not None:
@@ -240,9 +258,9 @@ def export_project(src_paths, export_path, target, ide,
240258
macros=macros)
241259
if zip_proj:
242260
if isinstance(zip_proj, basestring):
243-
zip_export(join(export_path, zip_proj), name, temp, files)
261+
zip_export(join(export_path, zip_proj), name, resource_dict, files)
244262
else:
245-
zip_export(zip_proj, name, temp, files)
263+
zip_export(zip_proj, name, resource_dict, files)
246264

247265
return exporter
248266

0 commit comments

Comments
 (0)