Skip to content

Commit

Permalink
WIP: Use .pth file to import distutils from setuptools
Browse files Browse the repository at this point in the history
  • Loading branch information
pganssle committed Jul 13, 2020
1 parent 37d81f4 commit ddc542e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions _distutils_importer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os
import sys

here = os.path.dirname(__file__)
NEW_DISTUTILS_LOCATION = os.path.join(here, 'distutils-shim-package')

sys.path.insert(0, NEW_DISTUTILS_LOCATION)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import setuptools.distutils_patch

from distutils import *
1 change: 1 addition & 0 deletions distutils_precedence.pth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import _distutils_importer
31 changes: 31 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys

import setuptools
from setuptools.command.install import install

here = os.path.dirname(__file__)

Expand Down Expand Up @@ -49,6 +50,7 @@ def _gen_console_scripts():

package_data = dict(
setuptools=['script (dev).tmpl', 'script.tmpl', 'site-patch.py'],
_distutils_importer=['distutils-shim-package/distutils/__init__.py'],
)

force_windows_specific_files = (
Expand Down Expand Up @@ -81,8 +83,37 @@ def pypi_link(pkg_filename):
return '/'.join(parts)


class install_with_pth(install):
"""
Custom install command to install a .pth file for distutils patching.
This is necessary because there's no standard way to install a `.pth` file
alongside your package (and there probably shouldn't be one), but we need
to do this in order to give precedence higher precedence to our version of
`distutils` than the standard library.
"""

def initialize_options(self):
install.initialize_options(self)

name = 'distutils_precedence'
with open(os.path.join(here, name + '.pth'), 'rt') as f:
contents = f.read()

self.extra_path = (name, contents)

def finalize_options(self):
install.finalize_options(self)

install_suffix = os.path.relpath(self.install_lib, self.install_libbase)

if install_suffix == self.extra_path[1]:
self.install_lib = self.install_libbase


setup_params = dict(
src_root=None,
cmdclass = { 'install': install_with_pth },
package_data=package_data,
entry_points={
"distutils.commands": [
Expand Down

0 comments on commit ddc542e

Please sign in to comment.