develop.py 6.6 KB

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