|
1 | 1 | import os |
2 | 2 | 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 |
5 | 5 |
|
6 | 6 | README_PATH = "README.md" |
7 | 7 |
|
8 | | -# Count only .java files in Easy, Medium, Hard |
| 8 | +# Count only .java files inside Easy, Medium, Hard (recursive) |
9 | 9 | def count_problems(): |
10 | 10 | count = 0 |
11 | 11 | for folder in ["Easy", "Medium", "Hard"]: |
12 | 12 | 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 |
16 | 17 | return count |
17 | 18 |
|
18 | | -# Get last commit date in human-readable IST format |
| 19 | +# Get current IST time in human-readable format |
19 | 20 | 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")) |
32 | 22 | return ist_time.strftime("%d %b %Y, %I:%M %p IST") |
33 | 23 |
|
34 | | -# Update README.md with the latest values |
| 24 | +# Update README.md placeholders |
35 | 25 | def update_readme(): |
36 | 26 | with open(README_PATH, "r", encoding="utf-8") as f: |
37 | 27 | content = f.read() |
38 | 28 |
|
39 | 29 | problems_count = count_problems() |
40 | 30 | updated_time = last_updated() |
41 | 31 |
|
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 |
45 | 37 | ) |
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 |
49 | 42 | ) |
50 | 43 |
|
51 | 44 | with open(README_PATH, "w", encoding="utf-8") as f: |
|
0 commit comments