blob: 914a424ae331dc3d17c3b82d4f95569c4767c3d3 [file] [log] [blame]
Dan Talaycod2ca1032010-03-10 14:40:26 -08001#!/usr/bin/env python
Dan Talaycof75360a2010-02-05 22:22:54 -08002"""This script generate class files for messenger and lavi in NOX,
3specifically it creates a Python class for each data structure.
4
5(C) Copyright Stanford University
6Author ykk
7Date January 2010
8"""
9import sys
10import os.path
11import getopt
12import cheader
13import lavi.pythonize
14
15def 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
27try:
28 opts, args = getopt.getopt(sys.argv[1:], "hm:n",
29 ["help","messenger-template","no-lavi"])
30except getopt.GetoptError:
31 usage()
32 sys.exit(2)
33
34#Check there is only NOX directory given
35if not (len(args) == 1):
36 usage()
37 sys.exit(2)
38
39#Parse options
40##Output LAVI
41outputlavi=True
42##Template file
43templatefile="include/messenger.template.py"
44for 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
57if not (os.path.isfile(args[0]+"/src/nox/coreapps/messenger/message.hh")):
58 print "Messenger header file not found!"
59 sys.exit(2)
60if (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
66msgheader = cheader.cheaderfile(args[0]+"/src/nox/coreapps/messenger/message.hh")
67mpynizer = lavi.pythonize.msgpythonizer(msgheader)
68if (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
74fileRef = open(args[0]+"/src/nox/coreapps/messenger/messenger.py", "w")
75for x in mpynizer.pycode(templatefile):
76 fileRef.write(x+"\n")
77fileRef.write("\n")
78fileRef.close()
79
80if (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()