Changeset 541ea280


Ignore:
Timestamp:
May 30, 2017, 12:29:20 AM (8 years ago)
Author:
Martin Hermant <martin.hermant@gmail.com>
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
Message:

gen_external.py : pepify

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/lib/gen_external.py

    r6cbf34b r541ea280  
    11import distutils.ccompiler
    2 import sys, os, subprocess, glob
     2import sys
     3import os
     4import subprocess
     5import glob
    36
    47header = os.path.join('src', 'aubio.h')
     
    1013
    1114skip_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
    4448
    4549def get_preprocessor():
     
    6064
    6165    cpp_cmd = None
    62     if hasattr(compiler, 'preprocessor'): # for unixccompiler
     66    if hasattr(compiler, 'preprocessor'):  # for unixccompiler
    6367        cpp_cmd = compiler.preprocessor
    64     elif hasattr(compiler, 'compiler'): # for ccompiler
     68    elif hasattr(compiler, 'compiler'):  # for ccompiler
    6569        cpp_cmd = compiler.compiler.split()
    6670        cpp_cmd += ['-E']
    67     elif hasattr(compiler, 'cc'): # for msvccompiler
     71    elif hasattr(compiler, 'cc'):  # for msvccompiler
    6872        cpp_cmd = compiler.cc.split()
    6973        cpp_cmd += ['-E']
     
    9599    print("Running command: {:s}".format(" ".join(cpp_cmd)))
    96100    proc = subprocess.Popen(cpp_cmd,
    97             stderr=subprocess.PIPE,
    98             stdout=subprocess.PIPE)
     101                            stderr=subprocess.PIPE,
     102                            stdout=subprocess.PIPE)
    99103    assert proc, 'Proc was none'
    100104    cpp_output = proc.stdout.read()
     
    103107        raise Exception("preprocessor output is empty:\n%s" % err_output)
    104108    elif err_output:
    105         print ("Warning: preprocessor produced warnings:\n%s" % err_output)
     109        print("Warning: preprocessor produced warnings:\n%s" % err_output)
    106110    if not isinstance(cpp_output, list):
    107111        cpp_output = [l.strip() for l in cpp_output.decode('utf8').split('\n')]
     
    112116    i = 1
    113117    while 1:
    114         if i >= len(cpp_output):break
     118        if i >= len(cpp_output):
     119            break
    115120        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)
    118123        elif ('}' in cpp_output[i]):
    119124            cpp_output[i] = cpp_output[i - 1] + ' ' + cpp_output[i]
     
    125130    tmp = []
    126131    for l in cpp_output:
    127         tmp+=[ l.replace(' *','* ')]
    128     cpp_output = tmp;
    129 
     132        tmp += [l.replace(' *', '* ')]
     133    cpp_output = tmp
    130134
    131135    return cpp_output
    132136
     137
    133138def 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)
    135140    cpp_objects = [a.split()[3][:-1] for a in typedefs]
    136141    return cpp_objects
     
    174179            shortname = o[6:-2]  # without aubio_
    175180            longname = o[:-2]  # without _t
    176         else: # support object not starting with aubio_ (fvec...)
     181        else:  # support object not starting with aubio_ (fvec...)
    177182            shortname = o
    178183            longname = shortname
    179        
     184
    180185        if shortname in skip_objects:
    181186            continue
     
    205210                    lib[shortname]['set'].append(fn)
    206211                else:
    207                     #print "no idea what to do about", fn
     212                    # print "no idea what to do about", fn
    208213                    lib[shortname]['other'].append(fn)
    209214    return lib
     215
    210216
    211217def print_c_declarations_results(lib, c_declarations):
     
    217223                    found = 1
    218224        if found == 0:
    219             print ("missing", fn)
     225            print("missing", fn)
    220226
    221227    for o in lib:
    222228        for family in lib[o]:
    223229            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]))
    225231            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]))
    227233            else:
    228                 print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family] ) )
     234                print("{:15s} {:10s} {:s}".format(o, family, lib[o][family]))
    229235
    230236
    231237def 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')))
    234242
    235243    c_declarations = get_c_declarations(header, usedouble=usedouble)
     
    246254    for o in lib:
    247255        out = source_header
    248         mapped = MappedObject(lib[o], usedouble = usedouble)
     256        mapped = MappedObject(lib[o], usedouble=usedouble)
    249257        out += mapped.gen_code()
    250258        output_file = os.path.join(output_path, 'gen-%s.c' % o)
    251259        with open(output_file, 'w') as f:
    252260            f.write(out)
    253             print ("wrote %s" % output_file )
     261            print("wrote %s" % output_file)
    254262            sources_list.append(output_file)
    255263
     
    263271  return ({pycheck_types});
    264272}}
    265 """.format(pycheck_types = check_types)
     273""".format(pycheck_types=check_types)
    266274
    267275    add_types = "".join(["""
    268276  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])
    270278    out += """
    271279
     
    274282{add_types}
    275283}}
    276 """.format(add_types = add_types)
     284""".format(add_types=add_types)
    277285
    278286    output_file = os.path.join(output_path, 'aubio-generated.c')
    279287    with open(output_file, 'w') as f:
    280288        f.write(out)
    281         print ("wrote %s" % output_file )
     289        print("wrote %s" % output_file)
    282290        sources_list.append(output_file)
    283291
     
    297305int generated_objects ( void );
    298306void add_generated_objects( PyObject *m );
    299 """.format(objlist = objlist)
     307""".format(objlist=objlist)
    300308
    301309    output_file = os.path.join(output_path, 'aubio-generated.h')
    302310    with open(output_file, 'w') as f:
    303311        f.write(out)
    304         print ("wrote %s" % output_file )
     312        print("wrote %s" % output_file)
    305313        # no need to add header to list of sources
    306314
     
    308316
    309317if __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]
    312322    generate_external(header, output_path)
Note: See TracChangeset for help on using the changeset viewer.