install.py 4.6 KB

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