blob: 8259d5812bd3e257ef4afe2f034e949d08595e98 [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')
24parse.add_argument('--rev', dest='rev', action='store_true',default=False, help='Convert proto to xproto')
Matteo Scandolo67654fa2017-06-09 09:33:17 -070025parse.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)')
Scott Bakerd0d35662018-03-26 09:58:07 -070029parse.add_argument('--version', action='version', version=__version__)
Scott Bakerc237f882018-09-28 14:12:47 -070030parse.add_argument("-v", "--verbosity", action="count", default=0, help="increase output verbosity")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070031
32group = parse.add_mutually_exclusive_group()
33group.add_argument('--dest-file', dest='dest_file', action='store',default=None, help='Output file name (if write-to-file is set to single)')
34group.add_argument('--dest-extension', dest='dest_extension', action='store',default=None, help='Output file extension (if write-to-file is set to single)')
35
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080036group = parse.add_mutually_exclusive_group(required=True)
37group.add_argument('--target', dest='target', action='store',default=None, help='Output format, corresponding to <output>.yaml file')
38group.add_argument('--checkers', dest='checkers', action='store', default=None, help='Comma-separated list of static checkers')
39
Matteo Scandolo67654fa2017-06-09 09:33:17 -070040parse.add_argument('files', metavar='<input file>', nargs='+', action='store', help='xproto files to compile')
41
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080042CHECK = 1
43GEN = 2
44
Matteo Scandolo67654fa2017-06-09 09:33:17 -070045class XosGen:
46
47 @staticmethod
48 def init(args=None):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070049 if not args:
50 args = parse.parse_args()
51
52 args.quiet = False
53
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080054 if args.target:
55 op = GEN
56 subdir = '/targets/'
57 elif args.checkers:
58 op = CHECK
59 subdir = '/checkers/'
60 else:
61 parse.error("At least one of --target and --checkers is required")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070062
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080063 operators = args.checkers.split(',') if hasattr(args, 'checkers') and args.checkers else [args.target]
Matteo Scandolo67654fa2017-06-09 09:33:17 -070064
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080065 for i in xrange(len(operators)):
66 if not '/' in operators[i]:
67 # if the target is not a path, it refer to a library included one
68 operators[i] = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + subdir + operators[i])
Matteo Scandolo67654fa2017-06-09 09:33:17 -070069
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080070 if not os.path.isabs(operators[i]):
71 operators[i] = os.path.abspath(os.getcwd() + '/' + operators[i])
Matteo Scandolo67654fa2017-06-09 09:33:17 -070072
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080073 if op == GEN:
74 # convert output to absolute path
75 if args.output is not None and not os.path.isabs(args.output):
76 args.output = os.path.abspath(os.getcwd() + '/' + args.output)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070077
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080078 operator = operators[0]
79
80 # check if there's a line that starts with +++ in the target
81 # if so, then the output file names are left to the target to decide
82 # also, if dest-file or dest-extension are supplied, then an error is generated.
83 plusplusplus = reduce(lambda acc, line: True if line.startswith('+++') else acc, open(operator).read().splitlines(), False)
84
85 if plusplusplus and args.write_to_file != 'target':
86 parse.error('%s chooses the names of the files that it generates, you must set --write-to-file to "target"' % operator)
87
88 if args.write_to_file != 'single' and (args.dest_file):
89 parse.error('--dest-file requires --write-to-file to be set to "single"')
90
91 if args.write_to_file != 'model' and (args.dest_extension):
92 parse.error('--dest-extension requires --write-to-file to be set to "model"')
93
94 else:
95 if args.write_to_file or args.dest_extension:
96 parse.error('Checkers cannot write to files')
Matteo Scandolo67654fa2017-06-09 09:33:17 -070097
98 inputs = []
99
100 for fname in args.files:
101 if not os.path.isabs(fname):
102 inputs.append(os.path.abspath(os.getcwd() + '/' + fname))
103 else:
104 inputs.append(fname)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800105
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700106 args.files = inputs
107
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800108 if op==GEN:
109 generated = XOSProcessor.process(args, operators[0])
110 if not args.output and not args.write_to_file:
111 print generated
112 elif op==CHECK:
113 for o in operators:
114 verdict_str = XOSProcessor.process(args, o)
115 vlst = verdict_str.split('\n')
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700116
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800117 try:
118 verdict = next(v for v in vlst if v.strip())
119 status_code, status_string = verdict.split(' ', 1)
120 status_code = int(status_code)
121 except:
122 print "Checker %s returned mangled output" % o
123 exit(1)
124
125 if status_code != 200:
126 print '%s: %s - %s' % (o, status_code, status_string)
127 exit(1)
128 else:
129 print '%s: OK'%o