diff --git a/setup.py b/setup.py index 972ca0d..57eaec9 100755 --- a/setup.py +++ b/setup.py @@ -26,6 +26,6 @@ entry_points={ "console_scripts": ["submit50=submit50.__main__:main"] }, - version="3.2.0", + version="3.2.1", include_package_data=True ) diff --git a/submit50/__main__.py b/submit50/__main__.py index 51e1a6a..0a3a72e 100755 --- a/submit50/__main__.py +++ b/submit50/__main__.py @@ -60,21 +60,35 @@ def check_announcements(): raise Error(res.text.strip()) -def check_version(): - """Check that submit50 is the latest version according to submit50.io.""" +def check_version(package_name=__package__, timeout=5): + """Check that submit50 is the latest version according to submit.cs50.io and PyPI.""" + if not __version__: + return + # Retrieve version info - res = requests.get(f"{SUBMIT_URL}/versions/submit50") + res = requests.get(f"{SUBMIT_URL}/versions/submit50", timeout=timeout) if res.status_code != 200: - raise Error(_("You have an unknown version of submit50. " + raise Error(_("Could not connect to submit.cs50.io." "Please visit our status page https://cs50.statuspage.io for more information.")) - # Check that latest version == version installed - required_version = version.parse(res.text.strip()) - local_version = version.parse(__version__) + # Get the minimum required version from submit.cs50.io + latest_io = version.parse(res.text.strip()) + current = version.parse(__version__) + + # Get PyPI version + latest_pypi = max( + requests.get(f"https://pypi.org/pypi/{package_name}/json", timeout=timeout).json()["releases"], + key=version.parse + ) + latest_pypi = version.parse(latest_pypi) + + # Check for minimum requirement + if current < latest_io: + raise Error(_(f"Current version {current} of {package_name} is no longer supported. Run \"pip3 install --upgrade {package_name}\" now to upgrade.")) - if required_version > local_version: - raise Error(_("You have an outdated version of submit50. " - "Please upgrade.")) + # Check for latest version + if latest_pypi > current: + cprint(f"A newer version of {package_name} is available. Run \"pip3 install --upgrade {package_name}\" now to upgrade.", "magenta") def setup_logging(level):