Skip to content

Commit 54e17f1

Browse files
committed
Simplified timestamp addition to end of a file
1 parent fdd6f92 commit 54e17f1

File tree

1 file changed

+9
-47
lines changed

1 file changed

+9
-47
lines changed

.jenkins/update_timestamps.py

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,64 +31,26 @@ def update_timestamp(file_path: str):
3131
with open(file_path, 'r') as file:
3232
lines = file.readlines()
3333

34-
author_line_index = -1
35-
36-
# Find the index of the author line and extract author's name and GitHub link
37-
for i, line in enumerate(lines):
38-
if re.search(r'(Author|Authors).*?:', line):
39-
author_line_index = i
40-
break
41-
4234
# Get current timestamp
4335
timestamp = get_last_commit_timestamp_for_file(file_path)
44-
timestamp_line = f'**Updated:** *{timestamp}*\n'
45-
46-
# If author line is found, add timestamp below it
47-
if author_line_index != -1:
48-
49-
if lines[author_line_index].startswith('#'):
50-
# We can assume we need a #, too
51-
timestamp_line = '# ' + timestamp_line
52-
53-
updated_lines = lines[:author_line_index + 1]
54-
# Check if the timestamp line exists below the author line or if there are only blank lines between them
55-
if author_line_index + 1 < len(lines) and (lines[author_line_index + 1].strip() == '' or re.search(r'\*\*Updated:\*\*\s\**\d{1,2}:\d{2} [AP]M, \w+ \d{1,2}, \d{4}\*', lines[author_line_index + 1])):
56-
# If timestamp line exists or there are only blank lines, update it
57-
i = author_line_index + 1
58-
while i < len(lines) and lines[i].strip() == '':
59-
# Find first non-empty line after Author
60-
updated_lines.append(lines[i])
61-
i += 1
62-
63-
if re.search(r'\*\*Updated:\*\*\s\**\d{1,2}:\d{2} [AP]M, \w+ \d{1,2}, \d{4}\*', lines[i]):
64-
updated_lines.append(timestamp_line)
65-
else:
66-
updated_lines[author_line_index + 1] = timestamp_line
67-
if i == author_line_index + 2: updated_lines.append('\n')
36+
timestamp_line = f'{"# " if file_path.endswith("py") else ""}**Updated:** *{timestamp}*\n'
37+
timestamp_pattern = r'\*\*Updated:\*\*\s\**\d{1,2}:\d{2} [AP]M, \w+ \d{1,2}, \d{4}\*'
6838

69-
updated_lines.extend(lines[i:])
70-
else:
71-
# If timestamp line does not exist and there are no blank lines, add it below author line
72-
updated_lines += [timestamp_line, '\n'] + lines[author_line_index + 1:]
39+
if not lines:
40+
lines = [timestamp_line]
7341
else:
74-
# If author line is not found, add timestamp to the last line
75-
updated_lines = lines
76-
77-
if file_path.endswith('.py'): timestamp_line = '# ' + timestamp_line
78-
7942
i = len(lines) - 1
80-
while i >= 0 and lines[i].strip() == '':
81-
# Go to the last non-blank line, check if it is the timestamp
43+
while i > 0 and not lines[i].strip():
8244
i -= 1
8345

84-
if i >= 0 and re.search(r'\*\*Updated:\*\*\s\**\d{1,2}:\d{2} [AP]M, \w+ \d{1,2}, \d{4}\*', lines[i]):
85-
updated_lines[i] = timestamp_line
46+
if re.search(timestamp_pattern, lines[i]):
47+
lines[i] = timestamp_line
8648
else:
87-
updated_lines.append(f'\n\n{timestamp_line}')
49+
lines.append('\n\n' + timestamp_line)
8850

8951
# Write updated lines back to file
9052
with open(file_path, 'w') as file:
91-
file.writelines(updated_lines)
53+
file.writelines(lines)
9254

9355

9456
file_path = sys.argv[1]

0 commit comments

Comments
 (0)