__init__.py 5.6 KB

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