如何在构建期间生成 python 代码并将其包含在 python 轮中?
How to generate python code during a build and include those in a 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
大楼中的:
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
安装我的包时使它工作?
您也可以覆盖 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 -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
安装我的包时使它工作?
您也可以覆盖 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
},
)