django-compose 0.1.0.dev9__py3-none-any.whl → 0.1.0.dev11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import functools
5
6
  import json
6
7
  import math
7
8
  import os
@@ -218,8 +219,18 @@ def index_components(distributions: Iterable[metadata.Distribution], *, group: s
218
219
 
219
220
 
220
221
  def installed_components(group: str) -> InstalledComponents:
221
- """Index installed metadata using native same-name installation precedence."""
222
+ """Index installed metadata using native same-name installation precedence.
222
223
 
224
+ The index is reused for the same group and import path until
225
+ importlib.invalidate_caches() runs, which a process that installs or writes
226
+ distribution metadata at runtime calls before discovering it, as for imports.
227
+ Code that replaces importlib.metadata functions must call it too.
228
+ """
229
+ return _installed_components(group, tuple(sys.path))
230
+
231
+
232
+ @functools.lru_cache(maxsize=8)
233
+ def _installed_components(group: str, path: tuple[str, ...]) -> InstalledComponents:
223
234
  def distributions() -> Iterator[metadata.Distribution]:
224
235
  names = {canonicalize_name(dist.metadata["Name"]) for dist in metadata.distributions()}
225
236
  for name in sorted(names):
@@ -228,6 +239,28 @@ def installed_components(group: str) -> InstalledComponents:
228
239
  return index_components(distributions(), group=group)
229
240
 
230
241
 
242
+ class _InstalledComponentsFinder:
243
+ """Find nothing; let importlib.invalidate_caches() clear the installed index."""
244
+
245
+ @staticmethod
246
+ def find_spec(*args: object) -> None:
247
+ return None
248
+
249
+ @staticmethod
250
+ def invalidate_caches() -> None:
251
+ _installed_components.cache_clear()
252
+
253
+
254
+ # A reload re-runs this module in the same namespace, where a registered finder
255
+ # already clears the new cache; register once per process.
256
+ if not any(
257
+ getattr(finder, "__module__", None) == __name__
258
+ and getattr(finder, "__qualname__", None) == _InstalledComponentsFinder.__qualname__
259
+ for finder in sys.meta_path
260
+ ):
261
+ sys.meta_path.append(_InstalledComponentsFinder)
262
+
263
+
231
264
  def enabled_apps(roots: Iterable[str], *, group: str | None = None) -> Closure:
232
265
  """Expand explicit roots using installed providers and selected requires_apps.
233
266
 
django_compose/sources.py CHANGED
@@ -9,6 +9,7 @@ from collections.abc import Iterable
9
9
  from dataclasses import dataclass, replace
10
10
  from importlib import metadata
11
11
  from importlib.machinery import BuiltinImporter, FrozenImporter, ModuleSpec, PathFinder
12
+ from itertools import chain
12
13
  from pathlib import Path
13
14
  from typing import Protocol
14
15
  from urllib.parse import unquote, urlsplit
@@ -42,41 +43,49 @@ class SourceResolver:
42
43
  distributions: Iterable[metadata.Distribution] | None = None,
43
44
  ) -> None:
44
45
  self.entries = tuple(sorted(entries, key=lambda entry: -len(entry.name)))
45
- self._distributions = distributions
46
- self._files: dict[Path, set[str]] | None = None
47
- self._projects: list[tuple[Path, str]] = []
46
+ # A one-shot iterable is held whole so a failed index build fails again on retry.
47
+ self._distributions = None if distributions is None else tuple(distributions)
48
+ self._owners: dict[Path, set[str]] | None = None
49
+
50
+ def _index(self) -> tuple[dict[Path, set[str]], dict[Path, set[str]]]:
51
+ """Key each resolved RECORD file and its ancestors, and each PEP 610 project, to owners."""
52
+ owners: dict[Path, set[str]] = {}
53
+ projects: dict[Path, set[str]] = {}
54
+ distributions = self._distributions
55
+ for distribution in metadata.distributions() if distributions is None else distributions:
56
+ name = distribution.metadata["Name"]
57
+ if not name:
58
+ continue
59
+ for file in distribution.files or ():
60
+ location = Path(str(distribution.locate_file(file))).resolve()
61
+ for directory in chain((location,), location.parents):
62
+ names = owners.setdefault(directory, set())
63
+ if name in names:
64
+ break # Every further ancestor already records this distribution.
65
+ names.add(name)
66
+ direct = distribution.read_text("direct_url.json")
67
+ if direct:
68
+ try:
69
+ direct_url = json.loads(direct)
70
+ url = urlsplit(direct_url.get("url", ""))
71
+ if "dir_info" in direct_url and url.scheme == "file" and url.netloc in ("", "localhost"):
72
+ projects.setdefault(Path(unquote(url.path)).resolve(), set()).add(name)
73
+ except ValueError, TypeError, AttributeError:
74
+ continue
75
+ return owners, projects
48
76
 
49
77
  def distribution_names(self, path: Path) -> tuple[str, ...]:
50
- """Match RECORD files or a PEP 610 local project directory, never package names."""
51
- if self._files is None:
52
- self._files = {}
53
- distributions = self._distributions
54
- for distribution in metadata.distributions() if distributions is None else distributions:
55
- name = distribution.metadata["Name"]
56
- if not name:
57
- continue
58
- for file in distribution.files or ():
59
- location = Path(str(distribution.locate_file(file))).resolve()
60
- self._files.setdefault(location, set()).add(name)
61
- direct = distribution.read_text("direct_url.json")
62
- if direct:
63
- try:
64
- record = json.loads(direct)
65
- url = urlsplit(record.get("url", ""))
66
- if "dir_info" in record and url.scheme == "file" and url.netloc in ("", "localhost"):
67
- self._projects.append((Path(unquote(url.path)).resolve(), name))
68
- except ValueError, TypeError, AttributeError:
69
- continue
78
+ """Match RECORD files or a PEP 610 local project directory, never package names.
79
+
80
+ A path is owned by every distribution recording a file at or beneath it. Lookups
81
+ read the once-built index by key, so their cost never grows with recorded files.
82
+ """
83
+ if self._owners is None:
84
+ self._owners, self._projects = self._index()
70
85
  location = path.resolve()
71
- names = self._files.get(location, set()).copy()
72
- if location.is_dir():
73
- names.update(
74
- name
75
- for file, owners in self._files.items()
76
- if file.is_relative_to(location)
77
- for name in owners
78
- )
79
- names.update(name for root, name in self._projects if location.is_relative_to(root))
86
+ names = set(self._owners.get(location, ()))
87
+ for directory in chain((location,), location.parents):
88
+ names.update(self._projects.get(directory, ()))
80
89
  return tuple(sorted(names))
81
90
 
82
91
  @staticmethod
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: django-compose
3
- Version: 0.1.0.dev9
3
+ Version: 0.1.0.dev11
4
4
  Summary: Compose reusable Django apps into a runnable project.
5
5
  Project-URL: Source, https://github.com/ang-ee/django-compose
6
6
  Project-URL: Documentation, https://github.com/ang-ee/django-compose/tree/main/docs
@@ -5,7 +5,7 @@ django_compose/artifacts.py,sha256=JIwTpHDB6dahVpOOrS9M8bHWV1356nGCjCo45Sds5vg,1
5
5
  django_compose/autoreload.py,sha256=Obguzio30T42nIjSX9HVmrU-CqtTrm7moU0Wd271Egg,4128
6
6
  django_compose/build.py,sha256=DQ3yVb7SZtflMeIhBSFJalVby62sU8vvpu-l93pIL9c,11536
7
7
  django_compose/cli.py,sha256=9YlvZY7-qPVtuTXpMA5KxtAZd-TzTVCrg-2F4cNodUM,1209
8
- django_compose/components.py,sha256=jNMkWm0Q15dbmVkK3jBdM_ldgqilFitvrfGaRHxmXx0,14518
8
+ django_compose/components.py,sha256=diJXnlEvFN4tfHPKYHRbXITxxFi1Tsal1CJ2w5M9AuQ,15697
9
9
  django_compose/conf.py,sha256=Y6wEhxZh2gT_XjooEjc_5Ry0MOEIaPH4yCOf6UlZo-U,15838
10
10
  django_compose/declarations.py,sha256=JJ09haPmgua4h2P9T8-fMos3CWHdQMpWkw15gXlL1nM,6900
11
11
  django_compose/history.py,sha256=-D9jT-2EvKH6HDo3EfDeTD6lxUirgQOxeDh4cJKLoAU,1291
@@ -18,7 +18,7 @@ django_compose/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
18
  django_compose/rendering.py,sha256=1OFZU7lLMO2rD4S9K1-g8wvdn8RKBnUuQTFPHnDWsHY,3647
19
19
  django_compose/serving.py,sha256=FoS3TNNpgPGLpfCHtGrW0JVJVT9DyI7sxJ5U4x0oVLU,15498
20
20
  django_compose/snapshot.py,sha256=pHVV9P-gDM_JukVxav3veICgDYc_8xm4RFLdr0mvM00,13554
21
- django_compose/sources.py,sha256=fLBGSHtAInnoTM1Z2Wdt74P_cbLUUP4MWD1l2uS_zAI,6629
21
+ django_compose/sources.py,sha256=lT3BMOUK2BwVsLj-esb6ItCR60AsvbMTq6Otcm7NVuA,7229
22
22
  django_compose/typing.py,sha256=13pd3JfIoBV8v4AHK-QgAg9J8C4Miz_D0dmHFEwUG4I,36089
23
23
  django_compose/management/__init__.py,sha256=z5LGwZCxfGd44kJWDpQZkdwclHwfFlBgCBfJjPmcdI0,7156
24
24
  django_compose/management/commands/__init__.py,sha256=_mgOwmIp5-GhgJd6tY4Wc89G0EJDiHF9tYo8F63ytm8,40
@@ -29,10 +29,10 @@ django_compose/management/commands/migrate.py,sha256=_MkiQRjQRwIr9dEUnKk4WEjLgz5
29
29
  django_compose/management/commands/provision.py,sha256=4GTPwH1Pp1V_YkztDZxeHuyAjDUHjZxhkdvFlrgDBfU,1035
30
30
  django_compose/management/commands/reset_migration_history.py,sha256=Q6ShGeiWOAFS-rZOWuP0lncjwYp9vjRRbaIuk7Aoth0,1428
31
31
  django_compose/management/commands/retire_history.py,sha256=3RV1-w5Niij0svr0gB_gAjBgskxCYtdg5pPfsGN287s,886
32
- django_compose-0.1.0.dev9.dist-info/METADATA,sha256=YUQt0oeVSlMZhpLS33zAOrxRksdiftylg9sDq75eWtY,4151
33
- django_compose-0.1.0.dev9.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
34
- django_compose-0.1.0.dev9.dist-info/entry_points.txt,sha256=-Iw1c8YAegCC53YCKgPXjIZn4aPS3MmEKbcMrCpUMFU,59
35
- django_compose-0.1.0.dev9.dist-info/licenses/COPYING,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
36
- django_compose-0.1.0.dev9.dist-info/licenses/LICENSE,sha256=46mU2C5kSwOnkqkw9XQAJlhBL2JAf1_uCD8lVcXyMRg,7652
37
- django_compose-0.1.0.dev9.dist-info/licenses/NOTICE,sha256=C4fEGI9_-a1GCPfAH9bC0ZvmZFS71b9Z4v6rjrT5dxA,450
38
- django_compose-0.1.0.dev9.dist-info/RECORD,,
32
+ django_compose-0.1.0.dev11.dist-info/METADATA,sha256=23fM5agSKgrFWgcdkC-q1X5fTMRbg38r6Jflgc4gM60,4152
33
+ django_compose-0.1.0.dev11.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
34
+ django_compose-0.1.0.dev11.dist-info/entry_points.txt,sha256=-Iw1c8YAegCC53YCKgPXjIZn4aPS3MmEKbcMrCpUMFU,59
35
+ django_compose-0.1.0.dev11.dist-info/licenses/COPYING,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
36
+ django_compose-0.1.0.dev11.dist-info/licenses/LICENSE,sha256=46mU2C5kSwOnkqkw9XQAJlhBL2JAf1_uCD8lVcXyMRg,7652
37
+ django_compose-0.1.0.dev11.dist-info/licenses/NOTICE,sha256=C4fEGI9_-a1GCPfAH9bC0ZvmZFS71b9Z4v6rjrT5dxA,450
38
+ django_compose-0.1.0.dev11.dist-info/RECORD,,