Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python2.5 |
| 2 | """This script reads struct from OpenFlow header file and output query |
| 3 | |
| 4 | (C) Copyright Stanford University |
| 5 | Author ykk |
| 6 | Date October 2009 |
| 7 | """ |
| 8 | import sys |
| 9 | import getopt |
| 10 | import openflow |
| 11 | |
| 12 | def usage(): |
| 13 | """Display usage |
| 14 | """ |
| 15 | print "Usage "+sys.argv[0]+" <options> struct_name\n"+\ |
| 16 | "Options:\n"+\ |
| 17 | "-h/--help\n\tPrint this usage guide\n"+\ |
| 18 | "-c/--cstruct\n\tPrint C struct\n"+\ |
| 19 | "-n/--name\n\tPrint names of struct\n"+\ |
| 20 | "-s/--size\n\tPrint size of struct\n"+\ |
| 21 | "" |
| 22 | |
| 23 | #Parse options and arguments |
| 24 | try: |
| 25 | opts, args = getopt.getopt(sys.argv[1:], "hcsn", |
| 26 | ["help","cstruct","size","names"]) |
| 27 | except getopt.GetoptError: |
| 28 | usage() |
| 29 | sys.exit(2) |
| 30 | |
| 31 | #Check there is only struct name |
| 32 | if not (len(args) == 1): |
| 33 | usage() |
| 34 | sys.exit(2) |
| 35 | |
| 36 | #Parse options |
| 37 | ##Print C struct |
| 38 | printc = False |
| 39 | ##Print names |
| 40 | printname = False |
| 41 | ##Print size |
| 42 | printsize = False |
| 43 | for opt,arg in opts: |
| 44 | if (opt in ("-h","--help")): |
| 45 | usage() |
| 46 | sys.exit(0) |
| 47 | elif (opt in ("-s","--size")): |
| 48 | printsize = True |
| 49 | elif (opt in ("-c","--cstruct")): |
| 50 | printc = True |
| 51 | elif (opt in ("-n","--names")): |
| 52 | printname = True |
| 53 | else: |
| 54 | assert (False,"Unhandled option :"+opt) |
| 55 | |
| 56 | pyopenflow = openflow.messages() |
| 57 | cstruct = pyopenflow.structs[args[0].strip()] |
| 58 | pattern = pyopenflow.get_pattern(cstruct) |
| 59 | |
| 60 | #Print C struct |
| 61 | if (printc): |
| 62 | print cstruct |
| 63 | |
| 64 | #Print pattern |
| 65 | print "Python pattern = "+str(pattern) |
| 66 | |
| 67 | #Print name |
| 68 | if (printname): |
| 69 | print cstruct.get_names() |
| 70 | |
| 71 | #Print size |
| 72 | if (printsize): |
| 73 | print "Size = "+str(pyopenflow.get_size(pattern)) |
| 74 | |