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 class files for messenger and lavi in NOX, |
| 3 | specifically it creates a Python class for each data structure. |
| 4 | |
| 5 | (C) Copyright Stanford University |
| 6 | Author ykk |
| 7 | Date January 2010 |
| 8 | """ |
| 9 | import sys |
| 10 | import os.path |
| 11 | import getopt |
| 12 | import cheader |
| 13 | import lavi.pythonize |
| 14 | |
| 15 | def usage(): |
| 16 | """Display usage |
| 17 | """ |
| 18 | print "Usage "+sys.argv[0]+" <options> nox_dir\n"+\ |
| 19 | "Options:\n"+\ |
| 20 | "-i/--input-dir\n\tSpecify input directory (nox src directory)\n"+\ |
| 21 | "-t/--template\n\tSpecify (non-default) template file\n"+\ |
| 22 | "-n/--no-lavi\n\tSpecify that LAVI's file will not be created\n"+\ |
| 23 | "-h/--help\n\tPrint this usage guide\n"+\ |
| 24 | "" |
| 25 | |
| 26 | #Parse options and arguments |
| 27 | try: |
| 28 | opts, args = getopt.getopt(sys.argv[1:], "hm:n", |
| 29 | ["help","messenger-template","no-lavi"]) |
| 30 | except getopt.GetoptError: |
| 31 | usage() |
| 32 | sys.exit(2) |
| 33 | |
| 34 | #Check there is only NOX directory given |
| 35 | if not (len(args) == 1): |
| 36 | usage() |
| 37 | sys.exit(2) |
| 38 | |
| 39 | #Parse options |
| 40 | ##Output LAVI |
| 41 | outputlavi=True |
| 42 | ##Template file |
| 43 | templatefile="include/messenger.template.py" |
| 44 | for opt,arg in opts: |
| 45 | if (opt in ("-h","--help")): |
| 46 | usage() |
| 47 | sys.exit(0) |
| 48 | elif (opt in ("-t","--template")): |
| 49 | templatefile=arg |
| 50 | elif (opt in ("-n","--no-lavi")): |
| 51 | outputlavi=False |
| 52 | else: |
| 53 | print "Unhandled option:"+opt |
| 54 | sys.exit(2) |
| 55 | |
| 56 | #Check for header file in NOX directory |
| 57 | if not (os.path.isfile(args[0]+"/src/nox/coreapps/messenger/message.hh")): |
| 58 | print "Messenger header file not found!" |
| 59 | sys.exit(2) |
| 60 | if (outputlavi): |
| 61 | if not (os.path.isfile(args[0]+"/src/nox/netapps/lavi/lavi-message.hh")): |
| 62 | print "LAVI message header file not found!" |
| 63 | sys.exit(2) |
| 64 | |
| 65 | #Get headerfile and pythonizer |
| 66 | msgheader = cheader.cheaderfile(args[0]+"/src/nox/coreapps/messenger/message.hh") |
| 67 | mpynizer = lavi.pythonize.msgpythonizer(msgheader) |
| 68 | if (outputlavi): |
| 69 | laviheader = cheader.cheaderfile([args[0]+"/src/nox/coreapps/messenger/message.hh", |
| 70 | args[0]+"/src/nox/netapps/lavi/lavi-message.hh"]) |
| 71 | lpynizer = lavi.pythonize.lavipythonizer(laviheader) |
| 72 | |
| 73 | #Generate Python code for messenger |
| 74 | fileRef = open(args[0]+"/src/nox/coreapps/messenger/messenger.py", "w") |
| 75 | for x in mpynizer.pycode(templatefile): |
| 76 | fileRef.write(x+"\n") |
| 77 | fileRef.write("\n") |
| 78 | fileRef.close() |
| 79 | |
| 80 | if (outputlavi): |
| 81 | fileRef = open(args[0]+"/src/nox/netapps/lavi/lavi.py", "w") |
| 82 | for x in lpynizer.pycode(templatefile): |
| 83 | fileRef.write(x.replace("def __init__(self,ipAddr,portNo=2603,debug=False):", |
| 84 | "def __init__(self,ipAddr,portNo=2503,debug=False):").\ |
| 85 | replace("def __init__(self, ipAddr, portNo=1304,debug=False):", |
| 86 | "def __init__(self, ipAddr, portNo=1305,debug=False):")+\ |
| 87 | "\n") |
| 88 | fileRef.write("\n") |
| 89 | fileRef.close() |