|
| 1 | +import os |
| 2 | +import re |
| 3 | +import requests |
| 4 | +import hashlib |
| 5 | +from PIL import Image |
| 6 | +from io import BytesIO |
| 7 | + |
| 8 | +# Updates all the links in the CHANGELOG.md file that point to PNG images to |
| 9 | +# WebP format hashed and stored locally in the 'docs/changelog-assets' directory. |
| 10 | + |
| 11 | +# Configuration |
| 12 | +CHANGELOG_PATH = 'CHANGELOG.md' |
| 13 | +ASSETS_DIR = os.path.join('docs', 'changelog-assets') |
| 14 | +IMAGE_URL_PATTERN = re.compile(r'\[([^\]]+)\]\((https://[^)]+\.png)\)') |
| 15 | + |
| 16 | +def ensure_assets_dir(): |
| 17 | + """Ensure that the assets directory exists.""" |
| 18 | + os.makedirs(ASSETS_DIR, exist_ok=True) |
| 19 | + print(f"Assets directory ensured at: {ASSETS_DIR}") |
| 20 | + |
| 21 | +def read_changelog(): |
| 22 | + """Read the content of the CHANGELOG.md file.""" |
| 23 | + with open(CHANGELOG_PATH, 'r', encoding='utf-8') as file: |
| 24 | + content = file.read() |
| 25 | + print(f"Read {len(content)} characters from {CHANGELOG_PATH}") |
| 26 | + return content |
| 27 | + |
| 28 | +def write_changelog(content): |
| 29 | + """Write the updated content back to the CHANGELOG.md file.""" |
| 30 | + with open(CHANGELOG_PATH, 'w', encoding='utf-8') as file: |
| 31 | + file.write(content) |
| 32 | + print(f"Updated {CHANGELOG_PATH}") |
| 33 | + |
| 34 | +def find_image_links(content): |
| 35 | + """Find all markdown links to .png images.""" |
| 36 | + matches = IMAGE_URL_PATTERN.findall(content) |
| 37 | + unique_urls = list(set(url for _, url in matches)) |
| 38 | + print(f"Found {len(unique_urls)} unique image URLs to process.") |
| 39 | + return unique_urls |
| 40 | + |
| 41 | +def download_image(url): |
| 42 | + """Download image from the given URL.""" |
| 43 | + try: |
| 44 | + response = requests.get(url, timeout=10) |
| 45 | + response.raise_for_status() |
| 46 | + print(f"Downloaded image from {url}") |
| 47 | + return response.content |
| 48 | + except requests.RequestException as e: |
| 49 | + print(f"Error downloading {url}: {e}") |
| 50 | + return None |
| 51 | + |
| 52 | +def convert_to_webp(image_data): |
| 53 | + """Convert image data to WebP format.""" |
| 54 | + try: |
| 55 | + with Image.open(BytesIO(image_data)) as img: |
| 56 | + with BytesIO() as output: |
| 57 | + img.save(output, format='WEBP', quality=80) |
| 58 | + webp_data = output.getvalue() |
| 59 | + print("Converted image to WebP format.") |
| 60 | + return webp_data |
| 61 | + except Exception as e: |
| 62 | + print(f"Error converting image to WebP: {e}") |
| 63 | + return None |
| 64 | + |
| 65 | +def hash_webp(webp_data): |
| 66 | + """ |
| 67 | + Hash the WebP data using BLAKE2b with a digest size of 128 bits (16 bytes) |
| 68 | + to match `b2sum --length=128`. |
| 69 | + """ |
| 70 | + blake2b_hash = hashlib.blake2b(webp_data, digest_size=16).hexdigest() |
| 71 | + print(f"Hashed WebP data to {blake2b_hash}") |
| 72 | + return blake2b_hash |
| 73 | + |
| 74 | +def save_webp(webp_data, hash_digest): |
| 75 | + """Save the WebP data to the assets directory with the hash as filename.""" |
| 76 | + filename = f"{hash_digest}.webp" |
| 77 | + filepath = os.path.join(ASSETS_DIR, filename) |
| 78 | + if not os.path.exists(filepath): |
| 79 | + with open(filepath, 'wb') as file: |
| 80 | + file.write(webp_data) |
| 81 | + print(f"Saved WebP image to {filepath}") |
| 82 | + else: |
| 83 | + print(f"WebP image already exists at {filepath}") |
| 84 | + return filepath |
| 85 | + |
| 86 | +def process_images(urls): |
| 87 | + """Process all image URLs: download, convert, hash, and save.""" |
| 88 | + url_to_new_path = {} |
| 89 | + for url in urls: |
| 90 | + print(f"Processing URL: {url}") |
| 91 | + image_data = download_image(url) |
| 92 | + if not image_data: |
| 93 | + continue |
| 94 | + |
| 95 | + webp_data = convert_to_webp(image_data) |
| 96 | + if not webp_data: |
| 97 | + continue |
| 98 | + |
| 99 | + hash_digest = hash_webp(webp_data) |
| 100 | + saved_path = save_webp(webp_data, hash_digest) |
| 101 | + |
| 102 | + # Store the relative path for replacement |
| 103 | + relative_path = os.path.relpath(saved_path, start=os.path.dirname(CHANGELOG_PATH)) |
| 104 | + relative_path = relative_path.replace(os.sep, '/') |
| 105 | + |
| 106 | + # Ensure the path starts with './' |
| 107 | + if not relative_path.startswith(('.', '/')): |
| 108 | + relative_path = f'./{relative_path}' |
| 109 | + |
| 110 | + url_to_new_path[url] = relative_path |
| 111 | + print(f"Updated path for {url}: {relative_path}") |
| 112 | + return url_to_new_path |
| 113 | + |
| 114 | +def update_changelog(content, url_mapping): |
| 115 | + """Update the changelog content with new relative WebP paths.""" |
| 116 | + def replace_url(match): |
| 117 | + text, url = match.groups() |
| 118 | + new_url = url_mapping.get(url, url) |
| 119 | + return f'[{text}]({new_url})' |
| 120 | + |
| 121 | + updated_content = IMAGE_URL_PATTERN.sub(replace_url, content) |
| 122 | + print("Changelog content updated with new image paths.") |
| 123 | + return updated_content |
| 124 | + |
| 125 | +def main(): |
| 126 | + ensure_assets_dir() |
| 127 | + content = read_changelog() |
| 128 | + image_urls = find_image_links(content) |
| 129 | + url_mapping = process_images(image_urls) |
| 130 | + if not url_mapping: |
| 131 | + print("No images were processed. Exiting.") |
| 132 | + return |
| 133 | + updated_content = update_changelog(content, url_mapping) |
| 134 | + write_changelog(updated_content) |
| 135 | + print("All done!") |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + main() |
| 139 | + |
0 commit comments