1#!/usr/bin/python 2 3import commands, os, re, string, sys, time 4 5def count_enclosed_functions (source): 6 func_count = 0 7 open_brace = 0 8 close_brace = 0 9 for ch in source: 10 if ch == '{': 11 open_brace += 1 12 elif ch == '}': 13 close_brace += 1 14 if open_brace == close_brace: 15 func_count += 1 16 if open_brace < close_brace: 17 print "count_enclosed_functions : open_brace < close_brace" 18 return -1 19 return func_count 20 21def find_function_prototype (source, proto_name): 22 proto_re = "(^[a-zA-Z_ \t]+\s+%s[^a-zA-Z0-9_]\s*\([^\)]+\)\s+;\n)" % (proto_name) 23 proto_result = re.search (proto_re, source, re.MULTILINE | re.DOTALL) 24 if not proto_result: 25 return None 26 proto_text = proto_result.groups ()[0] 27 return proto_text 28 29def find_function_definition (source, func_name): 30 func_re = "(\n[a-zA-Z_ \t]+\n%s[^a-zA-Z0-9_].* /\* %s \*/\n)" % (func_name, func_name) 31 func_result = re.search (func_re, source, re.MULTILINE | re.DOTALL) 32 if not func_result: 33 sys.exit (1) 34 return None 35 func_text = func_result.groups ()[0] 36 37 # Now to check that we only have one enclosing function. 38 func_count = count_enclosed_functions (func_text) 39 if func_count != 1: 40 return None 41 return func_text 42 43def find_include (source, inc_name): 44 inc_re = "(^#include\s+[\<\"]%s[\"\>]\s*)" % inc_name 45 inc_result = re.search (inc_re, source, re.MULTILINE | re.DOTALL) 46 if not inc_result: 47 return None 48 inc_text = inc_result.groups ()[0] 49 return inc_text 50 51def find_assign_statement (source, var_name): 52 var_re = "(^\s+%s\s*=[^;]+;)" % var_name 53 var_result = re.search (var_re, source, re.MULTILINE | re.DOTALL) 54 if not var_result: 55 return None 56 assign_text = var_result.groups ()[0] 57 return assign_text 58 59#-------------------------------------------------------------------------------- 60 61def remove_include (source, inc_name): 62 inc_text = find_include (source, inc_name) 63 if not inc_text: 64 print "remove_include : include '%s' not found. Exiting." % inc_name 65 sys.exit (1) 66 67 source = string.replace (source, inc_text, "") 68 return source 69 70def remove_assign (source, assign_name): 71 assign_text = find_assign (source, inc_name) 72 if not inc_text: 73 print "remove_include : include '%s' not found. Exiting." % inc_name 74 sys.exit (1) 75 76 source = string.replace (source, inc_text, "") 77 return source 78 79def remove_prototype (source, proto_name): 80 proto_text = find_function_prototype (source, proto_name) 81 if not proto_text: 82 print "remove_prototype : prototype '%s' not found. Exiting." % proto_name 83 sys.exit (1) 84 85 source = string.replace (source, proto_text, "") 86 return source 87 88def remove_function (source, func_name): 89 func_text = find_function_definition (source, func_name) 90 if not func_text: 91 print "remove_function : function '%s' not found. Exiting." % func_name 92 sys.exit (1) 93 94 source = string.replace (source, func_text, "/* Function %s() removed here. */\n" % func_name) 95 return source 96 97def remove_all_assignments (source, var): 98 count = 0 99 while 1: 100 assign_text = find_assign_statement (source, var) 101 if not assign_text: 102 if count != 0: 103 break 104 print "remove_all_assignments : variable '%s' not found. Exiting." % var 105 sys.exit (1) 106 107 source = string.replace (source, assign_text, "") 108 count += 1 109 return source 110 111 112 113#---------------------------------------------------------------- 114 115def remove_funcs_and_protos_from_file (filename, func_list): 116 source_code = open (filename, 'r').read () 117 118 for func in func_list: 119 source_code = remove_prototype (source_code, func) ; 120 source_code = remove_function (source_code, func) ; 121 open (filename, 'w').write (source_code) 122 123def remove_funcs_from_file (filename, func_list): 124 source_code = open (filename, 'r').read () 125 126 for func in func_list: 127 source_code = remove_function (source_code, func) ; 128 open (filename, 'w').write (source_code) 129 130def remove_protos_from_file (filename, func_list): 131 source_code = open (filename, 'r').read () 132 133 for func in func_list: 134 source_code = remove_prototype (source_code, func) ; 135 open (filename, 'w').write (source_code) 136 137def remove_includes_from_file (filename, inc_list): 138 source_code = open (filename, 'r').read () 139 140 for inc in inc_list: 141 source_code = remove_include (source_code, inc) ; 142 open (filename, 'w').write (source_code) 143 144def remove_all_assignments_from_file (filename, var_list): 145 source_code = open (filename, 'r').read () 146 147 for var in var_list: 148 source_code = remove_all_assignments (source_code, var) ; 149 open (filename, 'w').write (source_code) 150 151def remove_comment_start_end (filename, start_comment, end_comment): 152 source_code = open (filename, 'r').read () 153 154 while 1: 155 start_index = string.find (source_code, start_comment) 156 end_index = string.find (source_code, end_comment) 157 if start_index < 0 or end_index < start_index: 158 break 159 end_index += len (end_comment) 160 source_code = source_code [:start_index-1] + source_code [end_index:] ; 161 162 open (filename, 'w').write (source_code) 163 164def remove_strings_from_file (filename, str_list): 165 file_text = open (filename, 'r').read () 166 for current_str in str_list: 167 file_text = string.replace (file_text, current_str, '') 168 open (filename, 'w').write (file_text) 169 170def string_replace_in_file (filename, from_str, to_str): 171 file_text = open (filename, 'r').read () 172 file_text = string.replace (file_text, from_str, to_str) 173 open (filename, 'w').write (file_text) 174 175def remove_regex_from_file (filename, regex_list): 176 file_text = open (filename, 'r').read () 177 for regex in regex_list: 178 file_text = re.sub (regex, '', file_text, re.MULTILINE | re.DOTALL) 179 open (filename, 'w').write (file_text) 180 181#========================================================================== 182 183def find_configure_version (filename): 184 # AM_INIT_AUTOMAKE(libsndfile,0.0.21pre6) 185 file = open (filename) 186 while 1: 187 line = file.readline () 188 if re.search ("AC_INIT", line): 189 x = re.sub ("[^\(]+\(", "", line) 190 x = re.sub ("\).*\n", "", x) 191 x = string.split (x, ",") 192 package = x [0] 193 version = x [1] 194 break 195 file.close () 196 # version = re.escape (version) 197 return package, version 198 199def fix_configure_ac_file (filename): 200 data = open (filename, 'r').read () 201 data = string.replace (data, "AM_INIT_AUTOMAKE(libsndfile,", "AM_INIT_AUTOMAKE(libsndfile_lite,", 1) 202 203 file = open (filename, 'w') 204 file.write (data) 205 file.close () 206 207 208def make_dist_file (package, version): 209 print "Making dist file." 210 tar_gz_file = "%s-%s.tar.gz" % (package, version) 211 if os.path.exists (tar_gz_file): 212 return 213 if os.system ("make dist"): 214 sys.exit (1) 215 return 216 217def delete_files (file_list): 218 for file_name in file_list: 219 os.remove (file_name) 220 221#======================================================================= 222 223source_dir = os.getcwd () 224 225conf_package, conf_version = find_configure_version ('configure.ac') 226 227package_version = "%s-%s" % (conf_package, conf_version) 228lite_version = "%s_lite-%s" % (conf_package, conf_version) 229 230os.system ("rm -rf %s%s.tar.gz" % (source_dir, package_version)) 231 232os.system ("make dist") 233 234make_dist_file (conf_package, conf_version) 235 236os.chdir ("/tmp") 237 238print "Uncompressing .tar.gz file." 239os.system ("rm -rf %s" % package_version) 240if os.system ("tar zxf %s/%s.tar.gz" % (source_dir, package_version)): 241 sys.exit (1) 242 243 244print "Renaming to libsndfile_lite." 245os.system ("rm -rf %s" % lite_version) 246os.rename (package_version, lite_version) 247 248print "Changing into libsndfile_lite directory." 249os.chdir (lite_version) 250 251print "Removing un-neeed directories." 252delete_dirs = [ 'src/G72x' ] 253 254for dir_name in delete_dirs: 255 os.system ("rm -rf %s" % dir_name) 256 257print "Removing un-needed files." 258delete_files ([ 'src/ircam.c', 'src/nist.c', 259 'src/ima_adpcm.c', 'src/ms_adpcm.c', 'src/au_g72x.c', 260 'src/mat4.c', 'src/mat5.c', 'src/dwvw.c', 'src/paf.c', 261 'src/ogg.c', 'src/pvf.c', 'src/xi.c', 'src/htk.c', 262 'src/sd2.c', 'src/rx2.c', 'src/txw.c', 'src/wve.c', 263 'src/dwd.c', 'src/svx.c', 'src/voc.c', 'src/vox_adpcm.c', 264 'src/sds.c' 265 ]) 266 267 268print "Hacking 'configure.ac' and 'src/Makefile.am'." 269remove_strings_from_file ('configure.ac', [ 'src/G72x/Makefile' ]) 270remove_strings_from_file ('src/Makefile.am', [ 'G72x/libg72x.la', 'G72x', 271 'ircam.c', 'nist.c', 'ima_adpcm.c', 'ms_adpcm.c', 'au_g72x.c', 'mat4.c', 272 'mat5.c', 'dwvw.c', 'paf.c', 'ogg.c', 'pvf.c', 'xi.c', 'htk.c', 273 'sd2.c', 'rx2.c', 'txw.c', 'wve.c', 'dwd.c', 'svx.c', 'voc.c', 274 'vox_adpcm.c', 'sds.c' 275 ]) 276 277#---------------------------------------------------------------------------- 278 279print "Hacking header files." 280 281remove_protos_from_file ('src/common.h', [ 'xi_open', 'sd2_open', 'ogg_open', 282 'dwvw_init', 'paf_open', 'svx_open', 'nist_open', 'rx2_open', 'mat4_open', 283 'voc_open', 'txw_open', 'dwd_open', 'htk_open', 'wve_open', 'mat5_open', 284 'pvf_open', 'ircam_open', 'sds_open', 285 'float32_init', 'double64_init', 'aiff_ima_init', 'vox_adpcm_init', 286 'wav_w64_ima_init', 'wav_w64_msadpcm_init' 287 ]) 288 289remove_protos_from_file ('src/au.h', 290 [ 'au_g72x_reader_init', 'au_g72x_writer_init' ]) 291 292remove_protos_from_file ('src/wav_w64.h', [ 'msadpcm_write_adapt_coeffs' ]) 293 294#---------------------------------------------------------------------------- 295 296print "Hacking case statements." 297 298remove_comment_start_end ('src/sndfile.c', '/* Lite remove start */' , '/* Lite remove end */') 299remove_comment_start_end ('src/aiff.c', '/* Lite remove start */' , '/* Lite remove end */') 300remove_comment_start_end ('src/au.c', '/* Lite remove start */' , '/* Lite remove end */') 301remove_comment_start_end ('src/raw.c', '/* Lite remove start */' , '/* Lite remove end */') 302remove_comment_start_end ('src/w64.c', '/* Lite remove start */' , '/* Lite remove end */') 303remove_comment_start_end ('src/wav.c', '/* Lite remove start */' , '/* Lite remove end */') 304remove_comment_start_end ('src/double64.c', '/* Lite remove start */' , '/* Lite remove end */') 305remove_comment_start_end ('src/float32.c', '/* Lite remove start */' , '/* Lite remove end */') 306 307 308#---------------------------------------------------------------------------- 309 310print "Hacking src/pcm.c." 311remove_funcs_from_file ('src/pcm.c', [ 312 'f2sc_array', 'f2sc_clip_array', 'f2uc_array', 'f2uc_clip_array', 313 'f2bes_array', 'f2bes_clip_array', 'f2les_array', 'f2les_clip_array', 314 'f2let_array', 'f2let_clip_array', 'f2bet_array', 'f2bet_clip_array', 315 'f2bei_array', 'f2bei_clip_array', 'f2lei_array', 'f2lei_clip_array', 316 'd2sc_array', 'd2sc_clip_array', 'd2uc_array', 'd2uc_clip_array', 317 'd2bes_array', 'd2bes_clip_array', 'd2les_array', 'd2les_clip_array', 318 'd2let_array', 'd2let_clip_array', 'd2bet_array', 'd2bet_clip_array', 319 'd2bei_array', 'd2bei_clip_array', 'd2lei_array', 'd2lei_clip_array', 320 ]) 321 322remove_funcs_and_protos_from_file ('src/pcm.c', [ 323 'pcm_read_sc2f', 'pcm_read_uc2f', 'pcm_read_les2f', 'pcm_read_bes2f', 324 'pcm_read_let2f', 'pcm_read_bet2f', 'pcm_read_lei2f', 'pcm_read_bei2f', 325 'pcm_read_sc2d', 'pcm_read_uc2d', 'pcm_read_les2d', 'pcm_read_bes2d', 326 'pcm_read_let2d', 'pcm_read_bet2d', 'pcm_read_lei2d', 'pcm_read_bei2d', 327 'pcm_write_f2sc', 'pcm_write_f2uc', 'pcm_write_f2bes', 'pcm_write_f2les', 328 'pcm_write_f2bet', 'pcm_write_f2let', 'pcm_write_f2bei', 'pcm_write_f2lei', 329 'pcm_write_d2sc', 'pcm_write_d2uc', 'pcm_write_d2bes', 'pcm_write_d2les', 330 'pcm_write_d2bet', 'pcm_write_d2let', 'pcm_write_d2bei', 'pcm_write_d2lei', 331 332 'sc2f_array', 'uc2f_array', 'bes2f_array', 'les2f_array', 333 'bet2f_array', 'let2f_array', 'bei2f_array', 'lei2f_array', 334 'sc2d_array', 'uc2d_array', 'bes2d_array', 'les2d_array', 335 'bet2d_array', 'let2d_array', 'bei2d_array', 'lei2d_array' 336 ]) 337 338remove_includes_from_file ('src/pcm.c', [ 'float_cast.h' ]) 339remove_all_assignments_from_file ('src/pcm.c', [ 340 'psf-\>write_float', 'psf\-\>write_double', 341 'psf-\>read_float', 'psf\-\>read_double' ]) 342 343#---------------------------------------------------------------------------- 344print "Hacking src/ulaw.c." 345remove_funcs_and_protos_from_file ('src/ulaw.c', [ 346 'ulaw_read_ulaw2f', 'ulaw_read_ulaw2d', 347 'ulaw_write_f2ulaw', 'ulaw_write_d2ulaw', 348 'ulaw2f_array', 'ulaw2d_array', 'f2ulaw_array', 'd2ulaw_array' 349 ]) 350 351remove_includes_from_file ('src/ulaw.c', [ 'float_cast.h' ]) 352remove_all_assignments_from_file ('src/ulaw.c', [ 353 'psf-\>write_float', 'psf\-\>write_double', 354 'psf-\>read_float', 'psf\-\>read_double' ]) 355 356#---------------------------------------------------------------------------- 357 358print "Hacking src/alaw.c." 359remove_funcs_and_protos_from_file ('src/alaw.c', [ 360 'alaw_read_alaw2f', 'alaw_read_alaw2d', 361 'alaw_write_f2alaw', 'alaw_write_d2alaw', 362 'alaw2f_array', 'alaw2d_array', 'f2alaw_array', 'd2alaw_array' 363 ]) 364 365remove_includes_from_file ('src/alaw.c', [ 'float_cast.h' ]) 366remove_all_assignments_from_file ('src/alaw.c', [ 367 'psf-\>write_float', 'psf\-\>write_double', 368 'psf-\>read_float', 'psf\-\>read_double' ]) 369 370#---------------------------------------------------------------------------- 371 372print "Hacking src/gsm610.c." 373remove_funcs_and_protos_from_file ('src/gsm610.c', [ 374 'gsm610_read_f', 'gsm610_read_d', 'gsm610_write_f', 'gsm610_write_d' 375 ]) 376 377remove_includes_from_file ('src/gsm610.c', [ 'float_cast.h' ]) 378remove_all_assignments_from_file ('src/gsm610.c', [ 379 'psf-\>write_float', 'psf\-\>write_double', 380 'psf-\>read_float', 'psf\-\>read_double' ]) 381 382#---------------------------------------------------------------------------- 383 384print "Hacking src/float32.c." 385 386# string_replace_in_file ('src/float32.c', '"float_cast.h"', '<math.h>') 387remove_funcs_from_file ('src/float32.c', [ 'float32_init' ]) 388 389remove_funcs_and_protos_from_file ('src/float32.c', [ 390 'host_read_f2s', 'host_read_f2i', 'host_read_f', 'host_read_f2d', 391 'host_write_s2f', 'host_write_i2f', 'host_write_f', 'host_write_d2f', 392 'f2s_array', 'f2i_array', 'f2d_array', 's2f_array', 'i2f_array', 'd2f_array', 393 'float32_peak_update', 394 'replace_read_f2s', 'replace_read_f2i', 'replace_read_f', 'replace_read_f2d', 395 'replace_write_s2f', 'replace_write_i2f', 'replace_write_f', 'replace_write_d2f', 396 'bf2f_array', 'f2bf_array', 397 'float32_get_capability', 398 ]) 399 400#---------------------------------------------------------------------------- 401 402print "Hacking src/double64.c." 403remove_funcs_from_file ('src/double64.c', [ 'double64_init' ]) 404 405remove_funcs_and_protos_from_file ('src/double64.c', [ 406 'host_read_d2s', 'host_read_d2i', 'host_read_d2f', 'host_read_d', 407 'host_write_s2d', 'host_write_i2d', 'host_write_f2d', 'host_write_d', 408 'd2s_array', 'd2i_array', 'd2f_array', 409 's2d_array', 'i2d_array', 'f2d_array', 410 'double64_peak_update', 'double64_get_capability', 411 'replace_read_d2s', 'replace_read_d2i', 'replace_read_d2f', 'replace_read_d', 412 'replace_write_s2d', 'replace_write_i2d', 'replace_write_f2d', 'replace_write_d', 413 'd2bd_read', 'bd2d_write' 414 ]) 415 416#---------------------------------------------------------------------------- 417 418print "Hacking test programs." 419delete_files ([ 'tests/dwvw_test.c', 'tests/floating_point_test.c', 420 'tests/dft_cmp.c', 'tests/peak_chunk_test.c', 421 'tests/scale_clip_test.tpl', 'tests/scale_clip_test.def' 422 ]) 423 424remove_comment_start_end ('tests/write_read_test.def', '/* Lite remove start */', '/* Lite remove end */') 425remove_comment_start_end ('tests/write_read_test.tpl', '/* Lite remove start */', '/* Lite remove end */') 426 427remove_comment_start_end ('tests/Makefile.am', '# Lite remove start', '# Lite remove end') 428 429remove_strings_from_file ('tests/Makefile.am', [ 430 'scale_clip_test.tpl', 'scale_clip_test.def', 431 '\n\t./dwvw_test', 432 '\n\t./floating_point_test', '\n\t./scale_clip_test', 433 '\n\t./peak_chunk_test aiff', '\n\t./peak_chunk_test wav', 434 '\n\t./command_test norm', '\n\t./command_test peak', 435 '\n\t./lossy_comp_test wav_ima', '\n\t./lossy_comp_test wav_msadpcm', 436 '\n\t./lossy_comp_test au_g721', '\n\t./lossy_comp_test au_g723', 437 '\n\t./lossy_comp_test vox_adpcm', 438 '\n\t./lossy_comp_test w64_ima', '\n\t./lossy_comp_test w64_msadpcm', 439 'peak_chunk_test', 'dwvw_test', 'floating_point_test', 'scale_clip_test', 440 441 'paf-tests', 'svx-tests', 'nist-tests', 'ircam-tests', 'voc-tests', 442 'mat4-tests', 'mat5-tests', 'pvf-tests', 'xi-tests', 'htk-tests', 443 'sds-tests' 444 ]) 445 446remove_comment_start_end ('tests/pcm_test.c', '/* Lite remove start */', '/* Lite remove end */') 447remove_funcs_and_protos_from_file ('tests/pcm_test.c', [ 448 'pcm_test_float', 'pcm_test_double' 449 ]) 450 451remove_comment_start_end ('tests/lossy_comp_test.c', '/* Lite remove start */', '/* Lite remove end */') 452remove_funcs_and_protos_from_file ('tests/lossy_comp_test.c', [ 453 'lcomp_test_float', 'lcomp_test_double', 'sdlcomp_test_float', 'sdlcomp_test_double', 454 'smoothed_diff_float', 'smoothed_diff_double' 455 ]) 456 457remove_comment_start_end ('tests/multi_file_test.c', '/* Lite remove start */', '/* Lite remove end */') 458 459remove_strings_from_file ('tests/stdio_test.c', [ 460 '"paf",', '"svx",', '"nist",', '"ircam",', '"voc",', '"mat4",', '"mat5",', '"pvf",' 461 ]) 462 463remove_comment_start_end ('tests/pipe_test.c', '/* Lite remove start */', '/* Lite remove end */') 464 465#---------------------------------------------------------------------------- 466 467print "Fixing configure.ac file." 468fix_configure_ac_file ('configure.ac') 469 470print "Building and testing source." 471 # Try --disable-shared --disable-gcc-opt 472if os.system ("./reconfigure.mk && ./configure --disable-shared --disable-gcc-opt && make check"): 473 os.system ('PS1="FIX > " bash --norc') 474 sys.exit (1) 475 476print "Making distcheck" 477if os.system ("make distcheck"): 478 os.system ('PS1="FIX > " bash --norc') 479 sys.exit (1) 480 481print "Copying tarball" 482if os.system ("cp %s.tar.gz %s" % (lite_version, source_dir)): 483 print "??? %s.tar.gz ???" % lite_version 484 os.system ('PS1="FIX > " bash --norc') 485 sys.exit (1) 486 487os.chdir (source_dir) 488 489os.system ("rm -rf /tmp/%s" % lite_version) 490 491print "Done." 492