Search code examples
pythonsetuptoolsdistutilspython-wheel

How to generate python code during a build and include those in a python wheel?


We have a package that generates code by means of

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

This was integrated (read hacked) into our setup.py building by means of:

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

Which ugly as is, seems to work when building with the legacy setup.py, but when the package is built with wheel it doesn't work at all.

How can I make this work when installing my package by means of wheel?


Solution

  • You can override the wheel build process as well:

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