~ubuntu-branches/ubuntu/maverick/dolfin/maverick

« back to all changes in this revision

Viewing changes to scripts/dolfinreplace

  • Committer: Bazaar Package Importer
  • Author(s): Johannes Ring
  • Date: 2008-09-16 08:41:20 UTC
  • Revision ID: james.westby@ubuntu.com-20080916084120-i8k3u6lhx3mw3py3
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# Replace string in dolfin files
 
4
#
 
5
#     dolfinreplace s0 s1 [-s]
 
6
#
 
7
# Example:
 
8
#
 
9
#     dolfinreplace foo bar 
 
10
#
 
11
# Johan hake, 2008-19-09
 
12
#
 
13
# Modified by Anders Logg, 2008.
 
14
 
 
15
from os import path
 
16
 
 
17
# File post fixes to look in
 
18
post_fixes = ['.h','.cpp','.i','.tex','.py']
 
19
 
 
20
# Directories to explude
 
21
exclude_dir = [".hg"]
 
22
 
 
23
# Global variables
 
24
count         = 0
 
25
changed_files = []
 
26
dolfin_root   = path.abspath(path.join(path.dirname(path.abspath(__file__)),
 
27
                                       path.pardir))
 
28
 
 
29
# Output strings
 
30
post_fix_string = ", ".join( "'%s'"%s for s in post_fixes)
 
31
 
 
32
start_str = """Replacing '%s' with '%s' in all %s files..."""
 
33
 
 
34
report_str = """
 
35
%d occurences replaced in %d files:
 
36
 
 
37
%s
 
38
%s"""
 
39
 
 
40
def replace(args, dirname, filenames):
 
41
    """ Replace replaces[0] with replaces[1]
 
42
 
 
43
    In all fnames with post fixes in 'post_fixes' replace replaces[0] with replaces[1]
 
44
    """
 
45
    global count, changed_files
 
46
    s0, s1, simulate = args
 
47
    for exclude in exclude_dir:
 
48
        if exclude in filenames: filenames.remove(exclude)
 
49
    
 
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")
 
55
                input = file.read()
 
56
                num_changed = input.count(s0)
 
57
                count += num_changed
 
58
                output = input.replace(s0, s1)
 
59
                file.close()
 
60
                
 
61
                if not simulate:
 
62
                    file = open(fullpath_filename, "w")
 
63
                    file.write(output)
 
64
                    file.close()
 
65
                
 
66
                if num_changed > 0:
 
67
                    changed_files.append(path.basename(fullpath_filename))
 
68
 
 
69
def usage():
 
70
    print """Usage: dolfinreplace s0 s1 [-s]
 
71
 
 
72
Replaces string 's0' with 's1' in all
 
73
 
 
74
   %s
 
75
 
 
76
files in the dolfin directory tree.
 
77
 
 
78
  -s : Simulate
 
79
"""%post_fix_string
 
80
 
 
81
def main():
 
82
    import sys
 
83
    num_arg = len(sys.argv) -1
 
84
    if  num_arg < 2 or num_arg > 3:
 
85
        usage()
 
86
        sys.exit(1)
 
87
 
 
88
    # The replace info
 
89
    s0 = sys.argv[1]
 
90
    s1 = sys.argv[2]
 
91
 
 
92
    # If a third argument
 
93
    if num_arg == 3:
 
94
        if '-s' in sys.argv[3]:
 
95
            simulate = True
 
96
        else:
 
97
            usage()
 
98
            sys.exit(1)
 
99
    else:
 
100
        simulate = False
 
101
    
 
102
    print start_str%(s0,s1,post_fix_string)
 
103
 
 
104
    # Do it!
 
105
    path.walk(dolfin_root,replace,(s0,s1,simulate))
 
106
    if count > 0:
 
107
        if simulate:
 
108
            simulate_str = "\nSimulating, no replacement done..."
 
109
        else:
 
110
            simulate_str = ""
 
111
            
 
112
        print report_str % (count, len(changed_files), ", ".join(changed_files), simulate_str)
 
113
    else:
 
114
        print "\nDone\nNo occurens of %s found"%sys.argv[1]
 
115
    
 
116
if __name__ == "__main__":
 
117
    main()