3 # Automatically parse all directories and create the module
4 # initialization function
16 """This class processes all source files and create the init module."""
18 def __init__(self, dirs):
20 self.ignore_dirs = { "CVS" : "CVS directory" }
21 self.allowed_suffixes = [ ".c" ]
22 self.reg_string = re.compile(r"BE_REGISTER_MODULE_CONSTRUCTOR\((\w+)\)")
24 self.constructors = []
26 self.prolog += "void be_init_modules(void)\n"
28 self.prolog += "\tstatic int run_once = 0;\n"
30 self.prolog += "\tif (run_once)\n"
31 self.prolog += "\t\treturn;\n"
32 self.prolog += "\trun_once = 1;\n"
35 def verbose(self, flag):
36 """enables/disables verbose mode"""
39 def process_dirs(self):
40 """process all directories and collect the data"""
42 def _visit(self, dirname, names):
43 """called for every directory entry"""
45 basename = os.path.basename(dirname)
46 # check if this directory must be ignored
47 if self.ignore_dirs.has_key(basename):
49 print "Ignoring", self.ignore_dirs[basename]
53 print "Parsing", dirname, "..."
56 path = dirname + "/" + name
57 if os.path.isfile(path):
58 self.process_file(path)
60 # code of process_dirs
62 os.path.walk(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_suffixes:
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, path))
87 def update_init_func(self, name):
88 """generate the output"""
92 for fkt,path in self.constructors:
93 s += "/* %s */\n" % path
94 s += "void %s(void);\n" % fkt
97 for fkt,path in self.constructors:
98 s += "\t%s();\n" % fkt
101 if os.access(name, os.F_OK):
112 print name, "updated."
114 print name, "unchanged."
117 """Prints the usage"""
118 print "usage: %s [options] outname directory+ " % progname
123 """the main function"""
130 opts, args = getopt.getopt(argv[1:], "?v", ["help", "verbose"])
131 except getopt.GetoptError, msg:
138 if option in ('-?', '--help'):
141 elif option in ('-v', '--verbose'):
151 processor = Processor(dirs)
152 processor.verbose(verbose)
153 # process the directory and update the requested file
154 processor.update_init_func(gen_name)
156 if __name__ == "__main__":