1import re
2import sys
3
4# Write the config.c file
5
6never = ['marshal', '__main__', '__builtin__', 'sys', 'exceptions', '_warnings']
7
8def makeconfig(infp, outfp, modules, with_ifdef=0):
9    m1 = re.compile('-- ADDMODULE MARKER 1 --')
10    m2 = re.compile('-- ADDMODULE MARKER 2 --')
11    while 1:
12        line = infp.readline()
13        if not line: break
14        outfp.write(line)
15        if m1 and m1.search(line):
16            m1 = None
17            for mod in modules:
18                if mod in never:
19                    continue
20                if with_ifdef:
21                    outfp.write("#ifndef init%s\n"%mod)
22                outfp.write('extern void init%s(void);\n' % mod)
23                if with_ifdef:
24                    outfp.write("#endif\n")
25        elif m2 and m2.search(line):
26            m2 = None
27            for mod in modules:
28                if mod in never:
29                    continue
30                outfp.write('\t{"%s", init%s},\n' %
31                            (mod, mod))
32    if m1:
33        sys.stderr.write('MARKER 1 never found\n')
34    elif m2:
35        sys.stderr.write('MARKER 2 never found\n')
36
37
38# Test program.
39
40def test():
41    if not sys.argv[3:]:
42        print 'usage: python makeconfig.py config.c.in outputfile',
43        print 'modulename ...'
44        sys.exit(2)
45    if sys.argv[1] == '-':
46        infp = sys.stdin
47    else:
48        infp = open(sys.argv[1])
49    if sys.argv[2] == '-':
50        outfp = sys.stdout
51    else:
52        outfp = open(sys.argv[2], 'w')
53    makeconfig(infp, outfp, sys.argv[3:])
54    if outfp != sys.stdout:
55        outfp.close()
56    if infp != sys.stdin:
57        infp.close()
58
59if __name__ == '__main__':
60    test()
61