Skip to content

Commit 8ae5e0e

Browse files
authored
Merge pull request #116 from cs50/rongxin-patch-1
Replace pkg_resources with importlib.metadata for Python 3.12
2 parents 14d34fa + 68becf3 commit 8ae5e0e

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

cli50/__init__.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
import os
2-
from pkg_resources import DistributionNotFound, get_distribution
2+
import sys
3+
from importlib.metadata import PackageNotFoundError, version
34

4-
# https://stackoverflow.com/a/17638236/5156190
5-
try:
6-
7-
# Get package's distribution
8-
_dist = get_distribution("cli50")
9-
10-
# Normalize path for cross-OS compatibility
11-
_dist_loc = os.path.normcase(_dist.location)
12-
_here = os.path.normcase(__file__)
5+
# Require Python 3.8+
6+
if sys.version_info < (3, 8):
7+
sys.exit("You have an old version of python. Install version 3.8 or higher.")
138

14-
# This version is not installed, but another version is
15-
if not _here.startswith(os.path.join(_dist_loc, "cli50")):
16-
raise DistributionNotFound
17-
18-
except DistributionNotFound:
19-
__version__ = None
20-
21-
else:
22-
__version__ = _dist.version
9+
# Get version
10+
try:
11+
__version__ = version("cli50")
12+
except PackageNotFoundError:
13+
__version__ = "UNKNOWN"

cli50/__main__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import inflect
99
import json
1010
import os
11-
import pkg_resources
1211
import re
1312
import requests
1413
import shlex
1514
import shutil
1615
import subprocess
1716
import textwrap
1817
import tzlocal
18+
from importlib.resources import files
19+
from packaging import version
1920

2021
from . import __version__
2122

@@ -35,7 +36,7 @@
3536
TAG = "latest"
3637

3738
# Internationalization
38-
t = gettext.translation("cli50", pkg_resources.resource_filename("cli50", "locale"), fallback=True)
39+
t = gettext.translation("cli50", str(files("cli50").joinpath("locale")), fallback=True)
3940
t.install()
4041

4142

@@ -61,7 +62,7 @@ def main():
6162
# Check PyPI for newer version
6263
if __version__ and not args["fast"]:
6364
try:
64-
release = max(requests.get("https://pypi.org/pypi/cli50/json").json()["releases"], key=pkg_resources.parse_version)
65+
release = max(requests.get("https://pypi.org/pypi/cli50/json").json()["releases"], key=version.parse)
6566
assert release <= __version__
6667
except requests.RequestException:
6768
pass

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
classifiers=[
77
"Intended Audience :: Developers",
88
"Intended Audience :: System Administrators",
9-
"Programming Language :: Python :: 3.6",
9+
"Programming Language :: Python :: 3.8",
1010
"Topic :: Software Development"
1111
],
1212
message_extractors = {
@@ -15,7 +15,7 @@
1515
description="This is CS50 CLI, with which you can mount a directory inside of an Ubuntu container.",
1616
long_description=open("README.md").read(),
1717
license="GPLv3",
18-
install_requires=["inflect", "requests", "tzlocal"],
18+
install_requires=["inflect", "packaging", "requests", "tzlocal"],
1919
keywords="cli50",
2020
name="cli50",
2121
python_requires=">=3.8",
@@ -24,6 +24,6 @@
2424
"console_scripts": ["cli50=cli50.__main__:main"]
2525
},
2626
url="https://github.com/cs50/cli50",
27-
version="7.5.1",
27+
version="8.0.0",
2828
include_package_data=True
2929
)

0 commit comments

Comments
 (0)