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, 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 output file name') |
| 14 | print('\t-n|--name: the name of the service') |
| 15 | |
| 16 | |
| 17 | def main(): |
| 18 | try: |
| 19 | opts, args = getopt.getopt(sys.argv[1:], "hi:o:n:", ["help", "input=", "output=", "name="]) |
| 20 | except getopt.GetoptError as err: |
| 21 | logging.error( str(err)) # will print something like "option -a not recognized" |
| 22 | usage() |
| 23 | sys.exit(2) |
| 24 | |
| 25 | spec_file = None |
| 26 | output_file = None |
| 27 | name = None |
| 28 | |
| 29 | for o, a in opts: |
| 30 | if o in ("-h", "--help"): |
| 31 | usage() |
| 32 | sys.exit() |
| 33 | elif o in ("-i", "--input"): |
| 34 | spec_file = a |
| 35 | elif o in ("-o", "--output"): |
| 36 | output_file = a |
| 37 | elif o in ("-n", "--name"): |
| 38 | name = a |
| 39 | else: |
| 40 | logging.error( 'Unrecognized option: ' + o) |
| 41 | usage() |
| 42 | sys.exit(2) |
| 43 | |
| 44 | if spec_file is None or output_file is None: |
| 45 | logging.error( 'Incorrect arguments!') |
| 46 | usage() |
| 47 | sys.exit(2) |
| 48 | |
| 49 | dirname = os.path.dirname(output_file) |
| 50 | |
| 51 | if dirname is not None and len(dirname) > 0: |
| 52 | try: |
| 53 | os.stat(dirname) |
| 54 | except: |
| 55 | os.mkdir(dirname) |
| 56 | |
| 57 | meta_model = './data/meta_model/meta_policy_schema.yaml' |
| 58 | |
| 59 | builder = ToscaBuilder() |
| 60 | |
| 61 | builder.import_schema(meta_model) |
| 62 | builder.import_spec(spec_file) |
| 63 | if name is None: |
| 64 | name = builder.spec_import.name |
| 65 | if builder._using_policy() is False: |
| 66 | logging.warning( 'NO policy is defined in the spec') |
| 67 | return |
| 68 | builder.create_policy() |
| 69 | builder.export_policy(output_file) |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | main() |