setup.py 9.8 KB

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