• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3import os
4import sys
5import textwrap
6
7import setuptools
8from setuptools.command.install import install
9
10here = os.path.dirname(__file__)
11
12
13package_data = dict(
14    setuptools=['script (dev).tmpl', 'script.tmpl', 'site-patch.py'],
15)
16
17force_windows_specific_files = (
18    os.environ.get("SETUPTOOLS_INSTALL_WINDOWS_SPECIFIC_FILES", "1").lower()
19    not in ("", "0", "false", "no")
20)
21
22include_windows_files = sys.platform == 'win32' or force_windows_specific_files
23
24if include_windows_files:
25    package_data.setdefault('setuptools', []).extend(['*.exe'])
26    package_data.setdefault('setuptools.command', []).extend(['*.xml'])
27
28
29def pypi_link(pkg_filename):
30    """
31    Given the filename, including md5 fragment, construct the
32    dependency link for PyPI.
33    """
34    root = 'https://files.pythonhosted.org/packages/source'
35    name, sep, rest = pkg_filename.partition('-')
36    parts = root, name[0], name, pkg_filename
37    return '/'.join(parts)
38
39
40class install_with_pth(install):
41    """
42    Custom install command to install a .pth file for distutils patching.
43
44    This hack is necessary because there's no standard way to install behavior
45    on startup (and it's debatable if there should be one). This hack (ab)uses
46    the `extra_path` behavior in Setuptools to install a `.pth` file with
47    implicit behavior on startup to give higher precedence to the local version
48    of `distutils` over the version from the standard library.
49
50    Please do not replicate this behavior.
51    """
52
53    _pth_name = 'distutils-precedence'
54    _pth_contents = textwrap.dedent("""
55        import os
56        var = 'SETUPTOOLS_USE_DISTUTILS'
57        enabled = os.environ.get(var, 'local') == 'local'
58        enabled and __import__('_distutils_hack').add_shim()
59        """).lstrip().replace('\n', '; ')
60
61    def initialize_options(self):
62        install.initialize_options(self)
63        self.extra_path = self._pth_name, self._pth_contents
64
65    def finalize_options(self):
66        install.finalize_options(self)
67        self._restore_install_lib()
68
69    def _restore_install_lib(self):
70        """
71        Undo secondary effect of `extra_path` adding to `install_lib`
72        """
73        suffix = os.path.relpath(self.install_lib, self.install_libbase)
74
75        if suffix.strip() == self._pth_contents.strip():
76            self.install_lib = self.install_libbase
77
78
79setup_params = dict(
80    cmdclass={'install': install_with_pth},
81    package_data=package_data,
82)
83
84if __name__ == '__main__':
85    # allow setup.py to run from another directory
86    here and os.chdir(here)
87    dist = setuptools.setup(**setup_params)
88