Lusheng Ji | 6ca8167 | 2017-10-15 19:28:04 -0400 | [diff] [blame] | 1 | # ============LICENSE_START======================================================= |
| 2 | # org.onap.dcae |
| 3 | # ================================================================================ |
| 4 | # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. |
| 5 | # ================================================================================ |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # ============LICENSE_END========================================================= |
| 18 | # |
| 19 | # ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 20 | |
| 21 | import os, sys |
| 22 | from sets import Set |
| 23 | from jinja2 import Environment, PackageLoader, FileSystemLoader, meta |
| 24 | |
| 25 | ''' |
| 26 | @contextfunction |
| 27 | def get_exported_names(context): |
| 28 | return sorted(context.exported_vars) |
| 29 | ''' |
| 30 | |
| 31 | def build_context( jjt_directory ): |
| 32 | context = {} |
| 33 | for file in os.listdir(jjt_directory): |
| 34 | if file.endswith(".txt"): |
| 35 | with open(os.path.join(jjt_directory, file), 'r') as parameter_file: |
| 36 | value = parameter_file.readline().rstrip() |
| 37 | key = file.split('.txt', 1)[0] |
| 38 | context[key] = value |
| 39 | return context |
| 40 | |
| 41 | |
| 42 | def render_template(tpl_path, context): |
| 43 | path, filename = os.path.split(tpl_path) |
| 44 | return Environment(loader = FileSystemLoader(path)).get_template(filename).render(context) |
| 45 | |
| 46 | |
| 47 | def check_templates(jjt_directory, template_file_dir): |
| 48 | all_variables = Set([]) |
| 49 | env = Environment(loader=FileSystemLoader(template_file_dir)) |
| 50 | |
| 51 | |
| 52 | for infname in os.listdir(template_file_dir): |
| 53 | template_source = env.loader.get_source(env, infname)[0] |
| 54 | parsed_content = env.parse(template_source) |
| 55 | referenced_variables = meta.find_undeclared_variables(parsed_content) |
| 56 | all_variables.update(referenced_variables) |
| 57 | |
| 58 | context_variables = Set(build_context(jjt_directory).keys()) |
| 59 | undefined_variables = all_variables - context_variables |
| 60 | |
| 61 | if undefined_variables: |
| 62 | print("Error: referenced but unprovided variables: {}".format(undefined_variables)) |
| 63 | exit(1) |
| 64 | else: |
| 65 | print("All referenced template variables found. Proceed with de-templating") |
| 66 | |
| 67 | |
| 68 | # using context provided in jjt_directory to de-tempatize blueprint inputs in in_directory to out_directory |
| 69 | def detemplate_bpinputs(jjt_directory, in_directory, out_directory): |
| 70 | context = build_context(jjt_directory) |
| 71 | |
| 72 | for infname in os.listdir(in_directory): |
| 73 | infpath = os.path.join(in_directory, infname) |
| 74 | outfpath = os.path.join(out_directory, infname) |
| 75 | with open(outfpath, 'w') as f: |
| 76 | print ('detemplating {} to {}'.format(infpath, outfpath)) |
| 77 | inputs = render_template(infpath, context) |
| 78 | f.write(inputs) |
| 79 | |
Lusheng Ji | 9f223bf | 2017-10-17 16:00:06 -0400 | [diff] [blame] | 80 | |
| 81 | def add_keystone_20(conf_directory, filename): |
| 82 | fpath = os.path.join(conf_directory, filename) |
| 83 | newurl = '' |
Lusheng Ji | 8581247 | 2017-10-25 20:51:30 -0400 | [diff] [blame] | 84 | try: |
| 85 | with open(fpath, 'r') as f: |
| 86 | url = f.readline().rstrip() |
| 87 | if not url.endswith('/v2.0'): |
| 88 | if url.endswith('/'): |
| 89 | newurl = url + 'v2.0' |
| 90 | else: |
| 91 | newurl = url + '/v2.0' |
| 92 | except: |
| 93 | newurl = '' |
| 94 | print("add_keystone_20 eception reading file: " + conf_directory + "/" + filename) |
| 95 | |
Lusheng Ji | 9f223bf | 2017-10-17 16:00:06 -0400 | [diff] [blame] | 96 | if newurl: |
Lusheng Ji | 8581247 | 2017-10-25 20:51:30 -0400 | [diff] [blame] | 97 | try: |
| 98 | with open(fpath, 'w') as f: |
| 99 | f.write(newurl) |
| 100 | except: |
| 101 | print("add_keystone_20 eception writing file: " + conf_directory + "/" + filename) |
Lusheng Ji | 9f223bf | 2017-10-17 16:00:06 -0400 | [diff] [blame] | 102 | |
| 103 | |
Lusheng Ji | 6ca8167 | 2017-10-15 19:28:04 -0400 | [diff] [blame] | 104 | def main(): |
| 105 | if len(sys.argv) != 4: |
| 106 | print("Usgae: {} variable_def_dir template_dir template_output_dir".format(sys.argv[0])) |
| 107 | exit(1) |
| 108 | |
| 109 | print("De-templatizing templates in {} using variable defs from {}, results in {}".format(sys.argv[1], sys.argv[2], sys.argv[3])) |
Lusheng Ji | 8581247 | 2017-10-25 20:51:30 -0400 | [diff] [blame] | 110 | add_keystone_20(conf_directory = sys.argv[1], filename = 'keystone_url.txt') |
Lusheng Ji | 6ca8167 | 2017-10-15 19:28:04 -0400 | [diff] [blame] | 111 | check_templates(sys.argv[1], sys.argv[2]) |
Lusheng Ji | 9f223bf | 2017-10-17 16:00:06 -0400 | [diff] [blame] | 112 | detemplate_bpinputs(jjt_directory = sys.argv[1], in_directory = sys.argv[2], out_directory = sys.argv[3]) |
Lusheng Ji | 6ca8167 | 2017-10-15 19:28:04 -0400 | [diff] [blame] | 113 | |
| 114 | ######################################## |
| 115 | if __name__ == "__main__": |
| 116 | main() |