@@ -31,64 +31,26 @@ def update_timestamp(file_path: str):
31
31
with open (file_path , 'r' ) as file :
32
32
lines = file .readlines ()
33
33
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
-
42
34
# Get current timestamp
43
35
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}\*'
68
38
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 ]
73
41
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
-
79
42
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 ():
82
44
i -= 1
83
45
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
86
48
else :
87
- updated_lines .append (f '\n \n { timestamp_line } ' )
49
+ lines .append ('\n \n ' + timestamp_line )
88
50
89
51
# Write updated lines back to file
90
52
with open (file_path , 'w' ) as file :
91
- file .writelines (updated_lines )
53
+ file .writelines (lines )
92
54
93
55
94
56
file_path = sys .argv [1 ]
0 commit comments