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