support.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. from distutils import errors
  15. import os
  16. import os.path
  17. import shutil
  18. import sys
  19. import tempfile
  20. import commands
  21. C_PYTHON_DEV = """
  22. #include <Python.h>
  23. int main(int argc, char **argv) { return 0; }
  24. """
  25. C_PYTHON_DEV_ERROR_MESSAGE = """
  26. Could not find <Python.h>. This could mean the following:
  27. * You're on Ubuntu and haven't run `apt-get install <PY_REPR>-dev`.
  28. * You're on RHEL/Fedora and haven't run `yum install <PY_REPR>-devel` or
  29. `dnf install <PY_REPR>-devel` (make sure you also have redhat-rpm-config
  30. installed)
  31. * You're on Mac OS X and the usual Python framework was somehow corrupted
  32. (check your environment variables or try re-installing?)
  33. * You're on Windows and your Python installation was somehow corrupted
  34. (check your environment variables or try re-installing?)
  35. """
  36. if sys.version_info[0] == 2:
  37. PYTHON_REPRESENTATION = 'python'
  38. elif sys.version_info[0] == 3:
  39. PYTHON_REPRESENTATION = 'python3'
  40. else:
  41. raise NotImplementedError('Unsupported Python version: %s' % sys.version)
  42. C_CHECKS = {
  43. C_PYTHON_DEV:
  44. C_PYTHON_DEV_ERROR_MESSAGE.replace('<PY_REPR>', PYTHON_REPRESENTATION),
  45. }
  46. def _compile(compiler, source_string):
  47. tempdir = tempfile.mkdtemp()
  48. cpath = os.path.join(tempdir, 'a.c')
  49. with open(cpath, 'w') as cfile:
  50. cfile.write(source_string)
  51. try:
  52. compiler.compile([cpath])
  53. except errors.CompileError as error:
  54. return error
  55. finally:
  56. shutil.rmtree(tempdir)
  57. def _expect_compile(compiler, source_string, error_message):
  58. if _compile(compiler, source_string) is not None:
  59. sys.stderr.write(error_message)
  60. raise commands.CommandError(
  61. "Diagnostics found a compilation environment issue:\n{}".format(
  62. error_message))
  63. def diagnose_compile_error(build_ext, error):
  64. """Attempt to diagnose an error during compilation."""
  65. for c_check, message in C_CHECKS.items():
  66. _expect_compile(build_ext.compiler, c_check, message)
  67. python_sources = [
  68. source for source in build_ext.get_source_files()
  69. if source.startswith('./src/python') and source.endswith('c')
  70. ]
  71. for source in python_sources:
  72. if not os.path.isfile(source):
  73. raise commands.CommandError((
  74. "Diagnostics found a missing Python extension source file:\n{}\n\n"
  75. "This is usually because the Cython sources haven't been transpiled "
  76. "into C yet and you're building from source.\n"
  77. "Try setting the environment variable "
  78. "`GRPC_PYTHON_BUILD_WITH_CYTHON=1` when invoking `setup.py` or "
  79. "when using `pip`, e.g.:\n\n"
  80. "pip install -rrequirements.txt\n"
  81. "GRPC_PYTHON_BUILD_WITH_CYTHON=1 pip install .").format(source))
  82. def diagnose_attribute_error(build_ext, error):
  83. if any('_needs_stub' in arg for arg in error.args):
  84. raise commands.CommandError(
  85. "We expect a missing `_needs_stub` attribute from older versions of "
  86. "setuptools. Consider upgrading setuptools.")
  87. _ERROR_DIAGNOSES = {
  88. errors.CompileError: diagnose_compile_error,
  89. AttributeError: diagnose_attribute_error,
  90. }
  91. def diagnose_build_ext_error(build_ext, error, formatted):
  92. diagnostic = _ERROR_DIAGNOSES.get(type(error))
  93. if diagnostic is None:
  94. raise commands.CommandError(
  95. "\n\nWe could not diagnose your build failure. If you are unable to "
  96. "proceed, please file an issue at http://www.github.com/grpc/grpc "
  97. "with `[Python install]` in the title; please attach the whole log "
  98. "(including everything that may have appeared above the Python "
  99. "backtrace).\n\n{}".format(formatted))
  100. else:
  101. diagnostic(build_ext, error)