Skip to content

Commit e350a7c

Browse files
committed
gen_partition: add %include directive support
Add a pre-processing step that expands %include directives before parsing partitions.conf files. This allows splitting partition definitions into reusable fragments (e.g. shared NHLOS or HLOS partition lists) that can be included from multiple partitions.conf files. Signed-off-by: Loïc Minier <loic.minier@oss.qualcomm.com>
1 parent a13a8ce commit e350a7c

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

gen_partition.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import getopt
3131
import re
32+
import os
3233
import sys
3334
import xml.etree.ElementTree as ET
3435
from collections import OrderedDict
@@ -77,6 +78,36 @@ def usage():
7778
output_xml = None
7879

7980

81+
def expand_includes(filepath, include_stack=None):
82+
"""Read a file and expand %include directives recursively.
83+
84+
Returns a list of stripped lines.
85+
Paths in %include are resolved relative to the including file's directory.
86+
Raises ValueError on circular includes or missing files.
87+
"""
88+
if include_stack is None:
89+
include_stack = []
90+
filepath = os.path.realpath(filepath)
91+
if not os.path.exists(filepath):
92+
raise ValueError("File not found: %s" % filepath)
93+
if filepath in include_stack:
94+
raise ValueError("Circular include detected: %s\nInclude stack: %s" % (
95+
filepath, " -> ".join(include_stack)))
96+
# make a copy, instead of mutating as we're recursing
97+
include_stack = include_stack + [filepath]
98+
base_dir = os.path.dirname(filepath)
99+
lines = []
100+
with open(filepath) as f:
101+
for raw_line in f:
102+
stripped = raw_line.strip()
103+
if stripped.startswith('%include '):
104+
inc_path = stripped[len('%include '):]
105+
inc_full = os.path.join(base_dir, inc_path)
106+
lines.extend(expand_includes(inc_full, include_stack))
107+
else:
108+
lines.append(stripped)
109+
return lines
110+
80111
def disk_options(argv):
81112
disk_params = disk_params_defaults.copy()
82113
for opt, arg in argv:
@@ -257,11 +288,9 @@ def generate_partition_xml(disk_params, partitions, output_xml):
257288
except Exception as argerr:
258289
print(str(argerr))
259290
usage()
260-
f = open(input_file)
261-
while (line := f.readline()):
291+
for line in expand_includes(input_file):
262292
if re.search(r"^\s*#", line) or re.search(r"^\s*$", line):
263293
continue
264-
line = line.strip()
265294
if re.search("^--disk", line):
266295
if disk_entry is None:
267296
disk_entry = line
@@ -273,7 +302,7 @@ def generate_partition_xml(disk_params, partitions, output_xml):
273302
partition_entries.append(line)
274303
else:
275304
print("Ignoring %s" % (line))
276-
f.close()
305+
277306
except Exception as e:
278307
print("Error: ", e)
279308
sys.exit(1)

0 commit comments

Comments
 (0)