develop.py 7.2 KB

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