Skip to content

Commit 9f28fc5

Browse files
Improve file handling in psa_crypto.py
Signed-off-by: Ronald Cron <[email protected]>
1 parent aebd0d4 commit 9f28fc5

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

scripts/psa_crypto.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ def replace_all_sh_components(psa_crypto_root_path):
154154
after_components = 0
155155
components_start = re.compile(r"#### Basic checks")
156156
components_end = re.compile(r"#### Termination")
157-
new_all_sh = open(os.path.join(tests_scripts_path, "all.sh"), 'x')
158157

159-
with open(os.path.join(tests_scripts_path, "all.sh.bak"), 'rt') as all_sh:
158+
with open(os.path.join(tests_scripts_path, "all.sh"), 'x') as new_all_sh, \
159+
open(os.path.join(tests_scripts_path, "all.sh.bak"), 'rt') as all_sh:
160160
for line in all_sh:
161161
if before_components:
162162
if components_start.match(line) != None:
@@ -180,7 +180,6 @@ def replace_all_sh_components(psa_crypto_root_path):
180180
if after_components:
181181
new_all_sh.write(line)
182182

183-
new_all_sh.close()
184183
os.chmod(os.path.join(tests_scripts_path, "all.sh"), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
185184

186185
def extend_config_psa(psa_crypto_root_path):
@@ -193,33 +192,30 @@ def extend_config_psa(psa_crypto_root_path):
193192
ext_placeholder = re.compile(".*BELOW THIS LINE - PLACEHOLDER FOR PSA-CRYPTO ADDITIONAL CONFIG OPTIONS TRANSLATION")
194193
endif_mbedtls_psa_crypto_config = re.compile("#endif /\* MBEDTLS_PSA_CRYPTO_CONFIG \*/")
195194

196-
new_config_psa = open(os.path.join(include_mbedtls_path, "config_psa.h"), 'x')
197-
config_psa = open(os.path.join(include_mbedtls_path, "config_psa.h.bak"), 'rt')
198-
199-
for line in config_psa:
200-
if if_defined_mbedtls_psa_crypto_config_file.match(line) != None:
201-
new_config_psa.write("#if defined(PSA_CRYPTO_CONFIG_FILE)\n")
202-
elif include_mbedtls_psa_crypto_config_file.match(line) != None:
203-
new_config_psa.write("#include PSA_CRYPTO_CONFIG_FILE\n")
204-
elif ext_placeholder.match(line) != None:
205-
break
206-
else:
207-
new_config_psa.write(line)
208-
209-
with open(os.path.join(psa_crypto_root_path, "drivers", "builtin", "config_psa_ext.h"), 'rt') as ext:
210-
for line in ext:
211-
new_config_psa.write(line)
212-
213-
trailer = False
214-
for line in config_psa:
215-
if endif_mbedtls_psa_crypto_config.match(line) != None:
216-
new_config_psa.write("\n")
217-
trailer = True
218-
if trailer:
219-
new_config_psa.write(line)
220-
221-
config_psa.close()
222-
new_config_psa.close()
195+
with open(os.path.join(include_mbedtls_path, "config_psa.h"), 'x') as new_config_psa, \
196+
open(os.path.join(include_mbedtls_path, "config_psa.h.bak"), 'rt') as config_psa:
197+
198+
for line in config_psa:
199+
if if_defined_mbedtls_psa_crypto_config_file.match(line) != None:
200+
new_config_psa.write("#if defined(PSA_CRYPTO_CONFIG_FILE)\n")
201+
elif include_mbedtls_psa_crypto_config_file.match(line) != None:
202+
new_config_psa.write("#include PSA_CRYPTO_CONFIG_FILE\n")
203+
elif ext_placeholder.match(line) != None:
204+
break
205+
else:
206+
new_config_psa.write(line)
207+
208+
with open(os.path.join(psa_crypto_root_path, "drivers", "builtin", "config_psa_ext.h"), 'rt') as ext:
209+
for line in ext:
210+
new_config_psa.write(line)
211+
212+
trailer = False
213+
for line in config_psa:
214+
if endif_mbedtls_psa_crypto_config.match(line) != None:
215+
new_config_psa.write("\n")
216+
trailer = True
217+
if trailer:
218+
new_config_psa.write(line)
223219

224220
def main():
225221
parser = argparse.ArgumentParser(

0 commit comments

Comments
 (0)