Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

196 рядки
6.7 KiB

  1. import glob
  2. import os
  3. import setuptools
  4. from setuptools import _normalization, _path, namespaces
  5. from setuptools.command.easy_install import easy_install
  6. from ..unicode_utils import _read_utf8_with_fallback
  7. from distutils import log
  8. from distutils.errors import DistutilsOptionError
  9. from distutils.util import convert_path
  10. class develop(namespaces.DevelopInstaller, easy_install):
  11. """Set up package for development"""
  12. description = "install package in 'development mode'"
  13. user_options = easy_install.user_options + [
  14. ("uninstall", "u", "Uninstall this source package"),
  15. ("egg-path=", None, "Set the path to be used in the .egg-link file"),
  16. ]
  17. boolean_options = easy_install.boolean_options + ['uninstall']
  18. command_consumes_arguments = False # override base
  19. def run(self):
  20. if self.uninstall:
  21. self.multi_version = True
  22. self.uninstall_link()
  23. self.uninstall_namespaces()
  24. else:
  25. self.install_for_development()
  26. self.warn_deprecated_options()
  27. def initialize_options(self):
  28. self.uninstall = None
  29. self.egg_path = None
  30. easy_install.initialize_options(self)
  31. self.setup_path = None
  32. self.always_copy_from = '.' # always copy eggs installed in curdir
  33. def finalize_options(self) -> None:
  34. import pkg_resources
  35. ei = self.get_finalized_command("egg_info")
  36. self.args = [ei.egg_name]
  37. easy_install.finalize_options(self)
  38. self.expand_basedirs()
  39. self.expand_dirs()
  40. # pick up setup-dir .egg files only: no .egg-info
  41. self.package_index.scan(glob.glob('*.egg'))
  42. egg_link_fn = (
  43. _normalization.filename_component_broken(ei.egg_name) + '.egg-link'
  44. )
  45. self.egg_link = os.path.join(self.install_dir, egg_link_fn)
  46. self.egg_base = ei.egg_base
  47. if self.egg_path is None:
  48. self.egg_path = os.path.abspath(ei.egg_base)
  49. target = _path.normpath(self.egg_base)
  50. egg_path = _path.normpath(os.path.join(self.install_dir, self.egg_path))
  51. if egg_path != target:
  52. raise DistutilsOptionError(
  53. "--egg-path must be a relative path from the install"
  54. " directory to " + target
  55. )
  56. # Make a distribution for the package's source
  57. self.dist = pkg_resources.Distribution(
  58. target,
  59. pkg_resources.PathMetadata(target, os.path.abspath(ei.egg_info)),
  60. project_name=ei.egg_name,
  61. )
  62. self.setup_path = self._resolve_setup_path(
  63. self.egg_base,
  64. self.install_dir,
  65. self.egg_path,
  66. )
  67. @staticmethod
  68. def _resolve_setup_path(egg_base, install_dir, egg_path):
  69. """
  70. Generate a path from egg_base back to '.' where the
  71. setup script resides and ensure that path points to the
  72. setup path from $install_dir/$egg_path.
  73. """
  74. path_to_setup = egg_base.replace(os.sep, '/').rstrip('/')
  75. if path_to_setup != os.curdir:
  76. path_to_setup = '../' * (path_to_setup.count('/') + 1)
  77. resolved = _path.normpath(os.path.join(install_dir, egg_path, path_to_setup))
  78. curdir = _path.normpath(os.curdir)
  79. if resolved != curdir:
  80. raise DistutilsOptionError(
  81. "Can't get a consistent path to setup script from"
  82. " installation directory",
  83. resolved,
  84. curdir,
  85. )
  86. return path_to_setup
  87. def install_for_development(self) -> None:
  88. self.run_command('egg_info')
  89. # Build extensions in-place
  90. self.reinitialize_command('build_ext', inplace=True)
  91. self.run_command('build_ext')
  92. if setuptools.bootstrap_install_from:
  93. self.easy_install(setuptools.bootstrap_install_from)
  94. setuptools.bootstrap_install_from = None
  95. self.install_namespaces()
  96. # create an .egg-link in the installation dir, pointing to our egg
  97. log.info("Creating %s (link to %s)", self.egg_link, self.egg_base)
  98. if not self.dry_run:
  99. with open(self.egg_link, "w", encoding="utf-8") as f:
  100. f.write(self.egg_path + "\n" + self.setup_path)
  101. # postprocess the installed distro, fixing up .pth, installing scripts,
  102. # and handling requirements
  103. self.process_distribution(None, self.dist, not self.no_deps)
  104. def uninstall_link(self) -> None:
  105. if os.path.exists(self.egg_link):
  106. log.info("Removing %s (link to %s)", self.egg_link, self.egg_base)
  107. contents = [
  108. line.rstrip()
  109. for line in _read_utf8_with_fallback(self.egg_link).splitlines()
  110. ]
  111. if contents not in ([self.egg_path], [self.egg_path, self.setup_path]):
  112. log.warn("Link points to %s: uninstall aborted", contents)
  113. return
  114. if not self.dry_run:
  115. os.unlink(self.egg_link)
  116. if not self.dry_run:
  117. self.update_pth(self.dist) # remove any .pth link to us
  118. if self.distribution.scripts:
  119. # XXX should also check for entry point scripts!
  120. log.warn("Note: you must uninstall or replace scripts manually!")
  121. def install_egg_scripts(self, dist):
  122. if dist is not self.dist:
  123. # Installing a dependency, so fall back to normal behavior
  124. return easy_install.install_egg_scripts(self, dist)
  125. # create wrapper scripts in the script dir, pointing to dist.scripts
  126. # new-style...
  127. self.install_wrapper_scripts(dist)
  128. # ...and old-style
  129. for script_name in self.distribution.scripts or []:
  130. script_path = os.path.abspath(convert_path(script_name))
  131. script_name = os.path.basename(script_path)
  132. script_text = _read_utf8_with_fallback(script_path)
  133. self.install_script(dist, script_name, script_text, script_path)
  134. return None
  135. def install_wrapper_scripts(self, dist):
  136. dist = VersionlessRequirement(dist)
  137. return easy_install.install_wrapper_scripts(self, dist)
  138. class VersionlessRequirement:
  139. """
  140. Adapt a pkg_resources.Distribution to simply return the project
  141. name as the 'requirement' so that scripts will work across
  142. multiple versions.
  143. >>> from pkg_resources import Distribution
  144. >>> dist = Distribution(project_name='foo', version='1.0')
  145. >>> str(dist.as_requirement())
  146. 'foo==1.0'
  147. >>> adapted_dist = VersionlessRequirement(dist)
  148. >>> str(adapted_dist.as_requirement())
  149. 'foo'
  150. """
  151. def __init__(self, dist) -> None:
  152. self.__dist = dist
  153. def __getattr__(self, name: str):
  154. return getattr(self.__dist, name)
  155. def as_requirement(self):
  156. return self.project_name