__init__.py 5.6 KB

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