1from distutils.errors import DistutilsArgError
2import inspect
3import glob
4import warnings
5import platform
6import distutils.command.install as orig
7
8import setuptools
9
10# Prior to numpy 1.9, NumPy relies on the '_install' name, so provide it for
11# now. See https://github.com/pypa/setuptools/issues/199/
12_install = orig.install
13
14
15class install(orig.install):
16    """Use easy_install to install the package, w/dependencies"""
17
18    user_options = orig.install.user_options + [
19        ('old-and-unmanageable', None, "Try not to use this!"),
20        ('single-version-externally-managed', None,
21         "used by system package builders to create 'flat' eggs"),
22    ]
23    boolean_options = orig.install.boolean_options + [
24        'old-and-unmanageable', 'single-version-externally-managed',
25    ]
26    new_commands = [
27        ('install_egg_info', lambda self: True),
28        ('install_scripts', lambda self: True),
29    ]
30    _nc = dict(new_commands)
31
32    def initialize_options(self):
33        orig.install.initialize_options(self)
34        self.old_and_unmanageable = None
35        self.single_version_externally_managed = None
36
37    def finalize_options(self):
38        orig.install.finalize_options(self)
39        if self.root:
40            self.single_version_externally_managed = True
41        elif self.single_version_externally_managed:
42            if not self.root and not self.record:
43                raise DistutilsArgError(
44                    "You must specify --record or --root when building system"
45                    " packages"
46                )
47
48    def handle_extra_path(self):
49        if self.root or self.single_version_externally_managed:
50            # explicit backward-compatibility mode, allow extra_path to work
51            return orig.install.handle_extra_path(self)
52
53        # Ignore extra_path when installing an egg (or being run by another
54        # command without --root or --single-version-externally-managed
55        self.path_file = None
56        self.extra_dirs = ''
57
58    def run(self):
59        # Explicit request for old-style install?  Just do it
60        if self.old_and_unmanageable or self.single_version_externally_managed:
61            return orig.install.run(self)
62
63        if not self._called_from_setup(inspect.currentframe()):
64            # Run in backward-compatibility mode to support bdist_* commands.
65            orig.install.run(self)
66        else:
67            self.do_egg_install()
68
69    @staticmethod
70    def _called_from_setup(run_frame):
71        """
72        Attempt to detect whether run() was called from setup() or by another
73        command.  If called by setup(), the parent caller will be the
74        'run_command' method in 'distutils.dist', and *its* caller will be
75        the 'run_commands' method.  If called any other way, the
76        immediate caller *might* be 'run_command', but it won't have been
77        called by 'run_commands'. Return True in that case or if a call stack
78        is unavailable. Return False otherwise.
79        """
80        if run_frame is None:
81            msg = "Call stack not available. bdist_* commands may fail."
82            warnings.warn(msg)
83            if platform.python_implementation() == 'IronPython':
84                msg = "For best results, pass -X:Frames to enable call stack."
85                warnings.warn(msg)
86            return True
87        res = inspect.getouterframes(run_frame)[2]
88        caller, = res[:1]
89        info = inspect.getframeinfo(caller)
90        caller_module = caller.f_globals.get('__name__', '')
91        return (
92            caller_module == 'distutils.dist'
93            and info.function == 'run_commands'
94        )
95
96    def do_egg_install(self):
97
98        easy_install = self.distribution.get_command_class('easy_install')
99
100        cmd = easy_install(
101            self.distribution, args="x", root=self.root, record=self.record,
102        )
103        cmd.ensure_finalized()  # finalize before bdist_egg munges install cmd
104        cmd.always_copy_from = '.'  # make sure local-dir eggs get installed
105
106        # pick up setup-dir .egg files only: no .egg-info
107        cmd.package_index.scan(glob.glob('*.egg'))
108
109        self.run_command('bdist_egg')
110        args = [self.distribution.get_command_obj('bdist_egg').egg_output]
111
112        if setuptools.bootstrap_install_from:
113            # Bootstrap self-installation of setuptools
114            args.insert(0, setuptools.bootstrap_install_from)
115
116        cmd.args = args
117        cmd.run(show_deprecation=False)
118        setuptools.bootstrap_install_from = None
119
120
121# XXX Python 3.1 doesn't see _nc if this is inside the class
122install.sub_commands = (
123    [cmd for cmd in orig.install.sub_commands if cmd[0] not in install._nc] +
124    install.new_commands
125)
126