blob: fa734b5a8a72031c25bd8c670777c239dc8b7e64 [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')
Matteo Scandolo67654fa2017-06-09 09:33:17 -070024parse.add_argument('--output', dest='output', action='store',default=None, help='Destination dir')
25parse.add_argument('--attic', dest='attic', action='store',default=None, help='The location at which static files are stored')
26parse.add_argument('--kvpairs', dest='kv', action='store',default=None, help='Key value pairs to make available to the target')
27parse.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)')
28
29group = parse.add_mutually_exclusive_group()
30group.add_argument('--dest-file', dest='dest_file', action='store',default=None, help='Output file name (if write-to-file is set to single)')
31group.add_argument('--dest-extension', dest='dest_extension', action='store',default=None, help='Output file extension (if write-to-file is set to single)')
32
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080033group = parse.add_mutually_exclusive_group(required=True)
34group.add_argument('--target', dest='target', action='store',default=None, help='Output format, corresponding to <output>.yaml file')
35group.add_argument('--checkers', dest='checkers', action='store', default=None, help='Comma-separated list of static checkers')
36
Matteo Scandolo67654fa2017-06-09 09:33:17 -070037parse.add_argument('files', metavar='<input file>', nargs='+', action='store', help='xproto files to compile')
38
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080039CHECK = 1
40GEN = 2
41
Matteo Scandolo67654fa2017-06-09 09:33:17 -070042class XosGen:
43
44 @staticmethod
45 def init(args=None):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070046 if not args:
47 args = parse.parse_args()
48
49 args.quiet = False
50
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080051 if args.target:
52 op = GEN
53 subdir = '/targets/'
54 elif args.checkers:
55 op = CHECK
56 subdir = '/checkers/'
57 else:
58 parse.error("At least one of --target and --checkers is required")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070059
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080060 operators = args.checkers.split(',') if hasattr(args, 'checkers') and args.checkers else [args.target]
Matteo Scandolo67654fa2017-06-09 09:33:17 -070061
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080062 for i in xrange(len(operators)):
63 if not '/' in operators[i]:
64 # if the target is not a path, it refer to a library included one
65 operators[i] = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + subdir + operators[i])
Matteo Scandolo67654fa2017-06-09 09:33:17 -070066
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080067 if not os.path.isabs(operators[i]):
68 operators[i] = os.path.abspath(os.getcwd() + '/' + operators[i])
Matteo Scandolo67654fa2017-06-09 09:33:17 -070069
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080070 if op == GEN:
71 # convert output to absolute path
72 if args.output is not None and not os.path.isabs(args.output):
73 args.output = os.path.abspath(os.getcwd() + '/' + args.output)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070074
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080075 operator = operators[0]
76
77 # check if there's a line that starts with +++ in the target
78 # if so, then the output file names are left to the target to decide
79 # also, if dest-file or dest-extension are supplied, then an error is generated.
80 plusplusplus = reduce(lambda acc, line: True if line.startswith('+++') else acc, open(operator).read().splitlines(), False)
81
82 if plusplusplus and args.write_to_file != 'target':
83 parse.error('%s chooses the names of the files that it generates, you must set --write-to-file to "target"' % operator)
84
85 if args.write_to_file != 'single' and (args.dest_file):
86 parse.error('--dest-file requires --write-to-file to be set to "single"')
87
88 if args.write_to_file != 'model' and (args.dest_extension):
89 parse.error('--dest-extension requires --write-to-file to be set to "model"')
90
91 else:
92 if args.write_to_file or args.dest_extension:
93 parse.error('Checkers cannot write to files')
Matteo Scandolo67654fa2017-06-09 09:33:17 -070094
95 inputs = []
96
97 for fname in args.files:
98 if not os.path.isabs(fname):
99 inputs.append(os.path.abspath(os.getcwd() + '/' + fname))
100 else:
101 inputs.append(fname)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800102
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700103 args.files = inputs
104
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800105 if op==GEN:
106 generated = XOSProcessor.process(args, operators[0])
107 if not args.output and not args.write_to_file:
108 print generated
109 elif op==CHECK:
110 for o in operators:
111 verdict_str = XOSProcessor.process(args, o)
112 vlst = verdict_str.split('\n')
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700113
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800114 try:
115 verdict = next(v for v in vlst if v.strip())
116 status_code, status_string = verdict.split(' ', 1)
117 status_code = int(status_code)
118 except:
119 print "Checker %s returned mangled output" % o
120 exit(1)
121
122 if status_code != 200:
123 print '%s: %s - %s' % (o, status_code, status_string)
124 exit(1)
125 else:
126 print '%s: OK'%o