blob: ec1ba685514770c198939229acea803b62e07c22 [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__)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070030
31group = parse.add_mutually_exclusive_group()
32group.add_argument('--dest-file', dest='dest_file', action='store',default=None, help='Output file name (if write-to-file is set to single)')
33group.add_argument('--dest-extension', dest='dest_extension', action='store',default=None, help='Output file extension (if write-to-file is set to single)')
34
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080035group = parse.add_mutually_exclusive_group(required=True)
36group.add_argument('--target', dest='target', action='store',default=None, help='Output format, corresponding to <output>.yaml file')
37group.add_argument('--checkers', dest='checkers', action='store', default=None, help='Comma-separated list of static checkers')
38
Matteo Scandolo67654fa2017-06-09 09:33:17 -070039parse.add_argument('files', metavar='<input file>', nargs='+', action='store', help='xproto files to compile')
40
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080041CHECK = 1
42GEN = 2
43
Matteo Scandolo67654fa2017-06-09 09:33:17 -070044class XosGen:
45
46 @staticmethod
47 def init(args=None):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070048 if not args:
49 args = parse.parse_args()
50
51 args.quiet = False
52
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080053 if args.target:
54 op = GEN
55 subdir = '/targets/'
56 elif args.checkers:
57 op = CHECK
58 subdir = '/checkers/'
59 else:
60 parse.error("At least one of --target and --checkers is required")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070061
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080062 operators = args.checkers.split(',') if hasattr(args, 'checkers') and args.checkers else [args.target]
Matteo Scandolo67654fa2017-06-09 09:33:17 -070063
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080064 for i in xrange(len(operators)):
65 if not '/' in operators[i]:
66 # if the target is not a path, it refer to a library included one
67 operators[i] = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + subdir + operators[i])
Matteo Scandolo67654fa2017-06-09 09:33:17 -070068
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080069 if not os.path.isabs(operators[i]):
70 operators[i] = os.path.abspath(os.getcwd() + '/' + operators[i])
Matteo Scandolo67654fa2017-06-09 09:33:17 -070071
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080072 if op == GEN:
73 # convert output to absolute path
74 if args.output is not None and not os.path.isabs(args.output):
75 args.output = os.path.abspath(os.getcwd() + '/' + args.output)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070076
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080077 operator = operators[0]
78
79 # check if there's a line that starts with +++ in the target
80 # if so, then the output file names are left to the target to decide
81 # also, if dest-file or dest-extension are supplied, then an error is generated.
82 plusplusplus = reduce(lambda acc, line: True if line.startswith('+++') else acc, open(operator).read().splitlines(), False)
83
84 if plusplusplus and args.write_to_file != 'target':
85 parse.error('%s chooses the names of the files that it generates, you must set --write-to-file to "target"' % operator)
86
87 if args.write_to_file != 'single' and (args.dest_file):
88 parse.error('--dest-file requires --write-to-file to be set to "single"')
89
90 if args.write_to_file != 'model' and (args.dest_extension):
91 parse.error('--dest-extension requires --write-to-file to be set to "model"')
92
93 else:
94 if args.write_to_file or args.dest_extension:
95 parse.error('Checkers cannot write to files')
Matteo Scandolo67654fa2017-06-09 09:33:17 -070096
97 inputs = []
98
99 for fname in args.files:
100 if not os.path.isabs(fname):
101 inputs.append(os.path.abspath(os.getcwd() + '/' + fname))
102 else:
103 inputs.append(fname)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800104
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700105 args.files = inputs
106
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800107 if op==GEN:
108 generated = XOSProcessor.process(args, operators[0])
109 if not args.output and not args.write_to_file:
110 print generated
111 elif op==CHECK:
112 for o in operators:
113 verdict_str = XOSProcessor.process(args, o)
114 vlst = verdict_str.split('\n')
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700115
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800116 try:
117 verdict = next(v for v in vlst if v.strip())
118 status_code, status_string = verdict.split(' ', 1)
119 status_code = int(status_code)
120 except:
121 print "Checker %s returned mangled output" % o
122 exit(1)
123
124 if status_code != 200:
125 print '%s: %s - %s' % (o, status_code, status_string)
126 exit(1)
127 else:
128 print '%s: OK'%o