Bump to 2.3.1 to pick up the missing file.
[python/dscho.git] / Tools / scripts / h2py.py
blob393a4adc09840b64b8dfef91bd4cbe98fd8048a3
1 #! /usr/bin/env python
3 # Read #define's and translate to Python code.
4 # Handle #include statements.
5 # Handle #define macros with one argument.
6 # Anything that isn't recognized or doesn't translate into valid
7 # Python is ignored.
9 # Without filename arguments, acts as a filter.
10 # If one or more filenames are given, output is written to corresponding
11 # filenames in the local directory, translated to all uppercase, with
12 # the extension replaced by ".py".
14 # By passing one or more options of the form "-i regular_expression"
15 # you can specify additional strings to be ignored. This is useful
16 # e.g. to ignore casts to u_long: simply specify "-i '(u_long)'".
18 # XXX To do:
19 # - turn trailing C comments into Python comments
20 # - turn C Boolean operators "&& || !" into Python "and or not"
21 # - what to do about #if(def)?
22 # - what to do about macros with multiple parameters?
24 import sys, re, getopt, os
26 p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
28 p_macro = re.compile(
29 '^[\t ]*#[\t ]*define[\t ]+'
30 '([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')
32 p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
34 p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')
35 p_cpp_comment = re.compile('//.*')
37 ignores = [p_comment, p_cpp_comment]
39 p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
41 filedict = {}
42 importable = {}
44 try:
45 searchdirs=os.environ['include'].split(';')
46 except KeyError:
47 try:
48 searchdirs=os.environ['INCLUDE'].split(';')
49 except KeyError:
50 try:
51 if sys.platform.find("beos") == 0:
52 searchdirs=os.environ['BEINCLUDES'].split(';')
53 elif sys.platform.startswith("atheos"):
54 searchdirs=os.environ['C_INCLUDE_PATH'].split(':')
55 else:
56 raise KeyError
57 except KeyError:
58 searchdirs=['/usr/include']
60 def main():
61 global filedict
62 opts, args = getopt.getopt(sys.argv[1:], 'i:')
63 for o, a in opts:
64 if o == '-i':
65 ignores.append(re.compile(a))
66 if not args:
67 args = ['-']
68 for filename in args:
69 if filename == '-':
70 sys.stdout.write('# Generated by h2py from stdin\n')
71 process(sys.stdin, sys.stdout)
72 else:
73 fp = open(filename, 'r')
74 outfile = os.path.basename(filename)
75 i = outfile.rfind('.')
76 if i > 0: outfile = outfile[:i]
77 modname = outfile.upper()
78 outfile = modname + '.py'
79 outfp = open(outfile, 'w')
80 outfp.write('# Generated by h2py from %s\n' % filename)
81 filedict = {}
82 for dir in searchdirs:
83 if filename[:len(dir)] == dir:
84 filedict[filename[len(dir)+1:]] = None # no '/' trailing
85 importable[filename[len(dir)+1:]] = modname
86 break
87 process(fp, outfp)
88 outfp.close()
89 fp.close()
91 def process(fp, outfp, env = {}):
92 lineno = 0
93 while 1:
94 line = fp.readline()
95 if not line: break
96 lineno = lineno + 1
97 match = p_define.match(line)
98 if match:
99 # gobble up continuation lines
100 while line[-2:] == '\\\n':
101 nextline = fp.readline()
102 if not nextline: break
103 lineno = lineno + 1
104 line = line + nextline
105 name = match.group(1)
106 body = line[match.end():]
107 # replace ignored patterns by spaces
108 for p in ignores:
109 body = p.sub(' ', body)
110 # replace char literals by ord(...)
111 body = p_char.sub('ord(\\0)', body)
112 stmt = '%s = %s\n' % (name, body.strip())
113 ok = 0
114 try:
115 exec stmt in env
116 except:
117 sys.stderr.write('Skipping: %s' % stmt)
118 else:
119 outfp.write(stmt)
120 match = p_macro.match(line)
121 if match:
122 macro, arg = match.group(1, 2)
123 body = line[match.end():]
124 for p in ignores:
125 body = p.sub(' ', body)
126 body = p_char.sub('ord(\\0)', body)
127 stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
128 try:
129 exec stmt in env
130 except:
131 sys.stderr.write('Skipping: %s' % stmt)
132 else:
133 outfp.write(stmt)
134 match = p_include.match(line)
135 if match:
136 regs = match.regs
137 a, b = regs[1]
138 filename = line[a:b]
139 if importable.has_key(filename):
140 outfp.write('from %s import *\n' % importable[filename])
141 elif not filedict.has_key(filename):
142 filedict[filename] = None
143 inclfp = None
144 for dir in searchdirs:
145 try:
146 inclfp = open(dir + '/' + filename)
147 break
148 except IOError:
149 pass
150 if inclfp:
151 outfp.write(
152 '\n# Included from %s\n' % filename)
153 process(inclfp, outfp, env)
154 else:
155 sys.stderr.write('Warning - could not find file %s\n' %
156 filename)
158 main()