8000 Merge pull request #465 from python/feature/origin · python/importlib_metadata@537349c · GitHub
[go: up one dir, main page]

Skip to content

Commit 537349c

Browse files
authored
Merge pull request #465 from python/feature/origin
Provide `Distribution.origin` reflecting content of direct_url.json.
2 parents e886c99 + 51b3be4 commit 537349c

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

importlib_metadata/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import abc
44
import csv
55
import sys
6+
import json
67
import zipp
78
import email
9+
import types
810
import inspect
911
import pathlib
1012
import operator
@@ -625,6 +627,16 @@ def url_req_space(req):
625627
space = url_req_space(section.value)
626628
yield section.value + space + quoted_marker(section.name)
627629

630+
@property
631+
def origin(self):
632+
return self._load_json('direct_url.json')
633+
634+
def _load_json(self, filename):
635+
return pass_none(json.loads)(
636+
self.read_text(filename),
637+
object_hook=lambda data: types.SimpleNamespace(**data),
638+
)
639+
628640

629641
class DistributionFinder(MetaPathFinder):
630642
"""

newsfragments/404.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ``Distribution.origin`` supplying the ``direct_url.json`` in a ``SimpleNamespace``.

tests/fixtures.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
import copy
4+
import json
45
import shutil
56
import pathlib
67
import tempfile
@@ -131,6 +132,27 @@ def make_uppercase(self):
131132
build_files(files, self.site_dir)
132133

133134

135+
class DistInfoPkgEditable(DistInfoPkg):
136+
"""
137+
Package with a PEP 660 direct_url.json.
138+
"""
139+
140+
some_hash = '524127ce937f7cb65665130c695abd18ca386f60bb29687efb976faa1596fdcc'
141+
files: FilesSpec = {
142+
'distinfo_pkg-1.0.0.dist-info': {
143+
'direct_url.json': json.dumps(
144+
{
145+
"archive_info": {
146+
"hash": f"sha256={some_hash}",
147+
"hashes": {"sha256": f"{some_hash}"},
148+
},
149+
"url": "file:///path/to/distinfo_pkg-1.0.0.editable-py3-none-any.whl",
150+
}
151+
)
152+
},
153+
}
154+
155+
134156
class DistInfoPkgWithDot(OnSysPath, SiteBuilder):
135157
files: FilesSpec = {
136158
"pkg_dot-1.0.0.dist-info": {

tests/test_main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,10 @@ def import_names_from_package(package_name):
457457
# sources_fallback-pkg has one import ('sources_fallback') inferred from
458458
# SOURCES.txt (top_level.txt and installed-files.txt is missing)
459459
assert import_names_from_package('sources_fallback-pkg') == {'sources_fallback'}
460+
461+
462+
class EditableDistributionTest(fixtures.DistInfoPkgEditable, unittest.TestCase):
463+
def test_origin(self):
464+
dist = Distribution.from_name('distinfo-pkg')
465+
assert dist.origin.url.endswith('.whl')
466+
assert dist.origin.archive_info.hashes.sha256

0 commit comments

Comments
 (0)
0