Skip to content

Check for new versions and warn the user #10

@Tremeschin

Description

@Tremeschin

Pro-tip for PyPI scrappers (lol): https://pypi.org/pypi/shaderflow/json

This url is heavily cached on their CDN, so rate limiting is not an issue. It is, however, a network call that would take precious time away. As always, I am doing this in Python itself with the code below, moving to Pyaket would benefit everyone.

@contextlib.contextmanager
def package_info(package: str) -> Generator[DotMap, None, None]:
    base = Path(Environment.get("VIRTUAL_ENV", tempfile.gettempdir()))

    with BrokenCache.requests(
        cache_name=(base/"pypi-info.cache"),
        expire_after=3600,
    ) as requests:
        url = f"https://pypi.org/pypi/{package}/json"
        yield DotMap(json.loads(requests.get(url).text))

def check_new_version():
    from packaging.version import Version

    # Skip development binaries, as they aren't on PyPI
    if (current := Version(self.VERSION)).is_prerelease:
        return None

    with contextlib.suppress(Exception):
        with BrokenCache.package_info(self.APP_NAME.lower()) as package:
            latest = Version(package.info.version)

            # Newer version available
            if (current < latest):
                log.minor((
                    f"A newer version of the project [bold blue]v{latest}[/] is available! "
                    f"Get it at https://brokensrc.dev/get/releases/ (Current: v{current})"
                ))

            # Back to the future!
            elif (current > latest):
                log.error(f"[bold indian_red]For whatever reason, the current version [bold blue]v{self.VERSION}[/] is newer than the latest [bold blue]v{latest}[/][/]")
                log.error("[bold indian_red]• This is fine if you're running a development or prerelease version, don't worry[/]")
                log.error("[bold indian_red]• Otherwise, it was likely recalled for whatever reason, consider downgrading![/]")

# Warn: Must not interrupt user if actions are being taken (argv)
if Environment.flag("VERSION_CHECK", 1) and (not arguments()):
    with contextlib.suppress(Exception):
        check_new_version()

Sub-issues

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

Status

Working

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions