-
Notifications
You must be signed in to change notification settings - Fork 23
Add API to retrieve project metadata from an installed package. #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import os | ||
| import pathlib | ||
| import re | ||
| from importlib.metadata import metadata | ||
|
|
||
| from setuptools.config.setupcfg import read_configuration | ||
| from distutils.errors import DistutilsFileError | ||
|
|
@@ -47,6 +48,10 @@ def build_metadata(path, isolated=None): | |
| # metadata from PyPI, we just couldn't get the additional build data. | ||
| return {"_wheel_build_failed": True} | ||
|
|
||
| return normalize_metadata(metadata) | ||
|
||
|
|
||
|
|
||
| def normalize_metadata(metadata): | ||
| # As far as I can tell, we can't trust that the builders normalize the keys, | ||
| # so we do it here. Definitely most builders do not lower case them, which | ||
| # Core Metadata Specs recommend. | ||
|
|
@@ -72,6 +77,11 @@ def build_metadata(path, isolated=None): | |
| return data | ||
|
|
||
|
|
||
| def installed_metadata(name): | ||
| """Retrieve the metadata for an package that is installed in the environment.""" | ||
| return normalize_metadata(metadata(name)) | ||
|
|
||
|
|
||
| def get_build_data(path, isolated=None): | ||
| metadata = build_metadata(path, isolated=isolated) | ||
| # Check if there is a pyproject_toml | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -334,6 +334,40 @@ def test_complete(self): | |
| del data["_path"] # This changes, so I just ignore it | ||
| self.assertEqual(data, COMPLETE) | ||
|
|
||
| def test_installed(self): | ||
| # pyroma must be installed in the test environment. | ||
| data = projectdata.installed_metadata("pyroma") | ||
|
|
||
| # Verify some key metadata | ||
| self.assertEqual(data["name"], "pyroma") | ||
| self.assertEqual(data["summary"], "Test your project's packaging friendliness") | ||
| self.assertEqual(data["classifier"], [ | ||
| "Development Status :: 5 - Production/Stable", | ||
| "Intended Audience :: Developers", | ||
| "License :: OSI Approved :: MIT License", | ||
| "Operating System :: OS Independent", | ||
| "Programming Language :: Python", | ||
| "Programming Language :: Python :: 3", | ||
| "Programming Language :: Python :: 3.9", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Programming Language :: Python :: 3.13", | ||
| "Programming Language :: Python :: 3.14", | ||
| "Programming Language :: Python :: 3 :: Only", | ||
| "Programming Language :: Python :: Implementation :: CPython", | ||
| "Programming Language :: Python :: Implementation :: PyPy", | ||
| ]) | ||
| self.assertEqual(data["keywords"], "pypi,quality,testing") | ||
| self.assertEqual(data["author"], "Lennart Regebro") | ||
| self.assertEqual(data["author-email"], "[email protected]") | ||
| self.assertEqual(data["home-page"], "https://github.com/regebro/pyroma") | ||
| self.assertEqual(data["license"], "MIT") | ||
| self.assertEqual(data["license-file"], "LICENSE.txt") | ||
| self.assertEqual(data["project-url"], "Source Code, https://github.com/regebro/pyroma") | ||
| self.assertEqual(data["provides-extra"], "test") | ||
| self.assertEqual(data["requires-python"], "3.9") | ||
|
|
||
|
|
||
| class DistroDataTest(unittest.TestCase): | ||
| maxDiff = None | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth importing this under an alias, rather than having both
metadatathe import andmetadatathe variable in the same file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure - or we can rename some of the variables to avoid the collision.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with calling the variable just "md", that's what we use for metadata at my work anyway. :-)