blob: edaa2aa72db94666b812b23bc03c99753278fb3f [file] [log] [blame]
Masood Malekghassemi791cc312015-07-27 14:22:33 -07001# 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 Malekghassemi2a0cb492015-10-22 17:43:33 -070032import distutils
Masood Malekghassemi791cc312015-07-27 14:22:33 -070033import os
34import os.path
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -070035import subprocess
Masood Malekghassemi791cc312015-07-27 14:22:33 -070036import sys
37
38import setuptools
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -070039from setuptools.command import build_py
Masood Malekghassemi791cc312015-07-27 14:22:33 -070040
41
42class 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 Malekghassemi2a0cb492015-10-22 17:43:33 -070058
59 self.run_command('build_proto_modules')
Masood Malekghassemi2b841622015-07-28 17:39:02 -070060 result = pytest.main(self.pytest_args)
61 if result != 0:
62 raise SystemExit(result)
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -070063
64
65class 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
101class 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)