• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import os.path
17import shutil
18import sys
19import tempfile
20
21from distutils import errors
22
23import commands
24
25C_PYTHON_DEV = """
26#include <Python.h>
27int main(int argc, char **argv) { return 0; }
28"""
29C_PYTHON_DEV_ERROR_MESSAGE = """
30Could not find <Python.h>. This could mean the following:
31  * You're on Ubuntu and haven't run `apt-get install <PY_REPR>-dev`.
32  * You're on RHEL/Fedora and haven't run `yum install <PY_REPR>-devel` or
33    `dnf install <PY_REPR>-devel` (make sure you also have redhat-rpm-config
34    installed)
35  * You're on Mac OS X and the usual Python framework was somehow corrupted
36    (check your environment variables or try re-installing?)
37  * You're on Windows and your Python installation was somehow corrupted
38    (check your environment variables or try re-installing?)
39"""
40if sys.version_info[0] == 2:
41    PYTHON_REPRESENTATION = 'python'
42elif sys.version_info[0] == 3:
43    PYTHON_REPRESENTATION = 'python3'
44else:
45    raise NotImplementedError('Unsupported Python version: %s' % sys.version)
46
47C_CHECKS = {
48    C_PYTHON_DEV:
49        C_PYTHON_DEV_ERROR_MESSAGE.replace('<PY_REPR>', PYTHON_REPRESENTATION),
50}
51
52
53def _compile(compiler, source_string):
54    tempdir = tempfile.mkdtemp()
55    cpath = os.path.join(tempdir, 'a.c')
56    with open(cpath, 'w') as cfile:
57        cfile.write(source_string)
58    try:
59        compiler.compile([cpath])
60    except errors.CompileError as error:
61        return error
62    finally:
63        shutil.rmtree(tempdir)
64
65
66def _expect_compile(compiler, source_string, error_message):
67    if _compile(compiler, source_string) is not None:
68        sys.stderr.write(error_message)
69        raise commands.CommandError(
70            "Diagnostics found a compilation environment issue:\n{}".format(
71                error_message))
72
73
74def diagnose_compile_error(build_ext, error):
75    """Attempt to diagnose an error during compilation."""
76    for c_check, message in C_CHECKS.items():
77        _expect_compile(build_ext.compiler, c_check, message)
78    python_sources = [
79        source for source in build_ext.get_source_files()
80        if source.startswith('./src/python') and source.endswith('c')
81    ]
82    for source in python_sources:
83        if not os.path.isfile(source):
84            raise commands.CommandError((
85                "Diagnostics found a missing Python extension source file:\n{}\n\n"
86                "This is usually because the Cython sources haven't been transpiled "
87                "into C yet and you're building from source.\n"
88                "Try setting the environment variable "
89                "`GRPC_PYTHON_BUILD_WITH_CYTHON=1` when invoking `setup.py` or "
90                "when using `pip`, e.g.:\n\n"
91                "pip install -rrequirements.txt\n"
92                "GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .").format(source))
93
94
95def diagnose_attribute_error(build_ext, error):
96    if any('_needs_stub' in arg for arg in error.args):
97        raise commands.CommandError(
98            "We expect a missing `_needs_stub` attribute from older versions of "
99            "setuptools. Consider upgrading setuptools.")
100
101
102_ERROR_DIAGNOSES = {
103    errors.CompileError: diagnose_compile_error,
104    AttributeError: diagnose_attribute_error,
105}
106
107
108def diagnose_build_ext_error(build_ext, error, formatted):
109    diagnostic = _ERROR_DIAGNOSES.get(type(error))
110    if diagnostic is None:
111        raise commands.CommandError(
112            "\n\nWe could not diagnose your build failure. If you are unable to "
113            "proceed, please file an issue at http://www.github.com/grpc/grpc "
114            "with `[Python install]` in the title; please attach the whole log "
115            "(including everything that may have appeared above the Python "
116            "backtrace).\n\n{}".format(formatted))
117    else:
118        diagnostic(build_ext, error)
119