setup.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. NAME = 'PyYAML'
  2. VERSION = '3.12'
  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 persistance."""
  14. AUTHOR = "Kirill Simonov"
  15. AUTHOR_EMAIL = 'xi@resolvent.net'
  16. LICENSE = "MIT"
  17. PLATFORMS = "Any"
  18. URL = "http://pyyaml.org/wiki/PyYAML"
  19. DOWNLOAD_URL = "http://pyyaml.org/download/pyyaml/%s-%s.tar.gz" % (NAME, VERSION)
  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 :: Python",
  26. "Programming Language :: Python :: 2",
  27. "Programming Language :: Python :: 2.7",
  28. "Programming Language :: Python :: 3",
  29. "Programming Language :: Python :: 3.4",
  30. "Programming Language :: Python :: 3.5",
  31. "Topic :: Software Development :: Libraries :: Python Modules",
  32. "Topic :: Text Processing :: Markup",
  33. ]
  34. LIBYAML_CHECK = """
  35. #include <yaml.h>
  36. int main(void) {
  37. yaml_parser_t parser;
  38. yaml_emitter_t emitter;
  39. yaml_parser_initialize(&parser);
  40. yaml_parser_delete(&parser);
  41. yaml_emitter_initialize(&emitter);
  42. yaml_emitter_delete(&emitter);
  43. return 0;
  44. }
  45. """
  46. import sys, os.path, platform
  47. from distutils import log
  48. from distutils.core import setup, Command
  49. from distutils.core import Distribution as _Distribution
  50. from distutils.core import Extension as _Extension
  51. from distutils.dir_util import mkpath
  52. from distutils.command.build_ext import build_ext as _build_ext
  53. from distutils.command.bdist_rpm import bdist_rpm as _bdist_rpm
  54. from distutils.errors import DistutilsError, CompileError, LinkError, DistutilsPlatformError
  55. if 'setuptools.extension' in sys.modules:
  56. _Extension = sys.modules['setuptools.extension']._Extension
  57. sys.modules['distutils.core'].Extension = _Extension
  58. sys.modules['distutils.extension'].Extension = _Extension
  59. sys.modules['distutils.command.build_ext'].Extension = _Extension
  60. with_cython = False
  61. try:
  62. from Cython.Distutils.extension import Extension as _Extension
  63. from Cython.Distutils import build_ext as _build_ext
  64. with_cython = True
  65. except ImportError:
  66. pass
  67. try:
  68. from wheel.bdist_wheel import bdist_wheel
  69. except ImportError:
  70. bdist_wheel = None
  71. class Distribution(_Distribution):
  72. def __init__(self, attrs=None):
  73. _Distribution.__init__(self, attrs)
  74. if not self.ext_modules:
  75. return
  76. for idx in range(len(self.ext_modules)-1, -1, -1):
  77. ext = self.ext_modules[idx]
  78. if not isinstance(ext, Extension):
  79. continue
  80. setattr(self, ext.attr_name, None)
  81. self.global_options = [
  82. (ext.option_name, None,
  83. "include %s (default if %s is available)"
  84. % (ext.feature_description, ext.feature_name)),
  85. (ext.neg_option_name, None,
  86. "exclude %s" % ext.feature_description),
  87. ] + self.global_options
  88. self.negative_opt = self.negative_opt.copy()
  89. self.negative_opt[ext.neg_option_name] = ext.option_name
  90. def has_ext_modules(self):
  91. if not self.ext_modules:
  92. return False
  93. for ext in self.ext_modules:
  94. with_ext = self.ext_status(ext)
  95. if with_ext is None or with_ext:
  96. return True
  97. return False
  98. def ext_status(self, ext):
  99. implementation = platform.python_implementation()
  100. if implementation != 'CPython':
  101. return False
  102. if isinstance(ext, Extension):
  103. with_ext = getattr(self, ext.attr_name)
  104. return with_ext
  105. else:
  106. return True
  107. class Extension(_Extension):
  108. def __init__(self, name, sources, feature_name, feature_description,
  109. feature_check, **kwds):
  110. if not with_cython:
  111. for filename in sources[:]:
  112. base, ext = os.path.splitext(filename)
  113. if ext == '.pyx':
  114. sources.remove(filename)
  115. sources.append('%s.c' % base)
  116. _Extension.__init__(self, name, sources, **kwds)
  117. self.feature_name = feature_name
  118. self.feature_description = feature_description
  119. self.feature_check = feature_check
  120. self.attr_name = 'with_' + feature_name.replace('-', '_')
  121. self.option_name = 'with-' + feature_name
  122. self.neg_option_name = 'without-' + feature_name
  123. class build_ext(_build_ext):
  124. def run(self):
  125. optional = True
  126. disabled = True
  127. for ext in self.extensions:
  128. with_ext = self.distribution.ext_status(ext)
  129. if with_ext is None:
  130. disabled = False
  131. elif with_ext:
  132. optional = False
  133. disabled = False
  134. break
  135. if disabled:
  136. return
  137. try:
  138. _build_ext.run(self)
  139. except DistutilsPlatformError:
  140. exc = sys.exc_info()[1]
  141. if optional:
  142. log.warn(str(exc))
  143. log.warn("skipping build_ext")
  144. else:
  145. raise
  146. def get_source_files(self):
  147. self.check_extensions_list(self.extensions)
  148. filenames = []
  149. for ext in self.extensions:
  150. if with_cython:
  151. self.cython_sources(ext.sources, ext)
  152. for filename in ext.sources:
  153. filenames.append(filename)
  154. base = os.path.splitext(filename)[0]
  155. for ext in ['c', 'h', 'pyx', 'pxd']:
  156. filename = '%s.%s' % (base, ext)
  157. if filename not in filenames and os.path.isfile(filename):
  158. filenames.append(filename)
  159. return filenames
  160. def get_outputs(self):
  161. self.check_extensions_list(self.extensions)
  162. outputs = []
  163. for ext in self.extensions:
  164. fullname = self.get_ext_fullname(ext.name)
  165. filename = os.path.join(self.build_lib,
  166. self.get_ext_filename(fullname))
  167. if os.path.isfile(filename):
  168. outputs.append(filename)
  169. return outputs
  170. def build_extensions(self):
  171. self.check_extensions_list(self.extensions)
  172. for ext in self.extensions:
  173. with_ext = self.distribution.ext_status(ext)
  174. if with_ext is None:
  175. with_ext = self.check_extension_availability(ext)
  176. if not with_ext:
  177. continue
  178. if with_cython:
  179. ext.sources = self.cython_sources(ext.sources, ext)
  180. self.build_extension(ext)
  181. def check_extension_availability(self, ext):
  182. cache = os.path.join(self.build_temp, 'check_%s.out' % ext.feature_name)
  183. if not self.force and os.path.isfile(cache):
  184. data = open(cache).read().strip()
  185. if data == '1':
  186. return True
  187. elif data == '0':
  188. return False
  189. mkpath(self.build_temp)
  190. src = os.path.join(self.build_temp, 'check_%s.c' % ext.feature_name)
  191. open(src, 'w').write(ext.feature_check)
  192. log.info("checking if %s is compilable" % ext.feature_name)
  193. try:
  194. [obj] = self.compiler.compile([src],
  195. macros=ext.define_macros+[(undef,) for undef in ext.undef_macros],
  196. include_dirs=ext.include_dirs,
  197. extra_postargs=(ext.extra_compile_args or []),
  198. depends=ext.depends)
  199. except CompileError:
  200. log.warn("")
  201. log.warn("%s is not found or a compiler error: forcing --%s"
  202. % (ext.feature_name, ext.neg_option_name))
  203. log.warn("(if %s is installed correctly, you may need to"
  204. % ext.feature_name)
  205. log.warn(" specify the option --include-dirs or uncomment and")
  206. log.warn(" modify the parameter include_dirs in setup.cfg)")
  207. open(cache, 'w').write('0\n')
  208. return False
  209. prog = 'check_%s' % ext.feature_name
  210. log.info("checking if %s is linkable" % ext.feature_name)
  211. try:
  212. self.compiler.link_executable([obj], prog,
  213. output_dir=self.build_temp,
  214. libraries=ext.libraries,
  215. library_dirs=ext.library_dirs,
  216. runtime_library_dirs=ext.runtime_library_dirs,
  217. extra_postargs=(ext.extra_link_args or []))
  218. except LinkError:
  219. log.warn("")
  220. log.warn("%s is not found or a linker error: forcing --%s"
  221. % (ext.feature_name, ext.neg_option_name))
  222. log.warn("(if %s is installed correctly, you may need to"
  223. % ext.feature_name)
  224. log.warn(" specify the option --library-dirs or uncomment and")
  225. log.warn(" modify the parameter library_dirs in setup.cfg)")
  226. open(cache, 'w').write('0\n')
  227. return False
  228. open(cache, 'w').write('1\n')
  229. return True
  230. class bdist_rpm(_bdist_rpm):
  231. def _make_spec_file(self):
  232. argv0 = sys.argv[0]
  233. features = []
  234. for ext in self.distribution.ext_modules:
  235. if not isinstance(ext, Extension):
  236. continue
  237. with_ext = getattr(self.distribution, ext.attr_name)
  238. if with_ext is None:
  239. continue
  240. if with_ext:
  241. features.append('--'+ext.option_name)
  242. else:
  243. features.append('--'+ext.neg_option_name)
  244. sys.argv[0] = ' '.join([argv0]+features)
  245. spec_file = _bdist_rpm._make_spec_file(self)
  246. sys.argv[0] = argv0
  247. return spec_file
  248. class test(Command):
  249. user_options = []
  250. def initialize_options(self):
  251. pass
  252. def finalize_options(self):
  253. pass
  254. def run(self):
  255. build_cmd = self.get_finalized_command('build')
  256. build_cmd.run()
  257. sys.path.insert(0, build_cmd.build_lib)
  258. if sys.version_info[0] < 3:
  259. sys.path.insert(0, 'tests/lib')
  260. else:
  261. sys.path.insert(0, 'tests/lib3')
  262. import test_all
  263. if not test_all.main([]):
  264. raise DistutilsError("Tests failed")
  265. cmdclass = {
  266. 'build_ext': build_ext,
  267. 'bdist_rpm': bdist_rpm,
  268. 'test': test,
  269. }
  270. if bdist_wheel:
  271. cmdclass['bdist_wheel'] = bdist_wheel
  272. if __name__ == '__main__':
  273. setup(
  274. name=NAME,
  275. version=VERSION,
  276. description=DESCRIPTION,
  277. long_description=LONG_DESCRIPTION,
  278. author=AUTHOR,
  279. author_email=AUTHOR_EMAIL,
  280. license=LICENSE,
  281. platforms=PLATFORMS,
  282. url=URL,
  283. download_url=DOWNLOAD_URL,
  284. classifiers=CLASSIFIERS,
  285. package_dir={'': {2: 'lib', 3: 'lib3'}[sys.version_info[0]]},
  286. packages=['yaml'],
  287. ext_modules=[
  288. Extension('_yaml', ['ext/_yaml.pyx'],
  289. 'libyaml', "LibYAML bindings", LIBYAML_CHECK,
  290. libraries=['yaml']),
  291. ],
  292. distclass=Distribution,
  293. cmdclass=cmdclass,
  294. )