我们有一个通过以下方式生成代码的包
$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/