Deu*_*ina 4 python pip protocol-buffers setup.py
我有一些.protogRPC 文件要编译为 setup.py 脚本的一部分。这需要运行from grpc_tools import protoc和调用protoc 之前 setup(args)。目标是编译和安装 .pb 文件pip install pkgname。
例如
# setup.py
# generate our pb2 files in the temp directory structure
compile_protobufs(pkgname)
# this will package the generated files and put them in site-packages or .whl
setup(
name=pkgname,
install_requires=['grpcio-tools', ...],
...
)
Run Code Online (Sandbox Code Playgroud)
这按预期工作,我在我的站点包或轮子中获取 pb 文件,而不必将它们存在于源文件夹中。但是,这种模式意味着我不能天真地pip install pkgname从头开始,因为步骤compile_protobufs取决于grpcio-tools,直到setup().
我可以使用 setup_requires,但那是在砧板上。我可以先安装依赖项(现在我使用RUN pip install -r build-require.txt && pip install pkgname/),但似乎仍然应该有一种更清洁的方法。
我什至正确地使用这种模式还是我错过了一些包装成语?
我的标准:
_pb2.py每次都重新生成文件pip install.whl或 tar。看起来它已经记录在这里:
https://github.com/grpc/grpc/tree/master/tools/distrib/python/grpcio_tools#usage
所以你setup.py可能看起来像这样:
#!/usr/bin/env python3
import distutils.command.install
import setuptools
class build_package_protos(setuptools.Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
from grpc_tools import command
command.build_package_protos(self.distribution.package_dir[''])
class install(distutils.command.install.install):
_sub_command = ('build_package_protos', None,)
_sub_commands = distutils.command.install.install.sub_commands
sub_commands = [_sub_command] + _sub_commands
def setup():
setuptools.setup(
# see 'setup.cfg'
cmdclass={
'build_package_protos': build_package_protos,
'install': install,
},
setup_requires=[
'grpcio-tools',
],
)
if __name__ == '__main__':
setup()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
976 次 |
| 最近记录: |