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.

monkey.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """
  2. Monkey patching of distutils.
  3. """
  4. import sys
  5. import distutils.filelist
  6. import platform
  7. import types
  8. import functools
  9. from importlib import import_module
  10. import inspect
  11. from setuptools.extern import six
  12. import setuptools
  13. __all__ = []
  14. """
  15. Everything is private. Contact the project team
  16. if you think you need this functionality.
  17. """
  18. def _get_mro(cls):
  19. """
  20. Returns the bases classes for cls sorted by the MRO.
  21. Works around an issue on Jython where inspect.getmro will not return all
  22. base classes if multiple classes share the same name. Instead, this
  23. function will return a tuple containing the class itself, and the contents
  24. of cls.__bases__. See https://github.com/pypa/setuptools/issues/1024.
  25. """
  26. if platform.python_implementation() == "Jython":
  27. return (cls,) + cls.__bases__
  28. return inspect.getmro(cls)
  29. def get_unpatched(item):
  30. lookup = (
  31. get_unpatched_class if isinstance(item, six.class_types) else
  32. get_unpatched_function if isinstance(item, types.FunctionType) else
  33. lambda item: None
  34. )
  35. return lookup(item)
  36. def get_unpatched_class(cls):
  37. """Protect against re-patching the distutils if reloaded
  38. Also ensures that no other distutils extension monkeypatched the distutils
  39. first.
  40. """
  41. external_bases = (
  42. cls
  43. for cls in _get_mro(cls)
  44. if not cls.__module__.startswith('setuptools')
  45. )
  46. base = next(external_bases)
  47. if not base.__module__.startswith('distutils'):
  48. msg = "distutils has already been patched by %r" % cls
  49. raise AssertionError(msg)
  50. return base
  51. def patch_all():
  52. # we can't patch distutils.cmd, alas
  53. distutils.core.Command = setuptools.Command
  54. has_issue_12885 = sys.version_info <= (3, 5, 3)
  55. if has_issue_12885:
  56. # fix findall bug in distutils (http://bugs.python.org/issue12885)
  57. distutils.filelist.findall = setuptools.findall
  58. needs_warehouse = (
  59. sys.version_info < (2, 7, 13)
  60. or
  61. (3, 4) < sys.version_info < (3, 4, 6)
  62. or
  63. (3, 5) < sys.version_info <= (3, 5, 3)
  64. )
  65. if needs_warehouse:
  66. warehouse = 'https://upload.pypi.org/legacy/'
  67. distutils.config.PyPIRCCommand.DEFAULT_REPOSITORY = warehouse
  68. _patch_distribution_metadata_write_pkg_file()
  69. # Install Distribution throughout the distutils
  70. for module in distutils.dist, distutils.core, distutils.cmd:
  71. module.Distribution = setuptools.dist.Distribution
  72. # Install the patched Extension
  73. distutils.core.Extension = setuptools.extension.Extension
  74. distutils.extension.Extension = setuptools.extension.Extension
  75. if 'distutils.command.build_ext' in sys.modules:
  76. sys.modules['distutils.command.build_ext'].Extension = (
  77. setuptools.extension.Extension
  78. )
  79. patch_for_msvc_specialized_compiler()
  80. def _patch_distribution_metadata_write_pkg_file():
  81. """Patch write_pkg_file to also write Requires-Python/Requires-External"""
  82. distutils.dist.DistributionMetadata.write_pkg_file = (
  83. setuptools.dist.write_pkg_file
  84. )
  85. def patch_func(replacement, target_mod, func_name):
  86. """
  87. Patch func_name in target_mod with replacement
  88. Important - original must be resolved by name to avoid
  89. patching an already patched function.
  90. """
  91. original = getattr(target_mod, func_name)
  92. # set the 'unpatched' attribute on the replacement to
  93. # point to the original.
  94. vars(replacement).setdefault('unpatched', original)
  95. # replace the function in the original module
  96. setattr(target_mod, func_name, replacement)
  97. def get_unpatched_function(candidate):
  98. return getattr(candidate, 'unpatched')
  99. def patch_for_msvc_specialized_compiler():
  100. """
  101. Patch functions in distutils to use standalone Microsoft Visual C++
  102. compilers.
  103. """
  104. # import late to avoid circular imports on Python < 3.5
  105. msvc = import_module('setuptools.msvc')
  106. if platform.system() != 'Windows':
  107. # Compilers only availables on Microsoft Windows
  108. return
  109. def patch_params(mod_name, func_name):
  110. """
  111. Prepare the parameters for patch_func to patch indicated function.
  112. """
  113. repl_prefix = 'msvc9_' if 'msvc9' in mod_name else 'msvc14_'
  114. repl_name = repl_prefix + func_name.lstrip('_')
  115. repl = getattr(msvc, repl_name)
  116. mod = import_module(mod_name)
  117. if not hasattr(mod, func_name):
  118. raise ImportError(func_name)
  119. return repl, mod, func_name
  120. # Python 2.7 to 3.4
  121. msvc9 = functools.partial(patch_params, 'distutils.msvc9compiler')
  122. # Python 3.5+
  123. msvc14 = functools.partial(patch_params, 'distutils._msvccompiler')
  124. try:
  125. # Patch distutils.msvc9compiler
  126. patch_func(*msvc9('find_vcvarsall'))
  127. patch_func(*msvc9('query_vcvarsall'))
  128. except ImportError:
  129. pass
  130. try:
  131. # Patch distutils._msvccompiler._get_vc_env
  132. patch_func(*msvc14('_get_vc_env'))
  133. except ImportError:
  134. pass
  135. try:
  136. # Patch distutils._msvccompiler.gen_lib_options for Numpy
  137. patch_func(*msvc14('gen_lib_options'))
  138. except ImportError:
  139. pass