- Timestamp:
- May 30, 2017, 12:29:20 AM (8 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master
- Children:
- 40792b2
- Parents:
- 6cbf34b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/lib/gen_external.py
r6cbf34b r541ea280 1 1 import distutils.ccompiler 2 import sys, os, subprocess, glob 2 import sys 3 import os 4 import subprocess 5 import glob 3 6 4 7 header = os.path.join('src', 'aubio.h') … … 10 13 11 14 skip_objects = [ 12 # already in ext/ 13 'fft', 14 'pvoc', 15 'filter', 16 'filterbank', 17 # AUBIO_UNSTABLE 18 'hist', 19 'parameter', 20 'scale', 21 'beattracking', 22 'resampler', 23 'peakpicker', 24 'pitchfcomb', 25 'pitchmcomb', 26 'pitchschmitt', 27 'pitchspecacf', 28 'pitchyin', 29 'pitchyinfft', 30 'sink', 31 'sink_apple_audio', 32 'sink_sndfile', 33 'sink_wavwrite', 34 #'mfcc', 35 'source', 36 'source_apple_audio', 37 'source_sndfile', 38 'source_avcodec', 39 'source_wavread', 40 #'sampler', 41 'audio_unit', 42 'spectral_whitening', 43 ] 15 # already in ext/ 16 'fft', 17 'pvoc', 18 'filter', 19 'filterbank', 20 # AUBIO_UNSTABLE 21 'hist', 22 'parameter', 23 'scale', 24 'beattracking', 25 'resampler', 26 'peakpicker', 27 'pitchfcomb', 28 'pitchmcomb', 29 'pitchschmitt', 30 'pitchspecacf', 31 'pitchyin', 32 'pitchyinfft', 33 'sink', 34 'sink_apple_audio', 35 'sink_sndfile', 36 'sink_wavwrite', 37 #'mfcc', 38 'source', 39 'source_apple_audio', 40 'source_sndfile', 41 'source_avcodec', 42 'source_wavread', 43 #'sampler', 44 'audio_unit', 45 'spectral_whitening', 46 ] 47 44 48 45 49 def get_preprocessor(): … … 60 64 61 65 cpp_cmd = None 62 if hasattr(compiler, 'preprocessor'): # for unixccompiler66 if hasattr(compiler, 'preprocessor'): # for unixccompiler 63 67 cpp_cmd = compiler.preprocessor 64 elif hasattr(compiler, 'compiler'): # for ccompiler68 elif hasattr(compiler, 'compiler'): # for ccompiler 65 69 cpp_cmd = compiler.compiler.split() 66 70 cpp_cmd += ['-E'] 67 elif hasattr(compiler, 'cc'): # for msvccompiler71 elif hasattr(compiler, 'cc'): # for msvccompiler 68 72 cpp_cmd = compiler.cc.split() 69 73 cpp_cmd += ['-E'] … … 95 99 print("Running command: {:s}".format(" ".join(cpp_cmd))) 96 100 proc = subprocess.Popen(cpp_cmd, 97 stderr=subprocess.PIPE,98 stdout=subprocess.PIPE)101 stderr=subprocess.PIPE, 102 stdout=subprocess.PIPE) 99 103 assert proc, 'Proc was none' 100 104 cpp_output = proc.stdout.read() … … 103 107 raise Exception("preprocessor output is empty:\n%s" % err_output) 104 108 elif err_output: 105 print 109 print("Warning: preprocessor produced warnings:\n%s" % err_output) 106 110 if not isinstance(cpp_output, list): 107 111 cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')] … … 112 116 i = 1 113 117 while 1: 114 if i >= len(cpp_output):break 118 if i >= len(cpp_output): 119 break 115 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]117 cpp_output.pop(i -1)121 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i] 122 cpp_output.pop(i - 1) 118 123 elif ('}' in cpp_output[i]): 119 124 cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i] … … 125 130 tmp = [] 126 131 for l in cpp_output: 127 tmp+=[ l.replace(' *','* ')] 128 cpp_output = tmp; 129 132 tmp += [l.replace(' *', '* ')] 133 cpp_output = tmp 130 134 131 135 return cpp_output 132 136 137 133 138 def get_cpp_objects_from_c_declarations(c_declarations): 134 typedefs = filter(lambda y: y.startswith 139 typedefs = filter(lambda y: y.startswith('typedef struct _aubio'), c_declarations) 135 140 cpp_objects = [a.split()[3][:-1] for a in typedefs] 136 141 return cpp_objects … … 174 179 shortname = o[6:-2] # without aubio_ 175 180 longname = o[:-2] # without _t 176 else: # support object not starting with aubio_ (fvec...)181 else: # support object not starting with aubio_ (fvec...) 177 182 shortname = o 178 183 longname = shortname 179 184 180 185 if shortname in skip_objects: 181 186 continue … … 205 210 lib[shortname]['set'].append(fn) 206 211 else: 207 # print "no idea what to do about", fn212 # print "no idea what to do about", fn 208 213 lib[shortname]['other'].append(fn) 209 214 return lib 215 210 216 211 217 def print_c_declarations_results(lib, c_declarations): … … 217 223 found = 1 218 224 if found == 0: 219 print 225 print("missing", fn) 220 226 221 227 for o in lib: 222 228 for family in lib[o]: 223 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 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 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 231 237 def generate_external(header=header, output_path=output_path, usedouble=False, overwrite=True): 232 if not os.path.isdir(output_path): os.mkdir(output_path) 233 elif not overwrite: return sorted(glob.glob(os.path.join(output_path, '*.c'))) 238 if not os.path.isdir(output_path): 239 os.mkdir(output_path) 240 elif not overwrite: 241 return sorted(glob.glob(os.path.join(output_path, '*.c'))) 234 242 235 243 c_declarations = get_c_declarations(header, usedouble=usedouble) … … 246 254 for o in lib: 247 255 out = source_header 248 mapped = MappedObject(lib[o], usedouble =usedouble)256 mapped = MappedObject(lib[o], usedouble=usedouble) 249 257 out += mapped.gen_code() 250 258 output_file = os.path.join(output_path, 'gen-%s.c' % o) 251 259 with open(output_file, 'w') as f: 252 260 f.write(out) 253 print ("wrote %s" % output_file)261 print("wrote %s" % output_file) 254 262 sources_list.append(output_file) 255 263 … … 263 271 return ({pycheck_types}); 264 272 }} 265 """.format(pycheck_types =check_types)273 """.format(pycheck_types=check_types) 266 274 267 275 add_types = "".join([""" 268 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 278 out += """ 271 279 … … 274 282 {add_types} 275 283 }} 276 """.format(add_types =add_types)284 """.format(add_types=add_types) 277 285 278 286 output_file = os.path.join(output_path, 'aubio-generated.c') 279 287 with open(output_file, 'w') as f: 280 288 f.write(out) 281 print ("wrote %s" % output_file)289 print("wrote %s" % output_file) 282 290 sources_list.append(output_file) 283 291 … … 297 305 int generated_objects ( void ); 298 306 void add_generated_objects( PyObject *m ); 299 """.format(objlist =objlist)307 """.format(objlist=objlist) 300 308 301 309 output_file = os.path.join(output_path, 'aubio-generated.h') 302 310 with open(output_file, 'w') as f: 303 311 f.write(out) 304 print ("wrote %s" % output_file)312 print("wrote %s" % output_file) 305 313 # no need to add header to list of sources 306 314 … … 308 316 309 317 if __name__ == '__main__': 310 if len(sys.argv) > 1: header = sys.argv[1] 311 if len(sys.argv) > 2: output_path = sys.argv[2] 318 if len(sys.argv) > 1: 319 header = sys.argv[1] 320 if len(sys.argv) > 2: 321 output_path = sys.argv[2] 312 322 generate_external(header, output_path)
Note: See TracChangeset
for help on using the changeset viewer.