Stone, Avi (as206k) | 879e94b | 2018-04-12 16:41:45 +0300 | [diff] [blame] | 1 | #Author: Shu Shi |
| 2 | #emaiL: shushi@research.att.com |
| 3 | |
| 4 | from toscalib.tosca_workbook import ToscaWorkBook |
| 5 | from toscalib.tosca_builder import ToscaBuilder |
| 6 | |
| 7 | import getopt, sys, json, os, base64, logging |
| 8 | |
| 9 | def usage(): |
| 10 | print('OPTIONS:') |
| 11 | print('\t-h|--help: print this help message') |
| 12 | print('\t-i|--input: The PATH to spec file') |
| 13 | print('\t-o|--output: the folder for the output model ') |
| 14 | print('\t-n|--name: the name of the service') |
| 15 | print('\t-t|--import: the PATH to import file') |
| 16 | print('\t-m|--meta: the PATH to meta model file (default: ./data/meta_model/meta_tosca_schema.yaml') |
| 17 | |
| 18 | |
| 19 | def main(): |
| 20 | try: |
| 21 | opts, args = getopt.getopt(sys.argv[1:], "hi:o:n:t:m:", ["help", "input=", "output=", "name=", "import=", "meta="]) |
| 22 | except getopt.GetoptError as err: |
| 23 | # print help information and exit: |
| 24 | logging.error(str(err)) # will print something like "option -a not recognized" |
| 25 | usage() |
| 26 | sys.exit(2) |
| 27 | |
| 28 | spec_file = None |
| 29 | output_file = './data/tosca_model/temp' |
| 30 | name = None |
| 31 | meta_model = './data/meta_model/meta_tosca_schema.yaml' |
| 32 | import_file = None |
| 33 | |
| 34 | for o, a in opts: |
| 35 | if o in ("-h", "--help"): |
| 36 | usage() |
| 37 | sys.exit() |
| 38 | elif o in ("-i", "--input"): |
| 39 | spec_file = a |
| 40 | elif o in ("-o", "--output"): |
| 41 | output_file = a |
| 42 | elif o in ("-n", "--name"): |
| 43 | name = a |
| 44 | elif o in ("-t", "--import"): |
| 45 | import_file = a |
| 46 | elif o in ("-m", "--meta"): |
| 47 | meta_model = a |
| 48 | else: |
| 49 | logging.error('Unrecognized option: ' + o) |
| 50 | usage() |
| 51 | sys.exit(2) |
| 52 | |
| 53 | if spec_file is None: |
| 54 | logging.error('Incorrect arguments!') |
| 55 | usage() |
| 56 | sys.exit(2) |
| 57 | |
| 58 | if output_file is None: |
| 59 | model_prefix = './data/tosca_model' |
| 60 | else: |
| 61 | filename = output_file + '/schema.yaml' |
| 62 | dirname = os.path.dirname(filename) |
| 63 | try: |
| 64 | os.stat(dirname) |
| 65 | except: |
| 66 | os.mkdir(dirname) |
| 67 | model_prefix = output_file |
| 68 | |
| 69 | |
| 70 | builder = ToscaBuilder() |
| 71 | |
| 72 | builder.import_schema(meta_model) |
| 73 | if spec_file in ['stdin', '-']: |
| 74 | builder.import_spec_str(json.load(sys.stdin)) |
| 75 | else: |
| 76 | builder.import_spec(spec_file) |
| 77 | if import_file is not None: |
| 78 | builder.import_import(import_file) |
| 79 | |
| 80 | if name is None: |
| 81 | name = builder.spec_import.name |
| 82 | |
| 83 | builder.create_node_type(name) |
| 84 | schema_str = builder.export_schema(model_prefix+ '/schema.yaml') |
| 85 | builder.import_schema(model_prefix+ '/schema.yaml') |
| 86 | builder.create_model(name) |
| 87 | template_str = builder.export_model(model_prefix+ '/template.yaml') |
| 88 | builder.create_translate(name) |
| 89 | translate_str = builder.export_translation(model_prefix+ '/translate.yaml') |
| 90 | |
| 91 | if spec_file in ['stdin', '-']: |
| 92 | ret = {} |
| 93 | ret['schema'] = base64.encodestring(schema_str) |
| 94 | ret['template'] = base64.encodestring(template_str) |
| 95 | ret['translate'] = base64.encodestring(translate_str) |
| 96 | |
| 97 | print (json.dumps(ret)) |
| 98 | |
| 99 | if __name__ == "__main__": |
| 100 | main() |