Commit d65d2033 authored by Ted Themistokleous's avatar Ted Themistokleous
Browse files

Update license stamper to add current new year and update files

parent e3e58c69
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
# THE SOFTWARE. # THE SOFTWARE.
##################################################################################### #####################################################################################
import subprocess, os import subprocess, os
import datetime
#Debug flag #Debug flag
debug = False debug = False
...@@ -65,17 +66,20 @@ def hasKeySequence(inputfile, key_message): ...@@ -65,17 +66,20 @@ def hasKeySequence(inputfile, key_message):
result = False result = False
line_cnt = 0 line_cnt = 0
line_limit = 10 line_limit = 10
target_line = -1
for line in inputfile: for line in inputfile:
line_cnt = line_cnt + 1
if key_message in line: if key_message in line:
result = True result = True
target_line = line_cnt
break break
line_cnt = line_cnt + 1
if line_cnt >= line_limit: if line_cnt >= line_limit:
break break
return result return [result, target_line]
# Header and footer of the comment block # Header and footer of the comment block
...@@ -116,6 +120,10 @@ def openAndWriteFile(filename, message, commentChar): ...@@ -116,6 +120,10 @@ def openAndWriteFile(filename, message, commentChar):
save_markdown_lines = [] save_markdown_lines = []
modify_markdown = False modify_markdown = False
current_year = str(datetime.date.today().year)
needs_update = False
update_line = -1
#open save old contents and append things here #open save old contents and append things here
if debug is True: if debug is True:
print("Open", filename, end='') print("Open", filename, end='')
...@@ -155,12 +163,23 @@ def openAndWriteFile(filename, message, commentChar): ...@@ -155,12 +163,23 @@ def openAndWriteFile(filename, message, commentChar):
hasOtherLic = hasKeySequence(save, "Software License") hasOtherLic = hasKeySequence(save, "Software License")
#Check if we have a licence stamp already #Check if we have a licence stamp already
if hasAmdLic or hasOtherLic is True: if hasAmdLic[0] or hasOtherLic[0] is True:
if debug is True: hasOldAmdLic = hasKeySequence(
print("....Already Stamped: Skipping file ") save, "2015-" + current_year +
" Advanced Micro Devices, Inc. All rights reserved.")
if hasOldAmdLic[0] is True and hasOtherLic[0] is False:
if debug is True:
print("....License Out of Date: Updating file ")
needs_update = True
update_line = hasOldAmdLic[1]
else:
if debug is True:
print("....Already Stamped: Skipping file ")
contents.close() contents.close()
return return
except UnicodeDecodeError as eu: except UnicodeDecodeError as eu:
if debug is True: if debug is True:
...@@ -171,35 +190,50 @@ def openAndWriteFile(filename, message, commentChar): ...@@ -171,35 +190,50 @@ def openAndWriteFile(filename, message, commentChar):
if debug is True: if debug is True:
print("...Writing header", end='') print("...Writing header", end='')
with open(filename, 'w') as contents: if needs_update is True and update_line > -1:
#append the licence to the top of the file with open(filename, rw) as contents:
data = [next(filename) for x in range(update_line + 1)]
index = data[update_line].index("-")
data[update_line] = data[
update_line][:index] + current_year + data[update_line][index +
4:]
#write remaining contents
contents.write(data)
elif needs_update is False:
with open(filename, 'w') as contents:
#append the licence to the top of the file
#Append shebang before license #Append shebang before license
if add_shebang is True: if add_shebang is True:
contents.write(saved_shebang + "\n") contents.write(saved_shebang + "\n")
#Append markdown hooks before license #Append markdown hooks before license
if modify_markdown is True: if modify_markdown is True:
contents.write(''.join(str(x) for x in save_markdown_lines)) contents.write(''.join(str(x) for x in save_markdown_lines))
delim = topHeader(commentChar) delim = topHeader(commentChar)
if delim is not None: if delim is not None:
contents.write(delim) contents.write(delim)
#print(delim) #print(delim)
if modify_markdown is False: if modify_markdown is False:
for line in message: for line in message:
if line != '': if line != '':
contents.write(commentChar + " " + line + "\n") contents.write(commentChar + " " + line + "\n")
else: else:
contents.write(commentChar + "\n") contents.write(commentChar + "\n")
delim = bottomFooter(commentChar) delim = bottomFooter(commentChar)
if delim is not None: if delim is not None:
contents.write(delim) contents.write(delim)
#write remaining contents #write remaining contents
contents.write(save) contents.write(save)
if debug is True: if debug is True:
print("...done") print("...done")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment