ptr.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. """
  2. Implementation
  3. """
  4. import os as _os
  5. import shlex as _shlex
  6. import contextlib as _contextlib
  7. import sys as _sys
  8. import operator as _operator
  9. import itertools as _itertools
  10. import warnings as _warnings
  11. try:
  12. # ensure that map has the same meaning on Python 2
  13. from future_builtins import map
  14. except ImportError:
  15. pass
  16. import pkg_resources
  17. import setuptools.command.test as orig
  18. from setuptools import Distribution
  19. @_contextlib.contextmanager
  20. def _save_argv(repl=None):
  21. saved = _sys.argv[:]
  22. if repl is not None:
  23. _sys.argv[:] = repl
  24. try:
  25. yield saved
  26. finally:
  27. _sys.argv[:] = saved
  28. class CustomizedDist(Distribution):
  29. allow_hosts = None
  30. index_url = None
  31. def fetch_build_egg(self, req):
  32. """ Specialized version of Distribution.fetch_build_egg
  33. that respects respects allow_hosts and index_url. """
  34. from setuptools.command.easy_install import easy_install
  35. dist = Distribution({'script_args': ['easy_install']})
  36. dist.parse_config_files()
  37. opts = dist.get_option_dict('easy_install')
  38. keep = (
  39. 'find_links',
  40. 'site_dirs',
  41. 'index_url',
  42. 'optimize',
  43. 'site_dirs',
  44. 'allow_hosts',
  45. )
  46. for key in list(opts):
  47. if key not in keep:
  48. del opts[key] # don't use any other settings
  49. if self.dependency_links:
  50. links = self.dependency_links[:]
  51. if 'find_links' in opts:
  52. links = opts['find_links'][1].split() + links
  53. opts['find_links'] = ('setup', links)
  54. if self.allow_hosts:
  55. opts['allow_hosts'] = ('test', self.allow_hosts)
  56. if self.index_url:
  57. opts['index_url'] = ('test', self.index_url)
  58. install_dir_func = getattr(self, 'get_egg_cache_dir', _os.getcwd)
  59. install_dir = install_dir_func()
  60. cmd = easy_install(
  61. dist,
  62. args=["x"],
  63. install_dir=install_dir,
  64. exclude_scripts=True,
  65. always_copy=False,
  66. build_directory=None,
  67. editable=False,
  68. upgrade=False,
  69. multi_version=True,
  70. no_report=True,
  71. user=False,
  72. )
  73. cmd.ensure_finalized()
  74. return cmd.easy_install(req)
  75. class PyTest(orig.test):
  76. """
  77. >>> import setuptools
  78. >>> dist = setuptools.Distribution()
  79. >>> cmd = PyTest(dist)
  80. """
  81. user_options = [
  82. ('extras', None, "Install (all) setuptools extras when running tests"),
  83. (
  84. 'index-url=',
  85. None,
  86. "Specify an index url from which to retrieve " "dependencies",
  87. ),
  88. (
  89. 'allow-hosts=',
  90. None,
  91. "Whitelist of comma-separated hosts to allow "
  92. "when retrieving dependencies",
  93. ),
  94. (
  95. 'addopts=',
  96. None,
  97. "Additional options to be passed verbatim to the " "pytest runner",
  98. ),
  99. ]
  100. def initialize_options(self):
  101. self.extras = False
  102. self.index_url = None
  103. self.allow_hosts = None
  104. self.addopts = []
  105. self.ensure_setuptools_version()
  106. @staticmethod
  107. def ensure_setuptools_version():
  108. """
  109. Due to the fact that pytest-runner is often required (via
  110. setup-requires directive) by toolchains that never invoke
  111. it (i.e. they're only installing the package, not testing it),
  112. instead of declaring the dependency in the package
  113. metadata, assert the requirement at run time.
  114. """
  115. pkg_resources.require('setuptools>=27.3')
  116. def finalize_options(self):
  117. if self.addopts:
  118. self.addopts = _shlex.split(self.addopts)
  119. @staticmethod
  120. def marker_passes(marker):
  121. """
  122. Given an environment marker, return True if the marker is valid
  123. and matches this environment.
  124. """
  125. return (
  126. not marker
  127. or not pkg_resources.invalid_marker(marker)
  128. and pkg_resources.evaluate_marker(marker)
  129. )
  130. def install_dists(self, dist):
  131. """
  132. Extend install_dists to include extras support
  133. """
  134. return _itertools.chain(
  135. orig.test.install_dists(dist), self.install_extra_dists(dist)
  136. )
  137. def install_extra_dists(self, dist):
  138. """
  139. Install extras that are indicated by markers or
  140. install all extras if '--extras' is indicated.
  141. """
  142. extras_require = dist.extras_require or {}
  143. spec_extras = (
  144. (spec.partition(':'), reqs) for spec, reqs in extras_require.items()
  145. )
  146. matching_extras = (
  147. reqs
  148. for (name, sep, marker), reqs in spec_extras
  149. # include unnamed extras or all if self.extras indicated
  150. if (not name or self.extras)
  151. # never include extras that fail to pass marker eval
  152. and self.marker_passes(marker)
  153. )
  154. results = list(map(dist.fetch_build_eggs, matching_extras))
  155. return _itertools.chain.from_iterable(results)
  156. @staticmethod
  157. def _warn_old_setuptools():
  158. msg = (
  159. "pytest-runner will stop working on this version of setuptools; "
  160. "please upgrade to setuptools 30.4 or later or pin to "
  161. "pytest-runner < 5."
  162. )
  163. ver_str = pkg_resources.get_distribution('setuptools').version
  164. ver = pkg_resources.parse_version(ver_str)
  165. if ver < pkg_resources.parse_version('30.4'):
  166. _warnings.warn(msg)
  167. def run(self):
  168. """
  169. Override run to ensure requirements are available in this session (but
  170. don't install them anywhere).
  171. """
  172. self._warn_old_setuptools()
  173. dist = CustomizedDist()
  174. for attr in 'allow_hosts index_url'.split():
  175. setattr(dist, attr, getattr(self, attr))
  176. for attr in (
  177. 'dependency_links install_requires ' 'tests_require extras_require '
  178. ).split():
  179. setattr(dist, attr, getattr(self.distribution, attr))
  180. installed_dists = self.install_dists(dist)
  181. if self.dry_run:
  182. self.announce('skipping tests (dry run)')
  183. return
  184. paths = map(_operator.attrgetter('location'), installed_dists)
  185. with self.paths_on_pythonpath(paths):
  186. with self.project_on_sys_path():
  187. return self.run_tests()
  188. @property
  189. def _argv(self):
  190. return ['pytest'] + self.addopts
  191. def run_tests(self):
  192. """
  193. Invoke pytest, replacing argv. Return result code.
  194. """
  195. with _save_argv(_sys.argv[:1] + self.addopts):
  196. result_code = __import__('pytest').main()
  197. if result_code:
  198. raise SystemExit(result_code)