Newer
Older
#!/usr/bin/python
* (c) Copyright YEAR CORSIKA Project, corsika-project@lists.kit.edu
*
* See file AUTHORS for a list of contributors.
*
* This software is distributed under the terms of the GNU General Public
* Licence version 3 (GPL Version 3). See file LICENSE for a full version of
* the license.
Debug = 0 # 0: nothing, 1: checking, 2: filesystem
excludeDirs = ["ThirdParty", "git"]
excludeFiles = ['PhysicalConstants.h','CorsikaFenvOSX.cc', 'sgn.h']
extensions = [".cc", ".h", ".test"]
justCheck = True # T: only checking, F: also changing files
foundMissing = False
###############################################
#
def checkNote(filename):
global foundMissing, justCheck
if Debug>0:
print ("***********************************************")
print ("file: " + filename )
with open(filename, "r") as file:
for line in file.readlines():
lines.append(line)
file.close()
searchStatus = 0 # 0:before comment block, #1 in comment block, #2 found copyright
for iLine in range(len(lines)):
line = lines[iLine]
if "/*" in line and searchStatus==0:
searchStatus = 1
blockStart = iLine
if "copyright" in line.lower() and searchStatus>0:
searchStatus = 2
if "*/" in line:
if searchStatus>=2:
startNote.append(blockStart)
endNote.append(iLine)
if Debug>0:
txt = "states: n=" + str(len(startNote))
for i in xrange(len(startNote)):
txt += ", [" + str(startNote[i]) + "-" + str(endNote[i]) + "]"
print ("stats: ") + txt
regex = re.compile(re.escape(noteLines[iLine+1].strip(" \n")).replace('YEAR','....'))
if not re.match(regex, lines[startNote[0]+iLine].strip(" \n")):
foundMissing = True
print "needs update: " + filename + " new=\'" + noteLines[iLine+1] + "\' vs old=\'" + lines[startNote[0]+iLine].rstrip('\n') + "\'"
print ("isSame=" + str(isSame) + " " + str(len(startNote)))
# check if notice is the same, or we need to remove multiple notices...
print ("No copyright note in file: " + filename)
if justCheck:
foundMissing = True
return
# add (new) copyright notice here:
os.rename(filename, filename+".bak")
with open(filename, "w") as file:
file.write(text)
print " [remove " + str(iBlock) + "] " + (lines[iLine]).strip()
if lines[iLine].strip() != "": # if line after comment is empty, also remove it
skip = False
file.close()
###############################################
#
#def next_file(x, dir_name, files):
def next_file(dir_name, files):
for check in excludeDirs :
if check in dir_name:
if Debug>1:
print ("exclude-dir: " + check)
return True
for check in files :
filename, file_extension = os.path.splitext(check)
if '#' in check or '~' in check:
for check2 in excludeFiles :
if check2 in check:
excludeThisFile=True
if excludeThisFile:
continue
if file_extension in extensions:
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
print ("exclude-extension: " + os.path.join(dir_name, check))
###############################################
# the main program
def main(argv):
global justCheck, foundMissing, Debug
justCheck = True
Debug = 0
try:
opts, args = getopt.getopt(argv, "cIhd:", ["check", "implement", "debug="])
except getopt.GetoptError:
print 'do-copyright.py [--check] [--implement] [--debug=0]'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'do-copyright.py [--check] [--implement] [--debug=0]'
sys.exit()
elif opt in ("-c", "--check"):
justCheck = True
elif opt in ("-I", "--implement"):
justCheck = False
elif opt in ("-d", "--debug"):
Debug = int(arg)
if justCheck:
print 'Only checking. No changes. See \'do-copyright.py -h\' for options.'
for root, dirs, files in os.walk('./'):
next_file(root, files)