Nan Zhang | 8539a2a | 2018-05-15 14:00:05 -0700 | [diff] [blame] | 1 | """ |
| 2 | If setuptools is not already installed in the environment, it's not possible |
| 3 | to invoke setuptools' own commands. This routine will bootstrap this local |
| 4 | environment by creating a minimal egg-info directory and then invoking the |
| 5 | egg-info command to flesh out the egg-info directory. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import unicode_literals |
| 9 | |
| 10 | import os |
| 11 | import sys |
| 12 | import textwrap |
| 13 | import subprocess |
| 14 | import io |
| 15 | |
| 16 | |
| 17 | minimal_egg_info = textwrap.dedent(""" |
| 18 | [distutils.commands] |
| 19 | egg_info = setuptools.command.egg_info:egg_info |
| 20 | |
| 21 | [distutils.setup_keywords] |
| 22 | include_package_data = setuptools.dist:assert_bool |
| 23 | install_requires = setuptools.dist:check_requirements |
| 24 | extras_require = setuptools.dist:check_extras |
| 25 | entry_points = setuptools.dist:check_entry_points |
| 26 | |
| 27 | [egg_info.writers] |
| 28 | dependency_links.txt = setuptools.command.egg_info:overwrite_arg |
| 29 | entry_points.txt = setuptools.command.egg_info:write_entries |
| 30 | requires.txt = setuptools.command.egg_info:write_requirements |
| 31 | """) |
| 32 | |
| 33 | |
| 34 | def ensure_egg_info(): |
| 35 | if os.path.exists('setuptools.egg-info'): |
| 36 | return |
| 37 | print("adding minimal entry_points") |
| 38 | build_egg_info() |
| 39 | |
| 40 | |
| 41 | def build_egg_info(): |
| 42 | """ |
| 43 | Build a minimal egg-info, enough to invoke egg_info |
| 44 | """ |
| 45 | |
| 46 | os.mkdir('setuptools.egg-info') |
| 47 | with io.open('setuptools.egg-info/entry_points.txt', 'w') as ep: |
| 48 | ep.write(minimal_egg_info) |
| 49 | |
| 50 | |
| 51 | def run_egg_info(): |
| 52 | cmd = [sys.executable, 'setup.py', 'egg_info'] |
| 53 | print("Regenerating egg_info") |
| 54 | subprocess.check_call(cmd) |
| 55 | print("...and again.") |
| 56 | subprocess.check_call(cmd) |
| 57 | |
| 58 | |
| 59 | def main(): |
| 60 | ensure_egg_info() |
| 61 | run_egg_info() |
| 62 | |
| 63 | |
| 64 | __name__ == '__main__' and main() |