|
1 | 1 | import os |
2 | 2 | from datetime import datetime |
3 | | -from zoneinfo import ZoneInfo # Built-in in Python 3.9+ |
4 | | -import re |
| 3 | +import subprocess |
5 | 4 |
|
6 | 5 | README_PATH = "README.md" |
7 | 6 |
|
8 | | -# Count only .java files inside Easy, Medium, Hard (recursive) |
| 7 | +# Count problems |
9 | 8 | def count_problems(): |
10 | 9 | count = 0 |
11 | 10 | for folder in ["Easy", "Medium", "Hard"]: |
12 | 11 | if os.path.exists(folder): |
13 | | - for root, _, files in os.walk(folder): |
14 | | - for file in files: |
15 | | - if file.endswith(".java"): |
16 | | - count += 1 |
| 12 | + for file in os.listdir(folder): |
| 13 | + if os.path.isfile(os.path.join(folder, file)): |
| 14 | + count += 1 |
17 | 15 | return count |
18 | 16 |
|
19 | | -# Get current IST time in human-readable format |
| 17 | +# Get last commit date |
20 | 18 | def last_updated(): |
21 | | - ist_time = datetime.now(ZoneInfo("Asia/Kolkata")) |
22 | | - return ist_time.strftime("%d %b %Y, %I:%M %p IST") |
| 19 | + result = subprocess.run( |
| 20 | + ["git", "log", "-1", "--format=%cd", "--date=iso"], |
| 21 | + capture_output=True, |
| 22 | + text=True |
| 23 | + ) |
| 24 | + return result.stdout.strip() |
23 | 25 |
|
24 | | -# Update README.md placeholders |
25 | 26 | def update_readme(): |
26 | 27 | with open(README_PATH, "r", encoding="utf-8") as f: |
27 | 28 | content = f.read() |
28 | 29 |
|
29 | 30 | problems_count = count_problems() |
30 | 31 | updated_time = last_updated() |
31 | 32 |
|
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 |
| 33 | + content = content.replace( |
| 34 | + "Problems solved: <!--PROBLEMS_COUNT-->", |
| 35 | + f"Problems solved: {problems_count}" |
37 | 36 | ) |
38 | | - content = re.sub( |
39 | | - r"Last updated: <!--LAST_UPDATED-->.*", |
40 | | - f"Last updated: <!--LAST_UPDATED-->{updated_time}", |
41 | | - content |
| 37 | + content = content.replace( |
| 38 | + "Last updated: <!--LAST_UPDATED-->", |
| 39 | + f"Last updated: {updated_time}" |
42 | 40 | ) |
43 | 41 |
|
44 | 42 | with open(README_PATH, "w", encoding="utf-8") as f: |
|
0 commit comments