blob: 466c35d0ad7809f9c1dba2d5863f10b394ca8de8 [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
10sys.path.append('./bin')
11sys.path.append('./pylib')
12import getopt
13import openflow
14import time
15import output
16import of.pythonize
17
18def usage():
19 """Display usage
20 """
21 print "Usage "+sys.argv[0]+" <options> output_file\n"+\
22 "Options:\n"+\
23 "-i/--input\n\tSpecify (non-default) OpenFlow header\n"+\
24 "-t/--template\n\tSpecify (non-default) template file\n"+\
25 "-h/--help\n\tPrint this usage guide\n"+\
26 ""
27
28#Parse options and arguments
29try:
30 opts, args = getopt.getopt(sys.argv[1:], "hi:t:",
31 ["help","input","template"])
32except getopt.GetoptError:
33 usage()
34 sys.exit(2)
35
36#Check there is only output file
37if not (len(args) == 1):
38 usage()
39 sys.exit(2)
40
41#Parse options
42##Input
43headerfile=None
44##Template file
45templatefile=None
46for opt,arg in opts:
47 if (opt in ("-h","--help")):
48 usage()
49 sys.exit(0)
50 elif (opt in ("-i","--input")):
51 headerfile=arg
52 elif (opt in ("-t","--template")):
53 templatefile=arg
54 else:
55 print "Unhandled option:"+opt
56 sys.exit(2)
57
58#Generate Python code
59ofmsg = openflow.messages(headerfile)
60pynizer = of.pythonize.pythonizer(ofmsg)
61
62fileRef = open(args[0], "w")
63for x in pynizer.pycode(templatefile):
64 fileRef.write(x+"\n")
65fileRef.write("\n")
66fileRef.close()