|
| 1 | +"""A PEP 517 interface to setuptools |
| 2 | +
|
| 3 | +Previously, when a user or a command line tool (let's call it a "frontend") |
| 4 | +needed to make a request of setuptools to take a certain action, for |
| 5 | +example, generating a list of installation requirements, the frontend would |
| 6 | +would call "setup.py egg_info" or "setup.py bdist_wheel" on the command line. |
| 7 | +
|
| 8 | +PEP 517 defines a different method of interfacing with setuptools. Rather |
| 9 | +than calling "setup.py" directly, the frontend should: |
| 10 | +
|
| 11 | + 1. Set the current directory to the directory with a setup.py file |
| 12 | + 2. Import this module into a safe python interpreter (one in which |
| 13 | + setuptools can potentially set global variables or crash hard). |
| 14 | + 3. Call one of the functions defined in PEP 517. |
| 15 | +
|
| 16 | +What each function does is defined in PEP 517. However, here is a "casual" |
| 17 | +definition of the functions (this definition should not be relied on for |
| 18 | +bug reports or API stability): |
| 19 | +
|
| 20 | + - `build_wheel`: build a wheel in the folder and return the basename |
| 21 | + - `get_requires_for_build_wheel`: get the `setup_requires` to build |
| 22 | + - `prepare_metadata_for_build_wheel`: get the `install_requires` |
| 23 | + - `build_sdist`: build an sdist in the folder and return the basename |
| 24 | + - `get_requires_for_build_sdist`: get the `setup_requires` to build |
| 25 | +
|
| 26 | +Again, this is not a formal definition! Just a "taste" of the module. |
| 27 | +""" |
| 28 | + |
| 29 | +import os |
| 30 | +import sys |
| 31 | +import tokenize |
| 32 | +import shutil |
| 33 | +import contextlib |
| 34 | + |
| 35 | +import setuptools |
| 36 | +import distutils |
| 37 | + |
| 38 | + |
| 39 | +class SetupRequirementsError(BaseException): |
| 40 | + def __init__(self, specifiers): |
| 41 | + self.specifiers = specifiers |
| 42 | + |
| 43 | + |
| 44 | +class Distribution(setuptools.dist.Distribution): |
| 45 | + def fetch_build_eggs(self, specifiers): |
| 46 | + raise SetupRequirementsError(specifiers) |
| 47 | + |
| 48 | + @classmethod |
| 49 | + @contextlib.contextmanager |
| 50 | + def patch(cls): |
| 51 | + """ |
| 52 | + Replace |
| 53 | + distutils.dist.Distribution with this class |
| 54 | + for the duration of this context. |
| 55 | + """ |
| 56 | + orig = distutils.core.Distribution |
| 57 | + distutils.core.Distribution = cls |
| 58 | + try: |
| 59 | + yield |
| 60 | + finally: |
| 61 | + distutils.core.Distribution = orig |
| 62 | + |
| 63 | + |
| 64 | +def _run_setup(setup_script='setup.py'): |
| 65 | + # Note that we can reuse our build directory between calls |
| 66 | + # Correctness comes first, then optimization later |
| 67 | + __file__ = setup_script |
| 68 | + f = getattr(tokenize, 'open', open)(__file__) |
| 69 | + code = f.read().replace('\\r\\n', '\\n') |
| 70 | + f.close() |
| 71 | + exec(compile(code, __file__, 'exec')) |
| 72 | + |
| 73 | + |
| 74 | +def _fix_config(config_settings): |
| 75 | + config_settings = config_settings or {} |
| 76 | + config_settings.setdefault('--global-option', []) |
| 77 | + return config_settings |
| 78 | + |
| 79 | + |
| 80 | +def _get_build_requires(config_settings): |
| 81 | + config_settings = _fix_config(config_settings) |
| 82 | + requirements = ['setuptools', 'wheel'] |
| 83 | + |
| 84 | + sys.argv = sys.argv[:1] + ['egg_info'] + \ |
| 85 | + config_settings["--global-option"] |
| 86 | + try: |
| 87 | + with Distribution.patch(): |
| 88 | + _run_setup() |
| 89 | + except SetupRequirementsError as e: |
| 90 | + requirements += e.specifiers |
| 91 | + |
| 92 | + return requirements |
| 93 | + |
| 94 | + |
| 95 | +def get_requires_for_build_wheel(config_settings=None): |
| 96 | + config_settings = _fix_config(config_settings) |
| 97 | + return _get_build_requires(config_settings) |
| 98 | + |
| 99 | + |
| 100 | +def get_requires_for_build_sdist(config_settings=None): |
| 101 | + config_settings = _fix_config(config_settings) |
| 102 | + return _get_build_requires(config_settings) |
| 103 | + |
| 104 | + |
| 105 | +def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None): |
| 106 | + sys.argv = sys.argv[:1] + ['dist_info', '--egg-base', metadata_directory] |
| 107 | + _run_setup() |
| 108 | + |
| 109 | + dist_infos = [f for f in os.listdir(metadata_directory) |
| 110 | + if f.endswith('.dist-info')] |
| 111 | + |
| 112 | + assert len(dist_infos) == 1 |
| 113 | + return dist_infos[0] |
| 114 | + |
| 115 | + |
| 116 | +def build_wheel(wheel_directory, config_settings=None, |
| 117 | + metadata_directory=None): |
| 118 | + config_settings = _fix_config(config_settings) |
| 119 | + wheel_directory = os.path.abspath(wheel_directory) |
| 120 | + sys.argv = sys.argv[:1] + ['bdist_wheel'] + \ |
| 121 | + config_settings["--global-option"] |
| 122 | + _run_setup() |
| 123 | + if wheel_directory != 'dist': |
| 124 | + shutil.rmtree(wheel_directory) |
| 125 | + shutil.copytree('dist', wheel_directory) |
| 126 | + |
| 127 | + wheels = [f for f in os.listdir(wheel_directory) |
| 128 | + if f.endswith('.whl')] |
| 129 | + |
| 130 | + assert len(wheels) == 1 |
| 131 | + return wheels[0] |
| 132 | + |
| 133 | + |
| 134 | +def build_sdist(sdist_directory, config_settings=None): |
| 135 | + config_settings = _fix_config(config_settings) |
| 136 | + sdist_directory = os.path.abspath(sdist_directory) |
| 137 | + sys.argv = sys.argv[:1] + ['sdist'] + \ |
| 138 | + config_settings["--global-option"] |
| 139 | + _run_setup() |
| 140 | + if sdist_directory != 'dist': |
| 141 | + shutil.rmtree(sdist_directory) |
| 142 | + shutil.copytree('dist', sdist_directory) |
| 143 | + |
| 144 | + sdists = [f for f in os.listdir(sdist_directory) |
| 145 | + if f.endswith('.tar.gz')] |
| 146 | + |
| 147 | + assert len(sdists) == 1 |
| 148 | + return sdists[0] |
0 commit comments