3
# Replace string in dolfin files
5
# dolfinreplace s0 s1 [-s]
9
# dolfinreplace foo bar
11
# Johan hake, 2008-19-09
13
# Modified by Anders Logg, 2008.
17
# File post fixes to look in
18
post_fixes = ['.h','.cpp','.i','.tex','.py']
20
# Directories to explude
26
dolfin_root = path.abspath(path.join(path.dirname(path.abspath(__file__)),
30
post_fix_string = ", ".join( "'%s'"%s for s in post_fixes)
32
start_str = """Replacing '%s' with '%s' in all %s files..."""
35
%d occurences replaced in %d files:
40
def replace(args, dirname, filenames):
41
""" Replace replaces[0] with replaces[1]
43
In all fnames with post fixes in 'post_fixes' replace replaces[0] with replaces[1]
45
global count, changed_files
46
s0, s1, simulate = args
47
for exclude in exclude_dir:
48
if exclude in filenames: filenames.remove(exclude)
50
for filename in filenames:
51
if path.splitext(filename)[1] in post_fixes:
52
fullpath_filename = path.join(dirname,filename)
53
if path.isfile(fullpath_filename):
54
file = open(fullpath_filename, "r")
56
num_changed = input.count(s0)
58
output = input.replace(s0, s1)
62
file = open(fullpath_filename, "w")
67
changed_files.append(path.basename(fullpath_filename))
70
print """Usage: dolfinreplace s0 s1 [-s]
72
Replaces string 's0' with 's1' in all
76
files in the dolfin directory tree.
83
num_arg = len(sys.argv) -1
84
if num_arg < 2 or num_arg > 3:
94
if '-s' in sys.argv[3]:
102
print start_str%(s0,s1,post_fix_string)
105
path.walk(dolfin_root,replace,(s0,s1,simulate))
108
simulate_str = "\nSimulating, no replacement done..."
112
print report_str % (count, len(changed_files), ", ".join(changed_files), simulate_str)
114
print "\nDone\nNo occurens of %s found"%sys.argv[1]
116
if __name__ == "__main__":