Skip to content

Commit 11d2f82

Browse files
committed
pypi check wip
1 parent a17d9c5 commit 11d2f82

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

submit50/__main__.py

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

6262

63-
def check_version():
63+
def check_version(package_name=__package__, timeout=15):
6464
"""Check that submit50 is the latest version according to submit50.io."""
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. "
69-
"Please visit our status page https://cs50.statuspage.io for more information."))
71+
raise Error(_("Could not connect to submit.cs50.io."
72+
"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 submit.cs50.io version
75+
latest_io = version.parse(res.text.strip())
76+
current = version.parse(__version__)
7477

75-
if required_version > local_version:
76-
raise Error(_("You have an outdated version of submit50. "
77-
"Please upgrade."))
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+
pass
88+
89+
# Check for latest version
90+
if latest_pypi > current or latest_io > current:
91+
raise Error(_(f"A newer version of {package_name} is available. Run pip3 install --upgrade {package_name} to upgrade."))
92+
93+
# Resolve if submit.cs50.io version differs from PyPi
94+
if latest_pypi != latest_io:
95+
pass
7896

7997

8098
def setup_logging(level):
@@ -179,7 +197,7 @@ def check_slug_year(slug):
179197
# Ask if they want to continue
180198
if not re.match(f"^\s*(?:{_('y|yes')})\s*$", input(_("Do you want to continue with this submission (yes/no)? ")), re.I):
181199
raise Error(_("User aborted submission."))
182-
200+
183201
except ValueError:
184202
pass
185203

@@ -246,7 +264,7 @@ def main():
246264
check_announcements()
247265
check_version()
248266
check_slug_year(args.slug)
249-
267+
250268
user_name, commit_hash, message = lib50.push("submit50", args.slug, CONFIG_LOADER, prompt=prompt)
251269
print(message)
252270

0 commit comments

Comments
 (0)