Skip to content

Commit 31d72e6

Browse files
authored
Merge pull request #400 from cs50/version-check
Update version check
2 parents a17d9c5 + c5548a1 commit 31d72e6

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
entry_points={
2727
"console_scripts": ["submit50=submit50.__main__:main"]
2828
},
29-
version="3.2.0",
29+
version="3.2.1",
3030
include_package_data=True
3131
)

submit50/__main__.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,35 @@ def check_announcements():
6060
raise Error(res.text.strip())
6161

6262

63-
def check_version():
64-
"""Check that submit50 is the latest version according to submit50.io."""
63+
def check_version(package_name=__package__, timeout=5):
64+
"""Check that submit50 is the latest version according to submit.cs50.io and PyPI."""
65+
if not __version__:
66+
return
67+
6568
# Retrieve version info
66-
res = requests.get(f"{SUBMIT_URL}/versions/submit50")
69+
res = requests.get(f"{SUBMIT_URL}/versions/submit50", timeout=timeout)
6770
if res.status_code != 200:
68-
raise Error(_("You have an unknown version of submit50. "
71+
raise Error(_("Could not connect to submit.cs50.io."
6972
"Please visit our status page https://cs50.statuspage.io for more information."))
7073

71-
# Check that latest version == version installed
72-
required_version = version.parse(res.text.strip())
73-
local_version = version.parse(__version__)
74+
# Get the minimum required version from submit.cs50.io
75+
latest_io = version.parse(res.text.strip())
76+
current = version.parse(__version__)
77+
78+
# Get PyPI version
79+
latest_pypi = max(
80+
requests.get(f"https://pypi.org/pypi/{package_name}/json", timeout=timeout).json()["releases"],
81+
key=version.parse
82+
)
83+
latest_pypi = version.parse(latest_pypi)
84+
85+
# Check for minimum requirement
86+
if current < latest_io:
87+
raise Error(_(f"Current version {current} of {package_name} is no longer supported. Run \"pip3 install --upgrade {package_name}\" now to upgrade."))
7488

75-
if required_version > local_version:
76-
raise Error(_("You have an outdated version of submit50. "
77-
"Please upgrade."))
89+
# Check for latest version
90+
if latest_pypi > current:
91+
cprint(f"A newer version of {package_name} is available. Run \"pip3 install --upgrade {package_name}\" now to upgrade.", "magenta")
7892

7993

8094
def setup_logging(level):

0 commit comments

Comments
 (0)