Gilles Peskine | bdffaea | 2021-01-12 00:37:38 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """Edit test cases to use PSA dependencies instead of classic dependencies. |
| 4 | """ |
| 5 | |
| 6 | # Copyright The Mbed TLS Contributors |
| 7 | # SPDX-License-Identifier: Apache-2.0 |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. |
| 11 | # You may obtain a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | |
| 21 | import os |
| 22 | import sys |
| 23 | |
| 24 | def process_data_stanza(stanza, file_name, test_case_number): |
| 25 | """Update PSA crypto dependencies in one Mbed TLS test case. |
| 26 | |
| 27 | stanza is the test case text (including the description, the dependencies, |
| 28 | the line with the function and arguments, and optionally comments). Return |
| 29 | a new stanza with an updated dependency line, preserving everything else |
| 30 | (description, comments, arguments, etc.). |
| 31 | """ |
| 32 | #TODO: not implemented yet |
| 33 | return stanza |
| 34 | |
| 35 | def process_data_file(file_name, old_content): |
| 36 | """Update PSA crypto dependencies in an Mbed TLS test suite data file. |
| 37 | |
| 38 | Process old_content (the old content of the file) and return the new content. |
| 39 | """ |
| 40 | old_stanzas = old_content.split('\n\n') |
| 41 | new_stanzas = [process_data_stanza(stanza, file_name, n) |
| 42 | for n, stanza in enumerate(old_stanzas, start=1)] |
| 43 | return '\n\n'.join(new_stanzas) |
| 44 | |
| 45 | def update_file(file_name, old_content, new_content): |
| 46 | """Update the given file with the given new content. |
| 47 | |
| 48 | Replace the existing file. The previous version is renamed to *.bak. |
| 49 | Don't modify the file if the content was unchanged. |
| 50 | """ |
| 51 | if new_content == old_content: |
| 52 | return |
| 53 | backup = file_name + '.bak' |
| 54 | tmp = file_name + '.tmp' |
| 55 | with open(tmp, 'w', encoding='utf-8') as new_file: |
| 56 | new_file.write(new_content) |
| 57 | os.replace(file_name, backup) |
| 58 | os.replace(tmp, file_name) |
| 59 | |
| 60 | def process_file(file_name): |
| 61 | """Update PSA crypto dependencies in an Mbed TLS test suite data file. |
| 62 | |
| 63 | Replace the existing file. The previous version is renamed to *.bak. |
| 64 | Don't modify the file if the content was unchanged. |
| 65 | """ |
| 66 | old_content = open(file_name, encoding='utf-8').read() |
| 67 | if file_name.endswith('.data'): |
| 68 | new_content = process_data_file(file_name, old_content) |
| 69 | else: |
| 70 | raise Exception('File type not recognized: {}' |
| 71 | .format(file_name)) |
| 72 | update_file(file_name, old_content, new_content) |
| 73 | |
| 74 | def main(args): |
| 75 | for file_name in args: |
| 76 | process_file(file_name) |
| 77 | |
| 78 | if __name__ == '__main__': |
| 79 | main(sys.argv[1:]) |