Skip to content

Commit

Permalink
Add some missing setuptools command (incl pth file setup for editable…
Browse files Browse the repository at this point in the history
… wheels aka pep660).
  • Loading branch information
ionelmc committed Jul 1, 2024
1 parent a73fb89 commit 54e8750
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build-system]
requires = [
"setuptools>=30.3.0",
"setuptools>=64",
]

[tool.ruff.per-file-ignores]
Expand Down
39 changes: 39 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from os import fspath
from pathlib import Path

from setuptools import Command
from setuptools import find_packages
from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.easy_install import easy_install
from setuptools.command.editable_wheel import editable_wheel
from setuptools.command.install_lib import install_lib

pth_file = Path(__file__).parent.joinpath('src', 'manhole.pth')
Expand All @@ -20,6 +22,26 @@ def run(self):
self.copy_file(fspath(pth_file), fspath(Path(self.build_lib, pth_file.name)))


class PTHWheelPiggyback:
def __init__(self, strategy):
self.strategy = strategy

def __enter__(self):
self.strategy.__enter__()

def __exit__(self, exc_type, exc_val, exc_tb):
self.strategy.__exit__(exc_type, exc_val, exc_tb)

def __call__(self, wheel, files, mapping):
self.strategy(wheel, files, mapping)
wheel.writestr(fspath(pth_file.name), pth_file.read_bytes())


class EditableWheelWithPTH(editable_wheel):
def _select_strategy(self, dist_name, tag, lib):
return PTHWheelPiggyback(super()._select_strategy(dist_name, tag, lib))


class EasyInstallWithPTH(easy_install):
def run(self, *args, **kwargs):
super().run(*args, **kwargs)
Expand All @@ -43,6 +65,21 @@ def run(self):
self.copy_file(fspath(pth_file), str(Path(self.install_dir, pth_file.name)))


class GeneratePTH(Command):
user_options = [] # noqa: RUF012

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
with pth_file.open('w') as fh:
with pth_file.with_suffix('.embed').open() as sh:
fh.write(f"import os, sys;exec({sh.read().replace(' ', ' ')!r})")


def read(*names, **kwargs):
with Path(__file__).parent.joinpath(*names).open(encoding=kwargs.get('encoding', 'utf8')) as fh:
return fh.read()
Expand Down Expand Up @@ -118,5 +155,7 @@ def read(*names, **kwargs):
'easy_install': EasyInstallWithPTH,
'install_lib': InstallLibWithPTH,
'develop': DevelopWithPTH,
'editable_wheel': EditableWheelWithPTH,
'genpth': GeneratePTH,
},
)

0 comments on commit 54e8750

Please sign in to comment.