2323import stat
2424import re
2525import shutil
26- from distutils .dir_util import copy_tree
2726
2827def copy_of_psa_headers (mbedtls_root_path , psa_crypto_root_path ):
2928 source_path = os .path .join (mbedtls_root_path , "include" , "psa" )
@@ -91,33 +90,38 @@ def copy_from_scripts(mbedtls_root_path, psa_crypto_root_path):
9190 source_path = os .path .join (mbedtls_root_path , "scripts" )
9291 destination_path = os .path .join (psa_crypto_root_path , "scripts" )
9392
94- copy_tree (os .path .join (source_path , "data_files" , "driver_jsons" ),
95- os .path .join (destination_path , "data_files" , "driver_jsons" ))
96- copy_tree (os .path .join (source_path , "data_files" , "driver_templates" ),
97- os .path .join (destination_path , "data_files" , "driver_templates" ))
93+ shutil .copytree (os .path .join (source_path , "data_files" , "driver_jsons" ),
94+ os .path .join (destination_path , "data_files" , "driver_jsons" ),
95+ dirs_exist_ok = True )
96+ shutil .copytree (os .path .join (source_path , "data_files" , "driver_templates" ),
97+ os .path .join (destination_path , "data_files" , "driver_templates" ),
98+ dirs_exist_ok = True )
9899
99100 shutil .copy2 (os .path .join (source_path , "generate_driver_wrappers.py" ), destination_path )
100101 shutil .copy2 (os .path .join (source_path , "generate_psa_constants.py" ), destination_path )
101102 shutil .copy2 (os .path .join (source_path , "output_env.sh" ), destination_path )
102103 shutil .copy2 (os .path .join (source_path , "config.py" ), destination_path )
103104
104- copy_tree (os .path .join (source_path , "mbedtls_dev" ),
105- os .path .join (destination_path , "mbedtls_dev" ))
105+ shutil . copytree (os .path .join (source_path , "mbedtls_dev" ),
106+ os .path .join (destination_path , "mbedtls_dev" ), dirs_exist_ok = True )
106107
107108def copy_from_tests (mbedtls_root_path , psa_crypto_root_path ):
108109 source_path = os .path .join (mbedtls_root_path , "tests" )
109110 destination_path = os .path .join (psa_crypto_root_path , "tests" )
110111
111112 shutil .copy2 (os .path .join (source_path , "seedfile" ), destination_path )
112113
113- copy_tree ( os .path .join ( source_path , "include" ),
114- os .path .join ( destination_path , "include" ) )
114+ shutil .copytree (os .path .join (source_path , "include" ),
115+ os .path .join (destination_path , "include" ),
116+ dirs_exist_ok = True )
115117
116- copy_tree ( os .path .join ( source_path , "scripts" ),
117- os .path .join ( destination_path , "scripts" ) )
118+ shutil .copytree (os .path .join (source_path , "scripts" ),
119+ os .path .join (destination_path , "scripts" ),
120+ dirs_exist_ok = True )
118121
119- copy_tree ( os .path .join ( source_path , "src" ),
120- os .path .join ( destination_path , "src" ) )
122+ shutil .copytree (os .path .join (source_path , "src" ),
123+ os .path .join (destination_path , "src" ),
124+ dirs_exist_ok = True )
121125
122126 tests_suites_files = filter (lambda file_ : re .match (
123127 "test_suite_psa_crypto.*|helpers\.function|" \
@@ -150,9 +154,9 @@ def replace_all_sh_components(psa_crypto_root_path):
150154 after_components = 0
151155 components_start = re .compile (r"#### Basic checks" )
152156 components_end = re .compile (r"#### Termination" )
153- new_all_sh = open (os .path .join (tests_scripts_path , "all.sh" ), 'x' )
154157
155- 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 :
156160 for line in all_sh :
157161 if before_components :
158162 if components_start .match (line ) != None :
@@ -176,7 +180,6 @@ def replace_all_sh_components(psa_crypto_root_path):
176180 if after_components :
177181 new_all_sh .write (line )
178182
179- new_all_sh .close ()
180183 os .chmod (os .path .join (tests_scripts_path , "all.sh" ), stat .S_IEXEC | stat .S_IREAD | stat .S_IWRITE )
181184
182185def extend_config_psa (psa_crypto_root_path ):
@@ -187,9 +190,11 @@ def extend_config_psa(psa_crypto_root_path):
187190 if_defined_mbedtls_psa_crypto_config_file = re .compile ("#if defined\(MBEDTLS_PSA_CRYPTO_CONFIG_FILE\)" )
188191 include_mbedtls_psa_crypto_config_file = re .compile ("#include MBEDTLS_PSA_CRYPTO_CONFIG_FILE" )
189192 ext_placeholder = re .compile (".*BELOW THIS LINE - PLACEHOLDER FOR PSA-CRYPTO ADDITIONAL CONFIG OPTIONS TRANSLATION" )
190- new_config_psa = open (os .path .join (include_mbedtls_path , "config_psa.h" ), 'x' )
193+ endif_mbedtls_psa_crypto_config = re .compile ("#endif /\* MBEDTLS_PSA_CRYPTO_CONFIG \*/" )
194+
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 :
191197
192- with open (os .path .join (include_mbedtls_path , "config_psa.h.bak" ), 'rt' ) as config_psa :
193198 for line in config_psa :
194199 if if_defined_mbedtls_psa_crypto_config_file .match (line ) != None :
195200 new_config_psa .write ("#if defined(PSA_CRYPTO_CONFIG_FILE)\n " )
@@ -200,15 +205,17 @@ def extend_config_psa(psa_crypto_root_path):
200205 else :
201206 new_config_psa .write (line )
202207
203- config_psa .close ()
204-
205- with open (os .path .join (psa_crypto_root_path , "drivers" , "builtin" , "config_psa_ext.h" ), 'rt' ) as ext :
206- for line in ext :
207- new_config_psa .write (line )
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 )
208211
209- new_config_psa .write ("\n #endif /* MBEDTLS_PSA_CRYPTO_CONFIG */" )
210- new_config_psa .write ("\n #endif /* MBEDTLS_CONFIG_PSA_H */" )
211- new_config_psa .close ()
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 )
212219
213220def main ():
214221 parser = argparse .ArgumentParser (
0 commit comments