From d76ba17d520bc9ff5d53655b8a565c1657cf7cbf Mon Sep 17 00:00:00 2001 From: "Pierre R. Mai" Date: Fri, 10 Aug 2018 17:49:27 +0200 Subject: [PATCH] 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. --- setup.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 23df483d3..eeb37fe9f 100644 --- a/setup.py +++ b/setup.py @@ -4,10 +4,11 @@ import os import subprocess import sys +import re from distutils.spawn import find_executable from setuptools import setup -from setuptools.command.install import install +from setuptools.command.build_py import build_py # configure the version number from shutil import copyfile @@ -24,7 +25,7 @@ package_name = 'osi3' package_path = os.path.join(os.getcwd(), package_name) -class GenerateProtobuf(install): +class GenerateProtobufCommand(build_py): @staticmethod def find_protoc(): @@ -71,14 +72,23 @@ def find_protoc(): """ Generate Protobuf Messages """ def run(self): + if sys.hexversion >= 0x3000000: + pattern = re.compile('^import "osi_') + for source in self.osi_files: + with open(source) as src_file: + with open(os.path.join(package_path, source),"w") as dst_file: + for line in src_file: + dst_file.write(pattern.sub('import "osi3/osi_',line)) for source in self.osi_files: sys.stdout.write('Protobuf-compiling ' + source + '\n') + source_path = os.path.join(package_path, source) if sys.hexversion >= 0x3000000 else './' + source subprocess.check_call([self.find_protoc(), + '--proto_path=' + package_path, '--proto_path=.', '--python_out=' + package_path, - './' + source]) + source_path]) - install.run(self) + build_py.run(self) try: os.mkdir(package_path) @@ -103,7 +113,7 @@ def run(self): packages=[package_name], install_requires=['protobuf'], cmdclass={ - 'install': GenerateProtobuf, + 'build_py': GenerateProtobufCommand, }, url='https://github.com/OpenSimulationInterface/open-simulation-interface', license="MPL 2.0",