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, dirs):
22 self.ignore_dirs = { "CVS" : "CVS directory" }
23 self.allowed_suffixes = [ ".c" ]
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 # code of process_dirs
64 os.path.walk(dir, _visit, self)
66 def process_file(self, path):
68 # check, if it has an allowed suffix
69 index = path.rfind(".")
73 if suffix in self.allowed_suffixes:
74 # we found an allowed suffix
75 self.process_src(path)
77 def process_src(self, path):
78 """process a source file"""
80 for line in f.readlines():
81 m = self.reg_string.search(line)
85 print " Found constructor", fkt
86 self.constructors.append((fkt, path))
89 def update_init_func(self, name):
90 """generate the output"""
94 for fkt,path in self.constructors:
95 s += "/* %s */\n" % path
96 s += "void %s(void);\n" % fkt
99 for fkt,path in self.constructors:
100 s += "\t%s();\n" % fkt
103 if os.access(name, os.F_OK):
114 print name, "updated."
116 print name, "unchanged."
119 """Prints the usage"""
120 print "usage: %s [options] outname directory+ " % progname
125 """the main function"""
132 opts, args = getopt.getopt(argv[1:], "?v", ["help", "verbose"])
133 except getopt.GetoptError, msg:
140 if option in ('-?', '--help'):
143 elif option in ('-v', '--verbose'):
153 processor = Processor(dirs)
154 processor.verbose(verbose)
155 # process the directory and update the requested file
156 processor.update_init_func(gen_name)
158 if __name__ == "__main__":