develop.py 6.6 KB

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