Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix proto files installation #62

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 38 additions & 29 deletions control_plane/protos/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@


import os
import sys
import subprocess
import setuptools
from pathlib import Path
import shutil
from setuptools.command.develop import develop
from setuptools.command.install import install
from setuptools.command.egg_info import egg_info
from setuptools.command.sdist import sdist
from setuptools.command.build_py import build_py
from setuptools.command.develop import develop as _develop
from setuptools.command.bdist_egg import bdist_egg as _bdist_egg


with open("README.md", "r") as fh:
Expand All @@ -27,35 +20,43 @@
install_requires = f.read().splitlines()


def build_protos():
subprocess.call(['pip', 'install', 'grpcio-tools'])
class BuildPackageProtos(setuptools.Command):
"""Command to generate project *_pb2.py modules from proto files."""

PYTHON_PATH = sys.executable
user_options = []

dirpath = Path('./gen_py')
if dirpath.exists() and dirpath.is_dir():
shutil.rmtree(dirpath)
def initialize_options(self):
pass

os.makedirs('./gen_py')
def finalize_options(self):
pass

open('./gen_py/__init__.py', 'a').close()
def run(self):
# Build protos with strict mode enabled
# (i.e. exit with non-zero value if the proto compiling fails)
from grpc.tools import command
command.build_package_protos(self.distribution.package_dir[''], True)

# Generate python grpc stubs from proto files
print('Generation of python gRPC stubs')
args = "-I. --proto_path=. --python_out=./gen_py --grpc_python_out=./gen_py ./*.proto"
result = subprocess.call(
"%s -m grpc_tools.protoc %s" %
(PYTHON_PATH, args), shell=True)
if result != 0:
exit(-1)

class develop(_develop):

# Compile the proto files before running the setup
build_protos()
def run(self):
# Run build_proto_modules command
self.run_command('build_proto_modules')
# Run develop command
_develop.run(self)


class bdist_egg(_bdist_egg):
def run(self):
# Run build_proto_modules command
self.run_command('build_proto_modules')
# Run bdist_egg command
_bdist_egg.run(self)


packages = [
'.',
'',
]

setuptools.setup(
Expand All @@ -69,14 +70,22 @@ def build_protos():
url="https://github.com/netgroup/rose-srv6-control-plane",
packages=packages,
package_dir={
'': 'gen_py',
'': '.',
},
install_requires=install_requires,
setup_requires=[
'grpcio-tools',
],
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: Apache Software License',
'Operating System :: Linux',
'Programming Language :: Python',
],
cmdclass={
'build_proto_modules': BuildPackageProtos,
'bdist_egg': bdist_egg,
'develop': develop,
},
python_requires='>=3.6',
)