develop.py 8.0 KB

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