Skip to content

Commit dcba1e7

Browse files
committed
SPM code generator support for python 2 & 3
1 parent 0130138 commit dcba1e7

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

tools/spm/generate_partition_code.py

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@
2020
import json
2121
import os
2222
from os.path import join as path_join
23+
from six import integer_types, string_types
2324

2425
from jinja2 import Environment, FileSystemLoader, StrictUndefined
2526
from jsonschema import validate
27+
2628
__version__ = '1.0'
2729
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
2830
TEMPLATES_DIR = path_join(SCRIPT_DIR, 'templates')
29-
MANIFEST_TEMPLATES = filter(
30-
lambda filename: '_NAME_' in filename,
31-
[os.path.join(dp, f) for dp, dn, fn in os.walk(TEMPLATES_DIR) for f in fn if f.endswith('.tpl')]
32-
)
33-
COMMON_TEMPLATES = filter(
34-
lambda filename: '_NAME_' not in filename,
35-
[os.path.join(dp, f) for dp, dn, fn in os.walk(TEMPLATES_DIR) for f in fn if f.endswith('.tpl')]
36-
)
31+
MANIFEST_TEMPLATES = [filename for filename in
32+
[os.path.join(dp, f) for dp, dn, fn in
33+
os.walk(TEMPLATES_DIR) for f in fn if f.endswith('.tpl')]
34+
if '_NAME_' in filename]
35+
COMMON_TEMPLATES = [filename for filename in
36+
[os.path.join(dp, f) for dp, dn, fn in
37+
os.walk(TEMPLATES_DIR) for f in fn if f.endswith('.tpl')]
38+
if '_NAME_' not in filename]
3739
MANIFEST_FILE_PATTERN = '*_psa.json'
3840
MBED_OS_ROOT = os.path.abspath(path_join(SCRIPT_DIR, os.pardir, os.pardir))
3941
SPM_CORE_ROOT = path_join(MBED_OS_ROOT, 'components', 'TARGET_PSA', 'spm')
@@ -169,7 +171,7 @@ def __init__(self, line_num, signal):
169171
:param signal: IRQ line identifier inside the partition
170172
"""
171173
self.line_num = assert_int(line_num)
172-
assert isinstance(signal, basestring)
174+
assert isinstance(signal, string_types)
173175
self.signal = signal
174176

175177
def __eq__(self, other):
@@ -242,10 +244,10 @@ def __init__(
242244
irqs = [] if irqs is None else irqs
243245

244246
assert os.path.isfile(manifest_file)
245-
assert isinstance(partition_id, (int, long))
247+
assert isinstance(partition_id, integer_types)
246248
assert isinstance(heap_size, int)
247249
assert isinstance(stack_size, int)
248-
assert isinstance(entry_point, basestring)
250+
assert isinstance(entry_point, string_types)
249251
assert partition_type in self.PARTITION_TYPES
250252
assert partition_id > 0
251253

@@ -277,7 +279,7 @@ def __init__(
277279
assert isinstance(rot_srv, RotService)
278280

279281
for extern_sid in self.extern_sids:
280-
assert isinstance(extern_sid, basestring)
282+
assert isinstance(extern_sid, string_types)
281283

282284
assert len(self.extern_sids) == len(set(self.extern_sids)), \
283285
'Detected duplicates external SIDs in {}'.format(self.file)
@@ -341,7 +343,8 @@ def from_json(cls, manifest_file, skip_src=False):
341343

342344
mmio_regions = []
343345
for mmio_region in manifest.get('mmio_regions', []):
344-
mmio_regions.append(MmioRegion(partition_id=manifest['id'], **mmio_region))
346+
mmio_regions.append(
347+
MmioRegion(partition_id=manifest['id'], **mmio_region))
345348

346349
rot_services = []
347350
for rot_srv in manifest.get('services', []):
@@ -385,7 +388,7 @@ def find_dependencies(self, manifests):
385388
extern Root of Trust Services
386389
"""
387390

388-
manifests = filter(lambda man: man != self, manifests)
391+
manifests = [man for man in manifests if man != self]
389392
extern_sids_set = set(self.extern_sids)
390393
return [manifest.name for manifest in manifests
391394
if extern_sids_set.intersection(set(manifest.sids))]
@@ -414,9 +417,12 @@ def templates_to_files(self, templates, templates_base, output_dir):
414417

415418
def check_circular_call_dependencies(manifests):
416419
"""
417-
Check if there is a circular dependency between the partitions described by the manifests.
418-
A circular dependency might happen if there is a scenario in which a partition calls a Root of Trust Service in
419-
another partition which than calls another Root of Trust Service which resides in the originating partition.
420+
Check if there is a circular dependency between the partitions
421+
described by the manifests.
422+
A circular dependency might happen if there is a scenario in which a
423+
partition calls a Root of Trust Service in another partition which than
424+
calls another Root of Trust Service which resides in the
425+
originating partition.
420426
For example: Partition A has a Root of Trust Service A1 and extern sid B1,
421427
partition B has a Root of Trust Service B1 and extern sid A1.
422428
@@ -439,8 +445,8 @@ def check_circular_call_dependencies(manifests):
439445
while len(call_graph) > 0:
440446
# Find all the nodes that aren't called by anyone and
441447
# therefore can be removed.
442-
nodes_to_remove = filter(lambda x: len(call_graph[x]['called_by']) == 0,
443-
call_graph.keys())
448+
nodes_to_remove = [x for x in list(call_graph.keys()) if
449+
len(call_graph[x]['called_by']) == 0]
444450

445451
# If no node can be removed we have a circle.
446452
if not nodes_to_remove:
@@ -683,23 +689,26 @@ def generate_partitions_sources(manifest_files, extra_filters=None):
683689
return list(generated_folders)
684690

685691

686-
def generate_psa_setup(manifest_files, output_dir, weak_setup, extra_filters=None):
692+
def generate_psa_setup(manifest_files, output_dir, weak_setup,
693+
extra_filters=None):
687694
"""
688695
Process all the given manifest files and generate C setup code from them
689696
:param manifest_files: List of manifest files
690697
:param output_dir: Output directory for the generated files
691-
:param weak_setup: Is the functions/data in the setup file weak (can be overridden by another setup file)
698+
:param weak_setup: Is the functions/data in the setup file weak
699+
(can be overridden by another setup file)
692700
:param extra_filters: Dictionary of extra filters to use in the rendering
693701
process
694702
:return: path to the setup generated files
695703
"""
696704
autogen_folder = output_dir
697705
templates_dict = {
698-
t: path_join(autogen_folder, os.path.relpath(os.path.splitext(t)[0], TEMPLATES_DIR))
706+
t: path_join(autogen_folder,
707+
os.path.relpath(os.path.splitext(t)[0], TEMPLATES_DIR))
699708
for t in COMMON_TEMPLATES
700709
}
701710

702-
complete_source_list = templates_dict.values()
711+
complete_source_list = list(templates_dict.values())
703712

704713
# Construct lists of all the manifests and mmio_regions.
705714
region_list = []
@@ -710,10 +719,10 @@ def generate_psa_setup(manifest_files, output_dir, weak_setup, extra_filters=Non
710719
for region in manifest_obj.mmio_regions:
711720
region_list.append(region)
712721
complete_source_list.extend(
713-
manifest_obj.templates_to_files(
722+
list(manifest_obj.templates_to_files(
714723
MANIFEST_TEMPLATES,
715724
TEMPLATES_DIR,
716-
manifest_obj.autogen_folder).values()
725+
manifest_obj.autogen_folder).values())
717726
)
718727

719728
# Validate the correctness of the manifest collection.
@@ -739,7 +748,9 @@ def manifests_discovery(root_dir):
739748
manifest_files = set()
740749

741750
for root, dirs, files in os.walk(root_dir):
742-
to_add = [path_join(root, f) for f in fnmatch.filter(files, MANIFEST_FILE_PATTERN) if 'TARGET_IGNORE' not in root]
751+
to_add = [path_join(root, f) for f in
752+
fnmatch.filter(files, MANIFEST_FILE_PATTERN) if
753+
'TARGET_IGNORE' not in root]
743754
manifest_files.update(to_add)
744755

745756
return list(manifest_files)
@@ -752,22 +763,29 @@ def generate_psa_code():
752763
# Generate partition code for each manifest file
753764
generate_partitions_sources(manifest_files)
754765

755-
test_manifest_files = sorted([path for path in manifest_files if 'TESTS' in path])
756-
system_manifest_files = sorted(list(set(manifest_files) - set(test_manifest_files)))
766+
test_manifest_files = sorted(
767+
[path for path in manifest_files if 'TESTS' in path])
768+
system_manifest_files = sorted(
769+
list(set(manifest_files) - set(test_manifest_files)))
757770

758771
# Generate default system psa setup file (only system partitions)
759772
generate_psa_setup(system_manifest_files, SPM_CORE_ROOT, weak_setup=True)
760773

761-
tests_dir_content = [path_join(SPM_TESTS_ROOT, f) for f in os.listdir(SPM_TESTS_ROOT)]
774+
tests_dir_content = [path_join(SPM_TESTS_ROOT, f) for f in
775+
os.listdir(SPM_TESTS_ROOT)]
762776
spm_tests = [path for path in tests_dir_content if os.path.isdir(path)]
763777

764-
# Build a dictionary for test partition in the form of { test_root: manifest_list }
778+
# Build a dictionary for test partition in the form of:
779+
# { test_root: manifest_list }
765780
# For each test generate specific psa setup file (system + test partitions)
766781
tests_dict = {test_root: [] for test_root in spm_tests}
767782
for test_root in spm_tests:
768-
tests_dict[test_root] = filter(lambda manifest_path: test_root in manifest_path, test_manifest_files)
783+
tests_dict[test_root] = [manifest_path for manifest_path in
784+
test_manifest_files if
785+
test_root in manifest_path]
769786
tests_dict[test_root] += system_manifest_files
770-
generate_psa_setup(sorted(tests_dict[test_root]), test_root, weak_setup=False)
787+
generate_psa_setup(sorted(tests_dict[test_root]), test_root,
788+
weak_setup=False)
771789

772790

773791
if __name__ == '__main__':

0 commit comments

Comments
 (0)