1
0

__init__.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. """Extensions to the 'distutils' for large or complex distributions"""
  2. import os
  3. import functools
  4. # Disabled for now due to: #2228, #2230
  5. import setuptools.distutils_patch # noqa: F401
  6. import distutils.core
  7. import distutils.filelist
  8. import re
  9. from distutils.errors import DistutilsOptionError
  10. from distutils.util import convert_path
  11. from fnmatch import fnmatchcase
  12. from ._deprecation_warning import SetuptoolsDeprecationWarning
  13. from setuptools.extern.six import PY3, string_types
  14. from setuptools.extern.six.moves import filter, map
  15. import setuptools.version
  16. from setuptools.extension import Extension
  17. from setuptools.dist import Distribution
  18. from setuptools.depends import Require
  19. from . import monkey
  20. __metaclass__ = type
  21. __all__ = [
  22. 'setup', 'Distribution', 'Command', 'Extension', 'Require',
  23. 'SetuptoolsDeprecationWarning',
  24. 'find_packages'
  25. ]
  26. if PY3:
  27. __all__.append('find_namespace_packages')
  28. __version__ = setuptools.version.__version__
  29. bootstrap_install_from = None
  30. # If we run 2to3 on .py files, should we also convert docstrings?
  31. # Default: yes; assume that we can detect doctests reliably
  32. run_2to3_on_doctests = True
  33. # Standard package names for fixer packages
  34. lib2to3_fixer_packages = ['lib2to3.fixes']
  35. class PackageFinder:
  36. """
  37. Generate a list of all Python packages found within a directory
  38. """
  39. @classmethod
  40. def find(cls, where='.', exclude=(), include=('*',)):
  41. """Return a list all Python packages found within directory 'where'
  42. 'where' is the root directory which will be searched for packages. It
  43. should be supplied as a "cross-platform" (i.e. URL-style) path; it will
  44. be converted to the appropriate local path syntax.
  45. 'exclude' is a sequence of package names to exclude; '*' can be used
  46. as a wildcard in the names, such that 'foo.*' will exclude all
  47. subpackages of 'foo' (but not 'foo' itself).
  48. 'include' is a sequence of package names to include. If it's
  49. specified, only the named packages will be included. If it's not
  50. specified, all found packages will be included. 'include' can contain
  51. shell style wildcard patterns just like 'exclude'.
  52. """
  53. return list(cls._find_packages_iter(
  54. convert_path(where),
  55. cls._build_filter('ez_setup', '*__pycache__', *exclude),
  56. cls._build_filter(*include)))
  57. @classmethod
  58. def _find_packages_iter(cls, where, exclude, include):
  59. """
  60. All the packages found in 'where' that pass the 'include' filter, but
  61. not the 'exclude' filter.
  62. """
  63. for root, dirs, files in os.walk(where, followlinks=True):
  64. # Copy dirs to iterate over it, then empty dirs.
  65. all_dirs = dirs[:]
  66. dirs[:] = []
  67. for dir in all_dirs:
  68. full_path = os.path.join(root, dir)
  69. rel_path = os.path.relpath(full_path, where)
  70. package = rel_path.replace(os.path.sep, '.')
  71. # Skip directory trees that are not valid packages
  72. if ('.' in dir or not cls._looks_like_package(full_path)):
  73. continue
  74. # Should this package be included?
  75. if include(package) and not exclude(package):
  76. yield package
  77. # Keep searching subdirectories, as there may be more packages
  78. # down there, even if the parent was excluded.
  79. dirs.append(dir)
  80. @staticmethod
  81. def _looks_like_package(path):
  82. """Does a directory look like a package?"""
  83. return os.path.isfile(os.path.join(path, '__init__.py'))
  84. @staticmethod
  85. def _build_filter(*patterns):
  86. """
  87. Given a list of patterns, return a callable that will be true only if
  88. the input matches at least one of the patterns.
  89. """
  90. return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
  91. class PEP420PackageFinder(PackageFinder):
  92. @staticmethod
  93. def _looks_like_package(path):
  94. return True
  95. find_packages = PackageFinder.find
  96. if PY3:
  97. find_namespace_packages = PEP420PackageFinder.find
  98. def _install_setup_requires(attrs):
  99. # Note: do not use `setuptools.Distribution` directly, as
  100. # our PEP 517 backend patch `distutils.core.Distribution`.
  101. class MinimalDistribution(distutils.core.Distribution):
  102. """
  103. A minimal version of a distribution for supporting the
  104. fetch_build_eggs interface.
  105. """
  106. def __init__(self, attrs):
  107. _incl = 'dependency_links', 'setup_requires'
  108. filtered = {
  109. k: attrs[k]
  110. for k in set(_incl) & set(attrs)
  111. }
  112. distutils.core.Distribution.__init__(self, filtered)
  113. def finalize_options(self):
  114. """
  115. Disable finalize_options to avoid building the working set.
  116. Ref #2158.
  117. """
  118. dist = MinimalDistribution(attrs)
  119. # Honor setup.cfg's options.
  120. dist.parse_config_files(ignore_option_errors=True)
  121. if dist.setup_requires:
  122. dist.fetch_build_eggs(dist.setup_requires)
  123. def setup(**attrs):
  124. # Make sure we have any requirements needed to interpret 'attrs'.
  125. _install_setup_requires(attrs)
  126. return distutils.core.setup(**attrs)
  127. setup.__doc__ = distutils.core.setup.__doc__
  128. _Command = monkey.get_unpatched(distutils.core.Command)
  129. class Command(_Command):
  130. __doc__ = _Command.__doc__
  131. command_consumes_arguments = False
  132. def __init__(self, dist, **kw):
  133. """
  134. Construct the command for dist, updating
  135. vars(self) with any keyword parameters.
  136. """
  137. _Command.__init__(self, dist)
  138. vars(self).update(kw)
  139. def _ensure_stringlike(self, option, what, default=None):
  140. val = getattr(self, option)
  141. if val is None:
  142. setattr(self, option, default)
  143. return default
  144. elif not isinstance(val, string_types):
  145. raise DistutilsOptionError("'%s' must be a %s (got `%s`)"
  146. % (option, what, val))
  147. return val
  148. def ensure_string_list(self, option):
  149. r"""Ensure that 'option' is a list of strings. If 'option' is
  150. currently a string, we split it either on /,\s*/ or /\s+/, so
  151. "foo bar baz", "foo,bar,baz", and "foo, bar baz" all become
  152. ["foo", "bar", "baz"].
  153. """
  154. val = getattr(self, option)
  155. if val is None:
  156. return
  157. elif isinstance(val, string_types):
  158. setattr(self, option, re.split(r',\s*|\s+', val))
  159. else:
  160. if isinstance(val, list):
  161. ok = all(isinstance(v, string_types) for v in val)
  162. else:
  163. ok = False
  164. if not ok:
  165. raise DistutilsOptionError(
  166. "'%s' must be a list of strings (got %r)"
  167. % (option, val))
  168. def reinitialize_command(self, command, reinit_subcommands=0, **kw):
  169. cmd = _Command.reinitialize_command(self, command, reinit_subcommands)
  170. vars(cmd).update(kw)
  171. return cmd
  172. def _find_all_simple(path):
  173. """
  174. Find all files under 'path'
  175. """
  176. results = (
  177. os.path.join(base, file)
  178. for base, dirs, files in os.walk(path, followlinks=True)
  179. for file in files
  180. )
  181. return filter(os.path.isfile, results)
  182. def findall(dir=os.curdir):
  183. """
  184. Find all files under 'dir' and return the list of full filenames.
  185. Unless dir is '.', return full filenames with dir prepended.
  186. """
  187. files = _find_all_simple(dir)
  188. if dir == os.curdir:
  189. make_rel = functools.partial(os.path.relpath, start=dir)
  190. files = map(make_rel, files)
  191. return list(files)
  192. class sic(str):
  193. """Treat this string as-is (https://en.wikipedia.org/wiki/Sic)"""
  194. # Apply monkey patches
  195. monkey.patch_all()