Skip to content
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

Provide Distribution.origin reflecting content of direct_url.json. #465

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import abc
import csv
import sys
import json
import zipp
import email
import types
import inspect
import pathlib
import operator
Expand Down Expand Up @@ -625,6 +627,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):
"""
Expand Down
1 change: 1 addition & 0 deletions newsfragments/404.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``Distribution.origin`` supplying the ``direct_url.json`` in a ``SimpleNamespace``.
22 changes: 22 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import copy
import json
import shutil
import pathlib
import tempfile
Expand Down Expand Up @@ -131,6 +132,27 @@ 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",
}
)
},
}


class DistInfoPkgWithDot(OnSysPath, SiteBuilder):
files: FilesSpec = {
"pkg_dot-1.0.0.dist-info": {
Expand Down
7 changes: 7 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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