python - 如何在构建期间生成 python 代码并将它们包含在 python 轮中?

标签 python setuptools distutils python-wheel

我们有一个通过以下方式生成代码的包

$PYTHON -m grpc_tools.protoc -I="foo_proto" --python-out="$package/out" \
         --grpc_python_out="$package/out" ./path/to/file.proto
这被集成(读取黑客攻击)到我们的 setup.py 中通过以下方式 build :
from distutils.command.build_py import build_py

class BuildPyCommand(build_py):
    """
    Generate GRPC code before building the package.
    """
    def run(self):
        import subprocess
        subprocess.call(["./bin/generate_grpc.sh", sys.executable], shell=True)
        build_py.run(self)

setup(
      ....
      cmdclass={
        'build_py': BuildPyCommand
    },
)
虽然丑陋,但在使用遗留系统构建时似乎有效 setup.py ,但是当包是用 wheel 构建的时候它根本不起作用。
在通过 wheel 安装我的包时,我怎样才能做到这一点? ?

最佳答案

您也可以覆盖车轮构建过程:

from wheel.bdist_wheel import bdist_wheel
from distutils.command.build_py import build_py
import subprocess


def generate_grpc():
    subprocess.call(["./bin/generate_grpc.sh", sys.executable], shell=True)


class BuildPyCommand(build_py):
    """
    Generate GRPC code before building the package.
    """
    def run(self):
        generate_grpc()
        build_py.run(self)


class BDistWheelCommand(bdist_wheel):
    """
    Generate GRPC code before building a wheel.
    """
    def run(self):
        generate_grpc()
        bdist_wheel.run(self)


setup(
      ....
      cmdclass={
        'build_py': BuildPyCommand,
        'bdist_wheel': BDistWheelCommand
    },
)

关于python - 如何在构建期间生成 python 代码并将它们包含在 python 轮中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66816295/

相关文章:

python - 防止在旧的 Python 版本上安装包

python - 将 Python 应用程序作为 debian 包分发,但作为服务分发

python - 关于 bdist 目录层次结构的问题

python - 将列表索引拆分为新列表 python

python - OpenCV,ORB检测:与多张图像相比,如何返回最佳匹配?

python - 正则表达式中的可选括号

python - 使用 python setuptools 库时的导入问题

python - 调用setup.py中的函数(使用tox)

python - 为什么 setup.py 保留在我的分发包中?

python - sqlite:断言语句:获取具有最高值的名称