Masood Malekghassemi | 791cc31 | 2015-07-27 14:22:33 -0700 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | """Provides distutils command classes for the GRPC Python test setup process.""" |
| 31 | |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 32 | import distutils |
Masood Malekghassemi | 791cc31 | 2015-07-27 14:22:33 -0700 | [diff] [blame] | 33 | import os |
| 34 | import os.path |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 35 | import subprocess |
Masood Malekghassemi | 791cc31 | 2015-07-27 14:22:33 -0700 | [diff] [blame] | 36 | import sys |
| 37 | |
| 38 | import setuptools |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 39 | from setuptools.command import build_py |
Masood Malekghassemi | 791cc31 | 2015-07-27 14:22:33 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | class RunTests(setuptools.Command): |
| 43 | """Command to run all tests via py.test.""" |
| 44 | |
| 45 | description = '' |
| 46 | user_options = [('pytest-args=', 'a', 'arguments to pass to py.test')] |
| 47 | |
| 48 | def initialize_options(self): |
| 49 | self.pytest_args = [] |
| 50 | |
| 51 | def finalize_options(self): |
| 52 | pass |
| 53 | |
| 54 | def run(self): |
| 55 | # We import here to ensure that setup.py has had a chance to install the |
| 56 | # relevant package eggs first. |
| 57 | import pytest |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 58 | |
| 59 | self.run_command('build_proto_modules') |
Masood Malekghassemi | 2b84162 | 2015-07-28 17:39:02 -0700 | [diff] [blame] | 60 | result = pytest.main(self.pytest_args) |
| 61 | if result != 0: |
| 62 | raise SystemExit(result) |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 63 | |
| 64 | |
| 65 | class BuildProtoModules(setuptools.Command): |
| 66 | """Command to generate project *_pb2.py modules from proto files.""" |
| 67 | |
| 68 | description = '' |
| 69 | user_options = [] |
| 70 | |
| 71 | def initialize_options(self): |
| 72 | pass |
| 73 | |
| 74 | def finalize_options(self): |
| 75 | self.protoc_command = distutils.spawn.find_executable('protoc') |
| 76 | self.grpc_python_plugin_command = distutils.spawn.find_executable( |
| 77 | 'grpc_python_plugin') |
| 78 | |
| 79 | def run(self): |
| 80 | paths = [] |
| 81 | root_directory = os.getcwd() |
| 82 | for walk_root, directories, filenames in os.walk(root_directory): |
| 83 | for filename in filenames: |
| 84 | if filename.endswith('.proto'): |
| 85 | paths.append(os.path.join(walk_root, filename)) |
| 86 | command = [ |
| 87 | self.protoc_command, |
| 88 | '--plugin=protoc-gen-python-grpc={}'.format( |
| 89 | self.grpc_python_plugin_command), |
| 90 | '-I {}'.format(root_directory), |
| 91 | '--python_out={}'.format(root_directory), |
| 92 | '--python-grpc_out={}'.format(root_directory), |
| 93 | ] + paths |
| 94 | try: |
| 95 | subprocess.check_output(' '.join(command), cwd=root_directory, shell=True, |
| 96 | stderr=subprocess.STDOUT) |
| 97 | except subprocess.CalledProcessError as e: |
| 98 | raise Exception('{}\nOutput:\n{}'.format(e.message, e.output)) |
| 99 | |
| 100 | |
| 101 | class BuildPy(build_py.build_py): |
| 102 | """Custom project build command.""" |
| 103 | |
| 104 | def run(self): |
| 105 | self.run_command('build_proto_modules') |
| 106 | build_py.build_py.run(self) |