1"""
2If setuptools is not already installed in the environment, it's not possible
3to invoke setuptools' own commands. This routine will bootstrap this local
4environment by creating a minimal egg-info directory and then invoking the
5egg-info command to flesh out the egg-info directory.
6"""
7
8from __future__ import unicode_literals
9
10import os
11import sys
12import textwrap
13import subprocess
14import io
15
16
17minimal_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
34def 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
41def 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
51def 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
59def main():
60    ensure_egg_info()
61    run_egg_info()
62
63
64__name__ == '__main__' and main()
65