Skip to content

Commit

Permalink
Merge d17c85e into ff2d56b
Browse files Browse the repository at this point in the history
  • Loading branch information
arcivanov committed Jan 11, 2022
2 parents ff2d56b + d17c85e commit fdf1e18
Show file tree
Hide file tree
Showing 83 changed files with 2,727 additions and 7,180 deletions.
7 changes: 5 additions & 2 deletions build.py
Expand Up @@ -107,13 +107,16 @@ def initialize(project):
"virtualenv>=20.0.0",
"importlib-resources>=1.0",
"importlib-metadata>=0.12",
"filelock<=3.4.1",
"platformdirs<=2.4.0",
"typing-extensions",
"colorama~=0.4.3"
])
project.set_property("vendorize_cleanup_globs", ["bin",
"setuptools",
"easy_install.py"])
project.set_property("vendorize_preserve_metadata", ["virtualenv*"])
"easy_install.py",
"*.pth"])
project.set_property("vendorize_preserve_metadata", ["virtualenv*", "importlib_metadata*"])

project.set_property("coverage_break_build", False)
project.get_property("coverage_exceptions").extend(["pybuilder._vendor",
Expand Down
311 changes: 288 additions & 23 deletions src/main/python/pybuilder/_vendor/LICENSES

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/python/pybuilder/_vendor/__init__.py
@@ -1 +1 @@
__names__ = ['_distutils_hack', 'backports', 'colorama', 'distlib', 'filelock', 'importlib_metadata', 'importlib_resources', 'pkg_resources', 'platformdirs', 'six', 'tailer', 'tblib', 'typing_extensions', 'virtualenv', 'zipp']
__names__ = ['_distutils_hack', 'colorama', 'distlib', 'filelock', 'importlib_metadata', 'importlib_resources', 'pkg_resources', 'platformdirs', 'six', 'tailer', 'tblib', 'typing_extensions', 'virtualenv', 'zipp']
108 changes: 89 additions & 19 deletions src/main/python/pybuilder/_vendor/_distutils_hack/__init__.py
@@ -1,25 +1,19 @@
# don't import any costly modules
import sys
import os
import re
import importlib
import warnings


is_pypy = '__pypy__' in sys.builtin_module_names


warnings.filterwarnings('ignore',
r'.+ distutils\b.+ deprecated',
DeprecationWarning)


def warn_distutils_present():
if 'distutils' not in sys.modules:
return
if is_pypy and sys.version_info < (3, 7):
# PyPy for 3.6 unconditionally imports distutils, so bypass the warning
# https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
return
import warnings
warnings.warn(
"Distutils was imported before Setuptools, but importing Setuptools "
"also replaces the `distutils` module in `sys.modules`. This may lead "
Expand All @@ -32,8 +26,12 @@ def warn_distutils_present():
def clear_distutils():
if 'distutils' not in sys.modules:
return
import warnings
warnings.warn("Setuptools is replacing distutils.")
mods = [name for name in sys.modules if re.match(r'distutils\b', name)]
mods = [
name for name in sys.modules
if name == "distutils" or name.startswith("distutils.")
]
for name in mods:
del sys.modules[name]

Expand All @@ -42,17 +40,21 @@ def enabled():
"""
Allow selection of distutils by environment variable.
"""
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
return which == 'local'


def ensure_local_distutils():
import importlib
clear_distutils()
distutils = importlib.import_module('setuptools._distutils')
distutils.__name__ = 'distutils'
sys.modules['distutils'] = distutils

# sanity check that submodules load as expected
# With the DistutilsMetaFinder in place,
# perform an import to cause distutils to be
# loaded from setuptools._distutils. Ref #2906.
with shim():
importlib.import_module('distutils')

# check that submodules load as expected
core = importlib.import_module('distutils.core')
assert '_distutils' in core.__file__, core.__file__

Expand All @@ -69,6 +71,14 @@ def do_override():
ensure_local_distutils()


class _TrivialRe:
def __init__(self, *patterns):
self._patterns = patterns

def match(self, string):
return all(pat in string for pat in self._patterns)


class DistutilsMetaFinder:
def find_spec(self, fullname, path, target=None):
if path is not None:
Expand All @@ -79,18 +89,45 @@ def find_spec(self, fullname, path, target=None):
return method()

def spec_for_distutils(self):
import importlib
import importlib.abc
import importlib.util
import warnings

# warnings.filterwarnings() imports the re module
warnings._add_filter(
'ignore',
_TrivialRe("distutils", "deprecated"),
DeprecationWarning,
None,
0,
append=True
)

try:
mod = importlib.import_module('setuptools._distutils')
except Exception:
# There are a couple of cases where setuptools._distutils
# may not be present:
# - An older Setuptools without a local distutils is
# taking precedence. Ref #2957.
# - Path manipulation during sitecustomize removes
# setuptools from the path but only after the hook
# has been loaded. Ref #2980.
# In either case, fall back to stdlib behavior.
return

class DistutilsLoader(importlib.abc.Loader):

def create_module(self, spec):
return importlib.import_module('setuptools._distutils')
return mod

def exec_module(self, module):
pass

return importlib.util.spec_from_loader('distutils', DistutilsLoader())
return importlib.util.spec_from_loader(
'distutils', DistutilsLoader(), origin=mod.__file__
)

def spec_for_pip(self):
"""
Expand All @@ -99,25 +136,58 @@ def spec_for_pip(self):
"""
if self.pip_imported_during_build():
return
if self.is_get_pip():
return
clear_distutils()
self.spec_for_distutils = lambda: None

@staticmethod
def pip_imported_during_build():
@classmethod
def pip_imported_during_build(cls):
"""
Detect if pip is being imported in a build script. Ref #2355.
"""
import traceback
return any(
frame.f_globals['__file__'].endswith('setup.py')
cls.frame_file_is_setup(frame)
for frame, line in traceback.walk_stack(None)
)

@classmethod
def is_get_pip(cls):
"""
Detect if get-pip is being invoked. Ref #2993.
"""
try:
import __main__
return os.path.basename(__main__.__file__) == 'get-pip.py'
except AttributeError:
pass

@staticmethod
def frame_file_is_setup(frame):
"""
Return True if the indicated frame suggests a setup.py file.
"""
# some frames may not have __file__ (#2940)
return frame.f_globals.get('__file__', '').endswith('setup.py')


DISTUTILS_FINDER = DistutilsMetaFinder()


def add_shim():
DISTUTILS_FINDER in sys.meta_path or insert_shim()


class shim:
def __enter__(self):
insert_shim()

def __exit__(self, exc, value, tb):
remove_shim()


def insert_shim():
sys.meta_path.insert(0, DISTUTILS_FINDER)


Expand Down
1 change: 0 additions & 1 deletion src/main/python/pybuilder/_vendor/backports/__init__.py

This file was deleted.

0 comments on commit fdf1e18

Please sign in to comment.