-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_date.py
More file actions
49 lines (39 loc) · 1.6 KB
/
update_date.py
File metadata and controls
49 lines (39 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import subprocess
from datetime import datetime, timezone
# Get the list of modified files
result = subprocess.run(['git', 'diff', '--name-only', 'HEAD~1'], stdout=subprocess.PIPE)
modified_files = result.stdout.decode('utf-8').split()
# Debugging: Print the list of modified files
print("Modified files:", modified_files)
# Filter for Markdown files
modified_md_files = [f for f in modified_files if f.endswith('.md')]
# Debugging: Print the list of modified Markdown files
print("Modified Markdown files:", modified_md_files)
# Current date
current_date = datetime.now(timezone.utc).strftime('%Y-%m-%d')
# Function to update the last modified date in a file
def update_date_in_file(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
updated = False
with open(file_path, 'w') as file:
for line in lines:
if line.startswith('Last updated:'):
file.write(f'Last updated: {current_date}\n')
updated = True
else:
file.write(line)
if not updated:
file.write(f'\nLast updated: {current_date}\n')
# Check if there are any modified Markdown files
if not modified_md_files:
print("No modified Markdown files found.")
exit(0)
# Update the date in each modified Markdown file
for file_path in modified_md_files:
print(f"Updating file: {file_path}") # Debugging: Print the file being updated
update_date_in_file(file_path)
# Add and commit changes
subprocess.run(['git', 'add', '-A'])
subprocess.run(['git', 'commit', '-m', 'Update last modified date in Markdown files'])