Skip to content

Commit c890f83

Browse files
authored
Update update_readme.py
1 parent 7f4f0e6 commit c890f83

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

.github/scripts/update_readme.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
11
import os
22
from datetime import datetime
3-
import subprocess
4-
from zoneinfo import ZoneInfo # built-in since Python 3.9
3+
from zoneinfo import ZoneInfo # Built-in in Python 3.9+
4+
import re
55

66
README_PATH = "README.md"
77

8-
# Count only .java files in Easy, Medium, Hard
8+
# Count only .java files inside Easy, Medium, Hard (recursive)
99
def count_problems():
1010
count = 0
1111
for folder in ["Easy", "Medium", "Hard"]:
1212
if os.path.exists(folder):
13-
for file in os.listdir(folder):
14-
if os.path.isfile(os.path.join(folder, file)) and file.endswith(".java"):
15-
count += 1
13+
for root, _, files in os.walk(folder):
14+
for file in files:
15+
if file.endswith(".java"):
16+
count += 1
1617
return count
1718

18-
# Get last commit date in human-readable IST format
19+
# Get current IST time in human-readable format
1920
def last_updated():
20-
result = subprocess.run(
21-
["git", "log", "-1", "--format=%cd", "--date=iso"],
22-
capture_output=True,
23-
text=True
24-
)
25-
utc_time_str = result.stdout.strip()
26-
utc_time = datetime.fromisoformat(utc_time_str)
27-
28-
# Convert to IST without pytz
29-
ist_time = utc_time.astimezone(ZoneInfo("Asia/Kolkata"))
30-
31-
# Return formatted date
21+
ist_time = datetime.now(ZoneInfo("Asia/Kolkata"))
3222
return ist_time.strftime("%d %b %Y, %I:%M %p IST")
3323

34-
# Update README.md with the latest values
24+
# Update README.md placeholders
3525
def update_readme():
3626
with open(README_PATH, "r", encoding="utf-8") as f:
3727
content = f.read()
3828

3929
problems_count = count_problems()
4030
updated_time = last_updated()
4131

42-
content = content.replace(
43-
"Problems solved: <!--PROBLEMS_COUNT-->",
44-
f"Problems solved: {problems_count}"
32+
# Use regex to ensure replacement works even if extra spaces
33+
content = re.sub(
34+
r"Problems solved: <!--PROBLEMS_COUNT-->.*",
35+
f"Problems solved: <!--PROBLEMS_COUNT-->{problems_count}",
36+
content
4537
)
46-
content = content.replace(
47-
"Last updated: <!--LAST_UPDATED-->",
48-
f"Last updated: {updated_time}"
38+
content = re.sub(
39+
r"Last updated: <!--LAST_UPDATED-->.*",
40+
f"Last updated: <!--LAST_UPDATED-->{updated_time}",
41+
content
4942
)
5043

5144
with open(README_PATH, "w", encoding="utf-8") as f:

0 commit comments

Comments
 (0)