install.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. warnings.warn(
  28. "setup.py install is deprecated. "
  29. "Use build and pip and other standards-based tools.",
  30. setuptools.SetuptoolsDeprecationWarning,
  31. )
  32. orig.install.initialize_options(self)
  33. self.old_and_unmanageable = None
  34. self.single_version_externally_managed = None
  35. def finalize_options(self):
  36. orig.install.finalize_options(self)
  37. if self.root:
  38. self.single_version_externally_managed = True
  39. elif self.single_version_externally_managed:
  40. if not self.root and not self.record:
  41. raise DistutilsArgError(
  42. "You must specify --record or --root when building system"
  43. " packages"
  44. )
  45. def handle_extra_path(self):
  46. if self.root or self.single_version_externally_managed:
  47. # explicit backward-compatibility mode, allow extra_path to work
  48. return orig.install.handle_extra_path(self)
  49. # Ignore extra_path when installing an egg (or being run by another
  50. # command without --root or --single-version-externally-managed
  51. self.path_file = None
  52. self.extra_dirs = ''
  53. def run(self):
  54. # Explicit request for old-style install? Just do it
  55. if self.old_and_unmanageable or self.single_version_externally_managed:
  56. return orig.install.run(self)
  57. if not self._called_from_setup(inspect.currentframe()):
  58. # Run in backward-compatibility mode to support bdist_* commands.
  59. orig.install.run(self)
  60. else:
  61. self.do_egg_install()
  62. @staticmethod
  63. def _called_from_setup(run_frame):
  64. """
  65. Attempt to detect whether run() was called from setup() or by another
  66. command. If called by setup(), the parent caller will be the
  67. 'run_command' method in 'distutils.dist', and *its* caller will be
  68. the 'run_commands' method. If called any other way, the
  69. immediate caller *might* be 'run_command', but it won't have been
  70. called by 'run_commands'. Return True in that case or if a call stack
  71. is unavailable. Return False otherwise.
  72. """
  73. if run_frame is None:
  74. msg = "Call stack not available. bdist_* commands may fail."
  75. warnings.warn(msg)
  76. if platform.python_implementation() == 'IronPython':
  77. msg = "For best results, pass -X:Frames to enable call stack."
  78. warnings.warn(msg)
  79. return True
  80. frames = inspect.getouterframes(run_frame)
  81. for frame in frames[2:4]:
  82. caller, = frame[:1]
  83. info = inspect.getframeinfo(caller)
  84. caller_module = caller.f_globals.get('__name__', '')
  85. if caller_module == "setuptools.dist" and info.function == "run_command":
  86. # Starting from v61.0.0 setuptools overwrites dist.run_command
  87. continue
  88. return (
  89. caller_module == 'distutils.dist'
  90. and info.function == 'run_commands'
  91. )
  92. def do_egg_install(self):
  93. easy_install = self.distribution.get_command_class('easy_install')
  94. cmd = easy_install(
  95. self.distribution, args="x", root=self.root, record=self.record,
  96. )
  97. cmd.ensure_finalized() # finalize before bdist_egg munges install cmd
  98. cmd.always_copy_from = '.' # make sure local-dir eggs get installed
  99. # pick up setup-dir .egg files only: no .egg-info
  100. cmd.package_index.scan(glob.glob('*.egg'))
  101. self.run_command('bdist_egg')
  102. args = [self.distribution.get_command_obj('bdist_egg').egg_output]
  103. if setuptools.bootstrap_install_from:
  104. # Bootstrap self-installation of setuptools
  105. args.insert(0, setuptools.bootstrap_install_from)
  106. cmd.args = args
  107. cmd.run(show_deprecation=False)
  108. setuptools.bootstrap_install_from = None
  109. # XXX Python 3.1 doesn't see _nc if this is inside the class
  110. install.sub_commands = (
  111. [cmd for cmd in orig.install.sub_commands if cmd[0] not in install._nc] +
  112. install.new_commands
  113. )