Skip to content

Commit

Permalink
Restore the setuptools version hack
Browse files Browse the repository at this point in the history
  • Loading branch information
twm committed Jul 23, 2024
1 parent 2223994 commit 28a4719
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 72 deletions.
72 changes: 0 additions & 72 deletions _build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,78 +13,6 @@
We comply by re-publishing setuptools' ``build_meta``.
"""

import sys
from pathlib import Path

if sys.version_info > (3, 11):
import tomllib
else:
import tomli as tomllib

from importlib.metadata import Distribution, DistributionFinder

from setuptools import build_meta


def _entry_points_txt():
"""
Format the entry points from ``pyproject.toml`` as the INI-style
``entry_points.txt`` used in package metadata.
"""
with open(Path(__file__).parent / "pyproject.toml", "rb") as f:
data = tomllib.load(f)

lines = []
for section, pairs in data["project"]["entry-points"].items():
lines.append(f"[{section}]")
for key, value in pairs.items():
lines.append(f"{key} = {value}")
lines.append("")
return "\n".join(lines)


class IncrementalEntryPoints(Distribution):
"""
A distribution that exposes the incremental entry points by by
reading them from ``pyproject.toml``.
"""

def read_text(self, filename):
if filename == "METADATA":
return None
if filename == "PKG-INFO":
return (
"Metadata-Version: 2.1\n"
"Name: incrementalbuildhack\n"
"Version: 0.0.0\n"
)
if filename == "entry_points.txt":
return _entry_points_txt()
raise NotImplementedError(f"Can't synthesize {filename=}")

def locate_file(self, path):
raise NotImplementedError(f"Can't locate_file({path=})")


class EntryPointInjector(DistributionFinder):
"""
Inject incremental's setuptools entry points so that
setuptools can find Incremental's own version.
"""

def find_distributions(self, context):
if context.name is None:
yield IncrementalEntryPoints()

# No-op abstract methods from importlib.abc.MetaPathFinder:

def find_spec(self, fullname, path, target=None):
return None

def invalidate_caches(self):
pass


sys.meta_path.insert(0, EntryPointInjector())

__all__ = ["build_meta"]
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ use_incremental = "incremental:_get_distutils_version"
[project.entry-points."setuptools.finalize_distribution_options"]
incremental = "incremental:_get_setuptools_version"

[tool.setuptools.dynamic]
version = {attr = "incremental._setuptools_version"}

[tool.incremental]

[tool.black]
Expand Down
4 changes: 4 additions & 0 deletions src/incremental/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,8 @@ def _load_pyproject_toml(toml_path): # type: (str) -> Optional[_IncrementalConf
from ._version import __version__ # noqa: E402


def _setuptools_version(): # type: () -> str
return __version__.public()


__all__ = ["__version__", "Version", "getVersionString"]

0 comments on commit 28a4719

Please sign in to comment.