From ce071d4f10e18cd66d12035d5967052c448ecd5d Mon Sep 17 00:00:00 2001 From: Jeff Widman Date: Thu, 11 Apr 2024 12:43:17 -0500 Subject: [PATCH] Replace deprecated `pkg_resources` with stdlib `pkg_resources` has been deprecated by `setuptools` for quite a while: https://setuptools.pypa.io/en/latest/pkg_resources.html It's got some bugs/warts: * https://github.com/pypa/setuptools/issues/2531 * https://discuss.python.org/t/will-setuptools-remove-pkg-resource-module-in-the-future/27182 So switch to using `importlib` functions which are part of the Python standard library as of `3.8`. This is less error-prone, and also removes the need for `setuptools` to be installed in order for this panel to work. I realize we technically still support `3.7`, but I thought it was fine to change this particular panel to require `3.8`, as `3.7` support is best effort given that it's now EOL'd by the core Python team. I also removed the relative path location for specific libraries as it was simply blank for me on Python 3.12... I think showing the location of the site packages directory should suffice. If someone later wants to build this out further, they're more than welcome to. Note that `importlib.metadata.distributions()` does have an outstanding issue that it reports a local editable install twice, but they plan to eventually fix that: * https://github.com/pypa/setuptools/issues/4170 --- src/flask_debugtoolbar/panels/versions.py | 19 ++++--------------- .../templates/panels/versions.html | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/flask_debugtoolbar/panels/versions.py b/src/flask_debugtoolbar/panels/versions.py index 7d8e239..6af8b81 100644 --- a/src/flask_debugtoolbar/panels/versions.py +++ b/src/flask_debugtoolbar/panels/versions.py @@ -17,16 +17,6 @@ _ = lambda x: x -def relpath(location, python_lib): - location = os.path.normpath(location) - relative = os.path.relpath(location, python_lib) - if relative == os.path.curdir: - return '' - elif relative.startswith(os.path.pardir): - return location - return relative - - class VersionDebugPanel(DebugPanel): """ Panel that displays the Flask version. @@ -48,15 +38,14 @@ def title(self): def content(self): try: - import pkg_resources + import importlib.metadata except ImportError: packages = [] else: - packages = sorted(pkg_resources.working_set, - key=lambda p: p.project_name.lower()) + packages_metadata = [p.metadata for p in importlib.metadata.distributions()] + packages = sorted(packages_metadata, key=lambda p: p['Name'].lower()) return self.render('panels/versions.html', { 'packages': packages, - 'python_lib': os.path.normpath(get_path('platlib')), - 'relpath': relpath, + 'python_lib_dir': os.path.normpath(get_path('platlib')), }) diff --git a/src/flask_debugtoolbar/templates/panels/versions.html b/src/flask_debugtoolbar/templates/panels/versions.html index 9e9c64e..6125dff 100644 --- a/src/flask_debugtoolbar/templates/panels/versions.html +++ b/src/flask_debugtoolbar/templates/panels/versions.html @@ -1,10 +1,10 @@

Installed Packages

- Installation paths relative to: + Current Site Packages Directory:

-{{ python_lib }}
+{{ python_lib_dir }}
 
@@ -12,21 +12,23 @@

Installed Packages

- + + {% for package in packages %} - - - + + + + {% else %} - + - + {% endfor %}
Package VersionInstalled PathHomepageSummary
{{ package.project_name }}{{ package.version }}{{ relpath(package.location, python_lib) }}{{ package.get('Name') }}{{ package.get('Version') }}{{ package.get('Home-page') }}{{ package.get('Summary') }}
setuptoolsPython 3.8 NOT INSTALLEDInstall setuptools to display installed packages and version informationThis panel requires Python >= 3.8 in order to display installed packages and version information.