~ibmcharmers/charms/xenial/ibm-cinder-storwize-svc/trunk

« back to all changes in this revision

Viewing changes to .tox/py35/lib/python3.5/site-packages/setuptools/monkey.py

  • Committer: Ankammarao
  • Date: 2017-03-06 05:11:42 UTC
  • Revision ID: achittet@in.ibm.com-20170306051142-dpg27z4es1k56hfn
Marked tests folder executable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Monkey patching of distutils.
 
3
"""
 
4
 
 
5
import sys
 
6
import distutils.filelist
 
7
import platform
 
8
import types
 
9
import functools
 
10
import inspect
 
11
 
 
12
from .py26compat import import_module
 
13
import six
 
14
 
 
15
import setuptools
 
16
 
 
17
__all__ = []
 
18
"""
 
19
Everything is private. Contact the project team
 
20
if you think you need this functionality.
 
21
"""
 
22
 
 
23
 
 
24
def get_unpatched(item):
 
25
    lookup = (
 
26
        get_unpatched_class if isinstance(item, six.class_types) else
 
27
        get_unpatched_function if isinstance(item, types.FunctionType) else
 
28
        lambda item: None
 
29
    )
 
30
    return lookup(item)
 
31
 
 
32
 
 
33
def get_unpatched_class(cls):
 
34
    """Protect against re-patching the distutils if reloaded
 
35
 
 
36
    Also ensures that no other distutils extension monkeypatched the distutils
 
37
    first.
 
38
    """
 
39
    external_bases = (
 
40
        cls
 
41
        for cls in inspect.getmro(cls)
 
42
        if not cls.__module__.startswith('setuptools')
 
43
    )
 
44
    base = next(external_bases)
 
45
    if not base.__module__.startswith('distutils'):
 
46
        msg = "distutils has already been patched by %r" % cls
 
47
        raise AssertionError(msg)
 
48
    return base
 
49
 
 
50
 
 
51
def patch_all():
 
52
    # we can't patch distutils.cmd, alas
 
53
    distutils.core.Command = setuptools.Command
 
54
 
 
55
    has_issue_12885 = sys.version_info <= (3, 5, 3)
 
56
 
 
57
    if has_issue_12885:
 
58
        # fix findall bug in distutils (http://bugs.python.org/issue12885)
 
59
        distutils.filelist.findall = setuptools.findall
 
60
 
 
61
    needs_warehouse = (
 
62
        sys.version_info < (2, 7, 13)
 
63
        or
 
64
        (3, 0) < sys.version_info < (3, 3, 7)
 
65
        or
 
66
        (3, 4) < sys.version_info < (3, 4, 6)
 
67
        or
 
68
        (3, 5) < sys.version_info <= (3, 5, 3)
 
69
    )
 
70
 
 
71
    if needs_warehouse:
 
72
        warehouse = 'https://upload.pypi.org/legacy/'
 
73
        distutils.config.PyPIRCCommand.DEFAULT_REPOSITORY = warehouse
 
74
 
 
75
    _patch_distribution_metadata_write_pkg_file()
 
76
    _patch_distribution_metadata_write_pkg_info()
 
77
 
 
78
    # Install Distribution throughout the distutils
 
79
    for module in distutils.dist, distutils.core, distutils.cmd:
 
80
        module.Distribution = setuptools.dist.Distribution
 
81
 
 
82
    # Install the patched Extension
 
83
    distutils.core.Extension = setuptools.extension.Extension
 
84
    distutils.extension.Extension = setuptools.extension.Extension
 
85
    if 'distutils.command.build_ext' in sys.modules:
 
86
        sys.modules['distutils.command.build_ext'].Extension = (
 
87
            setuptools.extension.Extension
 
88
        )
 
89
 
 
90
    patch_for_msvc_specialized_compiler()
 
91
 
 
92
 
 
93
def _patch_distribution_metadata_write_pkg_file():
 
94
    """Patch write_pkg_file to also write Requires-Python/Requires-External"""
 
95
    distutils.dist.DistributionMetadata.write_pkg_file = (
 
96
        setuptools.dist.write_pkg_file
 
97
    )
 
98
 
 
99
 
 
100
def _patch_distribution_metadata_write_pkg_info():
 
101
    """
 
102
    Workaround issue #197 - Python 3 prior to 3.2.2 uses an environment-local
 
103
    encoding to save the pkg_info. Monkey-patch its write_pkg_info method to
 
104
    correct this undesirable behavior.
 
105
    """
 
106
    environment_local = (3,) <= sys.version_info[:3] < (3, 2, 2)
 
107
    if not environment_local:
 
108
        return
 
109
 
 
110
    distutils.dist.DistributionMetadata.write_pkg_info = (
 
111
        setuptools.dist.write_pkg_info
 
112
    )
 
113
 
 
114
 
 
115
def patch_func(replacement, target_mod, func_name):
 
116
    """
 
117
    Patch func_name in target_mod with replacement
 
118
 
 
119
    Important - original must be resolved by name to avoid
 
120
    patching an already patched function.
 
121
    """
 
122
    original = getattr(target_mod, func_name)
 
123
 
 
124
    # set the 'unpatched' attribute on the replacement to
 
125
    # point to the original.
 
126
    vars(replacement).setdefault('unpatched', original)
 
127
 
 
128
    # replace the function in the original module
 
129
    setattr(target_mod, func_name, replacement)
 
130
 
 
131
 
 
132
def get_unpatched_function(candidate):
 
133
    return getattr(candidate, 'unpatched')
 
134
 
 
135
 
 
136
def patch_for_msvc_specialized_compiler():
 
137
    """
 
138
    Patch functions in distutils to use standalone Microsoft Visual C++
 
139
    compilers.
 
140
    """
 
141
    # import late to avoid circular imports on Python < 3.5
 
142
    msvc = import_module('setuptools.msvc')
 
143
 
 
144
    if platform.system() != 'Windows':
 
145
        # Compilers only availables on Microsoft Windows
 
146
        return
 
147
 
 
148
    def patch_params(mod_name, func_name):
 
149
        """
 
150
        Prepare the parameters for patch_func to patch indicated function.
 
151
        """
 
152
        repl_prefix = 'msvc9_' if 'msvc9' in mod_name else 'msvc14_'
 
153
        repl_name = repl_prefix + func_name.lstrip('_')
 
154
        repl = getattr(msvc, repl_name)
 
155
        mod = import_module(mod_name)
 
156
        if not hasattr(mod, func_name):
 
157
            raise ImportError(func_name)
 
158
        return repl, mod, func_name
 
159
 
 
160
    # Python 2.7 to 3.4
 
161
    msvc9 = functools.partial(patch_params, 'distutils.msvc9compiler')
 
162
 
 
163
    # Python 3.5+
 
164
    msvc14 = functools.partial(patch_params, 'distutils._msvccompiler')
 
165
 
 
166
    try:
 
167
        # Patch distutils.msvc9compiler
 
168
        patch_func(*msvc9('find_vcvarsall'))
 
169
        patch_func(*msvc9('query_vcvarsall'))
 
170
    except ImportError:
 
171
        pass
 
172
 
 
173
    try:
 
174
        # Patch distutils._msvccompiler._get_vc_env
 
175
        patch_func(*msvc14('_get_vc_env'))
 
176
    except ImportError:
 
177
        pass
 
178
 
 
179
    try:
 
180
        # Patch distutils._msvccompiler.gen_lib_options for Numpy
 
181
        patch_func(*msvc14('gen_lib_options'))
 
182
    except ImportError:
 
183
        pass