Skip to content

Commit

Permalink
Merge pull request #339 from cs50/rongxin-patch-1
Browse files Browse the repository at this point in the history
Replace pkg_resources with importlib.metadata for Python 3.12
  • Loading branch information
rongxin-liu authored Nov 19, 2023
2 parents 3befc8f + d550e3d commit b20546a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
4 changes: 2 additions & 2 deletions setup.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
description="This is submit50, with which you can submit solutions to problems for CS50.",
long_description="This is submit50, with which you can submit solutions to problems for CS50.",
install_requires=["lib50>=3,<4", "requests>=2.19", "setuptools", "termcolor>=1.1"],
install_requires=["lib50>=3,<4", "packaging", "requests>=2.19", "setuptools", "termcolor>=1.1"],
keywords=["submit", "submit50"],
name="submit50",
python_requires=">=3.6",
Expand All @@ -26,6 +26,6 @@
entry_points={
"console_scripts": ["submit50=submit50.__main__:main"]
},
version="3.1.2",
version="3.1.3",
include_package_data=True
)
17 changes: 4 additions & 13 deletions submit50/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
from pkg_resources import get_distribution, DistributionNotFound
from importlib.metadata import PackageNotFoundError, version
import os

# https://stackoverflow.com/questions/17583443/what-is-the-correct-way-to-share-package-version-with-setup-py-and-the-package
try:
_dist = get_distribution("submit50")
# Normalize path for cross-OS compatibility.
_dist_loc = os.path.normcase(_dist.location)
_here = os.path.normcase(__file__)
if not _here.startswith(os.path.join(_dist_loc, "submit50")):
# This version is not installed, but another version is.
raise DistributionNotFound
except DistributionNotFound:
__version__ = "locally installed, no version information available"
else:
__version__ = _dist.version
__version__ = version("submit50")
except PackageNotFoundError:
__version__ = "UNKNOWN"

CONFIG_LOADER = __import__("lib50").config.Loader("submit50")
CONFIG_LOADER.scope("files", "include", "exclude", "require")
23 changes: 12 additions & 11 deletions submit50/__main__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import enum
import gettext
import logging
import pkg_resources
import re
import shutil
import sys
Expand All @@ -14,10 +13,12 @@
import requests
import termcolor

from importlib.resources import files
from packaging import version
from . import __version__, CONFIG_LOADER

# Internationalization
gettext.install("submit50", pkg_resources.resource_filename("submit50", "locale"))
gettext.install("submit50", str(files("submit50").joinpath("locale")))

SUBMIT_URL = "https://submit.cs50.io"

Expand Down Expand Up @@ -65,8 +66,8 @@ def check_version():
"Please visit our status page https://cs50.statuspage.io for more information."))

# Check that latest version == version installed
required_version = pkg_resources.parse_version(res.text.strip())
local_version = pkg_resources.parse_version(__version__)
required_version = version.parse(res.text.strip())
local_version = version.parse(__version__)

if required_version > local_version:
raise Error(_("You have an outdated version of submit50. "
Expand Down Expand Up @@ -126,7 +127,7 @@ def prompt(honesty, included, excluded):
# If there's no honesty question, continue.
if not honesty:
return True

# Prompt for honesty
try:
# Show default message
Expand All @@ -138,17 +139,17 @@ def prompt(honesty, included, excluded):
# If a custom message is configured, show that instead
else:
honesty_question = str(honesty)

# Get the user's answer
answer = input(honesty_question)
except EOFError:
answer = None
print()

# If no answer given, or yes is not given, don't continue
if not answer or not re.match(f"^\s*(?:{_('y|yes')})\s*$", answer, re.I):
return False

# Otherwise, do continue
return True

Expand Down Expand Up @@ -199,12 +200,12 @@ def main():
'\ndebug: adds the output of all commands run.')
)
parser.add_argument(
"-V", "--version",
action="version",
"-V", "--version",
action="version",
version=f"%(prog)s {__version__}"
)
parser.add_argument(
"slug",
"slug",
help=_("prescribed identifier of work to submit")
)

Expand Down

0 comments on commit b20546a

Please sign in to comment.