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.
pull/2714/head
Jimmy Brisson 2016-08-23 16:56:22 -05:00 committed by Anna Bridge
parent c541e1c6a3
commit a9b55147e2
1 changed files with 43 additions and 25 deletions

View File

@ -12,6 +12,7 @@ import zipfile
from tools.build_api import prepare_toolchain from tools.build_api import prepare_toolchain
from tools.build_api import scan_resources from tools.build_api import scan_resources
from tools.export import EXPORTERS from tools.export import EXPORTERS
from tools.toolchains import Resources
def get_exporter_toolchain(ide): def get_exporter_toolchain(ide):
@ -49,13 +50,16 @@ def subtract_basepath(resources, export_path):
'lib_dirs'] 'lib_dirs']
for key in keys: for key in keys:
vals = getattr(resources, key) vals = getattr(resources, key)
if type(vals) is set: if isinstance(vals, set):
vals = list(vals) vals = list(vals)
if type(vals) is list: if isinstance(vals, list):
new_vals = [] new_vals = []
for val in vals: for val in vals:
new_vals.append(rewrite_basepath(val, resources, export_path)) new_vals.append(rewrite_basepath(val, resources, export_path))
setattr(resources, key, new_vals) if isinstance(getattr(resources, key), set):
setattr(resources, key, set(new_vals))
else:
setattr(resources, key, new_vals)
elif vals: elif vals:
setattr(resources, key, rewrite_basepath(vals, resources, setattr(resources, key, rewrite_basepath(vals, resources,
export_path)) export_path))
@ -85,7 +89,7 @@ def prepare_project(src_paths, export_path, target, ide,
_, toolchain_name = get_exporter_toolchain(ide) _, toolchain_name = get_exporter_toolchain(ide)
# Pass all params to the unified prepare_resources() # Pass all params to the unified prepare_resources()
toolchain = prepare_toolchain(src_paths, export_path, target, toolchain = prepare_toolchain(src_paths, target,
toolchain_name, macros=macros, toolchain_name, macros=macros,
options=options, clean=clean, jobs=jobs, options=options, clean=clean, jobs=jobs,
notify=notify, silent=silent, verbose=verbose, notify=notify, silent=silent, verbose=verbose,
@ -111,7 +115,7 @@ def prepare_project(src_paths, export_path, target, ide,
def generate_project_files(resources, export_path, target, name, toolchain, ide, def generate_project_files(resources, export_path, target, name, toolchain, ide,
macros=None): macros=None):
"""Generate the project files for a project """Generate the project files for a project
Positional arguments: Positional arguments:
@ -148,16 +152,16 @@ def zip_export(file_name, prefix, resources, project_files):
with zipfile.ZipFile(file_name, "w") as zip_file: with zipfile.ZipFile(file_name, "w") as zip_file:
for prj_file in project_files: for prj_file in project_files:
zip_file.write(prj_file, join(prefix, basename(prj_file))) zip_file.write(prj_file, join(prefix, basename(prj_file)))
for source in resources.headers + resources.s_sources + \ for loc, res in resources.iteritems():
resources.c_sources + resources.cpp_sources + \ for source in \
resources.libraries + resources.hex_files + \ res.headers + res.s_sources + res.c_sources + res.cpp_sources +\
[resources.linker_script] + resources.bin_files \ res.libraries + res.hex_files + [res.linker_script] +\
+ resources.objects + resources.json_files: res.bin_files + res.objects + res.json_files:
if source: if source:
zip_file.write(source, zip_file.write(source,
join(prefix, join(prefix, loc,
relpath(source, relpath(source,
resources.file_basepath[source]))) res.file_basepath[source])))
def export_project(src_paths, export_path, target, ide, def export_project(src_paths, export_path, target, ide,
@ -194,11 +198,19 @@ def export_project(src_paths, export_path, target, ide,
""" """
# Convert src_path to a list if needed # Convert src_path to a list if needed
if type(src_paths) != type([]): if isinstance(src_paths, dict):
src_paths = [src_paths] paths = sum(src_paths.values(), [])
# Extend src_paths wiht libraries_paths elif isinstance(src_paths, list):
paths = src_paths[:]
else:
paths = [src_paths]
# Extend src_paths wit libraries_paths
if libraries_paths is not None: if libraries_paths is not None:
src_paths.extend(libraries_paths) paths.extend(libraries_paths)
if not isinstance(src_paths, dict):
src_paths = {"": paths}
# Export Directory # Export Directory
if exists(export_path) and clean: if exists(export_path) and clean:
@ -209,7 +221,7 @@ def export_project(src_paths, export_path, target, ide,
_, toolchain_name = get_exporter_toolchain(ide) _, toolchain_name = get_exporter_toolchain(ide)
# Pass all params to the unified prepare_resources() # Pass all params to the unified prepare_resources()
toolchain = prepare_toolchain(src_paths, target, toolchain_name, toolchain = prepare_toolchain(paths, target, toolchain_name,
macros=macros, options=options, clean=clean, macros=macros, options=options, clean=clean,
jobs=jobs, notify=notify, silent=silent, jobs=jobs, notify=notify, silent=silent,
verbose=verbose, extra_verbose=extra_verbose, verbose=verbose, extra_verbose=extra_verbose,
@ -219,17 +231,23 @@ def export_project(src_paths, export_path, target, ide,
name = basename(normpath(abspath(src_paths[0]))) name = basename(normpath(abspath(src_paths[0])))
# Call unified scan_resources # Call unified scan_resources
resources = scan_resources(src_paths, toolchain, inc_dirs=inc_dirs) resource_dict = {loc: scan_resources(path, toolchain, inc_dirs=inc_dirs)
for loc, path in src_paths.iteritems()}
resources = Resources()
toolchain.build_dir = export_path toolchain.build_dir = export_path
config_header = toolchain.get_config_header() config_header = toolchain.get_config_header()
resources.headers.append(config_header) resources.headers.append(config_header)
resources.file_basepath[config_header] = dirname(config_header) resources.file_basepath[config_header] = dirname(config_header)
temp = copy.deepcopy(resources)
if zip_proj: if zip_proj:
subtract_basepath(resources, export_path) subtract_basepath(resources, export_path)
for loc, res in resource_dict.iteritems():
temp = copy.deepcopy(res)
subtract_basepath(temp, join(export_path, loc))
resources.add(temp)
else: else:
resources.relative_to(export_path) for _, res in resource_dict.iteritems():
resources.add(res)
# Change linker script if specified # Change linker script if specified
if linker_script is not None: if linker_script is not None:
@ -240,9 +258,9 @@ def export_project(src_paths, export_path, target, ide,
macros=macros) macros=macros)
if zip_proj: if zip_proj:
if isinstance(zip_proj, basestring): if isinstance(zip_proj, basestring):
zip_export(join(export_path, zip_proj), name, temp, files) zip_export(join(export_path, zip_proj), name, resource_dict, files)
else: else:
zip_export(zip_proj, name, temp, files) zip_export(zip_proj, name, resource_dict, files)
return exporter return exporter