blob: 97b7c8ffe014f8d98100b49699adb28de6333c3f [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001#!/usr/bin/env python2.5
2"""This script generate openflow-packets.py which
3creates Python class for each data structure in openflow.h.
4
5(C) Copyright Stanford University
6Author ykk
7Date December 2009
8"""
9import sys
Dan Talayco11e598b2010-03-09 21:46:19 -080010#@todo Fix this include path mechanism
Dan Talaycof75360a2010-02-05 22:22:54 -080011sys.path.append('./bin')
12sys.path.append('./pylib')
13import getopt
14import openflow
15import time
16import output
17import of.pythonize
18
19def usage():
20 """Display usage
21 """
22 print "Usage "+sys.argv[0]+" <options> output_file\n"+\
23 "Options:\n"+\
24 "-i/--input\n\tSpecify (non-default) OpenFlow header\n"+\
25 "-t/--template\n\tSpecify (non-default) template file\n"+\
26 "-h/--help\n\tPrint this usage guide\n"+\
27 ""
28
29#Parse options and arguments
30try:
31 opts, args = getopt.getopt(sys.argv[1:], "hi:t:",
32 ["help","input","template"])
33except getopt.GetoptError:
34 usage()
35 sys.exit(2)
36
37#Check there is only output file
38if not (len(args) == 1):
39 usage()
40 sys.exit(2)
41
42#Parse options
43##Input
44headerfile=None
45##Template file
46templatefile=None
47for opt,arg in opts:
48 if (opt in ("-h","--help")):
49 usage()
50 sys.exit(0)
51 elif (opt in ("-i","--input")):
52 headerfile=arg
53 elif (opt in ("-t","--template")):
54 templatefile=arg
55 else:
56 print "Unhandled option:"+opt
57 sys.exit(2)
58
59#Generate Python code
60ofmsg = openflow.messages(headerfile)
61pynizer = of.pythonize.pythonizer(ofmsg)
62
63fileRef = open(args[0], "w")
64for x in pynizer.pycode(templatefile):
65 fileRef.write(x+"\n")
66fileRef.write("\n")
67fileRef.close()