blob: 27a7fb4e2f701c877da46c6a0732555e2d4f01a3 [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 *
Scott Bakerd0d35662018-03-26 09:58:07 -070021from version import __version__
Matteo Scandolo67654fa2017-06-09 09:33:17 -070022
23parse = argparse.ArgumentParser(description='XOS Generative Toolchain')
Scott Baker1f7791d2018-10-04 13:21:20 -070024parse.add_argument('--rev', dest='rev', action='store_true',
25 default=XOSProcessorArgs.default_rev, help='Convert proto to xproto')
26parse.add_argument('--output', dest='output', action='store',
27 default=XOSProcessorArgs.default_output, help='Destination dir')
28parse.add_argument('--attic', dest='attic', action='store',
29 default=XOSProcessorArgs.default_attic, help='The location at which static files are stored')
30parse.add_argument('--kvpairs', dest='kv', action='store',
31 default=XOSProcessorArgs.default_kvpairs, help='Key value pairs to make available to the target')
32parse.add_argument('--write-to-file', dest='write_to_file', choices = ['single', 'model', 'target'], action='store',
33 default=XOSProcessorArgs.default_write_to_file,
34 help='Single output file (single) or output file per model (model) or let target decide (target)')
Scott Bakerd0d35662018-03-26 09:58:07 -070035parse.add_argument('--version', action='version', version=__version__)
Scott Baker1f7791d2018-10-04 13:21:20 -070036parse.add_argument("-v", "--verbosity", action="count",
37 default=XOSProcessorArgs.default_verbosity, help="increase output verbosity")
38parse.add_argument("--include-models", dest="include_models", action="append",
39 default=XOSProcessorArgs.default_include_models, help="list of models to include")
40parse.add_argument("--include-apps", dest="include_apps", action="append",
41 default=XOSProcessorArgs.default_include_apps, help="list of models to include")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070042
43group = parse.add_mutually_exclusive_group()
Scott Baker1f7791d2018-10-04 13:21:20 -070044group.add_argument('--dest-file', dest='dest_file', action='store',
45 default=XOSProcessorArgs.default_dest_file, help='Output file name (if write-to-file is set to single)')
46group.add_argument('--dest-extension', dest='dest_extension', action='store',
47 default=XOSProcessorArgs.default_dest_extension, help='Output file extension (if write-to-file is set to single)')
Matteo Scandolo67654fa2017-06-09 09:33:17 -070048
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080049group = parse.add_mutually_exclusive_group(required=True)
Scott Baker1f7791d2018-10-04 13:21:20 -070050group.add_argument('--target', dest='target', action='store',
51 default=XOSProcessorArgs.default_target, help='Output format, corresponding to <output>.yaml file')
52group.add_argument('--checkers', dest='checkers', action='store',
53 default=XOSProcessorArgs.default_checkers, help='Comma-separated list of static checkers')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080054
Matteo Scandolo67654fa2017-06-09 09:33:17 -070055parse.add_argument('files', metavar='<input file>', nargs='+', action='store', help='xproto files to compile')
56
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080057CHECK = 1
58GEN = 2
59
Matteo Scandolo67654fa2017-06-09 09:33:17 -070060class XosGen:
61
62 @staticmethod
63 def init(args=None):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070064 if not args:
65 args = parse.parse_args()
66
67 args.quiet = False
68
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080069 if args.target:
70 op = GEN
71 subdir = '/targets/'
72 elif args.checkers:
73 op = CHECK
74 subdir = '/checkers/'
75 else:
76 parse.error("At least one of --target and --checkers is required")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070077
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080078 operators = args.checkers.split(',') if hasattr(args, 'checkers') and args.checkers else [args.target]
Matteo Scandolo67654fa2017-06-09 09:33:17 -070079
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080080 for i in xrange(len(operators)):
81 if not '/' in operators[i]:
82 # if the target is not a path, it refer to a library included one
83 operators[i] = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + subdir + operators[i])
Matteo Scandolo67654fa2017-06-09 09:33:17 -070084
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080085 if not os.path.isabs(operators[i]):
86 operators[i] = os.path.abspath(os.getcwd() + '/' + operators[i])
Matteo Scandolo67654fa2017-06-09 09:33:17 -070087
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080088 if op == GEN:
89 # convert output to absolute path
90 if args.output is not None and not os.path.isabs(args.output):
91 args.output = os.path.abspath(os.getcwd() + '/' + args.output)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070092
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080093 operator = operators[0]
94
95 # check if there's a line that starts with +++ in the target
96 # if so, then the output file names are left to the target to decide
97 # also, if dest-file or dest-extension are supplied, then an error is generated.
98 plusplusplus = reduce(lambda acc, line: True if line.startswith('+++') else acc, open(operator).read().splitlines(), False)
99
100 if plusplusplus and args.write_to_file != 'target':
101 parse.error('%s chooses the names of the files that it generates, you must set --write-to-file to "target"' % operator)
102
103 if args.write_to_file != 'single' and (args.dest_file):
104 parse.error('--dest-file requires --write-to-file to be set to "single"')
105
106 if args.write_to_file != 'model' and (args.dest_extension):
107 parse.error('--dest-extension requires --write-to-file to be set to "model"')
108
109 else:
110 if args.write_to_file or args.dest_extension:
111 parse.error('Checkers cannot write to files')
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700112
113 inputs = []
114
115 for fname in args.files:
116 if not os.path.isabs(fname):
117 inputs.append(os.path.abspath(os.getcwd() + '/' + fname))
118 else:
119 inputs.append(fname)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800120
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700121 args.files = inputs
122
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800123 if op==GEN:
124 generated = XOSProcessor.process(args, operators[0])
125 if not args.output and not args.write_to_file:
126 print generated
127 elif op==CHECK:
128 for o in operators:
129 verdict_str = XOSProcessor.process(args, o)
130 vlst = verdict_str.split('\n')
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700131
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800132 try:
133 verdict = next(v for v in vlst if v.strip())
134 status_code, status_string = verdict.split(' ', 1)
135 status_code = int(status_code)
136 except:
137 print "Checker %s returned mangled output" % o
138 exit(1)
139
140 if status_code != 200:
141 print '%s: %s - %s' % (o, status_code, status_string)
142 exit(1)
143 else:
144 print '%s: OK'%o