Miklos Balint | 3a05c9d | 2018-05-31 09:31:27 +0200 | [diff] [blame^] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | import os |
| 9 | from keyword_substitution import keyword_substitute, Verbosity, log_print |
| 10 | |
| 11 | VERBOSITY = Verbosity.warning |
| 12 | log_print(Verbosity.debug, "Setting verbosity to", VERBOSITY, verbosity=VERBOSITY) |
| 13 | |
| 14 | controlsymbol = "@!GENERATOR" |
| 15 | controlblockstart = "@!GENERATOR_BLOCK_START!@" |
| 16 | controlblockend = "@!GENERATOR_BLOCK_END!@" |
| 17 | controlconditionstart = "@!GENERATOR_CONDITIONAL_START!@" |
| 18 | controlconditionelse = "@!GENERATOR_CONDITIONAL_ELSE!@" |
| 19 | controlconditionend = "@!GENERATOR_CONDITIONAL_END!@" |
| 20 | control_print_iteration_counter = "@!GENERATOR_ITERATION_COUNTER!@" |
| 21 | control_print_donotedit_warning = "@!GENERATOR_DONOTEDIT_WARNING!@" |
| 22 | |
| 23 | donotedit_warning = "/*********** " + \ |
| 24 | "WARNING: This is an auto-generated file. Do not edit!" + \ |
| 25 | " ***********/\n" |
| 26 | |
| 27 | # All operations assume tf-m repo root as active working directory |
| 28 | |
| 29 | # Functions |
| 30 | def generate(db, outfile_name): |
| 31 | outfile = \ |
| 32 | open(outfile_name, "w") |
| 33 | with open(outfile_name + '.template', "r") as template_file: |
| 34 | template = template_file.readlines() |
| 35 | |
| 36 | output = [] |
| 37 | blocks = [] |
| 38 | blocklines = [] |
| 39 | MISSING_KEYS_ACTION = 'replace False' |
| 40 | inblock = False |
| 41 | iteration_counter = 0 |
| 42 | for lineno, line in enumerate(template): |
| 43 | if controlblockstart in line: |
| 44 | inblock = True |
| 45 | log_print(Verbosity.info, "Blockstart:", str(lineno)) |
| 46 | blocklines = [] |
| 47 | elif controlblockend in line: |
| 48 | inblock = False |
| 49 | iteration_counter = 0 |
| 50 | log_print(Verbosity.info, "Blocklines:", str(blocklines)) |
| 51 | for manifest in db: |
| 52 | print_blocked = False |
| 53 | for line in blocklines: |
| 54 | outlist = keyword_substitute(manifest, line, MISSING_KEYS_ACTION) |
| 55 | outstring = "" |
| 56 | for outline in outlist: |
| 57 | outstring += ''.join(outline) |
| 58 | log_print(Verbosity.info, outstring) |
| 59 | if controlconditionstart in outstring: |
| 60 | if 'False' in outstring: |
| 61 | log_print(Verbosity.info, "PRINT BLOCKED") |
| 62 | print_blocked = True |
| 63 | elif controlconditionend in outstring: |
| 64 | log_print(Verbosity.info, "PRINT ENABLED") |
| 65 | print_blocked = False |
| 66 | elif controlconditionelse in outstring: |
| 67 | log_print(Verbosity.info, "PRINT " + str(print_blocked)) |
| 68 | print_blocked = not print_blocked |
| 69 | else: |
| 70 | if control_print_iteration_counter in outstring: |
| 71 | outstring = outstring.replace( |
| 72 | control_print_iteration_counter, |
| 73 | str(iteration_counter)) |
| 74 | elif controlsymbol in outstring: |
| 75 | print "Invalid control symbol:", outstring |
| 76 | print "exiting" |
| 77 | exit(1) |
| 78 | if not print_blocked: |
| 79 | outfile.write(outstring) |
| 80 | iteration_counter += 1 |
| 81 | # end for manifest in db |
| 82 | blocks.append(blocklines) |
| 83 | elif inblock: |
| 84 | # inside a generator block |
| 85 | # append line to blocklines to be processed by template generator |
| 86 | blocklines.append(line) |
| 87 | else: |
| 88 | # outside a generator block |
| 89 | if control_print_donotedit_warning in line: |
| 90 | # print do not edit warning - Note: ignore rest of input line |
| 91 | line = donotedit_warning |
| 92 | elif control_print_iteration_counter in line: |
| 93 | # line contains an iteration counter request, replace with value |
| 94 | line = line.replace(control_print_iteration_counter, str(iteration_counter)) |
| 95 | elif controlsymbol in line: |
| 96 | print "Invalid control symbol:", line |
| 97 | print "exiting" |
| 98 | exit(1) |
| 99 | outfile.write(line) |
| 100 | log_print(Verbosity.debug, "Blocks:" + str(blocks)) |
| 101 | outfile.close() |
| 102 | # def generate() |
| 103 | |
| 104 | def generate_from_template_file(db, file_list): |
| 105 | for file in file_list: |
| 106 | outfile = file["output"] |
| 107 | generate(db, outfile) |