You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.9 KiB

4 years ago
  1. """Extensions to the 'distutils' for large or complex distributions"""
  2. import os
  3. import sys
  4. import functools
  5. import distutils.core
  6. import distutils.filelist
  7. from distutils.util import convert_path
  8. from fnmatch import fnmatchcase
  9. from ._deprecation_warning import SetuptoolsDeprecationWarning
  10. from setuptools.extern.six import PY3
  11. from setuptools.extern.six.moves import filter, map
  12. import setuptools.version
  13. from setuptools.extension import Extension
  14. from setuptools.dist import Distribution, Feature
  15. from setuptools.depends import Require
  16. from . import monkey
  17. __metaclass__ = type
  18. __all__ = [
  19. 'setup', 'Distribution', 'Feature', 'Command', 'Extension', 'Require',
  20. 'SetuptoolsDeprecationWarning',
  21. 'find_packages'
  22. ]
  23. if PY3:
  24. __all__.append('find_namespace_packages')
  25. __version__ = setuptools.version.__version__
  26. bootstrap_install_from = None
  27. # If we run 2to3 on .py files, should we also convert docstrings?
  28. # Default: yes; assume that we can detect doctests reliably
  29. run_2to3_on_doctests = True
  30. # Standard package names for fixer packages
  31. lib2to3_fixer_packages = ['lib2to3.fixes']
  32. class PackageFinder:
  33. """
  34. Generate a list of all Python packages found within a directory
  35. """
  36. @classmethod
  37. def find(cls, where='.', exclude=(), include=('*',)):
  38. """Return a list all Python packages found within directory 'where'
  39. 'where' is the root directory which will be searched for packages. It
  40. should be supplied as a "cross-platform" (i.e. URL-style) path; it will
  41. be converted to the appropriate local path syntax.
  42. 'exclude' is a sequence of package names to exclude; '*' can be used
  43. as a wildcard in the names, such that 'foo.*' will exclude all
  44. subpackages of 'foo' (but not 'foo' itself).
  45. 'include' is a sequence of package names to include. If it's
  46. specified, only the named packages will be included. If it's not
  47. specified, all found packages will be included. 'include' can contain
  48. shell style wildcard patterns just like 'exclude'.
  49. """
  50. return list(cls._find_packages_iter(
  51. convert_path(where),
  52. cls._build_filter('ez_setup', '*__pycache__', *exclude),
  53. cls._build_filter(*include)))
  54. @classmethod
  55. def _find_packages_iter(cls, where, exclude, include):
  56. """
  57. All the packages found in 'where' that pass the 'include' filter, but
  58. not the 'exclude' filter.
  59. """
  60. for root, dirs, files in os.walk(where, followlinks=True):
  61. # Copy dirs to iterate over it, then empty dirs.
  62. all_dirs = dirs[:]
  63. dirs[:] = []
  64. for dir in all_dirs:
  65. full_path = os.path.join(root, dir)
  66. rel_path = os.path.relpath(full_path, where)
  67. package = rel_path.replace(os.path.sep, '.')
  68. # Skip directory trees that are not valid packages
  69. if ('.' in dir or not cls._looks_like_package(full_path)):
  70. continue
  71. # Should this package be included?
  72. if include(package) and not exclude(package):
  73. yield package
  74. # Keep searching subdirectories, as there may be more packages
  75. # down there, even if the parent was excluded.
  76. dirs.append(dir)
  77. @staticmethod
  78. def _looks_like_package(path):
  79. """Does a directory look like a package?"""
  80. return os.path.isfile(os.path.join(path, '__init__.py'))
  81. @staticmethod
  82. def _build_filter(*patterns):
  83. """
  84. Given a list of patterns, return a callable that will be true only if
  85. the input matches at least one of the patterns.
  86. """
  87. return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
  88. class PEP420PackageFinder(PackageFinder):
  89. @staticmethod
  90. def _looks_like_package(path):
  91. return True
  92. find_packages = PackageFinder.find
  93. if PY3:
  94. find_namespace_packages = PEP420PackageFinder.find
  95. def _install_setup_requires(attrs):
  96. # Note: do not use `setuptools.Distribution` directly, as
  97. # our PEP 517 backend patch `distutils.core.Distribution`.
  98. dist = distutils.core.Distribution(dict(
  99. (k, v) for k, v in attrs.items()
  100. if k in ('dependency_links', 'setup_requires')
  101. ))
  102. # Honor setup.cfg's options.
  103. dist.parse_config_files(ignore_option_errors=True)
  104. if dist.setup_requires:
  105. dist.fetch_build_eggs(dist.setup_requires)
  106. def setup(**attrs):
  107. # Make sure we have any requirements needed to interpret 'attrs'.
  108. _install_setup_requires(attrs)
  109. return distutils.core.setup(**attrs)
  110. setup.__doc__ = distutils.core.setup.__doc__
  111. _Command = monkey.get_unpatched(distutils.core.Command)
  112. class Command(_Command):
  113. __doc__ = _Command.__doc__
  114. command_consumes_arguments = False
  115. def __init__(self, dist, **kw):
  116. """
  117. Construct the command for dist, updating
  118. vars(self) with any keyword parameters.
  119. """
  120. _Command.__init__(self, dist)
  121. vars(self).update(kw)
  122. def reinitialize_command(self, command, reinit_subcommands=0, **kw):
  123. cmd = _Command.reinitialize_command(self, command, reinit_subcommands)
  124. vars(cmd).update(kw)
  125. return cmd
  126. def _find_all_simple(path):
  127. """
  128. Find all files under 'path'
  129. """
  130. results = (
  131. os.path.join(base, file)
  132. for base, dirs, files in os.walk(path, followlinks=True)
  133. for file in files
  134. )
  135. return filter(os.path.isfile, results)
  136. def findall(dir=os.curdir):
  137. """
  138. Find all files under 'dir' and return the list of full filenames.
  139. Unless dir is '.', return full filenames with dir prepended.
  140. """
  141. files = _find_all_simple(dir)
  142. if dir == os.curdir:
  143. make_rel = functools.partial(os.path.relpath, start=dir)
  144. files = map(make_rel, files)
  145. return list(files)
  146. # Apply monkey patches
  147. monkey.patch_all()