11import os
22from pathlib import Path
3- from setuptools import find_namespace_packages , setup
3+ from setuptools import setup
44from setuptools .command .develop import develop as _develop
55from setuptools .command .install import install as _install
6+ from setuptools .command .build_py import build_py as _build_py
7+ from grpc_tools import protoc
8+ from pkg_resources import resource_filename
69
7- from snet .cli .utils .utils import compile_proto
8- from version import __version__
9-
10- PACKAGE_NAME = 'snet-cli'
11-
12-
13- this_directory = os .path .abspath (os .path .dirname (__file__ ))
14- with open (os .path .join (this_directory , 'README.md' ), encoding = 'utf-8' ) as f :
15- long_description = f .read ()
10+ def install_and_compile_proto ():
11+ """
12+ Compiles protobuf files directly.
13+ """
14+ proto_dir = Path (__file__ ).absolute ().parent .joinpath (
15+ "snet" , "cli" , "resources" , "proto" )
1616
17+ # Locate the standard grpc_tools internal protos (google/protobuf/...)
18+ grpc_protos_include = resource_filename ('grpc_tools' , '_proto' )
1719
18- with open ("./requirements.txt" ) as f :
19- requirements_str = f .read ()
20- requirements = requirements_str .split ("\n " )
20+ print (f"Proto directory: { proto_dir } " )
21+ print (f"Grpc include directory: { grpc_protos_include } " )
2122
23+ if not proto_dir .exists ():
24+ print (f"Warning: Proto directory not found at { proto_dir } " )
25+ return
2226
23- def install_and_compile_proto ():
24- proto_dir = Path (__file__ ).absolute ().parent .joinpath (
25- "snet" , "cli" , "resources" , "proto" )
26- print (proto_dir , "->" , proto_dir )
27+ # glob('*.proto') is non-recursive. It will NOT look inside subfolders.
2728 for fn in proto_dir .glob ('*.proto' ):
28- print ("Compiling protobuf" , fn )
29- compile_proto (proto_dir , proto_dir , proto_file = fn )
30-
29+ print (f"Compiling protobuf: { fn } " )
30+
31+ command = [
32+ 'grpc_tools.protoc' ,
33+ f'-I{ proto_dir } ' ,
34+ f'-I{ grpc_protos_include } ' , # <--- CRITICAL FIX: Add standard protos to include path
35+ f'--python_out={ proto_dir } ' ,
36+ f'--grpc_python_out={ proto_dir } ' ,
37+ str (fn )
38+ ]
39+
40+ if protoc .main (command ) != 0 :
41+ print (f"Error: Failed to compile { fn } " )
42+ raise RuntimeError (f"Protocol buffer compilation failed for { fn } " )
43+
44+ class build_py (_build_py ):
45+ """
46+ Override build_py to compile protos before building the wheel.
47+ This is the hook used by 'python -m build'.
48+ """
49+ def run (self ):
50+ self .execute (install_and_compile_proto , (), msg = "Compile protocol buffers" )
51+ _build_py .run (self )
3152
3253class develop (_develop ):
33- """Post-installation for development mode."""
34-
54+ """Post-installation for development mode (pip install -e .)."""
3555 def run (self ):
56+ self .execute (install_and_compile_proto , (), msg = "Compile protocol buffers" )
3657 _develop .run (self )
37- self .execute (install_and_compile_proto , (),
38- msg = "Compile protocol buffers" )
39-
4058
4159class install (_install ):
42- """Post-installation for installation mode."""
43-
60+ """Post-installation for legacy installation mode."""
4461 def run (self ):
62+ self .execute (install_and_compile_proto , (), msg = "Compile protocol buffers" )
4563 _install .run (self )
46- self .execute (install_and_compile_proto , (),
47- msg = "Compile protocol buffers" )
48-
4964
5065setup (
51- name = PACKAGE_NAME ,
52- version = __version__ ,
53- packages = find_namespace_packages (include = ['snet*' ]),
54- url = 'https://github.com/singnet/snet-cli' ,
55- author = "SingularityNET Foundation" ,
56- author_email = "info@singularitynet.io" ,
57- description = "SingularityNET CLI" ,
58- long_description = long_description ,
59- long_description_content_type = 'text/markdown' ,
60- license = "MIT" ,
61- python_requires = '>=3.10' ,
62- install_requires = requirements ,
63- include_package_data = True ,
6466 cmdclass = {
6567 'develop' : develop ,
6668 'install' : install ,
69+ 'build_py' : build_py ,
6770 },
68- entry_points = {
69- 'console_scripts' : [
70- 'snet = snet.cli:main'
71- ],
72- }
73- )
71+ )
0 commit comments