History

gen_external.py : pepify

Martin Hermant committed 9 years ago

Commit 541ea281 parent 6cbf34b1 file · +75 −65Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/lib/gen_external.py+75−65
@@ -1,5 +1,8 @@
1import distutils.ccompiler1import distutils.ccompiler
2import sys, os, subprocess, glob2import sys
3import os
4import subprocess
5import glob
3 6
4header = os.path.join('src', 'aubio.h')7header = os.path.join('src', 'aubio.h')
5output_path = os.path.join('python', 'gen')8output_path = os.path.join('python', 'gen')
@@ -9,38 +12,39 @@ source_header = """// this file is generated! do not modify · 3 unmodified lines
9"""12"""
10 13
11skip_objects = [14skip_objects = [
12 # already in ext/15 # already in ext/
13 'fft',16 'fft',
14 'pvoc',17 'pvoc',
15 'filter',18 'filter',
16 'filterbank',19 'filterbank',
17 # AUBIO_UNSTABLE20 # AUBIO_UNSTABLE
18 'hist',21 'hist',
19 'parameter',22 'parameter',
20 'scale',23 'scale',
21 'beattracking',24 'beattracking',
22 'resampler',25 'resampler',
23 'peakpicker',26 'peakpicker',
24 'pitchfcomb',27 'pitchfcomb',
25 'pitchmcomb',28 'pitchmcomb',
26 'pitchschmitt',29 'pitchschmitt',
27 'pitchspecacf',30 'pitchspecacf',
28 'pitchyin',31 'pitchyin',
29 'pitchyinfft',32 'pitchyinfft',
30 'sink',33 'sink',
31 'sink_apple_audio',34 'sink_apple_audio',
32 'sink_sndfile',35 'sink_sndfile',
33 'sink_wavwrite',36 'sink_wavwrite',
34 #'mfcc',37 #'mfcc',
35 'source',38 'source',
36 'source_apple_audio',39 'source_apple_audio',
37 'source_sndfile',40 'source_sndfile',
38 'source_avcodec',41 'source_avcodec',
39 'source_wavread',42 'source_wavread',
40 #'sampler',43 #'sampler',
41 'audio_unit',44 'audio_unit',
42 'spectral_whitening',45 'spectral_whitening',
43 ]46]
47
44 48
45def get_preprocessor():49def get_preprocessor():
46 # findout which compiler to use50 # findout which compiler to use
@@ -59,12 +63,12 @@ def get_preprocessor(): · 12 unmodified lines
59 print("Warning: failed initializing compiler ({:s})".format(repr(e)))63 print("Warning: failed initializing compiler ({:s})".format(repr(e)))
60 64
61 cpp_cmd = None65 cpp_cmd = None
62 if hasattr(compiler, 'preprocessor'): # for unixccompiler66 if hasattr(compiler, 'preprocessor'): # for unixccompiler
63 cpp_cmd = compiler.preprocessor67 cpp_cmd = compiler.preprocessor
64 elif hasattr(compiler, 'compiler'): # for ccompiler68 elif hasattr(compiler, 'compiler'): # for ccompiler
65 cpp_cmd = compiler.compiler.split()69 cpp_cmd = compiler.compiler.split()
66 cpp_cmd += ['-E']70 cpp_cmd += ['-E']
67 elif hasattr(compiler, 'cc'): # for msvccompiler71 elif hasattr(compiler, 'cc'): # for msvccompiler
68 cpp_cmd = compiler.cc.split()72 cpp_cmd = compiler.cc.split()
69 cpp_cmd += ['-E']73 cpp_cmd += ['-E']
70 74
@@ -94,15 +98,15 @@ def get_c_declarations(header=header, usedouble=False): · 23 unmodified lines
94 98
95 print("Running command: {:s}".format(" ".join(cpp_cmd)))99 print("Running command: {:s}".format(" ".join(cpp_cmd)))
96 proc = subprocess.Popen(cpp_cmd,100 proc = subprocess.Popen(cpp_cmd,
97 stderr=subprocess.PIPE,101 stderr=subprocess.PIPE,
98 stdout=subprocess.PIPE)102 stdout=subprocess.PIPE)
99 assert proc, 'Proc was none'103 assert proc, 'Proc was none'
100 cpp_output = proc.stdout.read()104 cpp_output = proc.stdout.read()
101 err_output = proc.stderr.read()105 err_output = proc.stderr.read()
102 if not cpp_output:106 if not cpp_output:
103 raise Exception("preprocessor output is empty:\n%s" % err_output)107 raise Exception("preprocessor output is empty:\n%s" % err_output)
104 elif err_output:108 elif err_output:
105 print ("Warning: preprocessor produced warnings:\n%s" % err_output)109 print("Warning: preprocessor produced warnings:\n%s" % err_output)
106 if not isinstance(cpp_output, list):110 if not isinstance(cpp_output, list):
107 cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')]111 cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')]
108 112
@@ -111,10 +115,11 @@ def get_c_declarations(header=header, usedouble=False): · 2 unmodified lines
111 115
112 i = 1116 i = 1
113 while 1:117 while 1:
114 if i >= len(cpp_output):break118 if i >= len(cpp_output):
119 break
115 if ('{' in cpp_output[i - 1]) and (not '}' in cpp_output[i - 1]) or (not ';' in cpp_output[i - 1]):120 if ('{' in cpp_output[i - 1]) and (not '}' in cpp_output[i - 1]) or (not ';' in cpp_output[i - 1]):
116 cpp_output[i] = cpp_output[i-1] + ' ' + cpp_output[i]121 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
117 cpp_output.pop(i-1)122 cpp_output.pop(i - 1)
118 elif ('}' in cpp_output[i]):123 elif ('}' in cpp_output[i]):
119 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]124 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
120 cpp_output.pop(i - 1)125 cpp_output.pop(i - 1)
@@ -124,14 +129,14 @@ def get_c_declarations(header=header, usedouble=False): · 3 unmodified lines
124 # clean pointer notations129 # clean pointer notations
125 tmp = []130 tmp = []
126 for l in cpp_output:131 for l in cpp_output:
127 tmp+=[ l.replace(' *','* ')]132 tmp += [l.replace(' *', '* ')]
128 cpp_output = tmp;133 cpp_output = tmp
129
130 134
131 return cpp_output135 return cpp_output
132 136
137
133def get_cpp_objects_from_c_declarations(c_declarations):138def get_cpp_objects_from_c_declarations(c_declarations):
134 typedefs = filter(lambda y: y.startswith ('typedef struct _aubio'), c_declarations)139 typedefs = filter(lambda y: y.startswith('typedef struct _aubio'), c_declarations)
135 cpp_objects = [a.split()[3][:-1] for a in typedefs]140 cpp_objects = [a.split()[3][:-1] for a in typedefs]
136 return cpp_objects141 return cpp_objects
137 142
@@ -173,10 +178,10 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations): · 35 unmodified lines
173 if o[:6] == 'aubio_':178 if o[:6] == 'aubio_':
174 shortname = o[6:-2] # without aubio_179 shortname = o[6:-2] # without aubio_
175 longname = o[:-2] # without _t180 longname = o[:-2] # without _t
176 else: # support object not starting with aubio_ (fvec...)181 else: # support object not starting with aubio_ (fvec...)
177 shortname = o182 shortname = o
178 longname = shortname183 longname = shortname
179 184
180 if shortname in skip_objects:185 if shortname in skip_objects:
181 continue186 continue
182 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}187 lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
@@ -204,10 +209,11 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations): · 21 unmodified lines
204 elif '_set_' in fn:209 elif '_set_' in fn:
205 lib[shortname]['set'].append(fn)210 lib[shortname]['set'].append(fn)
206 else:211 else:
207 #print "no idea what to do about", fn212 # print "no idea what to do about", fn
208 lib[shortname]['other'].append(fn)213 lib[shortname]['other'].append(fn)
209 return lib214 return lib
210 215
216
211def print_c_declarations_results(lib, c_declarations):217def print_c_declarations_results(lib, c_declarations):
212 for fn in c_declarations:218 for fn in c_declarations:
213 found = 0219 found = 0
@@ -216,21 +222,23 @@ def print_c_declarations_results(lib, c_declarations): · 2 unmodified lines
216 if fn in lib[o][family]:222 if fn in lib[o][family]:
217 found = 1223 found = 1
218 if found == 0:224 if found == 0:
219 print ("missing", fn)225 print("missing", fn)
220 226
221 for o in lib:227 for o in lib:
222 for family in lib[o]:228 for family in lib[o]:
223 if type(lib[o][family]) == str:229 if type(lib[o][family]) == str:
224 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )230 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
225 elif len(lib[o][family]) == 1:231 elif len(lib[o][family]) == 1:
226 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )232 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family][0]))
227 else:233 else:
228 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )234 print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
229 235
230 236
231def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):237def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True):
232 if not os.path.isdir(output_path): os.mkdir(output_path)238 if not os.path.isdir(output_path):
233 elif not overwrite: return sorted(glob.glob(os.path.join(output_path, '*.c')))239 os.mkdir(output_path)
240 elif not overwrite:
241 return sorted(glob.glob(os.path.join(output_path, '*.c')))
234 242
235 c_declarations = get_c_declarations(header, usedouble=usedouble)243 c_declarations = get_c_declarations(header, usedouble=usedouble)
236 cpp_objects = get_cpp_objects_from_c_declarations(c_declarations)244 cpp_objects = get_cpp_objects_from_c_declarations(c_declarations)
@@ -245,12 +253,12 @@ def generate_external(header=header, output_path=output_path, usedouble=False, o · 8 unmodified lines
245 from gen_code import MappedObject253 from gen_code import MappedObject
246 for o in lib:254 for o in lib:
247 out = source_header255 out = source_header
248 mapped = MappedObject(lib[o], usedouble = usedouble)256 mapped = MappedObject(lib[o], usedouble=usedouble)
249 out += mapped.gen_code()257 out += mapped.gen_code()
250 output_file = os.path.join(output_path, 'gen-%s.c' % o)258 output_file = os.path.join(output_path, 'gen-%s.c' % o)
251 with open(output_file, 'w') as f:259 with open(output_file, 'w') as f:
252 f.write(out)260 f.write(out)
253 print ("wrote %s" % output_file )261 print("wrote %s" % output_file)
254 sources_list.append(output_file)262 sources_list.append(output_file)
255 263
256 out = source_header264 out = source_header
@@ -262,23 +270,23 @@ int generated_types_ready (void) · 5 unmodified lines
262{{270{{
263 return ({pycheck_types});271 return ({pycheck_types});
264}}272}}
265""".format(pycheck_types = check_types)273""".format(pycheck_types=check_types)
266 274
267 add_types = "".join(["""275 add_types = "".join(["""
268 Py_INCREF (&Py_{name}Type);276 Py_INCREF (&Py_{name}Type);
269 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name = o) for o in lib])277 PyModule_AddObject(m, "{name}", (PyObject *) & Py_{name}Type);""".format(name=o) for o in lib])
270 out += """278 out += """
271 279
272void add_generated_objects ( PyObject *m )280void add_generated_objects ( PyObject *m )
273{{281{{
274{add_types}282{add_types}
275}}283}}
276""".format(add_types = add_types)284""".format(add_types=add_types)
277 285
278 output_file = os.path.join(output_path, 'aubio-generated.c')286 output_file = os.path.join(output_path, 'aubio-generated.c')
279 with open(output_file, 'w') as f:287 with open(output_file, 'w') as f:
280 f.write(out)288 f.write(out)
281 print ("wrote %s" % output_file )289 print("wrote %s" % output_file)
282 sources_list.append(output_file)290 sources_list.append(output_file)
283 291
284 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])292 objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
@@ -296,17 +304,19 @@ void add_generated_objects ( PyObject *m ) · 11 unmodified lines
296{objlist}304{objlist}
297int generated_objects ( void );305int generated_objects ( void );
298void add_generated_objects( PyObject *m );306void add_generated_objects( PyObject *m );
299""".format(objlist = objlist)307""".format(objlist=objlist)
300 308
301 output_file = os.path.join(output_path, 'aubio-generated.h')309 output_file = os.path.join(output_path, 'aubio-generated.h')
302 with open(output_file, 'w') as f:310 with open(output_file, 'w') as f:
303 f.write(out)311 f.write(out)
304 print ("wrote %s" % output_file )312 print("wrote %s" % output_file)
305 # no need to add header to list of sources313 # no need to add header to list of sources
306 314
307 return sorted(sources_list)315 return sorted(sources_list)
308 316
309if __name__ == '__main__':317if __name__ == '__main__':
310 if len(sys.argv) > 1: header = sys.argv[1]318 if len(sys.argv) > 1:
311 if len(sys.argv) > 2: output_path = sys.argv[2]319 header = sys.argv[1]
320 if len(sys.argv) > 2:
321 output_path = sys.argv[2]
312 generate_external(header, output_path)322 generate_external(header, output_path)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t