Skip to content

Commit b20546a

Browse files
authoredNov 19, 2023
Merge pull request #339 from cs50/rongxin-patch-1
Replace pkg_resources with importlib.metadata for Python 3.12
2 parents 3befc8f + d550e3d commit b20546a

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed
 

‎setup.py

100644100755
+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
description="This is submit50, with which you can submit solutions to problems for CS50.",
1818
long_description="This is submit50, with which you can submit solutions to problems for CS50.",
19-
install_requires=["lib50>=3,<4", "requests>=2.19", "setuptools", "termcolor>=1.1"],
19+
install_requires=["lib50>=3,<4", "packaging", "requests>=2.19", "setuptools", "termcolor>=1.1"],
2020
keywords=["submit", "submit50"],
2121
name="submit50",
2222
python_requires=">=3.6",
@@ -26,6 +26,6 @@
2626
entry_points={
2727
"console_scripts": ["submit50=submit50.__main__:main"]
2828
},
29-
version="3.1.2",
29+
version="3.1.3",
3030
include_package_data=True
3131
)

‎submit50/__init__.py

100644100755
+4-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
from pkg_resources import get_distribution, DistributionNotFound
1+
from importlib.metadata import PackageNotFoundError, version
22
import os
33

4-
# https://stackoverflow.com/questions/17583443/what-is-the-correct-way-to-share-package-version-with-setup-py-and-the-package
54
try:
6-
_dist = get_distribution("submit50")
7-
# Normalize path for cross-OS compatibility.
8-
_dist_loc = os.path.normcase(_dist.location)
9-
_here = os.path.normcase(__file__)
10-
if not _here.startswith(os.path.join(_dist_loc, "submit50")):
11-
# This version is not installed, but another version is.
12-
raise DistributionNotFound
13-
except DistributionNotFound:
14-
__version__ = "locally installed, no version information available"
15-
else:
16-
__version__ = _dist.version
5+
__version__ = version("submit50")
6+
except PackageNotFoundError:
7+
__version__ = "UNKNOWN"
178

189
CONFIG_LOADER = __import__("lib50").config.Loader("submit50")
1910
CONFIG_LOADER.scope("files", "include", "exclude", "require")

‎submit50/__main__.py

100644100755
+12-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import enum
44
import gettext
55
import logging
6-
import pkg_resources
76
import re
87
import shutil
98
import sys
@@ -14,10 +13,12 @@
1413
import requests
1514
import termcolor
1615

16+
from importlib.resources import files
17+
from packaging import version
1718
from . import __version__, CONFIG_LOADER
1819

1920
# Internationalization
20-
gettext.install("submit50", pkg_resources.resource_filename("submit50", "locale"))
21+
gettext.install("submit50", str(files("submit50").joinpath("locale")))
2122

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

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

6768
# Check that latest version == version installed
68-
required_version = pkg_resources.parse_version(res.text.strip())
69-
local_version = pkg_resources.parse_version(__version__)
69+
required_version = version.parse(res.text.strip())
70+
local_version = version.parse(__version__)
7071

7172
if required_version > local_version:
7273
raise Error(_("You have an outdated version of submit50. "
@@ -126,7 +127,7 @@ def prompt(honesty, included, excluded):
126127
# If there's no honesty question, continue.
127128
if not honesty:
128129
return True
129-
130+
130131
# Prompt for honesty
131132
try:
132133
# Show default message
@@ -138,17 +139,17 @@ def prompt(honesty, included, excluded):
138139
# If a custom message is configured, show that instead
139140
else:
140141
honesty_question = str(honesty)
141-
142+
142143
# Get the user's answer
143144
answer = input(honesty_question)
144145
except EOFError:
145146
answer = None
146147
print()
147-
148+
148149
# If no answer given, or yes is not given, don't continue
149150
if not answer or not re.match(f"^\s*(?:{_('y|yes')})\s*$", answer, re.I):
150151
return False
151-
152+
152153
# Otherwise, do continue
153154
return True
154155

@@ -199,12 +200,12 @@ def main():
199200
'\ndebug: adds the output of all commands run.')
200201
)
201202
parser.add_argument(
202-
"-V", "--version",
203-
action="version",
203+
"-V", "--version",
204+
action="version",
204205
version=f"%(prog)s {__version__}"
205206
)
206207
parser.add_argument(
207-
"slug",
208+
"slug",
208209
help=_("prescribed identifier of work to submit")
209210
)
210211

0 commit comments

Comments
 (0)
Please sign in to comment.