setup.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import os
  2. import re
  3. import sys
  4. import fnmatch
  5. # for command line options and supported environment variables, please
  6. # see the end of 'setupinfo.py'
  7. try:
  8. from setuptools import setup
  9. except ImportError:
  10. from distutils.core import setup
  11. import versioninfo
  12. import setupinfo
  13. # override these and pass --static for a static build. See
  14. # doc/build.txt for more information. If you do not pass --static
  15. # changing this will have no effect.
  16. STATIC_INCLUDE_DIRS = []
  17. STATIC_LIBRARY_DIRS = []
  18. STATIC_CFLAGS = []
  19. STATIC_BINARIES = []
  20. # create lxml-version.h file
  21. svn_version = versioninfo.svn_version()
  22. versioninfo.create_version_h(svn_version)
  23. print("Building lxml version %s." % svn_version)
  24. OPTION_RUN_TESTS = setupinfo.has_option('run-tests')
  25. branch_link = """
  26. After an official release of a new stable series, bug fixes may become
  27. available at
  28. https://github.com/lxml/lxml/tree/lxml-%(branch_version)s .
  29. Running ``easy_install lxml==%(branch_version)sbugfix`` will install
  30. the unreleased branch state from
  31. https://github.com/lxml/lxml/tarball/lxml-%(branch_version)s#egg=lxml-%(branch_version)sbugfix
  32. as soon as a maintenance branch has been established. Note that this
  33. requires Cython to be installed at an appropriate version for the build.
  34. """
  35. if versioninfo.is_pre_release():
  36. branch_link = ""
  37. extra_options = {}
  38. if 'setuptools' in sys.modules:
  39. extra_options['zip_safe'] = False
  40. try:
  41. import pkg_resources
  42. except ImportError:
  43. pass
  44. else:
  45. f = open("requirements.txt", "r")
  46. try:
  47. deps = [str(req) for req in pkg_resources.parse_requirements(f)]
  48. finally:
  49. f.close()
  50. extra_options['extras_require'] = {
  51. 'source': deps,
  52. 'cssselect': 'cssselect>=0.7',
  53. 'html5': 'html5lib',
  54. 'htmlsoup': 'BeautifulSoup4',
  55. }
  56. extra_options.update(setupinfo.extra_setup_args())
  57. extra_options['package_data'] = {
  58. 'lxml': [
  59. 'lxml.etree.h',
  60. 'lxml.etree_api.h',
  61. ],
  62. 'lxml.includes': [
  63. '*.pxd', '*.h'
  64. ],
  65. 'lxml.isoschematron': [
  66. 'resources/rng/iso-schematron.rng',
  67. 'resources/xsl/*.xsl',
  68. 'resources/xsl/iso-schematron-xslt1/*.xsl',
  69. 'resources/xsl/iso-schematron-xslt1/readme.txt'
  70. ],
  71. }
  72. extra_options['package_dir'] = {
  73. '': 'src'
  74. }
  75. extra_options['packages'] = [
  76. 'lxml', 'lxml.includes', 'lxml.html', 'lxml.isoschematron'
  77. ]
  78. def setup_extra_options():
  79. is_interesting_package = re.compile('^(libxml|libxslt|libexslt)$').match
  80. def extract_files(directories, pattern='*'):
  81. def get_files(root, dir_path, files):
  82. return [ (root, dir_path, filename)
  83. for filename in fnmatch.filter(files, pattern) ]
  84. file_list = []
  85. for dir_path in directories:
  86. dir_path = os.path.realpath(dir_path)
  87. for root, dirs, files in os.walk(dir_path):
  88. rel_dir = root[len(dir_path)+1:]
  89. if is_interesting_package(rel_dir):
  90. file_list.extend(get_files(root, rel_dir, files))
  91. return file_list
  92. def build_packages(files):
  93. packages = {}
  94. seen = set()
  95. for root_path, rel_path, filename in files:
  96. if filename in seen:
  97. # libxml2/libxslt header filenames are unique
  98. continue
  99. seen.add(filename)
  100. package_path = '.'.join(rel_path.split(os.sep))
  101. if package_path in packages:
  102. root, package_files = packages[package_path]
  103. if root != root_path:
  104. print("conflicting directories found for include package '%s': %s and %s"
  105. % (package_path, root_path, root))
  106. continue
  107. else:
  108. package_files = []
  109. packages[package_path] = (root_path, package_files)
  110. package_files.append(filename)
  111. return packages
  112. # Copy Global Extra Options
  113. extra_opts = dict(extra_options)
  114. # Build ext modules
  115. ext_modules = setupinfo.ext_modules(
  116. STATIC_INCLUDE_DIRS, STATIC_LIBRARY_DIRS,
  117. STATIC_CFLAGS, STATIC_BINARIES)
  118. extra_opts['ext_modules'] = ext_modules
  119. packages = extra_opts.get('packages', list())
  120. package_dir = extra_opts.get('package_dir', dict())
  121. package_data = extra_opts.get('package_data', dict())
  122. # Add lxml.include with (lxml, libxslt headers...)
  123. # python setup.py build --static --static-deps install
  124. # python setup.py bdist_wininst --static
  125. if setupinfo.OPTION_STATIC:
  126. include_dirs = [] # keep them in order
  127. for extension in ext_modules:
  128. for inc_dir in extension.include_dirs:
  129. if inc_dir not in include_dirs:
  130. include_dirs.append(inc_dir)
  131. header_packages = build_packages(extract_files(include_dirs))
  132. for package_path, (root_path, filenames) in header_packages.items():
  133. if package_path:
  134. package = 'lxml.includes.' + package_path
  135. packages.append(package)
  136. else:
  137. package = 'lxml.includes'
  138. package_data[package] = filenames
  139. package_dir[package] = root_path
  140. return extra_opts
  141. setup(
  142. name = "lxml",
  143. version = versioninfo.version(),
  144. author="lxml dev team",
  145. author_email="lxml-dev@lxml.de",
  146. maintainer="lxml dev team",
  147. maintainer_email="lxml-dev@lxml.de",
  148. url="http://lxml.de/",
  149. download_url="http://pypi.python.org/packages/source/l/lxml/lxml-%s.tar.gz" % versioninfo.version(),
  150. bugtrack_url="https://bugs.launchpad.net/lxml",
  151. description="Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API.",
  152. long_description=((("""\
  153. lxml is a Pythonic, mature binding for the libxml2 and libxslt libraries. It
  154. provides safe and convenient access to these libraries using the ElementTree
  155. API.
  156. It extends the ElementTree API significantly to offer support for XPath,
  157. RelaxNG, XML Schema, XSLT, C14N and much more.
  158. To contact the project, go to the `project home page
  159. <http://lxml.de/>`_ or see our bug tracker at
  160. https://launchpad.net/lxml
  161. In case you want to use the current in-development version of lxml,
  162. you can get it from the github repository at
  163. https://github.com/lxml/lxml . Note that this requires Cython to
  164. build the sources, see the build instructions on the project home
  165. page. To the same end, running ``easy_install lxml==dev`` will
  166. install lxml from
  167. https://github.com/lxml/lxml/tarball/master#egg=lxml-dev if you have
  168. an appropriate version of Cython installed.
  169. """ + branch_link) % { "branch_version" : versioninfo.branch_version() }) +
  170. versioninfo.changes()),
  171. classifiers = [
  172. versioninfo.dev_status(),
  173. 'Intended Audience :: Developers',
  174. 'Intended Audience :: Information Technology',
  175. 'License :: OSI Approved :: BSD License',
  176. 'Programming Language :: Cython',
  177. 'Programming Language :: Python :: 2',
  178. 'Programming Language :: Python :: 2.4',
  179. 'Programming Language :: Python :: 2.5',
  180. 'Programming Language :: Python :: 2.6',
  181. 'Programming Language :: Python :: 2.7',
  182. 'Programming Language :: Python :: 3',
  183. 'Programming Language :: Python :: 3.1',
  184. 'Programming Language :: Python :: 3.2',
  185. 'Programming Language :: Python :: 3.3',
  186. 'Programming Language :: Python :: 3.4',
  187. 'Programming Language :: C',
  188. 'Operating System :: OS Independent',
  189. 'Topic :: Text Processing :: Markup :: HTML',
  190. 'Topic :: Text Processing :: Markup :: XML',
  191. 'Topic :: Software Development :: Libraries :: Python Modules'
  192. ],
  193. **setup_extra_options()
  194. )
  195. if OPTION_RUN_TESTS:
  196. print("Running tests.")
  197. import test
  198. sys.exit( test.main(sys.argv[:1]) )