Eric S. Raymond | 1bb515b | 2001-03-18 11:27:58 +0000 | [diff] [blame^] | 1 | import re |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | # Write the config.c file |
| 5 | |
Guido van Rossum | efdf107 | 2000-07-28 10:34:48 +0000 | [diff] [blame] | 6 | never = ['marshal', '__main__', '__builtin__', 'sys', 'exceptions'] |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 8bd7382 | 1999-09-30 14:12:44 +0000 | [diff] [blame] | 8 | def makeconfig(infp, outfp, modules, with_ifdef=0): |
Eric S. Raymond | 1bb515b | 2001-03-18 11:27:58 +0000 | [diff] [blame^] | 9 | m1 = re.compile('-- ADDMODULE MARKER 1 --') |
| 10 | m2 = re.compile('-- ADDMODULE MARKER 2 --') |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 11 | while 1: |
| 12 | line = infp.readline() |
| 13 | if not line: break |
| 14 | outfp.write(line) |
| 15 | if m1 and m1.search(line) >= 0: |
| 16 | m1 = None |
| 17 | for mod in modules: |
| 18 | if mod in never: |
| 19 | continue |
Guido van Rossum | 8bd7382 | 1999-09-30 14:12:44 +0000 | [diff] [blame] | 20 | if with_ifdef: |
| 21 | outfp.write("#ifndef init%s\n"%mod) |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 22 | outfp.write('extern void init%s();\n' % mod) |
Guido van Rossum | 8bd7382 | 1999-09-30 14:12:44 +0000 | [diff] [blame] | 23 | if with_ifdef: |
| 24 | outfp.write("#endif\n") |
Guido van Rossum | 00ff433 | 1994-10-03 16:33:08 +0000 | [diff] [blame] | 25 | elif m2 and m2.search(line) >= 0: |
| 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 | |
| 40 | def test(): |
| 41 | import sys |
| 42 | if not sys.argv[3:]: |
| 43 | print 'usage: python makeconfig.py config.c.in outputfile', |
| 44 | print 'modulename ...' |
| 45 | sys.exit(2) |
| 46 | if sys.argv[1] == '-': |
| 47 | infp = sys.stdin |
| 48 | else: |
| 49 | infp = open(sys.argv[1]) |
| 50 | if sys.argv[2] == '-': |
| 51 | outfp = sys.stdout |
| 52 | else: |
| 53 | outfp = open(sys.argv[2], 'w') |
| 54 | makeconfig(infp, outfp, sys.argv[3:]) |
| 55 | if outfp != sys.stdout: |
| 56 | outfp.close() |
| 57 | if infp != sys.stdin: |
| 58 | infp.close() |
| 59 | |
| 60 | if __name__ == '__main__': |
| 61 | test() |