3 # Automatically parse all directories and create the module
4 # initialization function
18 """This class processes all source files and create the init module."""
20 def __init__(self, dir):
22 self.ignore_dirs = { "CVS" : "CVS directory" }
23 self.allowed_sfx = [ ".c", ".h" ]
24 self.reg_string = re.compile(r"BE_REGISTER_MODULE_CONSTRUCTOR\((\w+)\)")
26 self.constructors = []
28 self.prolog += "void be_init_modules(void)\n"
30 self.prolog += "\tstatic int run_once = 0;\n"
32 self.prolog += "\tif (run_once)\n"
33 self.prolog += "\t\treturn;\n"
34 self.prolog += "\trun_once = 1;\n"
37 def verbose(self, flag):
38 """enables/disables verbose mode"""
41 def process_dirs(self):
42 """process all directories and collect the data"""
44 def _visit(self, dirname, names):
45 """called for every directory entry"""
47 basename = os.path.basename(dirname)
48 # check if this directory must be ignored
49 if self.ignore_dirs.has_key(basename):
51 print "Ignoring", self.ignore_dirs[basename]
55 print "Parsing", dirname, "..."
58 path = dirname + "/" + name
59 if os.path.isfile(path):
60 self.process_file(path)
62 os.path.walk(self.dir, _visit, self)
64 def process_file(self, path):
66 # check, if it has an allowed suffix
67 index = path.rfind(".")
71 if suffix in self.allowed_sfx:
72 # we found an allowed suffix
73 self.process_src(path)
75 def process_src(self, path):
76 """process a source file"""
78 for line in f.readlines():
79 m = self.reg_string.search(line)
83 print " Found constructor", fkt
84 self.constructors.append(fkt)
87 def gen_output(self, name):
88 """generate the output"""
92 for fkt in self.constructors:
93 s += "void %s(void);\n" % fkt
96 for fkt in self.constructors:
97 s += "\t%s();\n" % fkt
100 if os.access(name, os.F_OK):
111 print name, "updated."
113 print name, "unchanged."
116 """Prints the usage"""
117 print "usage: %s [options] directory outname" % progname
122 """the main function"""
129 opts, args = getopt.getopt(argv[1:], "?v", ["help", "verbose"])
130 except getopt.GetoptError, msg:
137 if option in ('-?', '--help'):
140 elif option in ('-v', '--verbose'):
150 processor = Processor(dir)
151 processor.verbose(verbose)
152 processor.gen_output(gen_name)
154 if __name__ == "__main__":