Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 2 | """This script generate openflow-packets.py which |
| 3 | creates Python class for each data structure in openflow.h. |
| 4 | |
| 5 | (C) Copyright Stanford University |
| 6 | Author ykk |
| 7 | Date December 2009 |
| 8 | """ |
| 9 | import sys |
Dan Talayco | 11e598b | 2010-03-09 21:46:19 -0800 | [diff] [blame] | 10 | #@todo Fix this include path mechanism |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 11 | sys.path.append('./bin') |
| 12 | sys.path.append('./pylib') |
| 13 | import getopt |
| 14 | import openflow |
| 15 | import time |
| 16 | import output |
| 17 | import of.pythonize |
| 18 | |
| 19 | def 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 |
| 30 | try: |
| 31 | opts, args = getopt.getopt(sys.argv[1:], "hi:t:", |
| 32 | ["help","input","template"]) |
| 33 | except getopt.GetoptError: |
| 34 | usage() |
| 35 | sys.exit(2) |
| 36 | |
| 37 | #Check there is only output file |
| 38 | if not (len(args) == 1): |
| 39 | usage() |
| 40 | sys.exit(2) |
| 41 | |
| 42 | #Parse options |
| 43 | ##Input |
| 44 | headerfile=None |
| 45 | ##Template file |
| 46 | templatefile=None |
| 47 | for 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 |
| 60 | ofmsg = openflow.messages(headerfile) |
| 61 | pynizer = of.pythonize.pythonizer(ofmsg) |
| 62 | |
| 63 | fileRef = open(args[0], "w") |
| 64 | for x in pynizer.pycode(templatefile): |
| 65 | fileRef.write(x+"\n") |
| 66 | fileRef.write("\n") |
| 67 | fileRef.close() |