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