develop.py 8.0 KB

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