Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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

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