From 08e117ae806225fd704ab4221c1014b5fa495482 Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Wed, 17 Jun 2020 16:12:59 -0700 Subject: [PATCH] Improve build slightly --- setup.py | 1 + src/python/grpcio/_parallel_compile_patch.py | 5 ++- src/python/grpcio/commands.py | 45 +++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 807d005899d..3f27ebd4c14 100644 --- a/setup.py +++ b/setup.py @@ -383,6 +383,7 @@ COMMAND_CLASS = { 'build_py': commands.BuildPy, 'build_ext': commands.BuildExt, 'gather': commands.Gather, + 'clean': commands.Clean, } # Ensure that package data is copied over before any commands have been run: diff --git a/src/python/grpcio/_parallel_compile_patch.py b/src/python/grpcio/_parallel_compile_patch.py index b34aa17fd0b..e4d50c38311 100644 --- a/src/python/grpcio/_parallel_compile_patch.py +++ b/src/python/grpcio/_parallel_compile_patch.py @@ -22,7 +22,10 @@ import os try: BUILD_EXT_COMPILER_JOBS = int( - os.environ.get('GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS', '1')) + os.environ['GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS']) +except KeyError: + import multiprocessing + BUILD_EXT_COMPILER_JOBS = multiprocessing.cpu_count() except ValueError: BUILD_EXT_COMPILER_JOBS = 1 diff --git a/src/python/grpcio/commands.py b/src/python/grpcio/commands.py index b9106826b5d..6d7a286632d 100644 --- a/src/python/grpcio/commands.py +++ b/src/python/grpcio/commands.py @@ -13,6 +13,8 @@ # limitations under the License. """Provides distutils command classes for the GRPC Python setup process.""" +from __future__ import print_function + import distutils import glob import os @@ -218,7 +220,10 @@ class BuildExt(build_ext.build_ext): if platform.system() != 'Windows': return False # TODO(lidiz) Remove the generated a.out for success tests. - cc_test = subprocess.Popen(['cc', '-x', 'c', '-std=c++11', '-'], + cc_test = subprocess.Popen([ + distutils.ccompiler.executable_filename, '-x', 'c', + '-std=c++11', '-' + ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -290,3 +295,41 @@ class Gather(setuptools.Command): self.distribution.install_requires) if self.test and self.distribution.tests_require: self.distribution.fetch_build_eggs(self.distribution.tests_require) + + +class Clean(setuptools.Command): + """Command to clean build artifacts.""" + + description = 'Clean build artifacts.' + user_options = [] + + _FILE_PATTERNS = [ + 'python_build', + 'src/python/grpcio/__pycache__/', + 'src/python/grpcio/grpc/_cython/cygrpc.cpp', + 'src/python/grpcio/grpc/_cython/*.so', + 'src/python/grpcio/grpcio.egg-info/', + ] + _CURRENT_DIRECTORY = os.path.normpath( + os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../..")) + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + for path_spec in self._FILE_PATTERNS: + this_glob = os.path.normpath( + os.path.join(Clean._CURRENT_DIRECTORY, path_spec)) + abs_paths = glob.glob(this_glob) + for path in abs_paths: + if not str(path).startswith(Clean._CURRENT_DIRECTORY): + raise ValueError( + "Cowardly refusing to delete {}.".format(path)) + print("Removing {}".format(os.path.relpath(path))) + if os.path.isfile(path): + os.remove(str(path)) + else: + shutil.rmtree(str(path))