Recommended way to add custom build steps #3762
-
My current setup requires adding some custom build steps, e.g., for generating code from class BuildProtobuf(setuptools.Command):
"""Custom build command."""
user_options = [ ]
def initialize_options(self) -> None:
...
def finalize_options(self) -> None:
...
def run(self) -> None:
...
...
setuptools.setup(
packages=[...],
cmdclass={
'build_proto': BuildProtobuf,
...
},
) And then run the commands with #3422 looks connected, but I couldn't understand the approach and maybe there are some updates since. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
You can make an in-tree build backend that would be a thin wrapper around the setuptools' one. You can also make use of PEP 517 I've made something similar in the ansible/pylibssh repo and I'm working on another such thing in |
Beta Was this translation helpful? Give feedback.
-
Hi @jarkenau, I can successfully run the following: rm -rf /tmp/myproj
mkdir /tmp/myproj
cd /tmp/myproj
cat <<EOF > setup.py
from contextlib import suppress
from pathlib import Path
from setuptools import Command, setup
from setuptools.command.build import build
class CustomCommand(Command):
def initialize_options(self) -> None:
self.bdist_dir = None
def finalize_options(self) -> None:
with suppress(Exception):
self.bdist_dir = Path(self.get_finalized_command("bdist_wheel").bdist_dir)
def run(self) -> None:
if self.bdist_dir:
self.bdist_dir.mkdir(parents=True, exist_ok=True)
(self.bdist_dir / "file.txt").write_text("hello world", encoding="utf-8")
class CustomBuild(build):
sub_commands = [('build_custom', None)] + build.sub_commands
setup(cmdclass={'build': CustomBuild, 'build_custom': CustomCommand})
EOF
cat <<EOF > pyproject.toml
[build-system]
requires = ['setuptools']
build-backend = 'setuptools.build_meta'
[project]
name = 'hello-world'
version = '0.1'
EOF
python3.10 -m venv .venv
.venv/bin/python -m pip install -U pip build
.venv/bin/python -m build
unzip -l dist/*.whl
# Archive: dist/hello_world-0.1-py3-none-any.whl
# Length Date Time Name
# --------- ---------- ----- ----
# 11 2023-01-11 16:07 file.txt # <---------
# 54 2023-01-11 16:07 hello_world-0.1.dist-info/METADATA
# 92 2023-01-11 16:07 hello_world-0.1.dist-info/WHEEL
# 1 2023-01-11 16:07 hello_world-0.1.dist-info/top_level.txt
# 366 2023-01-11 16:07 hello_world-0.1.dist-info/RECORD
# --------- -------
# 524 5 files Have you considered a similar approach? |
Beta Was this translation helpful? Give feedback.
Hi @jarkenau,
I can successfully run the following: