Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 0b7dfbb | 1994-01-07 11:43:11 +0000 | [diff] [blame] | 2 | |
| 3 | # Add some standard cpp magic to a header file |
| 4 | |
| 5 | import sys |
Guido van Rossum | 0b7dfbb | 1994-01-07 11:43:11 +0000 | [diff] [blame] | 6 | |
| 7 | def main(): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 8 | args = sys.argv[1:] |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 9 | for filename in args: |
| 10 | process(filename) |
Guido van Rossum | 0b7dfbb | 1994-01-07 11:43:11 +0000 | [diff] [blame] | 11 | |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 12 | def process(filename): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 13 | try: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 14 | f = open(filename, 'r') |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 15 | except IOError, msg: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 16 | sys.stderr.write('%s: can\'t open: %s\n' % (filename, str(msg))) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 17 | return |
| 18 | data = f.read() |
| 19 | f.close() |
| 20 | if data[:2] <> '/*': |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 21 | sys.stderr.write('%s does not begin with C comment\n' % filename) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 22 | return |
| 23 | try: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 24 | f = open(filename, 'w') |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 25 | except IOError, msg: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 26 | sys.stderr.write('%s: can\'t write: %s\n' % (filename, str(msg))) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 27 | return |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 28 | sys.stderr.write('Processing %s ...\n' % filename) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 29 | magic = 'Py_' |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 30 | for c in filename: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 31 | if ord(c)<=0x80 and c.isalnum(): |
| 32 | magic = magic + c.upper() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 33 | else: magic = magic + '_' |
| 34 | sys.stdout = f |
| 35 | print '#ifndef', magic |
| 36 | print '#define', magic |
| 37 | print '#ifdef __cplusplus' |
| 38 | print 'extern "C" {' |
| 39 | print '#endif' |
| 40 | print |
| 41 | f.write(data) |
| 42 | print |
| 43 | print '#ifdef __cplusplus' |
| 44 | print '}' |
| 45 | print '#endif' |
| 46 | print '#endif /*', '!'+magic, '*/' |
Guido van Rossum | 0b7dfbb | 1994-01-07 11:43:11 +0000 | [diff] [blame] | 47 | |
| 48 | main() |