Update to wx3.0, add SSE optimizations based on target_cpu, fix compile warnings
[ctsim.git] / scripts / update_copyright.py
1 #!/usr/bin/python
2
3 '''
4 This script updates the copyright license lines for all files within a
5 particular directory.
6
7 usage:
8   $ python scripts/update_copyright.py <dir>
9 '''
10
11 import os
12 import sys
13 import re
14
15 def usage():
16     print __doc__
17
18 if len(sys.argv) != 2:
19     sys.stderr.write("error: please provide an input directory\n")
20     usage()
21     sys.exit(-1)
22 elif sys.argv[1] in ('-h','--help'):
23     usage()
24     sys.exit(0)
25
26 # new copyright
27 newcopy = '''\
28 Copyright (c) 2007 - 2015 Joseph Gaeddert
29
30 Permission is hereby granted, free of charge, to any person obtaining a copy
31 of this software and associated documentation files (the "Software"), to deal
32 in the Software without restriction, including without limitation the rights
33 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34 copies of the Software, and to permit persons to whom the Software is
35 furnished to do so, subject to the following conditions:
36
37 The above copyright notice and this permission notice shall be included in
38 all copies or substantial portions of the Software.
39
40 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46 THE SOFTWARE.'''.splitlines()
47
48 # add new line character to end of each line
49 for i in xrange(len(newcopy)):
50     newcopy[i] += '\n'
51
52 rootdir = sys.argv[1]
53
54 # specific files to ignore
55 ignore_files = ['update_copyright.py',]
56
57 # directories to ignore
58 ignore_directories = ['.git',]
59
60 # only look at these extensions
61 include_extensions = ['.h', '.c', '.md', '.tex', '.ac', '.in', '.m',]
62
63 # print alignment for status
64 align = 56
65
66 #
67 def update_copyright(filename=""):
68     """
69     Update copyright on file.
70     """
71     # try to open file
72     contents = []
73     with open( filename, 'r' ) as f:
74         # read lines from file (removing newline character at end)
75         for line in f:
76             #contents.append(line.strip('\n\r'))
77             contents.append(line)
78
79     # parse contents
80     index_start = -1
81     index_stop  = -1
82
83     # search for copyright; starts at top of file
84     for i in range(min(10, len(contents))):
85         if re.search(r'Copyright .* Joseph Gaeddert',contents[i]):
86             index_start = i
87             break
88
89     if index_start == -1:
90         print "  " + filename + ":" + " "*(align-len(filename)) + "no copyright found"
91         return
92
93     # look for end of copyright
94     #for i in range(index_start+15,index_start+15+min(10, len(contents))):
95     i = index_start + 15
96     if re.search(r'along with liquid.  If not, see',contents[i]):
97         index_stop = i
98     elif re.search(r'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM',contents[i]):
99         print "  " + filename + ":" + " "*(align-len(filename)) + "copyright already updated"
100         return
101     else:
102         print "  " + filename + ":" + " "*(align-len(filename)) + "could not find end of copyright"
103         return
104
105     # check comment type
106     m = re.search(r'^( \*|#+) +Copyright',contents[index_start])
107     if m:
108         comment = m.group(1)
109     else:
110         raise Exception('unexpected error')
111
112     # delete items in list
113     contents.__delslice__(index_start, index_stop+1)
114
115     # insert new copyright
116     for i in range(len(newcopy)):
117         # only add space after comment characters if string is not empty
118         # (e.g. print ' *' instead of ' * ')
119         space = ' ' if len(newcopy[i].strip()) > 0 else ''
120
121         # insert new comment
122         contents.insert(index_start+i, comment + space + newcopy[i])
123
124     # open original file for writing
125     with open( filename, 'w' ) as f:
126         for line in contents:
127             f.write(line)
128             
129     print "  " + filename + ":" + " "*(align-len(filename)) + "updated"
130
131 #
132 for root, subFolders, files in os.walk(rootdir):
133
134     # strip off leading './' if it exists
135     if root.startswith('./'):
136         root = root[2:]
137
138     # print root directory
139     print root
140     
141     # ignore certain directories
142     if root in ignore_directories:
143         print "(skipping directory)"
144         continue
145
146     # print subfolders
147     #for folder in subFolders:
148     #    print "%s has subdirectory %s" % (root, folder)
149
150     # parse each filename in directory
151     for filename in files:
152         filePath = os.path.join(root, filename)
153
154         # check filename
155         if filePath in ignore_files:
156             print "  " + filePath + ":" + " "*(align-len(filePath)) + "ignoring this specific file"
157             continue;
158
159         # check filename extension
160         baseName, extension = os.path.splitext(filename)
161         if extension not in include_extensions:
162             print "  " + filePath + ":" + " "*(align-len(filePath)) + "improper extension; ignoring file"
163             continue;
164
165         # continue on with this file
166         update_copyright(filePath)
167
168