Skip to content

Commit d6256c8

Browse files
committed
Added conditional log/parsing for early exit
1 parent 6c4bca4 commit d6256c8

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

.github/workflows/update-root-downloads-shield-weekly.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ jobs:
2828
- name: Update README shield
2929
id: update_shield
3030
run: |
31-
python3 utils/update_root_downloads_shield.py
32-
if [[ $? -eq 0 ]] ; then
33-
echo "Shield updated!"
31+
output=$(python3 utils/update_root_downloads_shield.py)
32+
echo "$output"
33+
if echo "$output" | grep -q "Shield updated!" ; then
3434
echo "SHIELD_UPDATED=true" >> $GITHUB_ENV
3535
else
36-
echo "No update needed."
3736
echo "SHIELD_UPDATED=false" >> $GITHUB_ENV
3837
fi
3938

utils/update_root_downloads_shield.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,22 @@ def write_file(file_path: str, lines: list[str]) -> None:
8080
with open(file_path, 'w', encoding='utf-8') as file:
8181
file.writelines(lines)
8282

83-
def update_downloads_shield(readme_path: str, downloads: int) -> None:
83+
def update_downloads_shield(readme_path: str, downloads: int) -> bool:
8484
import re
8585
lines = read_file(readme_path)
8686
shield_re = r'(?i)(<img[^>]+src="https://img.shields.io/badge/Downloads-)([\d,\.km]+)(-[a-f\d]{6})'
8787
downloads_str = f'{format_total(downloads).lower()}'
88+
shield_updated = False
8889
for idx, line in enumerate(lines):
8990
shield_match = re.search(shield_re, line)
9091
if shield_match:
9192
new_line = re.sub(shield_match.group(2), downloads_str, line)
92-
lines[idx] = new_line
93-
print(f'»»» {new_line.strip()}\n')
94-
write_file(readme_path, lines)
93+
if new_line != line:
94+
lines[idx] = new_line
95+
shield_updated = True
96+
print(f'»»» {new_line.strip()}\n')
97+
if shield_updated : write_file(readme_path, lines)
98+
return shield_updated
9599

96100
def main() -> None:
97101
grand_total_dls = 0
@@ -102,7 +106,7 @@ def main() -> None:
102106
print('-' *45)
103107
print(f"{'TOTAL DOWNLOADS':30} {grand_total_dls:,}\n")
104108
print(f'Updating {README_PATH}...\n')
105-
update_downloads_shield(README_PATH, grand_total_dls)
106-
print('Done!')
109+
shield_updated = update_downloads_shield(README_PATH, grand_total_dls)
110+
print('Shield updated!' if shield_updated else 'No update needed.')
107111

108112
if __name__ == '__main__' : main()

0 commit comments

Comments
 (0)