blob: 7893f2224132b16f87e138cd762b8986d59783f7 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandolo67654fa2017-06-09 09:33:17 -070017#!/usr/bin/python
18
19import argparse
20from generator import *
21
22parse = argparse.ArgumentParser(description='XOS Generative Toolchain')
23parse.add_argument('--rev', dest='rev', action='store_true',default=False, help='Convert proto to xproto')
24parse.add_argument('--target', dest='target', action='store',default=None, help='Output format, corresponding to <output>.yaml file', required=True)
25parse.add_argument('--output', dest='output', action='store',default=None, help='Destination dir')
26parse.add_argument('--attic', dest='attic', action='store',default=None, help='The location at which static files are stored')
27parse.add_argument('--kvpairs', dest='kv', action='store',default=None, help='Key value pairs to make available to the target')
28parse.add_argument('--write-to-file', dest='write_to_file', choices = ['single', 'model', 'target'], action='store',default=None, help='Single output file (single) or output file per model (model) or let target decide (target)')
29
30group = parse.add_mutually_exclusive_group()
31group.add_argument('--dest-file', dest='dest_file', action='store',default=None, help='Output file name (if write-to-file is set to single)')
32group.add_argument('--dest-extension', dest='dest_extension', action='store',default=None, help='Output file extension (if write-to-file is set to single)')
33
34parse.add_argument('files', metavar='<input file>', nargs='+', action='store', help='xproto files to compile')
35
36class XosGen:
37
38 @staticmethod
39 def init(args=None):
40
41 if not args:
42 args = parse.parse_args()
43
44 args.quiet = False
45
46 # convert output to absolute path
47 if args.output is not None and not os.path.isabs(args.output):
48 args.output = os.path.abspath(os.getcwd() + '/' + args.output)
49 if not '/' in args.target:
50 # if the target is not a path, it refer to a library included one
51 args.target = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/targets/" + args.target)
52 if not os.path.isabs(args.target):
53 args.target = os.path.abspath(os.getcwd() + '/' + args.target)
54
55 # check if there's a line that starts with +++ in the target
56 # if so, then the output file names are left to the target to decide
57 # also, if dest-file or dest-extension are supplied, then an error is generated.
58 plusplusplus = reduce(lambda acc, line: True if line.startswith('+++') else acc, open(args.target).read().splitlines(), False)
59
60 if plusplusplus and args.write_to_file != 'target':
61 parse.error('%s chooses the names of the files that it generates, you must set --write-to-file to "target"' % args.target)
62
63
64 if args.write_to_file != 'single' and (args.dest_file):
65 parse.error('--dest-file requires --write-to-file to be set to "single"')
66
67 if args.write_to_file != 'model' and (args.dest_extension):
68 parse.error('--dest-extension requires --write-to-file to be set to "model"')
69
70 inputs = []
71
72 for fname in args.files:
73 if not os.path.isabs(fname):
74 inputs.append(os.path.abspath(os.getcwd() + '/' + fname))
75 else:
76 inputs.append(fname)
77 args.files = inputs
78
79 generated = XOSGenerator.generate(args)
80
81 if not args.output and not args.write_to_file:
82 print generated