Skip to content

Commit 04436a0

Browse files
committed
Detect unrecognised and malformed template file variables
1 parent 714ea59 commit 04436a0

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

patch-runtime.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import argparse
77
import pathlib
8+
import re
89
import shutil
10+
import sys
911
import tomllib
1012
from typing import Dict
1113

@@ -77,6 +79,25 @@ def gen_from_template(
7779
for key, value in template_values.items():
7880
content = content.replace(f"$({key})", value)
7981

82+
# Check for any unrecognised template variables
83+
m = re.search(r"\$\([^\)]+\)", content)
84+
if m:
85+
print(
86+
f"Error: unrecognised template variable '{m.group(0)}'"
87+
f" in file {str(template_file)}",
88+
file=sys.stderr,
89+
)
90+
sys.exit(1)
91+
92+
m = re.search(r"\$\(\w*", content)
93+
if m:
94+
print(
95+
f"Error: malformed template variable '{m.group(0)}'"
96+
f" in file {str(template_file)}",
97+
file=sys.stderr,
98+
)
99+
sys.exit(1)
100+
80101
with open(out_file, "w", newline="\n") as f:
81102
f.write(content)
82103

@@ -101,10 +122,10 @@ def gen_templates_from_manifest(
101122
src = template_info["src"]
102123
dst = template_info["dst"]
103124

104-
for k,v in template_values.items():
125+
for k, v in template_values.items():
105126
src = src.replace(f"$({k})", v)
106127

107-
for k,v in template_values.items():
128+
for k, v in template_values.items():
108129
dst = dst.replace(f"$({k})", v)
109130

110131
gen_from_template(

0 commit comments

Comments
 (0)