Skip to content

Commit d76ba17

Browse files
committed
Fix python3 relative import issue and setup.py
This change builds the protobuf files inside the osi3 package path for Python2 to ensure that relative imports work, while not breaking the general cmake build setup. It also moves the protobuf generation to the build command phase for tidiness.
1 parent c372616 commit d76ba17

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

setup.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import os
55
import subprocess
66
import sys
7+
import re
78
from distutils.spawn import find_executable
89

910
from setuptools import setup
10-
from setuptools.command.install import install
11+
from setuptools.command.build_py import build_py
1112

1213
# configure the version number
1314
from shutil import copyfile
@@ -24,7 +25,7 @@
2425
package_name = 'osi3'
2526
package_path = os.path.join(os.getcwd(), package_name)
2627

27-
class GenerateProtobuf(install):
28+
class GenerateProtobufCommand(build_py):
2829

2930
@staticmethod
3031
def find_protoc():
@@ -71,14 +72,23 @@ def find_protoc():
7172
""" Generate Protobuf Messages """
7273

7374
def run(self):
75+
if sys.hexversion >= 0x3000000:
76+
pattern = re.compile('^import "osi_')
77+
for source in self.osi_files:
78+
with open(source) as src_file:
79+
with open(os.path.join(package_path, source),"w") as dst_file:
80+
for line in src_file:
81+
dst_file.write(pattern.sub('import "osi3/osi_',line))
7482
for source in self.osi_files:
7583
sys.stdout.write('Protobuf-compiling ' + source + '\n')
84+
source_path = os.path.join(package_path, source) if sys.hexversion >= 0x3000000 else './' + source
7685
subprocess.check_call([self.find_protoc(),
86+
'--proto_path=' + package_path,
7787
'--proto_path=.',
7888
'--python_out=' + package_path,
79-
'./' + source])
89+
source_path])
8090

81-
install.run(self)
91+
build_py.run(self)
8292

8393
try:
8494
os.mkdir(package_path)
@@ -103,7 +113,7 @@ def run(self):
103113
packages=[package_name],
104114
install_requires=['protobuf'],
105115
cmdclass={
106-
'install': GenerateProtobuf,
116+
'build_py': GenerateProtobufCommand,
107117
},
108118
url='https://github.com/OpenSimulationInterface/open-simulation-interface',
109119
license="MPL 2.0",

0 commit comments

Comments
 (0)