setup.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. from distutils.command.build_ext import build_ext
  2. from distutils.errors import CCompilerError
  3. from distutils.errors import DistutilsExecError
  4. from distutils.errors import DistutilsPlatformError
  5. import os
  6. import platform
  7. import re
  8. import sys
  9. from setuptools import Distribution as _Distribution
  10. from setuptools import Extension
  11. from setuptools import find_packages
  12. from setuptools import setup
  13. from setuptools.command.test import test as TestCommand
  14. cmdclass = {}
  15. if sys.version_info < (2, 7):
  16. raise Exception("SQLAlchemy requires Python 2.7 or higher.")
  17. cpython = platform.python_implementation() == "CPython"
  18. ext_modules = [
  19. Extension(
  20. "sqlalchemy.cprocessors",
  21. sources=["lib/sqlalchemy/cextension/processors.c"],
  22. ),
  23. Extension(
  24. "sqlalchemy.cresultproxy",
  25. sources=["lib/sqlalchemy/cextension/resultproxy.c"],
  26. ),
  27. Extension(
  28. "sqlalchemy.cutils", sources=["lib/sqlalchemy/cextension/utils.c"]
  29. ),
  30. ]
  31. ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)
  32. if sys.platform == "win32":
  33. # 2.6's distutils.msvc9compiler can raise an IOError when failing to
  34. # find the compiler
  35. # for TypeError, see https://github.com/pypa/setuptools/issues/1902
  36. ext_errors += (IOError, TypeError)
  37. class BuildFailed(Exception):
  38. def __init__(self):
  39. self.cause = sys.exc_info()[1] # work around py 2/3 different syntax
  40. class ve_build_ext(build_ext):
  41. # This class allows C extension building to fail.
  42. def run(self):
  43. try:
  44. build_ext.run(self)
  45. except DistutilsPlatformError:
  46. raise BuildFailed()
  47. def build_extension(self, ext):
  48. try:
  49. build_ext.build_extension(self, ext)
  50. except ext_errors:
  51. raise BuildFailed()
  52. except ValueError:
  53. # this can happen on Windows 64 bit, see Python issue 7511
  54. if "'path'" in str(sys.exc_info()[1]): # works with both py 2/3
  55. raise BuildFailed()
  56. raise
  57. cmdclass["build_ext"] = ve_build_ext
  58. class Distribution(_Distribution):
  59. def has_ext_modules(self):
  60. # We want to always claim that we have ext_modules. This will be fine
  61. # if we don't actually have them (such as on PyPy) because nothing
  62. # will get built, however we don't want to provide an overally broad
  63. # Wheel package when building a wheel without C support. This will
  64. # ensure that Wheel knows to treat us as if the build output is
  65. # platform specific.
  66. return True
  67. class PyTest(TestCommand):
  68. # from http://pytest.org/latest/goodpractices.html\
  69. # #integrating-with-setuptools-python-setup-py-test-pytest-runner
  70. # TODO: prefer pytest-runner package at some point, however it was
  71. # not working at the time of this comment.
  72. user_options = [("pytest-args=", "a", "Arguments to pass to py.test")]
  73. default_options = ["-n", "4", "-q", "--nomemory"]
  74. def initialize_options(self):
  75. TestCommand.initialize_options(self)
  76. self.pytest_args = ""
  77. def finalize_options(self):
  78. TestCommand.finalize_options(self)
  79. self.test_args = []
  80. self.test_suite = True
  81. def run_tests(self):
  82. import shlex
  83. # import here, cause outside the eggs aren't loaded
  84. import pytest
  85. errno = pytest.main(
  86. self.default_options + shlex.split(self.pytest_args)
  87. )
  88. sys.exit(errno)
  89. cmdclass["test"] = PyTest
  90. def status_msgs(*msgs):
  91. print("*" * 75)
  92. for msg in msgs:
  93. print(msg)
  94. print("*" * 75)
  95. with open(
  96. os.path.join(os.path.dirname(__file__), "lib", "sqlalchemy", "__init__.py")
  97. ) as v_file:
  98. VERSION = (
  99. re.compile(r""".*__version__ = ["'](.*?)['"]""", re.S)
  100. .match(v_file.read())
  101. .group(1)
  102. )
  103. with open(os.path.join(os.path.dirname(__file__), "README.rst")) as r_file:
  104. readme = r_file.read()
  105. def run_setup(with_cext):
  106. kwargs = {}
  107. if with_cext:
  108. kwargs["ext_modules"] = ext_modules
  109. else:
  110. kwargs["ext_modules"] = []
  111. setup(
  112. name="SQLAlchemy",
  113. version=VERSION,
  114. description="Database Abstraction Library",
  115. author="Mike Bayer",
  116. author_email="mike_mp@zzzcomputing.com",
  117. url="http://www.sqlalchemy.org",
  118. project_urls={
  119. "Documentation": "https://docs.sqlalchemy.org",
  120. "Issue Tracker": "https://github.com/sqlalchemy/sqlalchemy/",
  121. },
  122. packages=find_packages("lib"),
  123. package_dir={"": "lib"},
  124. license="MIT",
  125. cmdclass=cmdclass,
  126. tests_require=[
  127. "pytest>=2.5.2,!=3.9.1,!=3.9.2",
  128. "mock",
  129. "pytest-xdist",
  130. ],
  131. long_description=readme,
  132. python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
  133. classifiers=[
  134. "Development Status :: 5 - Production/Stable",
  135. "Intended Audience :: Developers",
  136. "License :: OSI Approved :: MIT License",
  137. "Programming Language :: Python",
  138. "Programming Language :: Python :: 2",
  139. "Programming Language :: Python :: 2.7",
  140. "Programming Language :: Python :: 3",
  141. "Programming Language :: Python :: 3.4",
  142. "Programming Language :: Python :: 3.5",
  143. "Programming Language :: Python :: 3.6",
  144. "Programming Language :: Python :: 3.7",
  145. "Programming Language :: Python :: 3.8",
  146. "Programming Language :: Python :: 3.9",
  147. "Programming Language :: Python :: Implementation :: CPython",
  148. "Programming Language :: Python :: Implementation :: PyPy",
  149. "Topic :: Database :: Front-Ends",
  150. "Operating System :: OS Independent",
  151. ],
  152. distclass=Distribution,
  153. extras_require={
  154. "mysql": ["mysqlclient"],
  155. "pymysql": ["pymysql"],
  156. "postgresql": ["psycopg2"],
  157. "postgresql_psycopg2binary": ["psycopg2-binary"],
  158. "postgresql_pg8000": ["pg8000"],
  159. "postgresql_psycopg2cffi": ["psycopg2cffi"],
  160. "oracle": ["cx_oracle"],
  161. "mssql_pyodbc": ["pyodbc"],
  162. "mssql_pymssql": ["pymssql"],
  163. "mssql": ["pyodbc"],
  164. },
  165. **kwargs
  166. )
  167. if not cpython:
  168. run_setup(False)
  169. status_msgs(
  170. "WARNING: C extensions are not supported on "
  171. + "this Python platform, speedups are not enabled.",
  172. "Plain-Python build succeeded.",
  173. )
  174. elif os.environ.get("DISABLE_SQLALCHEMY_CEXT"):
  175. run_setup(False)
  176. status_msgs(
  177. "DISABLE_SQLALCHEMY_CEXT is set; "
  178. + "not attempting to build C extensions.",
  179. "Plain-Python build succeeded.",
  180. )
  181. else:
  182. try:
  183. run_setup(True)
  184. except BuildFailed as exc:
  185. status_msgs(
  186. exc.cause,
  187. "WARNING: The C extension could not be compiled, "
  188. + "speedups are not enabled.",
  189. "Failure information, if any, is above.",
  190. "Retrying the build without the C extension now.",
  191. )
  192. run_setup(False)
  193. status_msgs(
  194. "WARNING: The C extension could not be compiled, "
  195. + "speedups are not enabled.",
  196. "Plain-Python build succeeded.",
  197. )