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