danielgtaylor / danielgtaylor/python-betterproto

How to wrap in a custom setuptools command?

Aperta
#219 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
question
Lingua principale
Python
Stelle
1.8k
Fork
234
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Is it possible to compile .proto files with the betterproto plugin from a custom setuptools command? For example, ```python setup.py build_proto```

This is what my setup.py looks like:
```
import os
import pkg_resources
from setuptools import Command, setup

class BuildProtoCommand(Command):
"""Command to generate project *_pb2.py modules from proto files."""

description = "build grpc protobuf modules"
user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
from grpc_tools import protoc

proto_files = []
inclusion_root = os.path.abspath(".")
for root, _, files in os.walk(inclusion_root):
for filename in files:
if filename.endswith(".proto"):
proto_files.append(os.path.abspath(os.path.join(root, filename)))

well_known_protos_include = pkg_resources.resource_filename("grpc_tools", "_proto")

for proto_file in proto_files:
proto_dir = os.path.dirname(proto_file)
command = [
"grpc_tools.protoc",
f"--proto_path={proto_dir}",
f"--proto_path={well_known_protos_include}",
f"--python_betterproto_out={proto_dir}",
proto_file,
]
if protoc.main(command) != 0:
raise Exception(f"error: {command} failed")

setup(
name="mypackage",
version="0.2.0",
cmdclass={
"build_proto": BuildProtoCommand,
},
packages=["mypackage"],
setup_requires=[
"betterproto[compiler]",
"grpcio-tools",
],
)
```

The problem here is that calling ```python setup.py build_proto``` causes all of the setup_requires packages to be installed as .eggs, and the ```protoc-gen-python_betterproto``` executable is not found by ```protoc```, so it doesn't know how to interpret the ```--python_betterproto_out``` flag.

I just wanted to double-check if this even supported functionality, or if betterproto is not intended to be used with setuptools at all and I need to migrate my package to pyproject.toml or something?

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia dall’esempio setup.py, in particolare da BuildProtoCommand.run e dalla configurazione setup_requires, quindi riproduci python setup.py build_proto. Verifica come il comando setuptools risolve grpc_tools.protoc e il plugin del compilatore betterproto; il lavoro è completato quando il comando genera correttamente i moduli oppure viene documentato l’approccio di packaging supportato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
build-system
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.