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