-
Notifications
You must be signed in to change notification settings - Fork 130
/
loxigen.py
executable file
·178 lines (140 loc) · 5.82 KB
/
loxigen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
# Copyright 2013, Big Switch Networks, Inc.
#
# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
# the following special exception:
#
# LOXI Exception
#
# As a special exception to the terms of the EPL, you may distribute libraries
# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
# that copyright and licensing notices generated by LoxiGen are not altered or removed
# from the LoxiGen Libraries and the notice provided below is (i) included in
# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
# documentation for the LoxiGen Libraries, if distributed in binary form.
#
# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
#
# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
# a copy of the EPL at:
#
# http://www.eclipse.org/legal/epl-v10.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# EPL for the specific language governing permissions and limitations
# under the EPL.
"""
@brief
Process openflow header files to create language specific LOXI interfaces
First cut at simple python script for processing input files
Internal notes
An input file for each supported OpenFlow version is passed in
on the command line.
Expected input file format:
These will probably be collapsed into a python dict or something
The first line has the ofC version identifier and nothing else
The second line has the openflow wire protocol value and nothing else
The main content is struct elements for each OF recognized class.
These are taken from current versions of openflow.h but are modified
a bit. See Overview for more information.
Class canonical form: A list of entries, each of which is a
pair "type, name;". The exception is when type is the keyword
'list' in which the syntax is "list(type) name;".
From this, internal representations are generated: For each
version, a dict indexed by class name. One element (members) is
an array giving the member name and type. From this, wire offsets
can be calculated.
@fixme Clean up the lang module architecture. It should provide a
list of files that it wants to generate and maps to the filenames,
subdirectory names and generation functions. It should also be
defined as a class, probably with the constructor taking the
language target.
@fixme Clean up global data structures such as versions and of_g
structures. They should probably be a class or classes as well.
"""
from collections import OrderedDict, defaultdict
import copy
import glob
from optparse import OptionParser
import os
import re
import string
import sys
import cmdline
from loxi_globals import OFVersions
import loxi_globals
import loxi_utils.loxi_utils as loxi_utils
import pyparsing
import loxi_front_end.parser as parser
import loxi_front_end.frontend as frontend
import loxi_ir
from generic_utils import *
root_dir = os.path.dirname(os.path.realpath(__file__))
def process_input_file(filename):
"""
Process an input file
Does not modify global state.
@param filename The input filename
@returns An OFInput object
"""
# Parse the input file
try:
with open(filename, encoding='utf-8') as f:
ast = parser.parse(f.read())
except pyparsing.ParseBaseException as e:
print("Parse error in %s: %s" % (os.path.basename(filename), str(e)))
sys.exit(1)
# Create the OFInput from the AST
try:
ofinput = frontend.create_ofinput(os.path.basename(filename), ast)
except frontend.InputError as e:
print("Error in %s: %s" % (os.path.basename(filename), str(e)))
sys.exit(1)
return ofinput
def read_input():
"""
Read in from files given on command line and update global state
@fixme Should select versions to support from command line
"""
ofinputs_by_version = defaultdict(lambda: [])
filenames = sorted(glob.glob("%s/openflow_input/*" % root_dir))
# Ignore emacs backup files
filenames = [x for x in filenames if not x.endswith('~')]
# Read input files
for filename in filenames:
log("Processing struct file: " + filename)
ofinput = process_input_file(filename)
for wire_version in ofinput.wire_versions:
version = loxi_globals.OFVersions.from_wire(wire_version)
if version in loxi_globals.OFVersions.target_versions:
ofinputs_by_version[wire_version].append(ofinput)
return ofinputs_by_version
def build_ir(ofinputs_by_version):
classes = []
enums = []
for wire_version, ofinputs in ofinputs_by_version.items():
version = OFVersions.from_wire(wire_version)
ofprotocol = loxi_ir.build_protocol(version, ofinputs)
loxi_globals.ir[version] = ofprotocol
loxi_globals.unified = loxi_ir.build_unified_ir(loxi_globals.ir)
# uncomment to dump the internal represenation for a given version
#print(loxi_globals.ir[OFVersions.VERSION_1_4])
################################################################
#
# Debug
#
################################################################
if __name__ == '__main__':
(options, args, target_versions) = cmdline.process_commandline()
# @fixme Use command line params to select log
logging.basicConfig(level = logging.INFO if not options.verbose else logging.DEBUG)
# Import the language file
lang_file = "lang_%s" % options.lang
lang_module = __import__(lang_file)
log("\nGenerating files for target language %s\n" % options.lang)
loxi_globals.OFVersions.target_versions = target_versions
inputs = read_input()
build_ir(inputs)
lang_module.generate(options.install_dir)