setup.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. NAME = 'PyYAML'
  2. VERSION = '5.4.1'
  3. DESCRIPTION = "YAML parser and emitter for Python"
  4. LONG_DESCRIPTION = """\
  5. YAML is a data serialization format designed for human readability
  6. and interaction with scripting languages. PyYAML is a YAML parser
  7. and emitter for Python.
  8. PyYAML features a complete YAML 1.1 parser, Unicode support, pickle
  9. support, capable extension API, and sensible error messages. PyYAML
  10. supports standard YAML tags and provides Python-specific tags that
  11. allow to represent an arbitrary Python object.
  12. PyYAML is applicable for a broad range of tasks from complex
  13. configuration files to object serialization and persistence."""
  14. AUTHOR = "Kirill Simonov"
  15. AUTHOR_EMAIL = 'xi@resolvent.net'
  16. LICENSE = "MIT"
  17. PLATFORMS = "Any"
  18. URL = "https://pyyaml.org/"
  19. DOWNLOAD_URL = "https://pypi.org/project/PyYAML/"
  20. CLASSIFIERS = [
  21. "Development Status :: 5 - Production/Stable",
  22. "Intended Audience :: Developers",
  23. "License :: OSI Approved :: MIT License",
  24. "Operating System :: OS Independent",
  25. "Programming Language :: Cython",
  26. "Programming Language :: Python",
  27. "Programming Language :: Python :: 2",
  28. "Programming Language :: Python :: 2.7",
  29. "Programming Language :: Python :: 3",
  30. "Programming Language :: Python :: 3.6",
  31. "Programming Language :: Python :: 3.7",
  32. "Programming Language :: Python :: 3.8",
  33. "Programming Language :: Python :: 3.9",
  34. "Programming Language :: Python :: Implementation :: CPython",
  35. "Programming Language :: Python :: Implementation :: PyPy",
  36. "Topic :: Software Development :: Libraries :: Python Modules",
  37. "Topic :: Text Processing :: Markup",
  38. ]
  39. PROJECT_URLS = {
  40. 'Bug Tracker': 'https://github.com/yaml/pyyaml/issues',
  41. 'CI': 'https://github.com/yaml/pyyaml/actions',
  42. 'Documentation': 'https://pyyaml.org/wiki/PyYAMLDocumentation',
  43. 'Mailing lists': 'http://lists.sourceforge.net/lists/listinfo/yaml-core',
  44. 'Source Code': 'https://github.com/yaml/pyyaml',
  45. }
  46. LIBYAML_CHECK = """
  47. #include <yaml.h>
  48. int main(void) {
  49. yaml_parser_t parser;
  50. yaml_emitter_t emitter;
  51. yaml_parser_initialize(&parser);
  52. yaml_parser_delete(&parser);
  53. yaml_emitter_initialize(&emitter);
  54. yaml_emitter_delete(&emitter);
  55. return 0;
  56. }
  57. """
  58. import sys, os, os.path, platform, warnings
  59. from distutils import log
  60. from setuptools import setup, Command, Distribution as _Distribution, Extension as _Extension
  61. from setuptools.command.build_ext import build_ext as _build_ext
  62. from distutils.errors import DistutilsError, CompileError, LinkError, DistutilsPlatformError
  63. with_cython = False
  64. if 'sdist' in sys.argv or os.environ.get('PYYAML_FORCE_CYTHON') == '1':
  65. # we need cython here
  66. with_cython = True
  67. try:
  68. from Cython.Distutils.extension import Extension as _Extension
  69. from Cython.Distutils import build_ext as _build_ext
  70. with_cython = True
  71. except ImportError:
  72. if with_cython:
  73. raise
  74. try:
  75. from wheel.bdist_wheel import bdist_wheel
  76. except ImportError:
  77. bdist_wheel = None
  78. # on Windows, disable wheel generation warning noise
  79. windows_ignore_warnings = [
  80. "Unknown distribution option: 'python_requires'",
  81. "Config variable 'Py_DEBUG' is unset",
  82. "Config variable 'WITH_PYMALLOC' is unset",
  83. "Config variable 'Py_UNICODE_SIZE' is unset",
  84. "Cython directive 'language_level' not set"
  85. ]
  86. if platform.system() == 'Windows':
  87. for w in windows_ignore_warnings:
  88. warnings.filterwarnings('ignore', w)
  89. class Distribution(_Distribution):
  90. def __init__(self, attrs=None):
  91. _Distribution.__init__(self, attrs)
  92. if not self.ext_modules:
  93. return
  94. for idx in range(len(self.ext_modules)-1, -1, -1):
  95. ext = self.ext_modules[idx]
  96. if not isinstance(ext, Extension):
  97. continue
  98. setattr(self, ext.attr_name, None)
  99. self.global_options = [
  100. (ext.option_name, None,
  101. "include %s (default if %s is available)"
  102. % (ext.feature_description, ext.feature_name)),
  103. (ext.neg_option_name, None,
  104. "exclude %s" % ext.feature_description),
  105. ] + self.global_options
  106. self.negative_opt = self.negative_opt.copy()
  107. self.negative_opt[ext.neg_option_name] = ext.option_name
  108. def has_ext_modules(self):
  109. if not self.ext_modules:
  110. return False
  111. for ext in self.ext_modules:
  112. with_ext = self.ext_status(ext)
  113. if with_ext is None or with_ext:
  114. return True
  115. return False
  116. def ext_status(self, ext):
  117. implementation = platform.python_implementation()
  118. if implementation not in ['CPython', 'PyPy']:
  119. return False
  120. if isinstance(ext, Extension):
  121. # the "build by default" behavior is implemented by this returning None
  122. with_ext = getattr(self, ext.attr_name) or os.environ.get('PYYAML_FORCE_{0}'.format(ext.feature_name.upper()))
  123. try:
  124. with_ext = int(with_ext) # attempt coerce envvar to int
  125. except TypeError:
  126. pass
  127. return with_ext
  128. else:
  129. return True
  130. class Extension(_Extension):
  131. def __init__(self, name, sources, feature_name, feature_description,
  132. feature_check, **kwds):
  133. if not with_cython:
  134. for filename in sources[:]:
  135. base, ext = os.path.splitext(filename)
  136. if ext == '.pyx':
  137. sources.remove(filename)
  138. sources.append('%s.c' % base)
  139. _Extension.__init__(self, name, sources, **kwds)
  140. self.feature_name = feature_name
  141. self.feature_description = feature_description
  142. self.feature_check = feature_check
  143. self.attr_name = 'with_' + feature_name.replace('-', '_')
  144. self.option_name = 'with-' + feature_name
  145. self.neg_option_name = 'without-' + feature_name
  146. class build_ext(_build_ext):
  147. def run(self):
  148. optional = True
  149. disabled = True
  150. for ext in self.extensions:
  151. with_ext = self.distribution.ext_status(ext)
  152. if with_ext is None:
  153. disabled = False
  154. elif with_ext:
  155. optional = False
  156. disabled = False
  157. break
  158. if disabled:
  159. return
  160. try:
  161. _build_ext.run(self)
  162. except DistutilsPlatformError:
  163. exc = sys.exc_info()[1]
  164. if optional:
  165. log.warn(str(exc))
  166. log.warn("skipping build_ext")
  167. else:
  168. raise
  169. def get_source_files(self):
  170. self.check_extensions_list(self.extensions)
  171. filenames = []
  172. for ext in self.extensions:
  173. if with_cython:
  174. self.cython_sources(ext.sources, ext)
  175. for filename in ext.sources:
  176. filenames.append(filename)
  177. base = os.path.splitext(filename)[0]
  178. for ext in ['c', 'h', 'pyx', 'pxd']:
  179. filename = '%s.%s' % (base, ext)
  180. if filename not in filenames and os.path.isfile(filename):
  181. filenames.append(filename)
  182. return filenames
  183. def get_outputs(self):
  184. self.check_extensions_list(self.extensions)
  185. outputs = []
  186. for ext in self.extensions:
  187. fullname = self.get_ext_fullname(ext.name)
  188. filename = os.path.join(self.build_lib,
  189. self.get_ext_filename(fullname))
  190. if os.path.isfile(filename):
  191. outputs.append(filename)
  192. return outputs
  193. def build_extensions(self):
  194. self.check_extensions_list(self.extensions)
  195. for ext in self.extensions:
  196. with_ext = self.distribution.ext_status(ext)
  197. if with_ext is not None and not with_ext:
  198. continue
  199. if with_cython:
  200. ext.sources = self.cython_sources(ext.sources, ext)
  201. try:
  202. self.build_extension(ext)
  203. except (CompileError, LinkError):
  204. if with_ext is not None:
  205. raise
  206. log.warn("Error compiling module, falling back to pure Python")
  207. class test(Command):
  208. user_options = []
  209. def initialize_options(self):
  210. pass
  211. def finalize_options(self):
  212. pass
  213. def run(self):
  214. build_cmd = self.get_finalized_command('build')
  215. build_cmd.run()
  216. sys.path.insert(0, build_cmd.build_lib)
  217. if sys.version_info[0] < 3:
  218. sys.path.insert(0, 'tests/lib')
  219. else:
  220. sys.path.insert(0, 'tests/lib3')
  221. import test_all
  222. if not test_all.main([]):
  223. raise DistutilsError("Tests failed")
  224. cmdclass = {
  225. 'build_ext': build_ext,
  226. 'test': test,
  227. }
  228. if bdist_wheel:
  229. cmdclass['bdist_wheel'] = bdist_wheel
  230. if __name__ == '__main__':
  231. setup(
  232. name=NAME,
  233. version=VERSION,
  234. description=DESCRIPTION,
  235. long_description=LONG_DESCRIPTION,
  236. author=AUTHOR,
  237. author_email=AUTHOR_EMAIL,
  238. license=LICENSE,
  239. platforms=PLATFORMS,
  240. url=URL,
  241. download_url=DOWNLOAD_URL,
  242. classifiers=CLASSIFIERS,
  243. project_urls=PROJECT_URLS,
  244. package_dir={'': {2: 'lib', 3: 'lib3'}[sys.version_info[0]]},
  245. packages=['yaml', '_yaml'],
  246. ext_modules=[
  247. Extension('yaml._yaml', ['yaml/_yaml.pyx'],
  248. 'libyaml', "LibYAML bindings", LIBYAML_CHECK,
  249. libraries=['yaml']),
  250. ],
  251. distclass=Distribution,
  252. cmdclass=cmdclass,
  253. python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*',
  254. )