Development of an internal social media platform with personalised dashboards for students
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.

__init__.py 5.7KB

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