OLD | NEW |
(Empty) | |
| 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 |
| 8 import six |
| 9 |
| 10 from pkg_resources import Distribution, PathMetadata, normalize_path |
| 11 from setuptools.command.easy_install import easy_install |
| 12 from setuptools import namespaces |
| 13 import setuptools |
| 14 |
| 15 |
| 16 class 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(os.path.join(install_dir, egg_path, path_to_se
tup)) |
| 99 if resolved != normalize_path(os.curdir): |
| 100 raise DistutilsOptionError( |
| 101 "Can't get a consistent path to setup script from" |
| 102 " installation directory", resolved, normalize_path(os.curdir)) |
| 103 return path_to_setup |
| 104 |
| 105 def install_for_development(self): |
| 106 if six.PY3 and getattr(self.distribution, 'use_2to3', False): |
| 107 # If we run 2to3 we can not do this inplace: |
| 108 |
| 109 # Ensure metadata is up-to-date |
| 110 self.reinitialize_command('build_py', inplace=0) |
| 111 self.run_command('build_py') |
| 112 bpy_cmd = self.get_finalized_command("build_py") |
| 113 build_path = normalize_path(bpy_cmd.build_lib) |
| 114 |
| 115 # Build extensions |
| 116 self.reinitialize_command('egg_info', egg_base=build_path) |
| 117 self.run_command('egg_info') |
| 118 |
| 119 self.reinitialize_command('build_ext', inplace=0) |
| 120 self.run_command('build_ext') |
| 121 |
| 122 # Fixup egg-link and easy-install.pth |
| 123 ei_cmd = self.get_finalized_command("egg_info") |
| 124 self.egg_path = build_path |
| 125 self.dist.location = build_path |
| 126 # XXX |
| 127 self.dist._provider = PathMetadata(build_path, ei_cmd.egg_info) |
| 128 else: |
| 129 # Without 2to3 inplace works fine: |
| 130 self.run_command('egg_info') |
| 131 |
| 132 # Build extensions in-place |
| 133 self.reinitialize_command('build_ext', inplace=1) |
| 134 self.run_command('build_ext') |
| 135 |
| 136 self.install_site_py() # ensure that target dir is site-safe |
| 137 if setuptools.bootstrap_install_from: |
| 138 self.easy_install(setuptools.bootstrap_install_from) |
| 139 setuptools.bootstrap_install_from = None |
| 140 |
| 141 self.install_namespaces() |
| 142 |
| 143 # create an .egg-link in the installation dir, pointing to our egg |
| 144 log.info("Creating %s (link to %s)", self.egg_link, self.egg_base) |
| 145 if not self.dry_run: |
| 146 with open(self.egg_link, "w") as f: |
| 147 f.write(self.egg_path + "\n" + self.setup_path) |
| 148 # postprocess the installed distro, fixing up .pth, installing scripts, |
| 149 # and handling requirements |
| 150 self.process_distribution(None, self.dist, not self.no_deps) |
| 151 |
| 152 def uninstall_link(self): |
| 153 if os.path.exists(self.egg_link): |
| 154 log.info("Removing %s (link to %s)", self.egg_link, self.egg_base) |
| 155 egg_link_file = open(self.egg_link) |
| 156 contents = [line.rstrip() for line in egg_link_file] |
| 157 egg_link_file.close() |
| 158 if contents not in ([self.egg_path], |
| 159 [self.egg_path, self.setup_path]): |
| 160 log.warn("Link points to %s: uninstall aborted", contents) |
| 161 return |
| 162 if not self.dry_run: |
| 163 os.unlink(self.egg_link) |
| 164 if not self.dry_run: |
| 165 self.update_pth(self.dist) # remove any .pth link to us |
| 166 if self.distribution.scripts: |
| 167 # XXX should also check for entry point scripts! |
| 168 log.warn("Note: you must uninstall or replace scripts manually!") |
| 169 |
| 170 def install_egg_scripts(self, dist): |
| 171 if dist is not self.dist: |
| 172 # Installing a dependency, so fall back to normal behavior |
| 173 return easy_install.install_egg_scripts(self, dist) |
| 174 |
| 175 # create wrapper scripts in the script dir, pointing to dist.scripts |
| 176 |
| 177 # new-style... |
| 178 self.install_wrapper_scripts(dist) |
| 179 |
| 180 # ...and old-style |
| 181 for script_name in self.distribution.scripts or []: |
| 182 script_path = os.path.abspath(convert_path(script_name)) |
| 183 script_name = os.path.basename(script_path) |
| 184 with io.open(script_path) as strm: |
| 185 script_text = strm.read() |
| 186 self.install_script(dist, script_name, script_text, script_path) |
| 187 |
| 188 def install_wrapper_scripts(self, dist): |
| 189 dist = VersionlessRequirement(dist) |
| 190 return easy_install.install_wrapper_scripts(self, dist) |
| 191 |
| 192 |
| 193 class VersionlessRequirement(object): |
| 194 """ |
| 195 Adapt a pkg_resources.Distribution to simply return the project |
| 196 name as the 'requirement' so that scripts will work across |
| 197 multiple versions. |
| 198 |
| 199 >>> dist = Distribution(project_name='foo', version='1.0') |
| 200 >>> str(dist.as_requirement()) |
| 201 'foo==1.0' |
| 202 >>> adapted_dist = VersionlessRequirement(dist) |
| 203 >>> str(adapted_dist.as_requirement()) |
| 204 'foo' |
| 205 """ |
| 206 |
| 207 def __init__(self, dist): |
| 208 self.__dist = dist |
| 209 |
| 210 def __getattr__(self, name): |
| 211 return getattr(self.__dist, name) |
| 212 |
| 213 def as_requirement(self): |
| 214 return self.project_name |
OLD | NEW |