From ae9ca4393295d961fe6c1b0f1cbc957f07ab4afd Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 8 Jun 2023 16:47:44 -0400 Subject: [PATCH 1/3] Add origin property. Ref #404. --- importlib_metadata/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 6ba414e5..79a5b17b 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -3,8 +3,10 @@ import abc import csv import sys +import json import zipp import email +import types import inspect import pathlib import operator @@ -618,6 +620,16 @@ def url_req_space(req): space = url_req_space(section.value) yield section.value + space + quoted_marker(section.name) + @property + def origin(self): + return self._load_json('direct_url.json') + + def _load_json(self, filename): + return pass_none(json.loads)( + self.read_text(filename), + object_hook=lambda data: types.SimpleNamespace(**data), + ) + class DistributionFinder(MetaPathFinder): """ From 84399188dd9792364603b2a95149f7c11961bca7 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 30 Jul 2023 15:35:14 -0400 Subject: [PATCH 2/3] Add changelog --- newsfragments/404.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/404.feature.rst diff --git a/newsfragments/404.feature.rst b/newsfragments/404.feature.rst new file mode 100644 index 00000000..47cf2447 --- /dev/null +++ b/newsfragments/404.feature.rst @@ -0,0 +1 @@ +Added ``Distribution.origin`` supplying the ``direct_url.json`` in a ``SimpleNamespace``. \ No newline at end of file From f480907325bcb79e614da9f826834c25b53839a7 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 30 Jul 2023 16:01:38 -0400 Subject: [PATCH 3/3] Add test capturing expectation. Ref #404 --- tests/fixtures.py | 26 ++++++++++++++++++++++++++ tests/test_main.py | 7 +++++++ 2 files changed, 33 insertions(+) diff --git a/tests/fixtures.py b/tests/fixtures.py index c0b0fa32..e39fc071 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,6 +1,7 @@ import os import sys import copy +import json import shutil import pathlib import tempfile @@ -127,6 +128,31 @@ def make_uppercase(self): build_files(files, self.site_dir) +class DistInfoPkgEditable(DistInfoPkg): + """ + Package with a PEP 660 direct_url.json. + """ + + some_hash = '524127ce937f7cb65665130c695abd18ca386f60bb29687efb976faa1596fdcc' + files: FilesSpec = { + 'distinfo_pkg-1.0.0.dist-info': { + 'direct_url.json': json.dumps( + { + "archive_info": { + "hash": f"sha256={some_hash}", + "hashes": {"sha256": f"{some_hash}"}, + }, + "url": "file:///path/to/distinfo_pkg-1.0.0.editable-py3-none-any.whl", + } + ) + }, + } + + def setUp(self): + super().setUp() + build_files(DistInfoPkgEditable.files, self.site_dir) + + class DistInfoPkgWithDot(OnSysPath, SiteDir): files: FilesSpec = { "pkg_dot-1.0.0.dist-info": { diff --git a/tests/test_main.py b/tests/test_main.py index 79181bf4..38377788 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -457,3 +457,10 @@ def import_names_from_package(package_name): # sources_fallback-pkg has one import ('sources_fallback') inferred from # SOURCES.txt (top_level.txt and installed-files.txt is missing) assert import_names_from_package('sources_fallback-pkg') == {'sources_fallback'} + + +class EditableDistributionTest(fixtures.DistInfoPkgEditable, unittest.TestCase): + def test_origin(self): + dist = Distribution.from_name('distinfo-pkg') + assert dist.origin.url.endswith('.whl') + assert dist.origin.archive_info.hashes.sha256