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):
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...
if (len(startNote)==0):
print ("No copyright note in file: " + filename)
# either we found a file with no copyright, or with wrong copyright notice here
if justCheck:
foundMissing = True
return
# add (new) copyright notice here:
os.rename(filename, filename+".bak")
with open(filename, "w") as file:
textReplace = re.sub(r"Copyright YEAR ", "Copyright " + forYear + " ", text)
file.write(textReplace)
print " [remove " + str(iBlock) + "] " + (lines[iLine]).strip()
if lines[iLine].strip() != "": # if line after comment is empty, also remove it
skip = False
file.close()
###############################################
#
# check files: loops over list of files,
# excludes if wished, process otherwise
#
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:
print ("exclude-extension: " + os.path.join(dir_name, check))
###############################################
# the main program
def main(argv):
opts, args = getopt.getopt(argv, "cAhd:", ["check", "add=", "debug="])
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
sys.exit()
elif opt in ("-c", "--check"):
justCheck = True
forYear = str(arg)
print 'Adding \'Copyright ' + forYear + '\' notice, where needed. '
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)