IAP GITLAB

Skip to content
Snippets Groups Projects
Commit 97ca9976 authored by ralfulrich's avatar ralfulrich
Browse files

update do-copyright script

parent 3e6ced5b
No related branches found
No related tags found
2 merge requests!234WIP: Initial example of python as script language from C++,!208Delete AUTHORS file
...@@ -34,6 +34,10 @@ justCheck = True ...@@ -34,6 +34,10 @@ justCheck = True
foundMissing: global variable set to True in case of any needed action foundMissing: global variable set to True in case of any needed action
""" """
foundMissing = False foundMissing = False
"""
updateMessage: T: update message, put preserve the YEAR, F: no special action
"""
updateMessage = False
""" """
forYear: replace this with year for copyright notice via command line forYear: replace this with year for copyright notice via command line
...@@ -43,7 +47,7 @@ forYear="YEAR" ...@@ -43,7 +47,7 @@ forYear="YEAR"
############################################### ###############################################
# #
def checkNote(filename, justCheck, forYear): def checkNote(filename, justCheck, forYear, updateMessage):
""" """
function to check, if the file 'filename' contains an exact copy function to check, if the file 'filename' contains an exact copy
of the copyright notice defined above. of the copyright notice defined above.
...@@ -54,6 +58,10 @@ def checkNote(filename, justCheck, forYear): ...@@ -54,6 +58,10 @@ def checkNote(filename, justCheck, forYear):
the function will attempt to put the correct notice exactly once the function will attempt to put the correct notice exactly once
at the top of the file. The copyright YEAR will be replace with at the top of the file. The copyright YEAR will be replace with
'forYear', e.g. forYear="2019" or forYear="2018-2020", etc. 'forYear', e.g. forYear="2019" or forYear="2018-2020", etc.
If 'updateMessage' is True, then the value of Year will be determined
from the previous copyright message, if possible. Thus, the copyright year
is preserved.
The global variable 'foundMissing' is set to True in the event The global variable 'foundMissing' is set to True in the event
where any changes are identified, BUT not implemented. where any changes are identified, BUT not implemented.
...@@ -99,6 +107,7 @@ def checkNote(filename, justCheck, forYear): ...@@ -99,6 +107,7 @@ def checkNote(filename, justCheck, forYear):
""" now check if first copyright notices is already identical... """ """ now check if first copyright notices is already identical... """
isSame = False isSame = False
prevYear = ""
if len(startNote)>0: if len(startNote)>0:
isSame = True isSame = True
noteLines = text.split('\n') noteLines = text.split('\n')
...@@ -106,12 +115,27 @@ def checkNote(filename, justCheck, forYear): ...@@ -106,12 +115,27 @@ def checkNote(filename, justCheck, forYear):
if startNote[0]+iLine >= len(lines): if startNote[0]+iLine >= len(lines):
isSame = False isSame = False
break break
regex = re.compile(re.escape(noteLines[iLine].strip(" \n")).replace('YEAR','....'))
if not re.match(regex, lines[startNote[0]+iLine].strip(" \n")): # only if updateMessage is True:
# read the YEAR from old message to use it for new message (->update message)
if updateMessage:
regexYear = re.compile(re.escape('Copyright (..+)'))
matchYear = re.match(regexYear, lines[startNote[0]+iLine].strip(" \n"))
if matchYear:
print (str(matchYear.groups()))
if len(matchYear.groups())>0:
prevYear = str(matchYear.groups()[0])
print (" group year: " + prevYear)
regex = re.compile(re.escape(noteLines[iLine].strip(" \n")).replace('YEAR','(..+)'))
match = re.match(regex, lines[startNote[0]+iLine].strip(" \n"))
if not match:
isSame = False isSame = False
foundMissing = True foundMissing = True
print ("needs update: " + filename + "\n [diff] new=\'" + noteLines[iLine] + "\' vs old=\'" + lines[startNote[0]+iLine].rstrip('\n') + "\'") print ("needs update: " + filename + "\n [diff] new=\'" + noteLines[iLine] +
"\' vs old=\'" + lines[startNote[0]+iLine].rstrip('\n') + "\'")
break break
if Debug>0: if Debug>0:
print ("isSame=" + str(isSame) + " " + str(len(startNote))) print ("isSame=" + str(isSame) + " " + str(len(startNote)))
...@@ -132,8 +156,11 @@ def checkNote(filename, justCheck, forYear): ...@@ -132,8 +156,11 @@ def checkNote(filename, justCheck, forYear):
os.rename(filename, filename+".bak") os.rename(filename, filename+".bak")
with open(filename, "w") as file: with open(filename, "w") as file:
textReplace = re.sub(r"Copyright YEAR ", "Copyright " + forYear + " ", text) if len(prevYear)>0:
textReplace = re.sub(r"Copyright YEAR ", "Copyright " + prevYear + " ", text)
else:
textReplace = re.sub(r"Copyright YEAR ", "Copyright " + forYear + " ", text)
file.write(textReplace) file.write(textReplace)
skip = False skip = False
...@@ -161,7 +188,7 @@ def checkNote(filename, justCheck, forYear): ...@@ -161,7 +188,7 @@ def checkNote(filename, justCheck, forYear):
############################################### ###############################################
# #
def next_file(dir_name, files, justCheck, forYear): def next_file(dir_name, files, justCheck, forYear, updateMessage):
""" """
check files: loops over list of files, check files: loops over list of files,
excludes if wished, process otherwise excludes if wished, process otherwise
...@@ -186,7 +213,7 @@ def next_file(dir_name, files, justCheck, forYear): ...@@ -186,7 +213,7 @@ def next_file(dir_name, files, justCheck, forYear):
if excludeThisFile: if excludeThisFile:
continue continue
if file_extension in extensions: if file_extension in extensions:
checkNote(os.path.join(dir_name, check), justCheck, forYear) checkNote(os.path.join(dir_name, check), justCheck, forYear, updateMessage)
else: else:
if Debug>1: if Debug>1:
print ("exclude-extension: " + os.path.join(dir_name, check)) print ("exclude-extension: " + os.path.join(dir_name, check))
...@@ -198,20 +225,24 @@ def main(argv): ...@@ -198,20 +225,24 @@ def main(argv):
""" """
the main program the main program
""" """
global justCheck, foundMissing, Debug, forYear global justCheck, foundMissing, updateMessage, Debug, forYear
justCheck = True justCheck = True
updateMessage = False
Debug = 0 Debug = 0
try: try:
opts, args = getopt.getopt(argv, "cAhd:", ["check", "add=", "debug="]) opts, args = getopt.getopt(argv, "c:uA:hd:", ["check=", "update-message", "add=", "help", "debug="])
except getopt.GetoptError: except getopt.GetoptError:
print ('do-copyright.py [--check] [--add=YEAR] [--debug=0]') print ('do-copyright.py [--check=1] [--add=YEAR] [--update-message] [--debug=0]')
sys.exit(2) sys.exit(2)
for opt, arg in opts: for opt, arg in opts:
if opt == '-h': if opt in ("-h", "--help"):
print ('do-copyright.py [--check] [--add=YEAR] [--debug=0]') print ('do-copyright.py [--check] [--add=YEAR] [--update-message] [--debug=0]')
sys.exit() sys.exit()
elif opt in ("-c", "--check"): elif opt in ("-c", "--check"):
justCheck = True justCheck = bool(arg)
elif opt in ("-u", "--update-message"):
updateMessage = True
print ('Preserve YEAR of existing message, where possible.')
elif opt in ("-A", "--add"): elif opt in ("-A", "--add"):
justCheck = False justCheck = False
forYear = str(arg) forYear = str(arg)
...@@ -221,9 +252,11 @@ def main(argv): ...@@ -221,9 +252,11 @@ def main(argv):
if justCheck: if justCheck:
print ('Only checking. No changes. See \'do-copyright.py -h\' for options.') print ('Only checking. No changes. See \'do-copyright.py -h\' for options.')
else:
print ('WARN: this will modify your files! ')
for root, dirs, files in os.walk('./'): for root, dirs, files in os.walk('./'):
next_file(root, files, justCheck, forYear) next_file(root, files, justCheck, forYear, updateMessage)
############################################### ###############################################
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment