如何在构建期间生成python代码并将其包含在python控制盘中?

2024-09-30 06:16:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我们有一个通过

$PYTHON -m grpc_tools.protoc -I="foo_proto" --python-out="$package/out" \
         --grpc_python_out="$package/out" ./path/to/file.proto

通过以下方式将其集成(读取黑客攻击)到我们的setup.py建筑中:

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安装软件包时,如何实现这一点


Tags: runpyimportbuildselfpackagegrpcsetup
1条回答
网友
1楼 · 发布于 2024-09-30 06:16:11

您还可以覆盖控制盘构建过程:

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
    },
)

相关问题 更多 >

    热门问题