|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import json
|
| 4 | +import re |
2 | 5 | import urllib.request
|
3 | 6 | from urllib.request import OpenerDirector
|
4 | 7 | from warnings import warn
|
@@ -46,21 +49,77 @@ def parse_version(data: str) -> str:
|
46 | 49 | return ""
|
47 | 50 |
|
48 | 51 |
|
| 52 | +def compare_versions(v1: tuple[int | str, ...], v2: tuple[int | str, ...]) -> bool: |
| 53 | + """Compare two version tuples. |
| 54 | + Returns True if v1 > v2, False otherwise. |
| 55 | +
|
| 56 | + Special rules: |
| 57 | + 1. Release version > pre-release version (e.g., (1, 4) > (1, 4, 'beta')) |
| 58 | + 2. Numeric parts are compared numerically |
| 59 | + 3. String parts are compared lexicographically |
| 60 | + """ |
| 61 | + # First compare common parts |
| 62 | + for p1, p2 in zip(v1, v2): |
| 63 | + if p1 != p2: |
| 64 | + # If both are same type, direct comparison works |
| 65 | + if isinstance(p1, int) and isinstance(p2, int): |
| 66 | + return p1 > p2 |
| 67 | + if isinstance(p1, str) and isinstance(p2, str): |
| 68 | + return p1 > p2 |
| 69 | + # If types differ, numbers are greater (release > pre-release) |
| 70 | + return isinstance(p1, int) |
| 71 | + |
| 72 | + # If we get here, all common parts are equal |
| 73 | + # Longer version is greater only if next element is a number |
| 74 | + if len(v1) > len(v2): |
| 75 | + return isinstance(v1[len(v2)], int) |
| 76 | + if len(v2) > len(v1): |
| 77 | + # v2 is longer, so v1 is greater only if v2's next part is a string (pre-release) |
| 78 | + return isinstance(v2[len(v1)], str) |
| 79 | + |
| 80 | + return False # Versions are equal |
| 81 | + |
| 82 | + |
| 83 | +def parse_version_parts(version_str: str) -> tuple[int | str, ...]: |
| 84 | + """Convert version string to tuple of (int | str) parts following PEP 440 conventions. |
| 85 | +
|
| 86 | + Examples: |
| 87 | + "1.4.24" -> (1, 4, 24) |
| 88 | + "1.4beta" -> (1, 4, "beta") |
| 89 | + "1.4.beta2" -> (1, 4, "beta", 2) |
| 90 | + "1.4.alpha2" -> (1, 4, "alpha", 2) |
| 91 | + """ |
| 92 | + parts = [] |
| 93 | + # First split by dots |
| 94 | + for part in version_str.split("."): |
| 95 | + # Then parse each part for numbers and letters |
| 96 | + segments = re.findall(r"([0-9]+|[a-zA-Z]+)", part) |
| 97 | + for segment in segments: |
| 98 | + if segment.isdigit(): |
| 99 | + parts.append(int(segment)) |
| 100 | + else: |
| 101 | + parts.append(segment.lower()) |
| 102 | + return tuple(parts) |
| 103 | + |
| 104 | + |
49 | 105 | def check_for_updates() -> None:
|
50 | 106 | try:
|
51 | 107 | data = fetch_version_info()
|
52 | 108 | latest_version = parse_version(data)
|
53 |
| - if latest_version and latest_version != current_version: |
54 |
| - warn( |
55 |
| - f"A new version of Albumentations is available: {latest_version} (you have {current_version}). " # noqa: S608 |
56 |
| - "Upgrade using: pip install -U albumentations. " |
57 |
| - "To disable automatic update checks, set the environment variable NO_ALBUMENTATIONS_UPDATE to 1.", |
58 |
| - UserWarning, |
59 |
| - stacklevel=2, |
60 |
| - ) |
61 |
| - except Exception as e: # General exception catch to ensure silent failure # noqa: BLE001 |
| 109 | + if latest_version: |
| 110 | + latest_parts = parse_version_parts(latest_version) |
| 111 | + current_parts = parse_version_parts(current_version) |
| 112 | + if compare_versions(latest_parts, current_parts): |
| 113 | + warn( |
| 114 | + f"A new version of Albumentations is available: {latest_version!r} (you have {current_version!r}). " |
| 115 | + "Upgrade using: pip install -U albumentations. " |
| 116 | + "To disable automatic update checks, set the environment variable NO_ALBUMENTATIONS_UPDATE to 1.", |
| 117 | + UserWarning, |
| 118 | + stacklevel=2, |
| 119 | + ) |
| 120 | + except Exception as e: # General exception catch to ensure silent failure # noqa: BLE001 |
62 | 121 | warn(
|
63 |
| - f"Failed to check for updates due to an unexpected error: {e}. " # noqa: S608 |
| 122 | + f"Failed to check for updates due to error: {e}. " |
64 | 123 | "To disable automatic update checks, set the environment variable NO_ALBUMENTATIONS_UPDATE to 1.",
|
65 | 124 | UserWarning,
|
66 | 125 | stacklevel=2,
|
|
0 commit comments