Path: blob/main/test/lib/python3.9/site-packages/setuptools/command/develop.py
4799 views
from distutils.util import convert_path1from distutils import log2from distutils.errors import DistutilsError, DistutilsOptionError3import os4import glob5import io67import pkg_resources8from setuptools.command.easy_install import easy_install9from setuptools import namespaces10import setuptools111213class develop(namespaces.DevelopInstaller, easy_install):14"""Set up package for development"""1516description = "install package in 'development mode'"1718user_options = easy_install.user_options + [19("uninstall", "u", "Uninstall this source package"),20("egg-path=", None, "Set the path to be used in the .egg-link file"),21]2223boolean_options = easy_install.boolean_options + ['uninstall']2425command_consumes_arguments = False # override base2627def run(self):28if self.uninstall:29self.multi_version = True30self.uninstall_link()31self.uninstall_namespaces()32else:33self.install_for_development()34self.warn_deprecated_options()3536def initialize_options(self):37self.uninstall = None38self.egg_path = None39easy_install.initialize_options(self)40self.setup_path = None41self.always_copy_from = '.' # always copy eggs installed in curdir4243def finalize_options(self):44ei = self.get_finalized_command("egg_info")45if ei.broken_egg_info:46template = "Please rename %r to %r before using 'develop'"47args = ei.egg_info, ei.broken_egg_info48raise DistutilsError(template % args)49self.args = [ei.egg_name]5051easy_install.finalize_options(self)52self.expand_basedirs()53self.expand_dirs()54# pick up setup-dir .egg files only: no .egg-info55self.package_index.scan(glob.glob('*.egg'))5657egg_link_fn = ei.egg_name + '.egg-link'58self.egg_link = os.path.join(self.install_dir, egg_link_fn)59self.egg_base = ei.egg_base60if self.egg_path is None:61self.egg_path = os.path.abspath(ei.egg_base)6263target = pkg_resources.normalize_path(self.egg_base)64egg_path = pkg_resources.normalize_path(65os.path.join(self.install_dir, self.egg_path)66)67if egg_path != target:68raise DistutilsOptionError(69"--egg-path must be a relative path from the install"70" directory to " + target71)7273# Make a distribution for the package's source74self.dist = pkg_resources.Distribution(75target,76pkg_resources.PathMetadata(target, os.path.abspath(ei.egg_info)),77project_name=ei.egg_name,78)7980self.setup_path = self._resolve_setup_path(81self.egg_base,82self.install_dir,83self.egg_path,84)8586@staticmethod87def _resolve_setup_path(egg_base, install_dir, egg_path):88"""89Generate a path from egg_base back to '.' where the90setup script resides and ensure that path points to the91setup path from $install_dir/$egg_path.92"""93path_to_setup = egg_base.replace(os.sep, '/').rstrip('/')94if path_to_setup != os.curdir:95path_to_setup = '../' * (path_to_setup.count('/') + 1)96resolved = pkg_resources.normalize_path(97os.path.join(install_dir, egg_path, path_to_setup)98)99if resolved != pkg_resources.normalize_path(os.curdir):100raise DistutilsOptionError(101"Can't get a consistent path to setup script from"102" installation directory",103resolved,104pkg_resources.normalize_path(os.curdir),105)106return path_to_setup107108def install_for_development(self):109self.run_command('egg_info')110111# Build extensions in-place112self.reinitialize_command('build_ext', inplace=1)113self.run_command('build_ext')114115if setuptools.bootstrap_install_from:116self.easy_install(setuptools.bootstrap_install_from)117setuptools.bootstrap_install_from = None118119self.install_namespaces()120121# create an .egg-link in the installation dir, pointing to our egg122log.info("Creating %s (link to %s)", self.egg_link, self.egg_base)123if not self.dry_run:124with open(self.egg_link, "w") as f:125f.write(self.egg_path + "\n" + self.setup_path)126# postprocess the installed distro, fixing up .pth, installing scripts,127# and handling requirements128self.process_distribution(None, self.dist, not self.no_deps)129130def uninstall_link(self):131if os.path.exists(self.egg_link):132log.info("Removing %s (link to %s)", self.egg_link, self.egg_base)133egg_link_file = open(self.egg_link)134contents = [line.rstrip() for line in egg_link_file]135egg_link_file.close()136if contents not in ([self.egg_path], [self.egg_path, self.setup_path]):137log.warn("Link points to %s: uninstall aborted", contents)138return139if not self.dry_run:140os.unlink(self.egg_link)141if not self.dry_run:142self.update_pth(self.dist) # remove any .pth link to us143if self.distribution.scripts:144# XXX should also check for entry point scripts!145log.warn("Note: you must uninstall or replace scripts manually!")146147def install_egg_scripts(self, dist):148if dist is not self.dist:149# Installing a dependency, so fall back to normal behavior150return easy_install.install_egg_scripts(self, dist)151152# create wrapper scripts in the script dir, pointing to dist.scripts153154# new-style...155self.install_wrapper_scripts(dist)156157# ...and old-style158for script_name in self.distribution.scripts or []:159script_path = os.path.abspath(convert_path(script_name))160script_name = os.path.basename(script_path)161with io.open(script_path) as strm:162script_text = strm.read()163self.install_script(dist, script_name, script_text, script_path)164165def install_wrapper_scripts(self, dist):166dist = VersionlessRequirement(dist)167return easy_install.install_wrapper_scripts(self, dist)168169170class VersionlessRequirement:171"""172Adapt a pkg_resources.Distribution to simply return the project173name as the 'requirement' so that scripts will work across174multiple versions.175176>>> from pkg_resources import Distribution177>>> dist = Distribution(project_name='foo', version='1.0')178>>> str(dist.as_requirement())179'foo==1.0'180>>> adapted_dist = VersionlessRequirement(dist)181>>> str(adapted_dist.as_requirement())182'foo'183"""184185def __init__(self, dist):186self.__dist = dist187188def __getattr__(self, name):189return getattr(self.__dist, name)190191def as_requirement(self):192return self.project_name193194195