develop.py 6.8 KB

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