OLD | NEW |
(Empty) | |
| 1 # Copyright 2016, 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 |
| 31 import os |
| 32 import os.path |
| 33 import shutil |
| 34 import sys |
| 35 import tempfile |
| 36 |
| 37 from distutils import errors |
| 38 |
| 39 import commands |
| 40 |
| 41 |
| 42 C_PYTHON_DEV = """ |
| 43 #include <Python.h> |
| 44 int main(int argc, char **argv) { return 0; } |
| 45 """ |
| 46 C_PYTHON_DEV_ERROR_MESSAGE = """ |
| 47 Could not find <Python.h>. This could mean the following: |
| 48 * You're on Ubuntu and haven't `apt-get install`ed `python-dev`. |
| 49 * You're on Mac OS X and the usual Python framework was somehow corrupted |
| 50 (check your environment variables or try re-installing?) |
| 51 * You're on Windows and your Python installation was somehow corrupted |
| 52 (check your environment variables or try re-installing?) |
| 53 * Note: Windows users should look into installing `vcpython27`. |
| 54 """ |
| 55 |
| 56 C_CHECKS = { |
| 57 C_PYTHON_DEV: C_PYTHON_DEV_ERROR_MESSAGE, |
| 58 } |
| 59 |
| 60 def _compile(compiler, source_string): |
| 61 tempdir = tempfile.mkdtemp() |
| 62 cpath = os.path.join(tempdir, 'a.c') |
| 63 with open(cpath, 'w') as cfile: |
| 64 cfile.write(source_string) |
| 65 try: |
| 66 compiler.compile([cpath]) |
| 67 except errors.CompileError as error: |
| 68 return error |
| 69 finally: |
| 70 shutil.rmtree(tempdir) |
| 71 |
| 72 def _expect_compile(compiler, source_string, error_message): |
| 73 if _compile(compiler, source_string) is not None: |
| 74 sys.stderr.write(error_message) |
| 75 raise commands.CommandError( |
| 76 "Diagnostics found a compilation environment issue:\n{}" |
| 77 .format(error_message)) |
| 78 |
| 79 def diagnose_compile_error(build_ext, error): |
| 80 """Attempt to diagnose an error during compilation.""" |
| 81 for c_check, message in C_CHECKS.items(): |
| 82 _expect_compile(build_ext.compiler, c_check, message) |
| 83 python_sources = [ |
| 84 source for source in build_ext.get_source_files() |
| 85 if source.startswith('./src/python') and source.endswith('c') |
| 86 ] |
| 87 for source in python_sources: |
| 88 if not os.path.isfile(source): |
| 89 raise commands.CommandError( |
| 90 ("Diagnostics found a missing Python extension source file:\n{}\n\n" |
| 91 "This is usually because the Cython sources haven't been transpiled " |
| 92 "into C yet and you're building from source.\n" |
| 93 "Try setting the environment variable " |
| 94 "`GRPC_PYTHON_BUILD_WITH_CYTHON=1` when invoking `setup.py` or " |
| 95 "when using `pip`, e.g.:\n\n" |
| 96 "pip install -rrequirements.txt\n" |
| 97 "GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .") |
| 98 .format(source) |
| 99 ) |
| 100 |
| 101 |
| 102 _ERROR_DIAGNOSES = { |
| 103 errors.CompileError: diagnose_compile_error |
| 104 } |
| 105 |
| 106 def diagnose_build_ext_error(build_ext, error, formatted): |
| 107 diagnostic = _ERROR_DIAGNOSES.get(type(error)) |
| 108 if diagnostic is None: |
| 109 raise commands.CommandError( |
| 110 "\n\nWe could not diagnose your build failure. Please file an issue at " |
| 111 "http://www.github.com/grpc/grpc with `[Python install]` in the title." |
| 112 "\n\n{}".format(formatted)) |
| 113 else: |
| 114 diagnostic(build_ext, error) |
| 115 |
OLD | NEW |