1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This script searches all directories listed in dirs (relative to ltfatroot)
# for occurrence of ltfatarghelper and replaces it with a defined code.
#
#
from __future__ import print_function
import os
import sys
from collections import defaultdict
# Directories to search (relative to ltfat root)
dirs = ('','auditory','blockproc','demos','filterbank','fourier','frames','gabor',
'nonstatgab','operators','quadratic','reference','signals','sigproc','wavelets')
#dirs = ('g1','g2','g3')
def query_yes_no(question, default="no"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is one of "yes" or "no".
"""
valid = {"yes":True, "y":True, "ye":True,
"no":False, "n":False}
if default == None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while 1:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid.keys():
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "\
"(or 'y' or 'n').\n")
def main():
ltfatroot = os.path.join(os.path.dirname(os.path.realpath(__file__)),os.pardir)
mfileswithltfatarghelper = defaultdict(list)
for directory in dirs:
files = list( x for x in os.listdir(os.path.join(ltfatroot,directory)) if x.endswith('.m') )
for ff in files:
if ff == 'ltfatarghelper.m': continue
fullpath = os.path.join(ltfatroot,os.path.join(directory,ff))
with open(fullpath,'r') as f:
flines = f.readlines()
for lineNo,line in enumerate(flines):
if 'ltfatarghelper' in line and not line.startswith('%'):
if '%MARKER' in line or 'error' in line: break # Do not use this file
mfileswithltfatarghelper[fullpath].append(lineNo)
print('Found in file {}:\n {}'.format(ff,line))
#for k,v in mfileswithltfatarghelper.items():
# print(k + '->' + str(v) )
for k,v in mfileswithltfatarghelper.items():
filelines = []
with open(k,'r') as f:
filelines = f.readlines()
for lineNo in reversed(v):
print('Processing ' + k + ' with line ' + str(lineNo))
origline = filelines[lineNo]
if not 'ltfatarghelper' in origline:
print('This is wrong in '+k + str(v)+ '\n ' + origline)
continue
fcall = filelines[lineNo].split("=")
toinsert = []
if len(fcall)<2:
# No ret args
toinsert.append("origpath = pwd;\n")
toinsert.append("cd(ltfatbasepath);\n")
toinsert.append(origline.rstrip()+'%MARKER\n')
toinsert.append("cd([ltfatbasepath,filesep,'mex']);\n")
toinsert.append(origline)
toinsert.append("cd(origpath);\n")
else:
lhs = fcall[0]
if '[' in lhs:
lhs = lhs[lhs.find('[')+1:lhs.rfind(']')]
rhs = fcall[1][fcall[1].find("(")+1:fcall[1].rfind(")")]
lhsList = list(x.strip() for x in lhs.split(','))
lhsList2 = list(x+'2' if not x.strip()=='~' else x for x in lhsList)
rhsList = rhs.split(',')
#print(str(lhsList) + " ----> " + str(rhsList))
newline = '['+ ','.join(lhsList2) +']' + '=' + fcall[1] + '\n'
toinsert.append("origpath = pwd;\n")
toinsert.append("cd(ltfatbasepath);\n")
toinsert.append("tic;\n")
toinsert.append(origline.rstrip()+'%MARKER\n')
toinsert.append("t0 =toc;\n")
toinsert.append("fprintf('MAT: %d using %s\\n',t0,which('ltfatarghelper'));\n")
toinsert.append("cd([ltfatbasepath,filesep,'mex']);\n")
toinsert.append("tic;\n")
toinsert.append(newline)
toinsert.append("t0 =toc;\n")
toinsert.append("fprintf('MEX: %d using %s\\n',t0,which('ltfatarghelper'));\n")
toinsert.append("cd(origpath);\n")
compareline = 'if ~('
arglist = []
for one,two in zip(lhsList,lhsList2):
if one == '~': continue
arglist.append('isequal('+one +','+two+')')
compareline += '&&'.join(arglist)
compareline += ')\n'
toinsert.append(compareline);
toinsert.append("error('ltfatarghelper test failed in "
"%s',upper(mfilename));\n");
toinsert.append('end\n');
#print('\n'.join(filelines))
del filelines[lineNo]
filelines[lineNo:lineNo] = toinsert
with open(k,'w') as f:
f.writelines(filelines)
if __name__ == '__main__':
if not query_yes_no('This will overwrite a bunch of files. Make sure you have a backup.'
' Should I continue?'):
print('Quitting...')
sys.exit()
main()
|