4 This script updates the copyright license lines for all files within a
8 $ python scripts/update_copyright.py <dir>
18 if len(sys.argv) != 2:
19 sys.stderr.write("error: please provide an input directory\n")
22 elif sys.argv[1] in ('-h','--help'):
28 Copyright (c) 2007 - 2015 Joseph Gaeddert
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:
37 The above copyright notice and this permission notice shall be included in
38 all copies or substantial portions of the Software.
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()
48 # add new line character to end of each line
49 for i in xrange(len(newcopy)):
54 # specific files to ignore
55 ignore_files = ['update_copyright.py',]
57 # directories to ignore
58 ignore_directories = ['.git',]
60 # only look at these extensions
61 include_extensions = ['.h', '.c', '.md', '.tex', '.ac', '.in', '.m',]
63 # print alignment for status
67 def update_copyright(filename=""):
69 Update copyright on file.
73 with open( filename, 'r' ) as f:
74 # read lines from file (removing newline character at end)
76 #contents.append(line.strip('\n\r'))
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]):
90 print " " + filename + ":" + " "*(align-len(filename)) + "no copyright found"
93 # look for end of copyright
94 #for i in range(index_start+15,index_start+15+min(10, len(contents))):
96 if re.search(r'along with liquid. If not, see',contents[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"
102 print " " + filename + ":" + " "*(align-len(filename)) + "could not find end of copyright"
106 m = re.search(r'^( \*|#+) +Copyright',contents[index_start])
110 raise Exception('unexpected error')
112 # delete items in list
113 contents.__delslice__(index_start, index_stop+1)
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 ''
122 contents.insert(index_start+i, comment + space + newcopy[i])
124 # open original file for writing
125 with open( filename, 'w' ) as f:
126 for line in contents:
129 print " " + filename + ":" + " "*(align-len(filename)) + "updated"
132 for root, subFolders, files in os.walk(rootdir):
134 # strip off leading './' if it exists
135 if root.startswith('./'):
138 # print root directory
141 # ignore certain directories
142 if root in ignore_directories:
143 print "(skipping directory)"
147 #for folder in subFolders:
148 # print "%s has subdirectory %s" % (root, folder)
150 # parse each filename in directory
151 for filename in files:
152 filePath = os.path.join(root, filename)
155 if filePath in ignore_files:
156 print " " + filePath + ":" + " "*(align-len(filePath)) + "ignoring this specific file"
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"
165 # continue on with this file
166 update_copyright(filePath)