diff --git a/newsfragments/917.feature.rst b/newsfragments/917.feature.rst new file mode 100644 index 0000000000..8b1ba1b794 --- /dev/null +++ b/newsfragments/917.feature.rst @@ -0,0 +1 @@ +Removed easy_install and package_index modules. diff --git a/setuptools/_scripts.py b/setuptools/_scripts.py new file mode 100644 index 0000000000..e3e8a191d4 --- /dev/null +++ b/setuptools/_scripts.py @@ -0,0 +1,353 @@ +from __future__ import annotations + +import os +import re +import shlex +import shutil +import struct +import subprocess +import sys +import textwrap +from collections.abc import Iterable +from typing import TYPE_CHECKING, TypedDict + +import pkg_resources + +if TYPE_CHECKING: + from typing_extensions import Self + +from .warnings import SetuptoolsWarning + +from distutils.command.build_scripts import first_line_re +from distutils.util import get_platform + + +class _SplitArgs(TypedDict, total=False): + comments: bool + posix: bool + + +class CommandSpec(list): + """ + A command spec for a #! header, specified as a list of arguments akin to + those passed to Popen. + """ + + options: list[str] = [] + split_args = _SplitArgs() + + @classmethod + def best(cls): + """ + Choose the best CommandSpec class based on environmental conditions. + """ + return cls + + @classmethod + def _sys_executable(cls): + _default = os.path.normpath(sys.executable) + return os.environ.get('__PYVENV_LAUNCHER__', _default) + + @classmethod + def from_param(cls, param: Self | str | Iterable[str] | None) -> Self: + """ + Construct a CommandSpec from a parameter to build_scripts, which may + be None. + """ + if isinstance(param, cls): + return param + if isinstance(param, str): + return cls.from_string(param) + if isinstance(param, Iterable): + return cls(param) + if param is None: + return cls.from_environment() + raise TypeError(f"Argument has an unsupported type {type(param)}") + + @classmethod + def from_environment(cls): + return cls([cls._sys_executable()]) + + @classmethod + def from_string(cls, string: str) -> Self: + """ + Construct a command spec from a simple string representing a command + line parseable by shlex.split. + """ + items = shlex.split(string, **cls.split_args) + return cls(items) + + def install_options(self, script_text: str): + self.options = shlex.split(self._extract_options(script_text)) + cmdline = subprocess.list2cmdline(self) + if not isascii(cmdline): + self.options[:0] = ['-x'] + + @staticmethod + def _extract_options(orig_script): + """ + Extract any options from the first line of the script. + """ + first = (orig_script + '\n').splitlines()[0] + match = _first_line_re().match(first) + options = match.group(1) or '' if match else '' + return options.strip() + + def as_header(self): + return self._render(self + list(self.options)) + + @staticmethod + def _strip_quotes(item): + _QUOTES = '"\'' + for q in _QUOTES: + if item.startswith(q) and item.endswith(q): + return item[1:-1] + return item + + @staticmethod + def _render(items): + cmdline = subprocess.list2cmdline( + CommandSpec._strip_quotes(item.strip()) for item in items + ) + return '#!' + cmdline + '\n' + + +class WindowsCommandSpec(CommandSpec): + split_args = _SplitArgs(posix=False) + + +class ScriptWriter: + """ + Encapsulates behavior around writing entry point scripts for console and + gui apps. + """ + + template = textwrap.dedent( + r""" + # EASY-INSTALL-ENTRY-SCRIPT: %(spec)r,%(group)r,%(name)r + import re + import sys + + # for compatibility with easy_install; see #2198 + __requires__ = %(spec)r + + try: + from importlib.metadata import distribution + except ImportError: + try: + from importlib_metadata import distribution + except ImportError: + from pkg_resources import load_entry_point + + + def importlib_load_entry_point(spec, group, name): + dist_name, _, _ = spec.partition('==') + matches = ( + entry_point + for entry_point in distribution(dist_name).entry_points + if entry_point.group == group and entry_point.name == name + ) + return next(matches).load() + + + globals().setdefault('load_entry_point', importlib_load_entry_point) + + + if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) + sys.exit(load_entry_point(%(spec)r, %(group)r, %(name)r)()) + """ + ).lstrip() + + command_spec_class = CommandSpec + + @classmethod + def get_args(cls, dist, header=None): + """ + Yield write_script() argument tuples for a distribution's + console_scripts and gui_scripts entry points. + """ + if header is None: + header = cls.get_header() + spec = str(dist.as_requirement()) + for type_ in 'console', 'gui': + group = type_ + '_scripts' + for name in dist.get_entry_map(group).keys(): + cls._ensure_safe_name(name) + script_text = cls.template % locals() + args = cls._get_script_args(type_, name, header, script_text) + yield from args + + @staticmethod + def _ensure_safe_name(name): + """ + Prevent paths in *_scripts entry point names. + """ + has_path_sep = re.search(r'[\\/]', name) + if has_path_sep: + raise ValueError("Path separators not allowed in script names") + + @classmethod + def best(cls): + """ + Select the best ScriptWriter for this environment. + """ + if sys.platform == 'win32' or (os.name == 'java' and os._name == 'nt'): + return WindowsScriptWriter.best() + else: + return cls + + @classmethod + def _get_script_args(cls, type_, name, header, script_text): + # Simply write the stub with no extension. + yield (name, header + script_text) + + @classmethod + def get_header( + cls, + script_text: str = "", + executable: str | CommandSpec | Iterable[str] | None = None, + ) -> str: + """Create a #! line, getting options (if any) from script_text""" + cmd = cls.command_spec_class.best().from_param(executable) + cmd.install_options(script_text) + return cmd.as_header() + + +class WindowsScriptWriter(ScriptWriter): + command_spec_class = WindowsCommandSpec + + @classmethod + def best(cls): + """ + Select the best ScriptWriter suitable for Windows + """ + writer_lookup = dict( + executable=WindowsExecutableLauncherWriter, + natural=cls, + ) + # for compatibility, use the executable launcher by default + launcher = os.environ.get('SETUPTOOLS_LAUNCHER', 'executable') + return writer_lookup[launcher] + + @classmethod + def _get_script_args(cls, type_, name, header, script_text): + "For Windows, add a .py extension" + ext = dict(console='.pya', gui='.pyw')[type_] + if ext not in os.environ['PATHEXT'].lower().split(';'): + msg = ( + "{ext} not listed in PATHEXT; scripts will not be " + "recognized as executables." + ).format(**locals()) + SetuptoolsWarning.emit(msg) + old = ['.pya', '.py', '-script.py', '.pyc', '.pyo', '.pyw', '.exe'] + old.remove(ext) + header = cls._adjust_header(type_, header) + blockers = [name + x for x in old] + yield name + ext, header + script_text, 't', blockers + + @classmethod + def _adjust_header(cls, type_, orig_header): + """ + Make sure 'pythonw' is used for gui and 'python' is used for + console (regardless of what sys.executable is). + """ + pattern = 'pythonw.exe' + repl = 'python.exe' + if type_ == 'gui': + pattern, repl = repl, pattern + pattern_ob = re.compile(re.escape(pattern), re.IGNORECASE) + new_header = pattern_ob.sub(string=orig_header, repl=repl) + return new_header if cls._use_header(new_header) else orig_header + + @staticmethod + def _use_header(new_header): + """ + Should _adjust_header use the replaced header? + + On non-windows systems, always use. On + Windows systems, only use the replaced header if it resolves + to an executable on the system. + """ + clean_header = new_header[2:-1].strip('"') + return sys.platform != 'win32' or shutil.which(clean_header) + + +class WindowsExecutableLauncherWriter(WindowsScriptWriter): + @classmethod + def _get_script_args(cls, type_, name, header, script_text): + """ + For Windows, add a .py extension and an .exe launcher + """ + if type_ == 'gui': + launcher_type = 'gui' + ext = '-script.pyw' + old = ['.pyw'] + else: + launcher_type = 'cli' + ext = '-script.py' + old = ['.py', '.pyc', '.pyo'] + hdr = cls._adjust_header(type_, header) + blockers = [name + x for x in old] + yield (name + ext, hdr + script_text, 't', blockers) + yield ( + name + '.exe', + get_win_launcher(launcher_type), + 'b', # write in binary mode + ) + if not is_64bit(): + # install a manifest for the launcher to prevent Windows + # from detecting it as an installer (which it will for + # launchers like easy_install.exe). Consider only + # adding a manifest for launchers detected as installers. + # See Distribute #143 for details. + m_name = name + '.exe.manifest' + yield (m_name, load_launcher_manifest(name), 't') + + +def get_win_launcher(type): + """ + Load the Windows launcher (executable) suitable for launching a script. + + `type` should be either 'cli' or 'gui' + + Returns the executable as a byte string. + """ + launcher_fn = f'{type}.exe' + if is_64bit(): + if get_platform() == "win-arm64": + launcher_fn = launcher_fn.replace(".", "-arm64.") + else: + launcher_fn = launcher_fn.replace(".", "-64.") + else: + launcher_fn = launcher_fn.replace(".", "-32.") + return pkg_resources.resource_string('setuptools', launcher_fn) + + +def load_launcher_manifest(name): + manifest = pkg_resources.resource_string(__name__, 'launcher manifest.xml') + return manifest.decode('utf-8') % vars() + + +def _first_line_re(): + """ + Return a regular expression based on first_line_re suitable for matching + strings. + """ + if isinstance(first_line_re.pattern, str): + return first_line_re + + # first_line_re in Python >=3.1.4 and >=3.2.1 is a bytes pattern. + return re.compile(first_line_re.pattern.decode()) + + +def is_64bit(): + return struct.calcsize("P") == 8 + + +def isascii(s): + try: + s.encode('ascii') + except UnicodeError: + return False + return True diff --git a/setuptools/_shutil.py b/setuptools/_shutil.py index 6acbb4281f..660459a110 100644 --- a/setuptools/_shutil.py +++ b/setuptools/_shutil.py @@ -51,3 +51,9 @@ def rmtree(path, ignore_errors=False, onexc=_auto_chmod): def rmdir(path, **opts): if os.path.isdir(path): rmtree(path, **opts) + + +def current_umask(): + tmp = os.umask(0o022) + os.umask(tmp) + return tmp diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index 5b96201dca..c4bc88d589 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -1,2323 +1,5 @@ -""" -Easy Install ------------- - -A tool for doing automatic download/extract/build of distutils-based Python -packages. For detailed documentation, see the accompanying EasyInstall.txt -file, or visit the `EasyInstall home page`__. - -__ https://setuptools.pypa.io/en/latest/deprecated/easy_install.html - -""" - -from __future__ import annotations - -import configparser -import contextlib -import io -import os -import random -import re -import shlex -import shutil -import site -import stat -import struct -import subprocess -import sys -import sysconfig -import tempfile -import textwrap -import warnings -import zipfile -import zipimport -from collections.abc import Iterable -from glob import glob -from sysconfig import get_path -from typing import TYPE_CHECKING, NoReturn, TypedDict - -from jaraco.text import yield_lines - -import pkg_resources -from pkg_resources import ( - DEVELOP_DIST, - Distribution, - DistributionNotFound, - EggMetadata, - Environment, - PathMetadata, - Requirement, - VersionConflict, - WorkingSet, - find_distributions, - get_distribution, - normalize_path, - resource_string, -) from setuptools import Command -from setuptools.archive_util import unpack_archive -from setuptools.command import bdist_egg, setopt -from setuptools.package_index import URL_SCHEME, PackageIndex, parse_requirement_arg -from setuptools.warnings import SetuptoolsDeprecationWarning, SetuptoolsWarning -from setuptools.wheel import Wheel - -from .._path import ensure_directory -from .._shutil import attempt_chmod_verbose as chmod, rmtree as _rmtree -from ..compat import py39, py312 - -from distutils import dir_util, log -from distutils.command import install -from distutils.command.build_scripts import first_line_re -from distutils.errors import ( - DistutilsArgError, - DistutilsError, - DistutilsOptionError, - DistutilsPlatformError, -) -from distutils.util import convert_path, get_platform, subst_vars - -if TYPE_CHECKING: - from typing_extensions import Self - -# Turn on PEP440Warnings -warnings.filterwarnings("default", category=pkg_resources.PEP440Warning) - -__all__ = [ - 'easy_install', - 'PthDistributions', - 'extract_wininst_cfg', - 'get_exe_prefixes', -] - - -def is_64bit(): - return struct.calcsize("P") == 8 - - -def _to_bytes(s): - return s.encode('utf8') - - -def isascii(s): - try: - s.encode('ascii') - except UnicodeError: - return False - return True - - -def _one_liner(text): - return textwrap.dedent(text).strip().replace('\n', '; ') class easy_install(Command): - """Manage a download/build/install process""" - - description = "Find/get/install Python packages" - command_consumes_arguments = True - - user_options = [ - ('prefix=', None, "installation prefix"), - ("zip-ok", "z", "install package as a zipfile"), - ("multi-version", "m", "make apps have to require() a version"), - ("upgrade", "U", "force upgrade (searches PyPI for latest versions)"), - ("install-dir=", "d", "install package to DIR"), - ("script-dir=", "s", "install scripts to DIR"), - ("exclude-scripts", "x", "Don't install scripts"), - ("always-copy", "a", "Copy all needed packages to install dir"), - ("index-url=", "i", "base URL of Python Package Index"), - ("find-links=", "f", "additional URL(s) to search for packages"), - ("build-directory=", "b", "download/extract/build in DIR; keep the results"), - ( - 'optimize=', - 'O', - 'also compile with optimization: -O1 for "python -O", ' - '-O2 for "python -OO", and -O0 to disable [default: -O0]', - ), - ('record=', None, "filename in which to record list of installed files"), - ('always-unzip', 'Z', "don't install as a zipfile, no matter what"), - ('site-dirs=', 'S', "list of directories where .pth files work"), - ('editable', 'e', "Install specified packages in editable form"), - ('no-deps', 'N', "don't install dependencies"), - ('allow-hosts=', 'H', "pattern(s) that hostnames must match"), - ('local-snapshots-ok', 'l', "allow building eggs from local checkouts"), - ('version', None, "print version information and exit"), - ( - 'no-find-links', - None, - "Don't load find-links defined in packages being installed", - ), - ('user', None, f"install in user site-package '{site.USER_SITE}'"), - ] - boolean_options = [ - 'zip-ok', - 'multi-version', - 'exclude-scripts', - 'upgrade', - 'always-copy', - 'editable', - 'no-deps', - 'local-snapshots-ok', - 'version', - 'user', - ] - - negative_opt = {'always-unzip': 'zip-ok'} - create_index = PackageIndex - - def initialize_options(self): - EasyInstallDeprecationWarning.emit() - - # the --user option seems to be an opt-in one, - # so the default should be False. - self.user = False - self.zip_ok = self.local_snapshots_ok = None - self.install_dir = self.script_dir = self.exclude_scripts = None - self.index_url = None - self.find_links = None - self.build_directory = None - self.args = None - self.optimize = self.record = None - self.upgrade = self.always_copy = self.multi_version = None - self.editable = self.no_deps = self.allow_hosts = None - self.root = self.prefix = self.no_report = None - self.version = None - self.install_purelib = None # for pure module distributions - self.install_platlib = None # non-pure (dists w/ extensions) - self.install_headers = None # for C/C++ headers - self.install_lib = None # set to either purelib or platlib - self.install_scripts = None - self.install_data = None - self.install_base = None - self.install_platbase = None - self.install_userbase = site.USER_BASE - self.install_usersite = site.USER_SITE - self.no_find_links = None - - # Options not specifiable via command line - self.package_index = None - self.pth_file = self.always_copy_from = None - self.site_dirs = None - self.installed_projects = {} - # Always read easy_install options, even if we are subclassed, or have - # an independent instance created. This ensures that defaults will - # always come from the standard configuration file(s)' "easy_install" - # section, even if this is a "develop" or "install" command, or some - # other embedding. - self._dry_run = None - self.verbose = self.distribution.verbose - self.distribution._set_command_options( - self, self.distribution.get_option_dict('easy_install') - ) - - def delete_blockers(self, blockers) -> None: - extant_blockers = ( - filename - for filename in blockers - if os.path.exists(filename) or os.path.islink(filename) - ) - list(map(self._delete_path, extant_blockers)) - - def _delete_path(self, path): - log.info("Deleting %s", path) - if self.dry_run: - return - - is_tree = os.path.isdir(path) and not os.path.islink(path) - remover = _rmtree if is_tree else os.unlink - remover(path) - - @staticmethod - def _render_version(): - """ - Render the Setuptools version and installation details, then exit. - """ - ver = f'{sys.version_info.major}.{sys.version_info.minor}' - dist = get_distribution('setuptools') - print(f'setuptools {dist.version} from {dist.location} (Python {ver})') - raise SystemExit - - def finalize_options(self) -> None: # noqa: C901 # is too complex (25) # FIXME - self.version and self._render_version() - - py_version = sys.version.split()[0] - - self.config_vars = dict(sysconfig.get_config_vars()) - - self.config_vars.update({ - 'dist_name': self.distribution.get_name(), - 'dist_version': self.distribution.get_version(), - 'dist_fullname': self.distribution.get_fullname(), - 'py_version': py_version, - 'py_version_short': f'{sys.version_info.major}.{sys.version_info.minor}', - 'py_version_nodot': f'{sys.version_info.major}{sys.version_info.minor}', - 'sys_prefix': self.config_vars['prefix'], - 'sys_exec_prefix': self.config_vars['exec_prefix'], - # Only POSIX systems have abiflags - 'abiflags': getattr(sys, 'abiflags', ''), - # Only python 3.9+ has platlibdir - 'platlibdir': getattr(sys, 'platlibdir', 'lib'), - }) - with contextlib.suppress(AttributeError): - # only for distutils outside stdlib - self.config_vars.update({ - 'implementation_lower': install._get_implementation().lower(), - 'implementation': install._get_implementation(), - }) - - # pypa/distutils#113 Python 3.9 compat - self.config_vars.setdefault( - 'py_version_nodot_plat', - getattr(sys, 'windir', '').replace('.', ''), - ) - - self.config_vars['userbase'] = self.install_userbase - self.config_vars['usersite'] = self.install_usersite - if self.user and not site.ENABLE_USER_SITE: - log.warn("WARNING: The user site-packages directory is disabled.") - - self._fix_install_dir_for_user_site() - - self.expand_basedirs() - self.expand_dirs() - - self._expand( - 'install_dir', - 'script_dir', - 'build_directory', - 'site_dirs', - ) - # If a non-default installation directory was specified, default the - # script directory to match it. - if self.script_dir is None: - self.script_dir = self.install_dir - - if self.no_find_links is None: - self.no_find_links = False - - # Let install_dir get set by install_lib command, which in turn - # gets its info from the install command, and takes into account - # --prefix and --home and all that other crud. - self.set_undefined_options('install_lib', ('install_dir', 'install_dir')) - # Likewise, set default script_dir from 'install_scripts.install_dir' - self.set_undefined_options('install_scripts', ('install_dir', 'script_dir')) - - if self.user and self.install_purelib: - self.install_dir = self.install_purelib - self.script_dir = self.install_scripts - # default --record from the install command - self.set_undefined_options('install', ('record', 'record')) - self.all_site_dirs = get_site_dirs() - self.all_site_dirs.extend(self._process_site_dirs(self.site_dirs)) - - if not self.editable: - self.check_site_dir() - default_index = os.getenv("__EASYINSTALL_INDEX", "https://pypi.org/simple/") - # ^ Private API for testing purposes only - self.index_url = self.index_url or default_index - self.shadow_path = self.all_site_dirs[:] - for path_item in self.install_dir, normalize_path(self.script_dir): - if path_item not in self.shadow_path: - self.shadow_path.insert(0, path_item) - - if self.allow_hosts is not None: - hosts = [s.strip() for s in self.allow_hosts.split(',')] - else: - hosts = ['*'] - if self.package_index is None: - self.package_index = self.create_index( - self.index_url, - search_path=self.shadow_path, - hosts=hosts, - ) - self.local_index = Environment(self.shadow_path + sys.path) - - if self.find_links is not None: - if isinstance(self.find_links, str): - self.find_links = self.find_links.split() - else: - self.find_links = [] - if self.local_snapshots_ok: - self.package_index.scan_egg_links(self.shadow_path + sys.path) - if not self.no_find_links: - self.package_index.add_find_links(self.find_links) - self.set_undefined_options('install_lib', ('optimize', 'optimize')) - self.optimize = self._validate_optimize(self.optimize) - - if self.editable and not self.build_directory: - raise DistutilsArgError( - "Must specify a build directory (-b) when using --editable" - ) - if not self.args: - raise DistutilsArgError( - "No urls, filenames, or requirements specified (see --help)" - ) - - self.outputs: list[str] = [] - - @staticmethod - def _process_site_dirs(site_dirs): - if site_dirs is None: - return - - normpath = map(normalize_path, sys.path) - site_dirs = [os.path.expanduser(s.strip()) for s in site_dirs.split(',')] - for d in site_dirs: - if not os.path.isdir(d): - log.warn("%s (in --site-dirs) does not exist", d) - elif normalize_path(d) not in normpath: - raise DistutilsOptionError(d + " (in --site-dirs) is not on sys.path") - else: - yield normalize_path(d) - - @staticmethod - def _validate_optimize(value): - try: - value = int(value) - if value not in range(3): - raise ValueError - except ValueError as e: - raise DistutilsOptionError("--optimize must be 0, 1, or 2") from e - - return value - - def _fix_install_dir_for_user_site(self): - """ - Fix the install_dir if "--user" was used. - """ - if not self.user: - return - - self.create_home_path() - if self.install_userbase is None: - msg = "User base directory is not specified" - raise DistutilsPlatformError(msg) - self.install_base = self.install_platbase = self.install_userbase - scheme_name = f'{os.name}_user' - self.select_scheme(scheme_name) - - def _expand_attrs(self, attrs): - for attr in attrs: - val = getattr(self, attr) - if val is not None: - if os.name == 'posix' or os.name == 'nt': - val = os.path.expanduser(val) - val = subst_vars(val, self.config_vars) - setattr(self, attr, val) - - def expand_basedirs(self) -> None: - """Calls `os.path.expanduser` on install_base, install_platbase and - root.""" - self._expand_attrs(['install_base', 'install_platbase', 'root']) - - def expand_dirs(self) -> None: - """Calls `os.path.expanduser` on install dirs.""" - dirs = [ - 'install_purelib', - 'install_platlib', - 'install_lib', - 'install_headers', - 'install_scripts', - 'install_data', - ] - self._expand_attrs(dirs) - - def run(self, show_deprecation: bool = True) -> None: - raise RuntimeError("easy_install command is disabled") - - def pseudo_tempname(self): - """Return a pseudo-tempname base in the install directory. - This code is intentionally naive; if a malicious party can write to - the target directory you're already in deep doodoo. - """ - try: - pid = os.getpid() - except Exception: - pid = random.randint(0, sys.maxsize) - return os.path.join(self.install_dir, f"test-easy-install-{pid}") - - def warn_deprecated_options(self) -> None: - pass - - def check_site_dir(self) -> None: # is too complex (12) # FIXME - """Verify that self.install_dir is .pth-capable dir, if needed""" - - instdir = normalize_path(self.install_dir) - pth_file = os.path.join(instdir, 'easy-install.pth') - - if not os.path.exists(instdir): - try: - os.makedirs(instdir) - except OSError: - self.cant_write_to_target() - - # Is it a configured, PYTHONPATH, implicit, or explicit site dir? - is_site_dir = instdir in self.all_site_dirs - - if not is_site_dir and not self.multi_version: - # No? Then directly test whether it does .pth file processing - is_site_dir = self.check_pth_processing() - else: - # make sure we can write to target dir - testfile = self.pseudo_tempname() + '.write-test' - test_exists = os.path.exists(testfile) - try: - if test_exists: - os.unlink(testfile) - open(testfile, 'wb').close() - os.unlink(testfile) - except OSError: - self.cant_write_to_target() - - if not is_site_dir and not self.multi_version: - # Can't install non-multi to non-site dir with easy_install - pythonpath = os.environ.get('PYTHONPATH', '') - log.warn(self.__no_default_msg, self.install_dir, pythonpath) - - if is_site_dir: - if self.pth_file is None: - self.pth_file = PthDistributions(pth_file, self.all_site_dirs) - else: - self.pth_file = None - - if self.multi_version and not os.path.exists(pth_file): - self.pth_file = None # don't create a .pth file - self.install_dir = instdir - - __cant_write_msg = textwrap.dedent( - """ - can't create or remove files in install directory - - The following error occurred while trying to add or remove files in the - installation directory: - - %s - - The installation directory you specified (via --install-dir, --prefix, or - the distutils default setting) was: - - %s - """ - ).lstrip() - - __not_exists_id = textwrap.dedent( - """ - This directory does not currently exist. Please create it and try again, or - choose a different installation directory (using the -d or --install-dir - option). - """ - ).lstrip() - - __access_msg = textwrap.dedent( - """ - Perhaps your account does not have write access to this directory? If the - installation directory is a system-owned directory, you may need to sign in - as the administrator or "root" account. If you do not have administrative - access to this machine, you may wish to choose a different installation - directory, preferably one that is listed in your PYTHONPATH environment - variable. - - For information on other options, you may wish to consult the - documentation at: - - https://setuptools.pypa.io/en/latest/deprecated/easy_install.html - - Please make the appropriate changes for your system and try again. - """ - ).lstrip() - - def cant_write_to_target(self) -> NoReturn: - msg = self.__cant_write_msg % ( - sys.exc_info()[1], - self.install_dir, - ) - - if not os.path.exists(self.install_dir): - msg += '\n' + self.__not_exists_id - else: - msg += '\n' + self.__access_msg - raise DistutilsError(msg) - - def check_pth_processing(self): # noqa: C901 - """Empirically verify whether .pth files are supported in inst. dir""" - instdir = self.install_dir - log.info("Checking .pth file support in %s", instdir) - pth_file = self.pseudo_tempname() + ".pth" - ok_file = pth_file + '.ok' - ok_exists = os.path.exists(ok_file) - tmpl = ( - _one_liner( - """ - import os - f = open({ok_file!r}, 'w', encoding="utf-8") - f.write('OK') - f.close() - """ - ) - + '\n' - ) - try: - if ok_exists: - os.unlink(ok_file) - dirname = os.path.dirname(ok_file) - os.makedirs(dirname, exist_ok=True) - f = open(pth_file, 'w', encoding=py312.PTH_ENCODING) - # ^-- Python<3.13 require encoding="locale" instead of "utf-8", - # see python/cpython#77102. - except OSError: - self.cant_write_to_target() - else: - try: - f.write(tmpl.format(**locals())) - f.close() - f = None - executable = sys.executable - if os.name == 'nt': - dirname, basename = os.path.split(executable) - alt = os.path.join(dirname, 'pythonw.exe') - use_alt = basename.lower() == 'python.exe' and os.path.exists(alt) - if use_alt: - # use pythonw.exe to avoid opening a console window - executable = alt - - from distutils.spawn import spawn - - spawn([executable, '-E', '-c', 'pass'], 0) - - if os.path.exists(ok_file): - log.info("TEST PASSED: %s appears to support .pth files", instdir) - return True - finally: - if f: - f.close() - if os.path.exists(ok_file): - os.unlink(ok_file) - if os.path.exists(pth_file): - os.unlink(pth_file) - if not self.multi_version: - log.warn("TEST FAILED: %s does NOT support .pth files", instdir) - return False - - def install_egg_scripts(self, dist) -> None: - """Write all the scripts for `dist`, unless scripts are excluded""" - if not self.exclude_scripts and dist.metadata_isdir('scripts'): - for script_name in dist.metadata_listdir('scripts'): - if dist.metadata_isdir('scripts/' + script_name): - # The "script" is a directory, likely a Python 3 - # __pycache__ directory, so skip it. - continue - self.install_script( - dist, script_name, dist.get_metadata('scripts/' + script_name) - ) - self.install_wrapper_scripts(dist) - - def add_output(self, path) -> None: - if os.path.isdir(path): - for base, dirs, files in os.walk(path): - for filename in files: - self.outputs.append(os.path.join(base, filename)) - else: - self.outputs.append(path) - - def not_editable(self, spec) -> None: - if self.editable: - raise DistutilsArgError( - f"Invalid argument {spec!r}: you can't use filenames or URLs " - "with --editable (except via the --find-links option)." - ) - - def check_editable(self, spec) -> None: - if not self.editable: - return - - if os.path.exists(os.path.join(self.build_directory, spec.key)): - raise DistutilsArgError( - f"{spec.key!r} already exists in {self.build_directory}; can't do a checkout there" - ) - - @contextlib.contextmanager - def _tmpdir(self): - tmpdir = tempfile.mkdtemp(prefix="easy_install-") - try: - # cast to str as workaround for #709 and #710 and #712 - yield str(tmpdir) - finally: - os.path.exists(tmpdir) and _rmtree(tmpdir) - - def easy_install(self, spec, deps: bool = False) -> Distribution | None: - with self._tmpdir() as tmpdir: - if not isinstance(spec, Requirement): - if URL_SCHEME(spec): - # It's a url, download it to tmpdir and process - self.not_editable(spec) - dl = self.package_index.download(spec, tmpdir) - return self.install_item(None, dl, tmpdir, deps, True) - - elif os.path.exists(spec): - # Existing file or directory, just process it directly - self.not_editable(spec) - return self.install_item(None, spec, tmpdir, deps, True) - else: - spec = parse_requirement_arg(spec) - - self.check_editable(spec) - dist = self.package_index.fetch_distribution( - spec, - tmpdir, - self.upgrade, - self.editable, - not self.always_copy, - self.local_index, - ) - if dist is None: - msg = f"Could not find suitable distribution for {spec!r}" - if self.always_copy: - msg += " (--always-copy skips system and development eggs)" - raise DistutilsError(msg) - elif dist.precedence == DEVELOP_DIST: - # .egg-info dists don't need installing, just process deps - self.process_distribution(spec, dist, deps, "Using") - return dist - else: - return self.install_item(spec, dist.location, tmpdir, deps) - - def install_item( - self, spec, download, tmpdir, deps, install_needed: bool = False - ) -> Distribution | None: - # Installation is also needed if file in tmpdir or is not an egg - install_needed = install_needed or bool(self.always_copy) - install_needed = install_needed or os.path.dirname(download) == tmpdir - install_needed = install_needed or not download.endswith('.egg') - install_needed = install_needed or ( - self.always_copy_from is not None - and os.path.dirname(normalize_path(download)) - == normalize_path(self.always_copy_from) - ) - - if spec and not install_needed: - # at this point, we know it's a local .egg, we just don't know if - # it's already installed. - for dist in self.local_index[spec.project_name]: - if dist.location == download: - break - else: - install_needed = True # it's not in the local index - - log.info("Processing %s", os.path.basename(download)) - - if install_needed: - dists = self.install_eggs(spec, download, tmpdir) - for dist in dists: - self.process_distribution(spec, dist, deps) - else: - dists = [self.egg_distribution(download)] - self.process_distribution(spec, dists[0], deps, "Using") - - if spec is not None: - for dist in dists: - if dist in spec: - return dist - return None - - def select_scheme(self, name): - try: - install._select_scheme(self, name) - except AttributeError: - # stdlib distutils - install.install.select_scheme(self, name.replace('posix', 'unix')) - - # FIXME: 'easy_install.process_distribution' is too complex (12) - def process_distribution( # noqa: C901 - self, - requirement, - dist, - deps: bool = True, - *info, - ) -> None: - self.update_pth(dist) - self.package_index.add(dist) - if dist in self.local_index[dist.key]: - self.local_index.remove(dist) - self.local_index.add(dist) - self.install_egg_scripts(dist) - self.installed_projects[dist.key] = dist - log.info(self.installation_report(requirement, dist, *info)) - if dist.has_metadata('dependency_links.txt') and not self.no_find_links: - self.package_index.add_find_links( - dist.get_metadata_lines('dependency_links.txt') - ) - if not deps and not self.always_copy: - return - elif requirement is not None and dist.key != requirement.key: - log.warn("Skipping dependencies for %s", dist) - return # XXX this is not the distribution we were looking for - elif requirement is None or dist not in requirement: - # if we wound up with a different version, resolve what we've got - distreq = dist.as_requirement() - requirement = Requirement(str(distreq)) - log.info("Processing dependencies for %s", requirement) - try: - distros = WorkingSet([]).resolve( - [requirement], self.local_index, self.easy_install - ) - except DistributionNotFound as e: - raise DistutilsError(str(e)) from e - except VersionConflict as e: - raise DistutilsError(e.report()) from e - if self.always_copy or self.always_copy_from: - # Force all the relevant distros to be copied or activated - for dist in distros: - if dist.key not in self.installed_projects: - self.easy_install(dist.as_requirement()) - log.info("Finished processing dependencies for %s", requirement) - - def should_unzip(self, dist) -> bool: - if self.zip_ok is not None: - return not self.zip_ok - if dist.has_metadata('not-zip-safe'): - return True - if not dist.has_metadata('zip-safe'): - return True - return False - - def maybe_move(self, spec, dist_filename, setup_base): - dst = os.path.join(self.build_directory, spec.key) - if os.path.exists(dst): - msg = "%r already exists in %s; build directory %s will not be kept" - log.warn(msg, spec.key, self.build_directory, setup_base) - return setup_base - if os.path.isdir(dist_filename): - setup_base = dist_filename - else: - if os.path.dirname(dist_filename) == setup_base: - os.unlink(dist_filename) # get it out of the tmp dir - contents = os.listdir(setup_base) - if len(contents) == 1: - dist_filename = os.path.join(setup_base, contents[0]) - if os.path.isdir(dist_filename): - # if the only thing there is a directory, move it instead - setup_base = dist_filename - ensure_directory(dst) - shutil.move(setup_base, dst) - return dst - - def install_wrapper_scripts(self, dist) -> None: - if self.exclude_scripts: - return - for args in ScriptWriter.best().get_args(dist): - self.write_script(*args) - - def install_script(self, dist, script_name, script_text, dev_path=None) -> None: - """Generate a legacy script wrapper and install it""" - spec = str(dist.as_requirement()) - is_script = is_python_script(script_text, script_name) - - if is_script: - body = self._load_template(dev_path) % locals() - script_text = ScriptWriter.get_header(script_text) + body - self.write_script(script_name, _to_bytes(script_text), 'b') - - @staticmethod - def _load_template(dev_path): - """ - There are a couple of template scripts in the package. This - function loads one of them and prepares it for use. - """ - # See https://github.com/pypa/setuptools/issues/134 for info - # on script file naming and downstream issues with SVR4 - name = 'script.tmpl' - if dev_path: - name = name.replace('.tmpl', ' (dev).tmpl') - - raw_bytes = resource_string('setuptools', name) - return raw_bytes.decode('utf-8') - - def write_script(self, script_name, contents, mode: str = "t", blockers=()) -> None: - """Write an executable file to the scripts directory""" - self.delete_blockers( # clean up old .py/.pyw w/o a script - [os.path.join(self.script_dir, x) for x in blockers] - ) - log.info("Installing %s script to %s", script_name, self.script_dir) - target = os.path.join(self.script_dir, script_name) - self.add_output(target) - - if self.dry_run: - return - - mask = current_umask() - ensure_directory(target) - if os.path.exists(target): - os.unlink(target) - - encoding = None if "b" in mode else "utf-8" - with open(target, "w" + mode, encoding=encoding) as f: - f.write(contents) - chmod(target, 0o777 - mask) - - def install_eggs(self, spec, dist_filename, tmpdir) -> list[Distribution]: - # .egg dirs or files are already built, so just return them - installer_map = { - '.egg': self.install_egg, - '.exe': self.install_exe, - '.whl': self.install_wheel, - } - try: - install_dist = installer_map[dist_filename.lower()[-4:]] - except KeyError: - pass - else: - return [install_dist(dist_filename, tmpdir)] - - # Anything else, try to extract and build - setup_base = tmpdir - if os.path.isfile(dist_filename) and not dist_filename.endswith('.py'): - unpack_archive(dist_filename, tmpdir, self.unpack_progress) - elif os.path.isdir(dist_filename): - setup_base = os.path.abspath(dist_filename) - - if ( - setup_base.startswith(tmpdir) # something we downloaded - and self.build_directory - and spec is not None - ): - setup_base = self.maybe_move(spec, dist_filename, setup_base) - - # Find the setup.py file - setup_script = os.path.join(setup_base, 'setup.py') - - if not os.path.exists(setup_script): - setups = glob(os.path.join(setup_base, '*', 'setup.py')) - if not setups: - raise DistutilsError( - f"Couldn't find a setup script in {os.path.abspath(dist_filename)}" - ) - if len(setups) > 1: - raise DistutilsError( - f"Multiple setup scripts in {os.path.abspath(dist_filename)}" - ) - setup_script = setups[0] - - # Now run it, and return the result - if self.editable: - log.info(self.report_editable(spec, setup_script)) - return [] - else: - return self.build_and_install(setup_script, setup_base) - - def egg_distribution(self, egg_path): - if os.path.isdir(egg_path): - metadata = PathMetadata(egg_path, os.path.join(egg_path, 'EGG-INFO')) - else: - metadata = EggMetadata(zipimport.zipimporter(egg_path)) - return Distribution.from_filename(egg_path, metadata=metadata) - - # FIXME: 'easy_install.install_egg' is too complex (11) - def install_egg(self, egg_path, tmpdir): - destination = os.path.join( - self.install_dir, - os.path.basename(egg_path), - ) - destination = os.path.abspath(destination) - if not self.dry_run: - ensure_directory(destination) - - dist = self.egg_distribution(egg_path) - if not ( - os.path.exists(destination) and os.path.samefile(egg_path, destination) - ): - if os.path.isdir(destination) and not os.path.islink(destination): - dir_util.remove_tree(destination, dry_run=self.dry_run) - elif os.path.exists(destination): - self.execute( - os.unlink, - (destination,), - "Removing " + destination, - ) - try: - new_dist_is_zipped = False - if os.path.isdir(egg_path): - if egg_path.startswith(tmpdir): - f, m = shutil.move, "Moving" - else: - f, m = shutil.copytree, "Copying" - elif self.should_unzip(dist): - self.mkpath(destination) - f, m = self.unpack_and_compile, "Extracting" - else: - new_dist_is_zipped = True - if egg_path.startswith(tmpdir): - f, m = shutil.move, "Moving" - else: - f, m = shutil.copy2, "Copying" - self.execute( - f, - (egg_path, destination), - (m + " %s to %s") - % (os.path.basename(egg_path), os.path.dirname(destination)), - ) - update_dist_caches( - destination, - fix_zipimporter_caches=new_dist_is_zipped, - ) - except Exception: - update_dist_caches(destination, fix_zipimporter_caches=False) - raise - - self.add_output(destination) - return self.egg_distribution(destination) - - def install_exe(self, dist_filename, tmpdir): - # See if it's valid, get data - cfg = extract_wininst_cfg(dist_filename) - if cfg is None: - raise DistutilsError( - f"{dist_filename} is not a valid distutils Windows .exe" - ) - # Create a dummy distribution object until we build the real distro - dist = Distribution( - None, - project_name=cfg.get('metadata', 'name'), - version=cfg.get('metadata', 'version'), - platform=get_platform(), - ) - - # Convert the .exe to an unpacked egg - egg_path = os.path.join(tmpdir, dist.egg_name() + '.egg') - dist.location = egg_path - egg_tmp = egg_path + '.tmp' - _egg_info = os.path.join(egg_tmp, 'EGG-INFO') - pkg_inf = os.path.join(_egg_info, 'PKG-INFO') - ensure_directory(pkg_inf) # make sure EGG-INFO dir exists - dist._provider = PathMetadata(egg_tmp, _egg_info) # XXX - self.exe_to_egg(dist_filename, egg_tmp) - - # Write EGG-INFO/PKG-INFO - if not os.path.exists(pkg_inf): - with open(pkg_inf, 'w', encoding="utf-8") as f: - f.write('Metadata-Version: 1.0\n') - for k, v in cfg.items('metadata'): - if k != 'target_version': - k = k.replace('_', '-').title() - f.write(f'{k}: {v}\n') - script_dir = os.path.join(_egg_info, 'scripts') - # delete entry-point scripts to avoid duping - self.delete_blockers([ - os.path.join(script_dir, args[0]) for args in ScriptWriter.get_args(dist) - ]) - # Build .egg file from tmpdir - bdist_egg.make_zipfile( - egg_path, - egg_tmp, - verbose=self.verbose, - dry_run=self.dry_run, - ) - # install the .egg - return self.install_egg(egg_path, tmpdir) - - # FIXME: 'easy_install.exe_to_egg' is too complex (12) - def exe_to_egg(self, dist_filename, egg_tmp) -> None: # noqa: C901 - """Extract a bdist_wininst to the directories an egg would use""" - # Check for .pth file and set up prefix translations - prefixes = get_exe_prefixes(dist_filename) - to_compile = [] - native_libs = [] - top_level = set() - - def process(src, dst): - s = src.lower() - for old, new in prefixes: - if s.startswith(old): - src = new + src[len(old) :] - parts = src.split('/') - dst = os.path.join(egg_tmp, *parts) - dl = dst.lower() - if dl.endswith('.pyd') or dl.endswith('.dll'): - parts[-1] = bdist_egg.strip_module(parts[-1]) - top_level.add([os.path.splitext(parts[0])[0]]) - native_libs.append(src) - elif dl.endswith('.py') and old != 'SCRIPTS/': - top_level.add([os.path.splitext(parts[0])[0]]) - to_compile.append(dst) - return dst - if not src.endswith('.pth'): - log.warn("WARNING: can't process %s", src) - return None - - # extract, tracking .pyd/.dll->native_libs and .py -> to_compile - unpack_archive(dist_filename, egg_tmp, process) - stubs = [] - for res in native_libs: - if res.lower().endswith('.pyd'): # create stubs for .pyd's - parts = res.split('/') - resource = parts[-1] - parts[-1] = bdist_egg.strip_module(parts[-1]) + '.py' - pyfile = os.path.join(egg_tmp, *parts) - to_compile.append(pyfile) - stubs.append(pyfile) - bdist_egg.write_stub(resource, pyfile) - self.byte_compile(to_compile) # compile .py's - bdist_egg.write_safety_flag( - os.path.join(egg_tmp, 'EGG-INFO'), bdist_egg.analyze_egg(egg_tmp, stubs) - ) # write zip-safety flag - - for name in 'top_level', 'native_libs': - if locals()[name]: - txt = os.path.join(egg_tmp, 'EGG-INFO', name + '.txt') - if not os.path.exists(txt): - with open(txt, 'w', encoding="utf-8") as f: - f.write('\n'.join(locals()[name]) + '\n') - - def install_wheel(self, wheel_path, tmpdir): - wheel = Wheel(wheel_path) - assert wheel.is_compatible() - destination = os.path.join(self.install_dir, wheel.egg_name()) - destination = os.path.abspath(destination) - if not self.dry_run: - ensure_directory(destination) - if os.path.isdir(destination) and not os.path.islink(destination): - dir_util.remove_tree(destination, dry_run=self.dry_run) - elif os.path.exists(destination): - self.execute( - os.unlink, - (destination,), - "Removing " + destination, - ) - try: - self.execute( - wheel.install_as_egg, - (destination,), - ( - f"Installing {os.path.basename(wheel_path)} to {os.path.dirname(destination)}" - ), - ) - finally: - update_dist_caches(destination, fix_zipimporter_caches=False) - self.add_output(destination) - return self.egg_distribution(destination) - - __mv_warning = textwrap.dedent( - """ - Because this distribution was installed --multi-version, before you can - import modules from this package in an application, you will need to - 'import pkg_resources' and then use a 'require()' call similar to one of - these examples, in order to select the desired version: - - pkg_resources.require("%(name)s") # latest installed version - pkg_resources.require("%(name)s==%(version)s") # this exact version - pkg_resources.require("%(name)s>=%(version)s") # this version or higher - """ - ).lstrip() - - __id_warning = textwrap.dedent( - """ - Note also that the installation directory must be on sys.path at runtime for - this to work. (e.g. by being the application's script directory, by being on - PYTHONPATH, or by being added to sys.path by your code.) - """ - ) - - def installation_report(self, req, dist, what: str = "Installed") -> str: - """Helpful installation message for display to package users""" - msg = "\n%(what)s %(eggloc)s%(extras)s" - if self.multi_version and not self.no_report: - msg += '\n' + self.__mv_warning - if self.install_dir not in map(normalize_path, sys.path): - msg += '\n' + self.__id_warning - - eggloc = dist.location - name = dist.project_name - version = dist.version - extras = '' # TODO: self.report_extras(req, dist) - return msg % locals() - - __editable_msg = textwrap.dedent( - """ - Extracted editable version of %(spec)s to %(dirname)s - - If it uses setuptools in its setup script, you can activate it in - "development" mode by going to that directory and running:: - - %(python)s setup.py develop - - See the setuptools documentation for the "develop" command for more info. - """ - ).lstrip() - - def report_editable(self, spec, setup_script): - dirname = os.path.dirname(setup_script) - python = sys.executable - return '\n' + self.__editable_msg % locals() - - def run_setup(self, setup_script, setup_base, args) -> NoReturn: - raise NotImplementedError("easy_install support has been removed") - - def build_and_install(self, setup_script, setup_base): - args = ['bdist_egg', '--dist-dir'] - - dist_dir = tempfile.mkdtemp( - prefix='egg-dist-tmp-', dir=os.path.dirname(setup_script) - ) - try: - self._set_fetcher_options(os.path.dirname(setup_script)) - args.append(dist_dir) - - self.run_setup(setup_script, setup_base, args) - all_eggs = Environment([dist_dir]) - eggs = [ - self.install_egg(dist.location, setup_base) - for key in all_eggs - for dist in all_eggs[key] - ] - if not eggs and not self.dry_run: - log.warn("No eggs found in %s (setup script problem?)", dist_dir) - return eggs - finally: - _rmtree(dist_dir) - log.set_verbosity(self.verbose) # restore our log verbosity - - def _set_fetcher_options(self, base): - """ - When easy_install is about to run bdist_egg on a source dist, that - source dist might have 'setup_requires' directives, requiring - additional fetching. Ensure the fetcher options given to easy_install - are available to that command as well. - """ - # find the fetch options from easy_install and write them out - # to the setup.cfg file. - ei_opts = self.distribution.get_option_dict('easy_install').copy() - fetch_directives = ( - 'find_links', - 'site_dirs', - 'index_url', - 'optimize', - 'allow_hosts', - ) - fetch_options = {} - for key, val in ei_opts.items(): - if key not in fetch_directives: - continue - fetch_options[key] = val[1] - # create a settings dictionary suitable for `edit_config` - settings = dict(easy_install=fetch_options) - cfg_filename = os.path.join(base, 'setup.cfg') - setopt.edit_config(cfg_filename, settings) - - def update_pth(self, dist) -> None: # noqa: C901 # is too complex (11) # FIXME - if self.pth_file is None: - return - - for d in self.pth_file[dist.key]: # drop old entries - if not self.multi_version and d.location == dist.location: - continue - - log.info("Removing %s from easy-install.pth file", d) - self.pth_file.remove(d) - if d.location in self.shadow_path: - self.shadow_path.remove(d.location) - - if not self.multi_version: - if dist.location in self.pth_file.paths: - log.info( - "%s is already the active version in easy-install.pth", - dist, - ) - else: - log.info("Adding %s to easy-install.pth file", dist) - self.pth_file.add(dist) # add new entry - if dist.location not in self.shadow_path: - self.shadow_path.append(dist.location) - - if self.dry_run: - return - - self.pth_file.save() - - if dist.key != 'setuptools': - return - - # Ensure that setuptools itself never becomes unavailable! - # XXX should this check for latest version? - filename = os.path.join(self.install_dir, 'setuptools.pth') - if os.path.islink(filename): - os.unlink(filename) - - with open(filename, 'wt', encoding=py312.PTH_ENCODING) as f: - # ^-- Python<3.13 require encoding="locale" instead of "utf-8", - # see python/cpython#77102. - f.write(self.pth_file.make_relative(dist.location) + '\n') - - def unpack_progress(self, src, dst): - # Progress filter for unpacking - log.debug("Unpacking %s to %s", src, dst) - return dst # only unpack-and-compile skips files for dry run - - def unpack_and_compile(self, egg_path, destination) -> None: - to_compile = [] - to_chmod = [] - - def pf(src, dst): - if dst.endswith('.py') and not src.startswith('EGG-INFO/'): - to_compile.append(dst) - elif dst.endswith('.dll') or dst.endswith('.so'): - to_chmod.append(dst) - self.unpack_progress(src, dst) - return not self.dry_run and dst or None - - unpack_archive(egg_path, destination, pf) - self.byte_compile(to_compile) - if not self.dry_run: - for f in to_chmod: - mode = ((os.stat(f)[stat.ST_MODE]) | 0o555) & 0o7755 - chmod(f, mode) - - def byte_compile(self, to_compile) -> None: - if sys.dont_write_bytecode: - return - - from distutils.util import byte_compile - - try: - # try to make the byte compile messages quieter - log.set_verbosity(self.verbose - 1) - - byte_compile(to_compile, optimize=0, force=True, dry_run=self.dry_run) - if self.optimize: - byte_compile( - to_compile, - optimize=self.optimize, - force=True, - dry_run=self.dry_run, - ) - finally: - log.set_verbosity(self.verbose) # restore original verbosity - - __no_default_msg = textwrap.dedent( - """ - bad install directory or PYTHONPATH - - You are attempting to install a package to a directory that is not - on PYTHONPATH and which Python does not read ".pth" files from. The - installation directory you specified (via --install-dir, --prefix, or - the distutils default setting) was: - - %s - - and your PYTHONPATH environment variable currently contains: - - %r - - Here are some of your options for correcting the problem: - - * You can choose a different installation directory, i.e., one that is - on PYTHONPATH or supports .pth files - - * You can add the installation directory to the PYTHONPATH environment - variable. (It must then also be on PYTHONPATH whenever you run - Python and want to use the package(s) you are installing.) - - * You can set up the installation directory to support ".pth" files by - using one of the approaches described here: - - https://setuptools.pypa.io/en/latest/deprecated/easy_install.html#custom-installation-locations - - - Please make the appropriate changes for your system and try again. - """ - ).strip() - - def create_home_path(self) -> None: - """Create directories under ~.""" - if not self.user: - return - home = convert_path(os.path.expanduser("~")) - for path in only_strs(self.config_vars.values()): - if path.startswith(home) and not os.path.isdir(path): - self.debug_print(f"os.makedirs('{path}', 0o700)") - os.makedirs(path, 0o700) - - INSTALL_SCHEMES = dict( - posix=dict( - install_dir='$base/lib/python$py_version_short/site-packages', - script_dir='$base/bin', - ), - ) - - DEFAULT_SCHEME = dict( - install_dir='$base/Lib/site-packages', - script_dir='$base/Scripts', - ) - - def _expand(self, *attrs): - config_vars = self.get_finalized_command('install').config_vars - - if self.prefix: - # Set default install_dir/scripts from --prefix - config_vars = dict(config_vars) - config_vars['base'] = self.prefix - scheme = self.INSTALL_SCHEMES.get(os.name, self.DEFAULT_SCHEME) - for attr, val in scheme.items(): - if getattr(self, attr, None) is None: - setattr(self, attr, val) - - from distutils.util import subst_vars - - for attr in attrs: - val = getattr(self, attr) - if val is not None: - val = subst_vars(val, config_vars) - if os.name == 'posix': - val = os.path.expanduser(val) - setattr(self, attr, val) - - -def _pythonpath(): - items = os.environ.get('PYTHONPATH', '').split(os.pathsep) - return filter(None, items) - - -def get_site_dirs(): - """ - Return a list of 'site' dirs - """ - - sitedirs = [] - - # start with PYTHONPATH - sitedirs.extend(_pythonpath()) - - prefixes = [sys.prefix] - if sys.exec_prefix != sys.prefix: - prefixes.append(sys.exec_prefix) - for prefix in prefixes: - if not prefix: - continue - - if sys.platform in ('os2emx', 'riscos'): - sitedirs.append(os.path.join(prefix, "Lib", "site-packages")) - elif os.sep == '/': - sitedirs.extend([ - os.path.join( - prefix, - "lib", - f"python{sys.version_info.major}.{sys.version_info.minor}", - "site-packages", - ), - os.path.join(prefix, "lib", "site-python"), - ]) - else: - sitedirs.extend([ - prefix, - os.path.join(prefix, "lib", "site-packages"), - ]) - if sys.platform != 'darwin': - continue - - # for framework builds *only* we add the standard Apple - # locations. Currently only per-user, but /Library and - # /Network/Library could be added too - if 'Python.framework' not in prefix: - continue - - home = os.environ.get('HOME') - if not home: - continue - - home_sp = os.path.join( - home, - 'Library', - 'Python', - f'{sys.version_info.major}.{sys.version_info.minor}', - 'site-packages', - ) - sitedirs.append(home_sp) - lib_paths = get_path('purelib'), get_path('platlib') - - sitedirs.extend(s for s in lib_paths if s not in sitedirs) - - if site.ENABLE_USER_SITE: - sitedirs.append(site.USER_SITE) - - with contextlib.suppress(AttributeError): - sitedirs.extend(site.getsitepackages()) - - return list(map(normalize_path, sitedirs)) - - -def expand_paths(inputs): # noqa: C901 # is too complex (11) # FIXME - """Yield sys.path directories that might contain "old-style" packages""" - - seen = set() - - for dirname in inputs: - dirname = normalize_path(dirname) - if dirname in seen: - continue - - seen.add(dirname) - if not os.path.isdir(dirname): - continue - - files = os.listdir(dirname) - yield dirname, files - - for name in files: - if not name.endswith('.pth'): - # We only care about the .pth files - continue - if name in ('easy-install.pth', 'setuptools.pth'): - # Ignore .pth files that we control - continue - - # Read the .pth file - content = _read_pth(os.path.join(dirname, name)) - lines = list(yield_lines(content)) - - # Yield existing non-dupe, non-import directory lines from it - for line in lines: - if line.startswith("import"): - continue - - line = normalize_path(line.rstrip()) - if line in seen: - continue - - seen.add(line) - if not os.path.isdir(line): - continue - - yield line, os.listdir(line) - - -def extract_wininst_cfg(dist_filename): - """Extract configuration data from a bdist_wininst .exe - - Returns a configparser.RawConfigParser, or None - """ - f = open(dist_filename, 'rb') - try: - endrec = zipfile._EndRecData(f) - if endrec is None: - return None - - prepended = (endrec[9] - endrec[5]) - endrec[6] - if prepended < 12: # no wininst data here - return None - f.seek(prepended - 12) - - tag, cfglen, _bmlen = struct.unpack("egg path translations for a given .exe file""" - - prefixes = [ - ('PURELIB/', ''), - ('PLATLIB/pywin32_system32', ''), - ('PLATLIB/', ''), - ('SCRIPTS/', 'EGG-INFO/scripts/'), - ('DATA/lib/site-packages', ''), - ] - z = zipfile.ZipFile(exe_filename) - try: - for info in z.infolist(): - name = info.filename - parts = name.split('/') - if len(parts) == 3 and parts[2] == 'PKG-INFO': - if parts[1].endswith('.egg-info'): - prefixes.insert(0, ('/'.join(parts[:2]), 'EGG-INFO/')) - break - if len(parts) != 2 or not name.endswith('.pth'): - continue - if name.endswith('-nspkg.pth'): - continue - if parts[0].upper() in ('PURELIB', 'PLATLIB'): - contents = z.read(name).decode() - for pth in yield_lines(contents): - pth = pth.strip().replace('\\', '/') - if not pth.startswith('import'): - prefixes.append(((f'{parts[0]}/{pth}/'), '')) - finally: - z.close() - prefixes = [(x.lower(), y) for x, y in prefixes] - prefixes.sort() - prefixes.reverse() - return prefixes - - -class PthDistributions(Environment): - """A .pth file with Distribution paths in it""" - - def __init__(self, filename, sitedirs=()) -> None: - self.filename = filename - self.sitedirs = list(map(normalize_path, sitedirs)) - self.basedir = normalize_path(os.path.dirname(self.filename)) - self.paths, self.dirty = self._load() - # keep a copy if someone manually updates the paths attribute on the instance - self._init_paths = self.paths[:] - super().__init__([], None, None) - for path in yield_lines(self.paths): - list(map(self.add, find_distributions(path, True))) - - def _load_raw(self): - paths = [] - dirty = saw_import = False - seen = set(self.sitedirs) - content = _read_pth(self.filename) - for line in content.splitlines(): - path = line.rstrip() - # still keep imports and empty/commented lines for formatting - paths.append(path) - if line.startswith(('import ', 'from ')): - saw_import = True - continue - stripped_path = path.strip() - if not stripped_path or stripped_path.startswith('#'): - continue - # skip non-existent paths, in case somebody deleted a package - # manually, and duplicate paths as well - normalized_path = normalize_path(os.path.join(self.basedir, path)) - if normalized_path in seen or not os.path.exists(normalized_path): - log.debug("cleaned up dirty or duplicated %r", path) - dirty = True - paths.pop() - continue - seen.add(normalized_path) - # remove any trailing empty/blank line - while paths and not paths[-1].strip(): - paths.pop() - dirty = True - return paths, dirty or (paths and saw_import) - - def _load(self): - if os.path.isfile(self.filename): - return self._load_raw() - return [], False - - def save(self) -> None: - """Write changed .pth file back to disk""" - # first reload the file - last_paths, last_dirty = self._load() - # and check that there are no difference with what we have. - # there can be difference if someone else has written to the file - # since we first loaded it. - # we don't want to lose the eventual new paths added since then. - for path in last_paths[:]: - if path not in self.paths: - self.paths.append(path) - log.info("detected new path %r", path) - last_dirty = True - else: - last_paths.remove(path) - # also, re-check that all paths are still valid before saving them - for path in self.paths[:]: - if path not in last_paths and not path.startswith(( - 'import ', - 'from ', - '#', - )): - absolute_path = os.path.join(self.basedir, path) - if not os.path.exists(absolute_path): - self.paths.remove(path) - log.info("removing now non-existent path %r", path) - last_dirty = True - - self.dirty |= last_dirty or self.paths != self._init_paths - if not self.dirty: - return - - rel_paths = list(map(self.make_relative, self.paths)) - if rel_paths: - log.debug("Saving %s", self.filename) - lines = self._wrap_lines(rel_paths) - data = '\n'.join(lines) + '\n' - if os.path.islink(self.filename): - os.unlink(self.filename) - with open(self.filename, 'wt', encoding=py312.PTH_ENCODING) as f: - # ^-- Python<3.13 require encoding="locale" instead of "utf-8", - # see python/cpython#77102. - f.write(data) - elif os.path.exists(self.filename): - log.debug("Deleting empty %s", self.filename) - os.unlink(self.filename) - - self.dirty = False - self._init_paths[:] = self.paths[:] - - @staticmethod - def _wrap_lines(lines): - return lines - - def add(self, dist) -> None: - """Add `dist` to the distribution map""" - new_path = dist.location not in self.paths and ( - dist.location not in self.sitedirs - or - # account for '.' being in PYTHONPATH - dist.location == os.getcwd() - ) - if new_path: - self.paths.append(dist.location) - self.dirty = True - super().add(dist) - - def remove(self, dist) -> None: - """Remove `dist` from the distribution map""" - while dist.location in self.paths: - self.paths.remove(dist.location) - self.dirty = True - super().remove(dist) - - def make_relative(self, path): - npath, last = os.path.split(normalize_path(path)) - baselen = len(self.basedir) - parts = [last] - sep = os.altsep == '/' and '/' or os.sep - while len(npath) >= baselen: - if npath == self.basedir: - parts.append(os.curdir) - parts.reverse() - return sep.join(parts) - npath, last = os.path.split(npath) - parts.append(last) - else: - return path - - -class RewritePthDistributions(PthDistributions): - @classmethod - def _wrap_lines(cls, lines): - yield cls.prelude - yield from lines - yield cls.postlude - - prelude = _one_liner( - """ - import sys - sys.__plen = len(sys.path) - """ - ) - postlude = _one_liner( - """ - import sys - new = sys.path[sys.__plen:] - del sys.path[sys.__plen:] - p = getattr(sys, '__egginsert', 0) - sys.path[p:p] = new - sys.__egginsert = p + len(new) - """ - ) - - -if os.environ.get('SETUPTOOLS_SYS_PATH_TECHNIQUE', 'raw') == 'rewrite': - PthDistributions = RewritePthDistributions # type: ignore[misc] # Overwriting type - - -def _first_line_re(): - """ - Return a regular expression based on first_line_re suitable for matching - strings. - """ - if isinstance(first_line_re.pattern, str): - return first_line_re - - # first_line_re in Python >=3.1.4 and >=3.2.1 is a bytes pattern. - return re.compile(first_line_re.pattern.decode()) - - -def update_dist_caches(dist_path, fix_zipimporter_caches): - """ - Fix any globally cached `dist_path` related data - - `dist_path` should be a path of a newly installed egg distribution (zipped - or unzipped). - - sys.path_importer_cache contains finder objects that have been cached when - importing data from the original distribution. Any such finders need to be - cleared since the replacement distribution might be packaged differently, - e.g. a zipped egg distribution might get replaced with an unzipped egg - folder or vice versa. Having the old finders cached may then cause Python - to attempt loading modules from the replacement distribution using an - incorrect loader. - - zipimport.zipimporter objects are Python loaders charged with importing - data packaged inside zip archives. If stale loaders referencing the - original distribution, are left behind, they can fail to load modules from - the replacement distribution. E.g. if an old zipimport.zipimporter instance - is used to load data from a new zipped egg archive, it may cause the - operation to attempt to locate the requested data in the wrong location - - one indicated by the original distribution's zip archive directory - information. Such an operation may then fail outright, e.g. report having - read a 'bad local file header', or even worse, it may fail silently & - return invalid data. - - zipimport._zip_directory_cache contains cached zip archive directory - information for all existing zipimport.zipimporter instances and all such - instances connected to the same archive share the same cached directory - information. - - If asked, and the underlying Python implementation allows it, we can fix - all existing zipimport.zipimporter instances instead of having to track - them down and remove them one by one, by updating their shared cached zip - archive directory information. This, of course, assumes that the - replacement distribution is packaged as a zipped egg. - - If not asked to fix existing zipimport.zipimporter instances, we still do - our best to clear any remaining zipimport.zipimporter related cached data - that might somehow later get used when attempting to load data from the new - distribution and thus cause such load operations to fail. Note that when - tracking down such remaining stale data, we can not catch every conceivable - usage from here, and we clear only those that we know of and have found to - cause problems if left alive. Any remaining caches should be updated by - whomever is in charge of maintaining them, i.e. they should be ready to - handle us replacing their zip archives with new distributions at runtime. - - """ - # There are several other known sources of stale zipimport.zipimporter - # instances that we do not clear here, but might if ever given a reason to - # do so: - # * Global setuptools pkg_resources.working_set (a.k.a. 'master working - # set') may contain distributions which may in turn contain their - # zipimport.zipimporter loaders. - # * Several zipimport.zipimporter loaders held by local variables further - # up the function call stack when running the setuptools installation. - # * Already loaded modules may have their __loader__ attribute set to the - # exact loader instance used when importing them. Python 3.4 docs state - # that this information is intended mostly for introspection and so is - # not expected to cause us problems. - normalized_path = normalize_path(dist_path) - _uncache(normalized_path, sys.path_importer_cache) - if fix_zipimporter_caches: - _replace_zip_directory_cache_data(normalized_path) - else: - # Here, even though we do not want to fix existing and now stale - # zipimporter cache information, we still want to remove it. Related to - # Python's zip archive directory information cache, we clear each of - # its stale entries in two phases: - # 1. Clear the entry so attempting to access zip archive information - # via any existing stale zipimport.zipimporter instances fails. - # 2. Remove the entry from the cache so any newly constructed - # zipimport.zipimporter instances do not end up using old stale - # zip archive directory information. - # This whole stale data removal step does not seem strictly necessary, - # but has been left in because it was done before we started replacing - # the zip archive directory information cache content if possible, and - # there are no relevant unit tests that we can depend on to tell us if - # this is really needed. - _remove_and_clear_zip_directory_cache_data(normalized_path) - - -def _collect_zipimporter_cache_entries(normalized_path, cache): - """ - Return zipimporter cache entry keys related to a given normalized path. - - Alternative path spellings (e.g. those using different character case or - those using alternative path separators) related to the same path are - included. Any sub-path entries are included as well, i.e. those - corresponding to zip archives embedded in other zip archives. - - """ - result = [] - prefix_len = len(normalized_path) - for p in cache: - np = normalize_path(p) - if np.startswith(normalized_path) and np[prefix_len : prefix_len + 1] in ( - os.sep, - '', - ): - result.append(p) - return result - - -def _update_zipimporter_cache(normalized_path, cache, updater=None): - """ - Update zipimporter cache data for a given normalized path. - - Any sub-path entries are processed as well, i.e. those corresponding to zip - archives embedded in other zip archives. - - Given updater is a callable taking a cache entry key and the original entry - (after already removing the entry from the cache), and expected to update - the entry and possibly return a new one to be inserted in its place. - Returning None indicates that the entry should not be replaced with a new - one. If no updater is given, the cache entries are simply removed without - any additional processing, the same as if the updater simply returned None. - - """ - for p in _collect_zipimporter_cache_entries(normalized_path, cache): - # N.B. pypy's custom zipimport._zip_directory_cache implementation does - # not support the complete dict interface: - # * Does not support item assignment, thus not allowing this function - # to be used only for removing existing cache entries. - # * Does not support the dict.pop() method, forcing us to use the - # get/del patterns instead. For more detailed information see the - # following links: - # https://github.com/pypa/setuptools/issues/202#issuecomment-202913420 - # https://foss.heptapod.net/pypy/pypy/-/blob/144c4e65cb6accb8e592f3a7584ea38265d1873c/pypy/module/zipimport/interp_zipimport.py - old_entry = cache[p] - del cache[p] - new_entry = updater and updater(p, old_entry) - if new_entry is not None: - cache[p] = new_entry - - -def _uncache(normalized_path, cache): - _update_zipimporter_cache(normalized_path, cache) - - -def _remove_and_clear_zip_directory_cache_data(normalized_path): - def clear_and_remove_cached_zip_archive_directory_data(path, old_entry): - old_entry.clear() - - _update_zipimporter_cache( - normalized_path, - zipimport._zip_directory_cache, - updater=clear_and_remove_cached_zip_archive_directory_data, - ) - - -# PyPy Python implementation does not allow directly writing to the -# zipimport._zip_directory_cache and so prevents us from attempting to correct -# its content. The best we can do there is clear the problematic cache content -# and have PyPy repopulate it as needed. The downside is that if there are any -# stale zipimport.zipimporter instances laying around, attempting to use them -# will fail due to not having its zip archive directory information available -# instead of being automatically corrected to use the new correct zip archive -# directory information. -if '__pypy__' in sys.builtin_module_names: - _replace_zip_directory_cache_data = _remove_and_clear_zip_directory_cache_data -else: - - def _replace_zip_directory_cache_data(normalized_path): - def replace_cached_zip_archive_directory_data(path, old_entry): - # N.B. In theory, we could load the zip directory information just - # once for all updated path spellings, and then copy it locally and - # update its contained path strings to contain the correct - # spelling, but that seems like a way too invasive move (this cache - # structure is not officially documented anywhere and could in - # theory change with new Python releases) for no significant - # benefit. - old_entry.clear() - zipimport.zipimporter(path) - old_entry.update(zipimport._zip_directory_cache[path]) - return old_entry - - _update_zipimporter_cache( - normalized_path, - zipimport._zip_directory_cache, - updater=replace_cached_zip_archive_directory_data, - ) - - -def is_python(text, filename=''): - "Is this string a valid Python script?" - try: - compile(text, filename, 'exec') - except (SyntaxError, TypeError): - return False - else: - return True - - -def is_sh(executable): - """Determine if the specified executable is a .sh (contains a #! line)""" - try: - with open(executable, encoding='latin-1') as fp: - magic = fp.read(2) - except OSError: - return executable - return magic == '#!' - - -def nt_quote_arg(arg): - """Quote a command line argument according to Windows parsing rules""" - return subprocess.list2cmdline([arg]) - - -def is_python_script(script_text, filename): - """Is this text, as a whole, a Python script? (as opposed to shell/bat/etc.""" - if filename.endswith('.py') or filename.endswith('.pyw'): - return True # extension says it's Python - if is_python(script_text, filename): - return True # it's syntactically valid Python - if script_text.startswith('#!'): - # It begins with a '#!' line, so check if 'python' is in it somewhere - return 'python' in script_text.splitlines()[0].lower() - - return False # Not any Python I can recognize - - -class _SplitArgs(TypedDict, total=False): - comments: bool - posix: bool - - -class CommandSpec(list): - """ - A command spec for a #! header, specified as a list of arguments akin to - those passed to Popen. - """ - - options: list[str] = [] - split_args = _SplitArgs() - - @classmethod - def best(cls): - """ - Choose the best CommandSpec class based on environmental conditions. - """ - return cls - - @classmethod - def _sys_executable(cls): - _default = os.path.normpath(sys.executable) - return os.environ.get('__PYVENV_LAUNCHER__', _default) - - @classmethod - def from_param(cls, param: Self | str | Iterable[str] | None) -> Self: - """ - Construct a CommandSpec from a parameter to build_scripts, which may - be None. - """ - if isinstance(param, cls): - return param - if isinstance(param, str): - return cls.from_string(param) - if isinstance(param, Iterable): - return cls(param) - if param is None: - return cls.from_environment() - raise TypeError(f"Argument has an unsupported type {type(param)}") - - @classmethod - def from_environment(cls): - return cls([cls._sys_executable()]) - - @classmethod - def from_string(cls, string: str) -> Self: - """ - Construct a command spec from a simple string representing a command - line parseable by shlex.split. - """ - items = shlex.split(string, **cls.split_args) - return cls(items) - - def install_options(self, script_text: str): - self.options = shlex.split(self._extract_options(script_text)) - cmdline = subprocess.list2cmdline(self) - if not isascii(cmdline): - self.options[:0] = ['-x'] - - @staticmethod - def _extract_options(orig_script): - """ - Extract any options from the first line of the script. - """ - first = (orig_script + '\n').splitlines()[0] - match = _first_line_re().match(first) - options = match.group(1) or '' if match else '' - return options.strip() - - def as_header(self): - return self._render(self + list(self.options)) - - @staticmethod - def _strip_quotes(item): - _QUOTES = '"\'' - for q in _QUOTES: - if item.startswith(q) and item.endswith(q): - return item[1:-1] - return item - - @staticmethod - def _render(items): - cmdline = subprocess.list2cmdline( - CommandSpec._strip_quotes(item.strip()) for item in items - ) - return '#!' + cmdline + '\n' - - -# For pbr compat; will be removed in a future version. -sys_executable = CommandSpec._sys_executable() - - -class WindowsCommandSpec(CommandSpec): - split_args = _SplitArgs(posix=False) - - -class ScriptWriter: - """ - Encapsulates behavior around writing entry point scripts for console and - gui apps. - """ - - template = textwrap.dedent( - r""" - # EASY-INSTALL-ENTRY-SCRIPT: %(spec)r,%(group)r,%(name)r - import re - import sys - - # for compatibility with easy_install; see #2198 - __requires__ = %(spec)r - - try: - from importlib.metadata import distribution - except ImportError: - try: - from importlib_metadata import distribution - except ImportError: - from pkg_resources import load_entry_point - - - def importlib_load_entry_point(spec, group, name): - dist_name, _, _ = spec.partition('==') - matches = ( - entry_point - for entry_point in distribution(dist_name).entry_points - if entry_point.group == group and entry_point.name == name - ) - return next(matches).load() - - - globals().setdefault('load_entry_point', importlib_load_entry_point) - - - if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) - sys.exit(load_entry_point(%(spec)r, %(group)r, %(name)r)()) - """ - ).lstrip() - - command_spec_class = CommandSpec - - @classmethod - def get_args(cls, dist, header=None): - """ - Yield write_script() argument tuples for a distribution's - console_scripts and gui_scripts entry points. - """ - if header is None: - header = cls.get_header() - spec = str(dist.as_requirement()) - for type_ in 'console', 'gui': - group = type_ + '_scripts' - for name in dist.get_entry_map(group).keys(): - cls._ensure_safe_name(name) - script_text = cls.template % locals() - args = cls._get_script_args(type_, name, header, script_text) - yield from args - - @staticmethod - def _ensure_safe_name(name): - """ - Prevent paths in *_scripts entry point names. - """ - has_path_sep = re.search(r'[\\/]', name) - if has_path_sep: - raise ValueError("Path separators not allowed in script names") - - @classmethod - def best(cls): - """ - Select the best ScriptWriter for this environment. - """ - if sys.platform == 'win32' or (os.name == 'java' and os._name == 'nt'): - return WindowsScriptWriter.best() - else: - return cls - - @classmethod - def _get_script_args(cls, type_, name, header, script_text): - # Simply write the stub with no extension. - yield (name, header + script_text) - - @classmethod - def get_header( - cls, - script_text: str = "", - executable: str | CommandSpec | Iterable[str] | None = None, - ) -> str: - """Create a #! line, getting options (if any) from script_text""" - cmd = cls.command_spec_class.best().from_param(executable) - cmd.install_options(script_text) - return cmd.as_header() - - -class WindowsScriptWriter(ScriptWriter): - command_spec_class = WindowsCommandSpec - - @classmethod - def best(cls): - """ - Select the best ScriptWriter suitable for Windows - """ - writer_lookup = dict( - executable=WindowsExecutableLauncherWriter, - natural=cls, - ) - # for compatibility, use the executable launcher by default - launcher = os.environ.get('SETUPTOOLS_LAUNCHER', 'executable') - return writer_lookup[launcher] - - @classmethod - def _get_script_args(cls, type_, name, header, script_text): - "For Windows, add a .py extension" - ext = dict(console='.pya', gui='.pyw')[type_] - if ext not in os.environ['PATHEXT'].lower().split(';'): - msg = ( - "{ext} not listed in PATHEXT; scripts will not be " - "recognized as executables." - ).format(**locals()) - SetuptoolsWarning.emit(msg) - old = ['.pya', '.py', '-script.py', '.pyc', '.pyo', '.pyw', '.exe'] - old.remove(ext) - header = cls._adjust_header(type_, header) - blockers = [name + x for x in old] - yield name + ext, header + script_text, 't', blockers - - @classmethod - def _adjust_header(cls, type_, orig_header): - """ - Make sure 'pythonw' is used for gui and 'python' is used for - console (regardless of what sys.executable is). - """ - pattern = 'pythonw.exe' - repl = 'python.exe' - if type_ == 'gui': - pattern, repl = repl, pattern - pattern_ob = re.compile(re.escape(pattern), re.IGNORECASE) - new_header = pattern_ob.sub(string=orig_header, repl=repl) - return new_header if cls._use_header(new_header) else orig_header - - @staticmethod - def _use_header(new_header): - """ - Should _adjust_header use the replaced header? - - On non-windows systems, always use. On - Windows systems, only use the replaced header if it resolves - to an executable on the system. - """ - clean_header = new_header[2:-1].strip('"') - return sys.platform != 'win32' or shutil.which(clean_header) - - -class WindowsExecutableLauncherWriter(WindowsScriptWriter): - @classmethod - def _get_script_args(cls, type_, name, header, script_text): - """ - For Windows, add a .py extension and an .exe launcher - """ - if type_ == 'gui': - launcher_type = 'gui' - ext = '-script.pyw' - old = ['.pyw'] - else: - launcher_type = 'cli' - ext = '-script.py' - old = ['.py', '.pyc', '.pyo'] - hdr = cls._adjust_header(type_, header) - blockers = [name + x for x in old] - yield (name + ext, hdr + script_text, 't', blockers) - yield ( - name + '.exe', - get_win_launcher(launcher_type), - 'b', # write in binary mode - ) - if not is_64bit(): - # install a manifest for the launcher to prevent Windows - # from detecting it as an installer (which it will for - # launchers like easy_install.exe). Consider only - # adding a manifest for launchers detected as installers. - # See Distribute #143 for details. - m_name = name + '.exe.manifest' - yield (m_name, load_launcher_manifest(name), 't') - - -def get_win_launcher(type): - """ - Load the Windows launcher (executable) suitable for launching a script. - - `type` should be either 'cli' or 'gui' - - Returns the executable as a byte string. - """ - launcher_fn = f'{type}.exe' - if is_64bit(): - if get_platform() == "win-arm64": - launcher_fn = launcher_fn.replace(".", "-arm64.") - else: - launcher_fn = launcher_fn.replace(".", "-64.") - else: - launcher_fn = launcher_fn.replace(".", "-32.") - return resource_string('setuptools', launcher_fn) - - -def load_launcher_manifest(name): - manifest = pkg_resources.resource_string(__name__, 'launcher manifest.xml') - return manifest.decode('utf-8') % vars() - - -def current_umask(): - tmp = os.umask(0o022) - os.umask(tmp) - return tmp - - -def only_strs(values): - """ - Exclude non-str values. Ref #3063. - """ - return filter(lambda val: isinstance(val, str), values) - - -def _read_pth(fullname: str) -> str: - # Python<3.13 require encoding="locale" instead of "utf-8", see python/cpython#77102 - # In the case old versions of setuptools are producing `pth` files with - # different encodings that might be problematic... So we fallback to "locale". - - try: - with open(fullname, encoding=py312.PTH_ENCODING) as f: - return f.read() - except UnicodeDecodeError: # pragma: no cover - # This error may only happen for Python >= 3.13 - # TODO: Possible deprecation warnings to be added in the future: - # ``.pth file {fullname!r} is not UTF-8.`` - # Your environment contain {fullname!r} that cannot be read as UTF-8. - # This is likely to have been produced with an old version of setuptools. - # Please be mindful that this is deprecated and in the future, non-utf8 - # .pth files may cause setuptools to fail. - with open(fullname, encoding=py39.LOCALE_ENCODING) as f: - return f.read() - - -class EasyInstallDeprecationWarning(SetuptoolsDeprecationWarning): - _SUMMARY = "easy_install command is deprecated." - _DETAILS = """ - Please avoid running ``setup.py`` and ``easy_install``. - Instead, use pypa/build, pypa/installer or other - standards-based tools. - """ - _SEE_URL = "https://github.com/pypa/setuptools/issues/917" - # _DUE_DATE not defined yet + """Stubbed command for temporary pbr compatibility.""" diff --git a/setuptools/command/install_scripts.py b/setuptools/command/install_scripts.py index 4401cf693d..02a73e818b 100644 --- a/setuptools/command/install_scripts.py +++ b/setuptools/command/install_scripts.py @@ -34,7 +34,7 @@ def _install_ep_scripts(self): # Delay import side-effects from pkg_resources import Distribution, PathMetadata - from . import easy_install as ei + from .. import _scripts ei_cmd = self.get_finalized_command("egg_info") dist = Distribution( @@ -45,7 +45,7 @@ def _install_ep_scripts(self): ) bs_cmd = self.get_finalized_command('build_scripts') exec_param = getattr(bs_cmd, 'executable', None) - writer = ei.ScriptWriter + writer = _scripts.ScriptWriter if exec_param == sys.executable: # In case the path to the Python executable contains a space, wrap # it so it's not split up. @@ -58,7 +58,7 @@ def _install_ep_scripts(self): def write_script(self, script_name, contents, mode: str = "t", *ignored) -> None: """Write an executable file to the scripts directory""" - from setuptools.command.easy_install import chmod, current_umask + from .._shutil import attempt_chmod_verbose as chmod, current_umask log.info("Installing %s script to %s", script_name, self.install_dir) target = os.path.join(self.install_dir, script_name) diff --git a/setuptools/package_index.py b/setuptools/package_index.py deleted file mode 100644 index 3500c2d86f..0000000000 --- a/setuptools/package_index.py +++ /dev/null @@ -1,1179 +0,0 @@ -"""PyPI and direct package downloading.""" - -from __future__ import annotations - -import base64 -import configparser -import hashlib -import html -import http.client -import io -import itertools -import os -import re -import shutil -import socket -import subprocess -import sys -import urllib.error -import urllib.parse -import urllib.request -from fnmatch import translate -from functools import wraps -from typing import NamedTuple - -from more_itertools import unique_everseen - -import setuptools -from pkg_resources import ( - BINARY_DIST, - CHECKOUT_DIST, - DEVELOP_DIST, - EGG_DIST, - SOURCE_DIST, - Distribution, - Environment, - Requirement, - find_distributions, - normalize_path, - parse_version, - safe_name, - safe_version, - to_filename, -) -from setuptools.wheel import Wheel - -from .unicode_utils import _cfg_read_utf8_with_fallback, _read_utf8_with_fallback - -from distutils import log -from distutils.errors import DistutilsError - -EGG_FRAGMENT = re.compile(r'^egg=([-A-Za-z0-9_.+!]+)$') -HREF = re.compile(r"""href\s*=\s*['"]?([^'"> ]+)""", re.I) -PYPI_MD5 = re.compile( - r'([^<]+)\n\s+\(md5\)' -) -URL_SCHEME = re.compile('([-+.a-z0-9]{2,}):', re.I).match -EXTENSIONS = ".tar.gz .tar.bz2 .tar .zip .tgz".split() - -__all__ = [ - 'PackageIndex', - 'distros_for_url', - 'parse_bdist_wininst', - 'interpret_distro_name', -] - -_SOCKET_TIMEOUT = 15 - -user_agent = f"setuptools/{setuptools.__version__} Python-urllib/{sys.version_info.major}.{sys.version_info.minor}" - - -def parse_requirement_arg(spec): - try: - return Requirement.parse(spec) - except ValueError as e: - raise DistutilsError( - f"Not a URL, existing file, or requirement spec: {spec!r}" - ) from e - - -def parse_bdist_wininst(name): - """Return (base,pyversion) or (None,None) for possible .exe name""" - - lower = name.lower() - base, py_ver, plat = None, None, None - - if lower.endswith('.exe'): - if lower.endswith('.win32.exe'): - base = name[:-10] - plat = 'win32' - elif lower.startswith('.win32-py', -16): - py_ver = name[-7:-4] - base = name[:-16] - plat = 'win32' - elif lower.endswith('.win-amd64.exe'): - base = name[:-14] - plat = 'win-amd64' - elif lower.startswith('.win-amd64-py', -20): - py_ver = name[-7:-4] - base = name[:-20] - plat = 'win-amd64' - return base, py_ver, plat - - -def egg_info_for_url(url): - parts = urllib.parse.urlparse(url) - _scheme, server, path, _parameters, _query, fragment = parts - base = urllib.parse.unquote(path.split('/')[-1]) - if server == 'sourceforge.net' and base == 'download': # XXX Yuck - base = urllib.parse.unquote(path.split('/')[-2]) - if '#' in base: - base, fragment = base.split('#', 1) - return base, fragment - - -def distros_for_url(url, metadata=None): - """Yield egg or source distribution objects that might be found at a URL""" - base, fragment = egg_info_for_url(url) - yield from distros_for_location(url, base, metadata) - if fragment: - match = EGG_FRAGMENT.match(fragment) - if match: - yield from interpret_distro_name( - url, match.group(1), metadata, precedence=CHECKOUT_DIST - ) - - -def distros_for_location(location, basename, metadata=None): - """Yield egg or source distribution objects based on basename""" - if basename.endswith('.egg.zip'): - basename = basename[:-4] # strip the .zip - if basename.endswith('.egg') and '-' in basename: - # only one, unambiguous interpretation - return [Distribution.from_location(location, basename, metadata)] - if basename.endswith('.whl') and '-' in basename: - wheel = Wheel(basename) - if not wheel.is_compatible(): - return [] - return [ - Distribution( - location=location, - project_name=wheel.project_name, - version=wheel.version, - # Increase priority over eggs. - precedence=EGG_DIST + 1, - ) - ] - if basename.endswith('.exe'): - win_base, py_ver, platform = parse_bdist_wininst(basename) - if win_base is not None: - return interpret_distro_name( - location, win_base, metadata, py_ver, BINARY_DIST, platform - ) - # Try source distro extensions (.zip, .tgz, etc.) - # - for ext in EXTENSIONS: - if basename.endswith(ext): - basename = basename[: -len(ext)] - return interpret_distro_name(location, basename, metadata) - return [] # no extension matched - - -def distros_for_filename(filename, metadata=None): - """Yield possible egg or source distribution objects based on a filename""" - return distros_for_location( - normalize_path(filename), os.path.basename(filename), metadata - ) - - -def interpret_distro_name( - location, basename, metadata, py_version=None, precedence=SOURCE_DIST, platform=None -): - """Generate the interpretation of a source distro name - - Note: if `location` is a filesystem filename, you should call - ``pkg_resources.normalize_path()`` on it before passing it to this - routine! - """ - - parts = basename.split('-') - if not py_version and any(re.match(r'py\d\.\d$', p) for p in parts[2:]): - # it is a bdist_dumb, not an sdist -- bail out - return - - # find the pivot (p) that splits the name from the version. - # infer the version as the first item that has a digit. - for p in range(len(parts)): - if parts[p][:1].isdigit(): - break - else: - p = len(parts) - - yield Distribution( - location, - metadata, - '-'.join(parts[:p]), - '-'.join(parts[p:]), - py_version=py_version, - precedence=precedence, - platform=platform, - ) - - -def unique_values(func): - """ - Wrap a function returning an iterable such that the resulting iterable - only ever yields unique items. - """ - - @wraps(func) - def wrapper(*args, **kwargs): - return unique_everseen(func(*args, **kwargs)) - - return wrapper - - -REL = re.compile(r"""<([^>]*\srel\s{0,10}=\s{0,10}['"]?([^'" >]+)[^>]*)>""", re.I) -""" -Regex for an HTML tag with 'rel="val"' attributes. -""" - - -@unique_values -def find_external_links(url, page): - """Find rel="homepage" and rel="download" links in `page`, yielding URLs""" - - for match in REL.finditer(page): - tag, rel = match.groups() - rels = set(map(str.strip, rel.lower().split(','))) - if 'homepage' in rels or 'download' in rels: - for match in HREF.finditer(tag): - yield urllib.parse.urljoin(url, htmldecode(match.group(1))) - - for tag in ("Home Page", "Download URL"): - pos = page.find(tag) - if pos != -1: - match = HREF.search(page, pos) - if match: - yield urllib.parse.urljoin(url, htmldecode(match.group(1))) - - -class ContentChecker: - """ - A null content checker that defines the interface for checking content - """ - - def feed(self, block): - """ - Feed a block of data to the hash. - """ - return - - def is_valid(self): - """ - Check the hash. Return False if validation fails. - """ - return True - - def report(self, reporter, template): - """ - Call reporter with information about the checker (hash name) - substituted into the template. - """ - return - - -class HashChecker(ContentChecker): - pattern = re.compile( - r'(?Psha1|sha224|sha384|sha256|sha512|md5)=' - r'(?P[a-f0-9]+)' - ) - - def __init__(self, hash_name, expected) -> None: - self.hash_name = hash_name - self.hash = hashlib.new(hash_name) - self.expected = expected - - @classmethod - def from_url(cls, url): - "Construct a (possibly null) ContentChecker from a URL" - fragment = urllib.parse.urlparse(url)[-1] - if not fragment: - return ContentChecker() - match = cls.pattern.search(fragment) - if not match: - return ContentChecker() - return cls(**match.groupdict()) - - def feed(self, block): - self.hash.update(block) - - def is_valid(self): - return self.hash.hexdigest() == self.expected - - def report(self, reporter, template): - msg = template % self.hash_name - return reporter(msg) - - -class PackageIndex(Environment): - """A distribution index that scans web pages for download URLs""" - - def __init__( - self, - index_url: str = "https://pypi.org/simple/", - hosts=('*',), - ca_bundle=None, - verify_ssl: bool = True, - *args, - **kw, - ) -> None: - super().__init__(*args, **kw) - self.index_url = index_url + "/"[: not index_url.endswith('/')] - self.scanned_urls: dict = {} - self.fetched_urls: dict = {} - self.package_pages: dict = {} - self.allows = re.compile('|'.join(map(translate, hosts))).match - self.to_scan: list = [] - self.opener = urllib.request.urlopen - - def add(self, dist): - # ignore invalid versions - try: - parse_version(dist.version) - except Exception: - return None - return super().add(dist) - - # FIXME: 'PackageIndex.process_url' is too complex (14) - def process_url(self, url, retrieve: bool = False) -> None: # noqa: C901 - """Evaluate a URL as a possible download, and maybe retrieve it""" - if url in self.scanned_urls and not retrieve: - return - self.scanned_urls[url] = True - if not URL_SCHEME(url): - self.process_filename(url) - return - else: - dists = list(distros_for_url(url)) - if dists: - if not self.url_ok(url): - return - self.debug("Found link: %s", url) - - if dists or not retrieve or url in self.fetched_urls: - list(map(self.add, dists)) - return # don't need the actual page - - if not self.url_ok(url): - self.fetched_urls[url] = True - return - - self.info("Reading %s", url) - self.fetched_urls[url] = True # prevent multiple fetch attempts - tmpl = "Download error on %s: %%s -- Some packages may not be found!" - f = self.open_url(url, tmpl % url) - if f is None: - return - if isinstance(f, urllib.error.HTTPError) and f.code == 401: - self.info(f"Authentication error: {f.msg}") - self.fetched_urls[f.url] = True - if 'html' not in f.headers.get('content-type', '').lower(): - f.close() # not html, we can't process it - return - - base = f.url # handle redirects - page = f.read() - if not isinstance(page, str): - # In Python 3 and got bytes but want str. - if isinstance(f, urllib.error.HTTPError): - # Errors have no charset, assume latin1: - charset = 'latin-1' - else: - charset = f.headers.get_param('charset') or 'latin-1' - page = page.decode(charset, "ignore") - f.close() - for match in HREF.finditer(page): - link = urllib.parse.urljoin(base, htmldecode(match.group(1))) - self.process_url(link) - if url.startswith(self.index_url) and getattr(f, 'code', None) != 404: - page = self.process_index(url, page) - - def process_filename(self, fn, nested: bool = False) -> None: - # process filenames or directories - if not os.path.exists(fn): - self.warn("Not found: %s", fn) - return - - if os.path.isdir(fn) and not nested: - path = os.path.realpath(fn) - for item in os.listdir(path): - self.process_filename(os.path.join(path, item), True) - - dists = distros_for_filename(fn) - if dists: - self.debug("Found: %s", fn) - list(map(self.add, dists)) - - def url_ok(self, url, fatal: bool = False) -> bool: - s = URL_SCHEME(url) - is_file = s and s.group(1).lower() == 'file' - if is_file or self.allows(urllib.parse.urlparse(url)[1]): - return True - msg = ( - "\nNote: Bypassing %s (disallowed host; see " - "https://setuptools.pypa.io/en/latest/deprecated/" - "easy_install.html#restricting-downloads-with-allow-hosts for details).\n" - ) - if fatal: - raise DistutilsError(msg % url) - else: - self.warn(msg, url) - return False - - def scan_egg_links(self, search_path) -> None: - dirs = filter(os.path.isdir, search_path) - egg_links = ( - (path, entry) - for path in dirs - for entry in os.listdir(path) - if entry.endswith('.egg-link') - ) - list(itertools.starmap(self.scan_egg_link, egg_links)) - - def scan_egg_link(self, path, entry) -> None: - content = _read_utf8_with_fallback(os.path.join(path, entry)) - # filter non-empty lines - lines = list(filter(None, map(str.strip, content.splitlines()))) - - if len(lines) != 2: - # format is not recognized; punt - return - - egg_path, _setup_path = lines - - for dist in find_distributions(os.path.join(path, egg_path)): - dist.location = os.path.join(path, *lines) - dist.precedence = SOURCE_DIST - self.add(dist) - - def _scan(self, link): - # Process a URL to see if it's for a package page - NO_MATCH_SENTINEL = None, None - if not link.startswith(self.index_url): - return NO_MATCH_SENTINEL - - parts = list(map(urllib.parse.unquote, link[len(self.index_url) :].split('/'))) - if len(parts) != 2 or '#' in parts[1]: - return NO_MATCH_SENTINEL - - # it's a package page, sanitize and index it - pkg = safe_name(parts[0]) - ver = safe_version(parts[1]) - self.package_pages.setdefault(pkg.lower(), {})[link] = True - return to_filename(pkg), to_filename(ver) - - def process_index(self, url, page): - """Process the contents of a PyPI page""" - - # process an index page into the package-page index - for match in HREF.finditer(page): - try: - self._scan(urllib.parse.urljoin(url, htmldecode(match.group(1)))) - except ValueError: - pass - - pkg, ver = self._scan(url) # ensure this page is in the page index - if not pkg: - return "" # no sense double-scanning non-package pages - - # process individual package page - for new_url in find_external_links(url, page): - # Process the found URL - base, frag = egg_info_for_url(new_url) - if base.endswith('.py') and not frag: - if ver: - new_url += f'#egg={pkg}-{ver}' - else: - self.need_version_info(url) - self.scan_url(new_url) - - return PYPI_MD5.sub( - lambda m: '{}'.format(*m.group(1, 3, 2)), page - ) - - def need_version_info(self, url) -> None: - self.scan_all( - "Page at %s links to .py file(s) without version info; an index " - "scan is required.", - url, - ) - - def scan_all(self, msg=None, *args) -> None: - if self.index_url not in self.fetched_urls: - if msg: - self.warn(msg, *args) - self.info("Scanning index of all packages (this may take a while)") - self.scan_url(self.index_url) - - def find_packages(self, requirement) -> None: - self.scan_url(self.index_url + requirement.unsafe_name + '/') - - if not self.package_pages.get(requirement.key): - # Fall back to safe version of the name - self.scan_url(self.index_url + requirement.project_name + '/') - - if not self.package_pages.get(requirement.key): - # We couldn't find the target package, so search the index page too - self.not_found_in_index(requirement) - - for url in list(self.package_pages.get(requirement.key, ())): - # scan each page that might be related to the desired package - self.scan_url(url) - - def obtain(self, requirement, installer=None): - self.prescan() - self.find_packages(requirement) - for dist in self[requirement.key]: - if dist in requirement: - return dist - self.debug("%s does not match %s", requirement, dist) - return super().obtain(requirement, installer) - - def check_hash(self, checker, filename, tfp) -> None: - """ - checker is a ContentChecker - """ - checker.report(self.debug, f"Validating %s checksum for {filename}") - if not checker.is_valid(): - tfp.close() - os.unlink(filename) - raise DistutilsError( - f"{checker.hash.name} validation failed for {os.path.basename(filename)}; " - "possible download problem?" - ) - - def add_find_links(self, urls) -> None: - """Add `urls` to the list that will be prescanned for searches""" - for url in urls: - if ( - self.to_scan is None # if we have already "gone online" - or not URL_SCHEME(url) # or it's a local file/directory - or url.startswith('file:') - or list(distros_for_url(url)) # or a direct package link - ): - # then go ahead and process it now - self.scan_url(url) - else: - # otherwise, defer retrieval till later - self.to_scan.append(url) - - def prescan(self): - """Scan urls scheduled for prescanning (e.g. --find-links)""" - if self.to_scan: - list(map(self.scan_url, self.to_scan)) - self.to_scan = None # from now on, go ahead and process immediately - - def not_found_in_index(self, requirement) -> None: - if self[requirement.key]: # we've seen at least one distro - meth, msg = self.info, "Couldn't retrieve index page for %r" - else: # no distros seen for this name, might be misspelled - meth, msg = self.warn, "Couldn't find index page for %r (maybe misspelled?)" - meth(msg, requirement.unsafe_name) - self.scan_all() - - def download(self, spec, tmpdir): - """Locate and/or download `spec` to `tmpdir`, returning a local path - - `spec` may be a ``Requirement`` object, or a string containing a URL, - an existing local filename, or a project/version requirement spec - (i.e. the string form of a ``Requirement`` object). If it is the URL - of a .py file with an unambiguous ``#egg=name-version`` tag (i.e., one - that escapes ``-`` as ``_`` throughout), a trivial ``setup.py`` is - automatically created alongside the downloaded file. - - If `spec` is a ``Requirement`` object or a string containing a - project/version requirement spec, this method returns the location of - a matching distribution (possibly after downloading it to `tmpdir`). - If `spec` is a locally existing file or directory name, it is simply - returned unchanged. If `spec` is a URL, it is downloaded to a subpath - of `tmpdir`, and the local filename is returned. Various errors may be - raised if a problem occurs during downloading. - """ - if not isinstance(spec, Requirement): - scheme = URL_SCHEME(spec) - if scheme: - # It's a url, download it to tmpdir - found = self._download_url(spec, tmpdir) - base, fragment = egg_info_for_url(spec) - if base.endswith('.py'): - found = self.gen_setup(found, fragment, tmpdir) - return found - elif os.path.exists(spec): - # Existing file or directory, just return it - return spec - else: - spec = parse_requirement_arg(spec) - return getattr(self.fetch_distribution(spec, tmpdir), 'location', None) - - def fetch_distribution( # noqa: C901 # is too complex (14) # FIXME - self, - requirement, - tmpdir, - force_scan: bool = False, - source: bool = False, - develop_ok: bool = False, - local_index=None, - ) -> Distribution | None: - """Obtain a distribution suitable for fulfilling `requirement` - - `requirement` must be a ``pkg_resources.Requirement`` instance. - If necessary, or if the `force_scan` flag is set, the requirement is - searched for in the (online) package index as well as the locally - installed packages. If a distribution matching `requirement` is found, - the returned distribution's ``location`` is the value you would have - gotten from calling the ``download()`` method with the matching - distribution's URL or filename. If no matching distribution is found, - ``None`` is returned. - - If the `source` flag is set, only source distributions and source - checkout links will be considered. Unless the `develop_ok` flag is - set, development and system eggs (i.e., those using the ``.egg-info`` - format) will be ignored. - """ - # process a Requirement - self.info("Searching for %s", requirement) - skipped = set() - dist = None - - def find(req, env: Environment | None = None): - if env is None: - env = self - # Find a matching distribution; may be called more than once - - for dist in env[req.key]: - if dist.precedence == DEVELOP_DIST and not develop_ok: - if dist not in skipped: - self.warn( - "Skipping development or system egg: %s", - dist, - ) - skipped.add(dist) - continue - - test = dist in req and (dist.precedence <= SOURCE_DIST or not source) - if test: - loc = self.download(dist.location, tmpdir) - dist.download_location = loc - if os.path.exists(dist.download_location): - return dist - - return None - - if force_scan: - self.prescan() - self.find_packages(requirement) - dist = find(requirement) - - if not dist and local_index is not None: - dist = find(requirement, local_index) - - if dist is None: - if self.to_scan is not None: - self.prescan() - dist = find(requirement) - - if dist is None and not force_scan: - self.find_packages(requirement) - dist = find(requirement) - - if dist is None: - self.warn( - "No local packages or working download links found for %s%s", - (source and "a source distribution of " or ""), - requirement, - ) - return None - else: - self.info("Best match: %s", dist) - return dist.clone(location=dist.download_location) - - def fetch( - self, requirement, tmpdir, force_scan: bool = False, source: bool = False - ) -> str | None: - """Obtain a file suitable for fulfilling `requirement` - - DEPRECATED; use the ``fetch_distribution()`` method now instead. For - backward compatibility, this routine is identical but returns the - ``location`` of the downloaded distribution instead of a distribution - object. - """ - dist = self.fetch_distribution(requirement, tmpdir, force_scan, source) - if dist is not None: - return dist.location - return None - - def gen_setup(self, filename, fragment, tmpdir): - match = EGG_FRAGMENT.match(fragment) - dists = ( - match - and [ - d - for d in interpret_distro_name(filename, match.group(1), None) - if d.version - ] - or [] - ) - - if len(dists) == 1: # unambiguous ``#egg`` fragment - basename = os.path.basename(filename) - - # Make sure the file has been downloaded to the temp dir. - if os.path.dirname(filename) != tmpdir: - dst = os.path.join(tmpdir, basename) - if not (os.path.exists(dst) and os.path.samefile(filename, dst)): - shutil.copy2(filename, dst) - filename = dst - - with open(os.path.join(tmpdir, 'setup.py'), 'w', encoding="utf-8") as file: - file.write( - "from setuptools import setup\n" - f"setup(name={dists[0].project_name!r}, version={dists[0].version!r}, py_modules=[{os.path.splitext(basename)[0]!r}])\n" - ) - return filename - - elif match: - raise DistutilsError( - f"Can't unambiguously interpret project/version identifier {fragment!r}; " - "any dashes in the name or version should be escaped using " - f"underscores. {dists!r}" - ) - else: - raise DistutilsError( - "Can't process plain .py files without an '#egg=name-version'" - " suffix to enable automatic setup script generation." - ) - - dl_blocksize = 8192 - - def _download_to(self, url, filename): - self.info("Downloading %s", url) - # Download the file - fp = None - try: - checker = HashChecker.from_url(url) - fp = self.open_url(url) - if isinstance(fp, urllib.error.HTTPError): - raise DistutilsError(f"Can't download {url}: {fp.code} {fp.msg}") - headers = fp.info() - blocknum = 0 - bs = self.dl_blocksize - size = -1 - if "content-length" in headers: - # Some servers return multiple Content-Length headers :( - sizes = headers.get_all('Content-Length') - size = max(map(int, sizes)) - self.reporthook(url, filename, blocknum, bs, size) - with open(filename, 'wb') as tfp: - while True: - block = fp.read(bs) - if block: - checker.feed(block) - tfp.write(block) - blocknum += 1 - self.reporthook(url, filename, blocknum, bs, size) - else: - break - self.check_hash(checker, filename, tfp) - return headers - finally: - if fp: - fp.close() - - def reporthook(self, url, filename, blocknum, blksize, size) -> None: - pass # no-op - - # FIXME: - def open_url(self, url, warning=None): # noqa: C901 # is too complex (12) - if url.startswith('file:'): - return local_open(url) - try: - return open_with_auth(url, self.opener) - except (ValueError, http.client.InvalidURL) as v: - msg = ' '.join([str(arg) for arg in v.args]) - if warning: - self.warn(warning, msg) - else: - raise DistutilsError(f'{url} {msg}') from v - except urllib.error.HTTPError as v: - return v - except urllib.error.URLError as v: - if warning: - self.warn(warning, v.reason) - else: - raise DistutilsError(f"Download error for {url}: {v.reason}") from v - except http.client.BadStatusLine as v: - if warning: - self.warn(warning, v.line) - else: - raise DistutilsError( - f'{url} returned a bad status line. The server might be ' - f'down, {v.line}' - ) from v - except (http.client.HTTPException, OSError) as v: - if warning: - self.warn(warning, v) - else: - raise DistutilsError(f"Download error for {url}: {v}") from v - - @staticmethod - def _sanitize(name): - r""" - Replace unsafe path directives with underscores. - - >>> san = PackageIndex._sanitize - >>> san('/home/user/.ssh/authorized_keys') - '_home_user_.ssh_authorized_keys' - >>> san('..\\foo\\bing') - '__foo_bing' - >>> san('D:bar') - 'D_bar' - >>> san('C:\\bar') - 'C__bar' - >>> san('foo..bar') - 'foo..bar' - >>> san('D:../foo') - 'D___foo' - """ - pattern = '|'.join(( - # drive letters - r':', - # path separators - r'[/\\]', - # parent dirs - r'(?:(?<=([/\\]|:))\.\.(?=[/\\]|$))|(?:^\.\.(?=[/\\]|$))', - )) - return re.sub(pattern, r'_', name) - - @classmethod - def _resolve_download_filename(cls, url, tmpdir): - """ - >>> import pathlib - >>> du = PackageIndex._resolve_download_filename - >>> root = getfixture('tmp_path') - >>> url = 'https://files.pythonhosted.org/packages/a9/5a/0db.../setuptools-78.1.0.tar.gz' - >>> str(pathlib.Path(du(url, root)).relative_to(root)) - 'setuptools-78.1.0.tar.gz' - """ - name, _fragment = egg_info_for_url(url) - name = cls._sanitize( - name - or - # default if URL has no path contents - '__downloaded__' - ) - - # strip any extra .zip before download - name = re.sub(r'\.egg\.zip$', '.egg', name) - - return os.path.join(tmpdir, name) - - def _download_url(self, url, tmpdir): - """ - Determine the download filename. - """ - filename = self._resolve_download_filename(url, tmpdir) - return self._download_vcs(url, filename) or self._download_other(url, filename) - - @staticmethod - def _resolve_vcs(url): - """ - >>> rvcs = PackageIndex._resolve_vcs - >>> rvcs('git+http://foo/bar') - 'git' - >>> rvcs('hg+https://foo/bar') - 'hg' - >>> rvcs('git:myhost') - 'git' - >>> rvcs('hg:myhost') - >>> rvcs('http://foo/bar') - """ - scheme = urllib.parse.urlsplit(url).scheme - pre, sep, _post = scheme.partition('+') - # svn and git have their own protocol; hg does not - allowed = set(['svn', 'git'] + ['hg'] * bool(sep)) - return next(iter({pre} & allowed), None) - - def _download_vcs(self, url, spec_filename): - vcs = self._resolve_vcs(url) - if not vcs: - return None - if vcs == 'svn': - raise DistutilsError( - f"Invalid config, SVN download is not supported: {url}" - ) - - filename, _, _ = spec_filename.partition('#') - url, rev = self._vcs_split_rev_from_url(url) - - self.info(f"Doing {vcs} clone from {url} to {filename}") - subprocess.check_call([vcs, 'clone', '--quiet', url, filename]) - - co_commands = dict( - git=[vcs, '-C', filename, 'checkout', '--quiet', rev], - hg=[vcs, '--cwd', filename, 'up', '-C', '-r', rev, '-q'], - ) - if rev is not None: - self.info(f"Checking out {rev}") - subprocess.check_call(co_commands[vcs]) - - return filename - - def _download_other(self, url, filename): - scheme = urllib.parse.urlsplit(url).scheme - if scheme == 'file': # pragma: no cover - return urllib.request.url2pathname(urllib.parse.urlparse(url).path) - # raise error if not allowed - self.url_ok(url, True) - return self._attempt_download(url, filename) - - def scan_url(self, url) -> None: - self.process_url(url, True) - - def _attempt_download(self, url, filename): - headers = self._download_to(url, filename) - if 'html' in headers.get('content-type', '').lower(): - return self._invalid_download_html(url, headers, filename) - else: - return filename - - def _invalid_download_html(self, url, headers, filename): - os.unlink(filename) - raise DistutilsError(f"Unexpected HTML page found at {url}") - - @staticmethod - def _vcs_split_rev_from_url(url): - """ - Given a possible VCS URL, return a clean URL and resolved revision if any. - - >>> vsrfu = PackageIndex._vcs_split_rev_from_url - >>> vsrfu('git+https://github.com/pypa/setuptools@v69.0.0#egg-info=setuptools') - ('https://github.com/pypa/setuptools', 'v69.0.0') - >>> vsrfu('git+https://github.com/pypa/setuptools#egg-info=setuptools') - ('https://github.com/pypa/setuptools', None) - >>> vsrfu('http://foo/bar') - ('http://foo/bar', None) - """ - parts = urllib.parse.urlsplit(url) - - clean_scheme = parts.scheme.split('+', 1)[-1] - - # Some fragment identification fails - no_fragment_path, _, _ = parts.path.partition('#') - - pre, sep, post = no_fragment_path.rpartition('@') - clean_path, rev = (pre, post) if sep else (post, None) - - resolved = parts._replace( - scheme=clean_scheme, - path=clean_path, - # discard the fragment - fragment='', - ).geturl() - - return resolved, rev - - def debug(self, msg, *args) -> None: - log.debug(msg, *args) - - def info(self, msg, *args) -> None: - log.info(msg, *args) - - def warn(self, msg, *args) -> None: - log.warn(msg, *args) - - -# This pattern matches a character entity reference (a decimal numeric -# references, a hexadecimal numeric reference, or a named reference). -entity_sub = re.compile(r'&(#(\d+|x[\da-fA-F]+)|[\w.:-]+);?').sub - - -def decode_entity(match): - what = match.group(0) - return html.unescape(what) - - -def htmldecode(text): - """ - Decode HTML entities in the given text. - - >>> htmldecode( - ... 'https://../package_name-0.1.2.tar.gz' - ... '?tokena=A&tokenb=B">package_name-0.1.2.tar.gz') - 'https://../package_name-0.1.2.tar.gz?tokena=A&tokenb=B">package_name-0.1.2.tar.gz' - """ - return entity_sub(decode_entity, text) - - -def socket_timeout(timeout=15): - def _socket_timeout(func): - def _socket_timeout(*args, **kwargs): - old_timeout = socket.getdefaulttimeout() - socket.setdefaulttimeout(timeout) - try: - return func(*args, **kwargs) - finally: - socket.setdefaulttimeout(old_timeout) - - return _socket_timeout - - return _socket_timeout - - -def _encode_auth(auth): - """ - Encode auth from a URL suitable for an HTTP header. - >>> str(_encode_auth('username%3Apassword')) - 'dXNlcm5hbWU6cGFzc3dvcmQ=' - - Long auth strings should not cause a newline to be inserted. - >>> long_auth = 'username:' + 'password'*10 - >>> chr(10) in str(_encode_auth(long_auth)) - False - """ - auth_s = urllib.parse.unquote(auth) - # convert to bytes - auth_bytes = auth_s.encode() - encoded_bytes = base64.b64encode(auth_bytes) - # convert back to a string - encoded = encoded_bytes.decode() - # strip the trailing carriage return - return encoded.replace('\n', '') - - -class Credential(NamedTuple): - """ - A username/password pair. - - Displayed separated by `:`. - >>> str(Credential('username', 'password')) - 'username:password' - """ - - username: str - password: str - - def __str__(self) -> str: - return f'{self.username}:{self.password}' - - -class PyPIConfig(configparser.RawConfigParser): - def __init__(self): - """ - Load from ~/.pypirc - """ - defaults = dict.fromkeys(['username', 'password', 'repository'], '') - super().__init__(defaults) - - rc = os.path.join(os.path.expanduser('~'), '.pypirc') - if os.path.exists(rc): - _cfg_read_utf8_with_fallback(self, rc) - - @property - def creds_by_repository(self): - sections_with_repositories = [ - section - for section in self.sections() - if self.get(section, 'repository').strip() - ] - - return dict(map(self._get_repo_cred, sections_with_repositories)) - - def _get_repo_cred(self, section): - repo = self.get(section, 'repository').strip() - return repo, Credential( - self.get(section, 'username').strip(), - self.get(section, 'password').strip(), - ) - - def find_credential(self, url): - """ - If the URL indicated appears to be a repository defined in this - config, return the credential for that repository. - """ - for repository, cred in self.creds_by_repository.items(): - if url.startswith(repository): - return cred - return None - - -def open_with_auth(url, opener=urllib.request.urlopen): - """Open a urllib2 request, handling HTTP authentication""" - - parsed = urllib.parse.urlparse(url) - scheme, netloc, path, params, query, frag = parsed - - # Double scheme does not raise on macOS as revealed by a - # failing test. We would expect "nonnumeric port". Refs #20. - if netloc.endswith(':'): - raise http.client.InvalidURL("nonnumeric port: ''") - - if scheme in ('http', 'https'): - auth, address = _splituser(netloc) - else: - auth, address = (None, None) - - if not auth: - cred = PyPIConfig().find_credential(url) - if cred: - auth = str(cred) - info = cred.username, url - log.info('Authenticating as %s for %s (from .pypirc)', *info) - - if auth: - auth = "Basic " + _encode_auth(auth) - parts = scheme, address, path, params, query, frag - new_url = urllib.parse.urlunparse(parts) - request = urllib.request.Request(new_url) - request.add_header("Authorization", auth) - else: - request = urllib.request.Request(url) - - request.add_header('User-Agent', user_agent) - fp = opener(request) - - if auth: - # Put authentication info back into request URL if same host, - # so that links found on the page will work - s2, h2, path2, param2, query2, frag2 = urllib.parse.urlparse(fp.url) - if s2 == scheme and h2 == address: - parts = s2, netloc, path2, param2, query2, frag2 - fp.url = urllib.parse.urlunparse(parts) - - return fp - - -# copy of urllib.parse._splituser from Python 3.8 -# See https://github.com/python/cpython/issues/80072. -def _splituser(host): - """splituser('user[:passwd]@host[:port]') - --> 'user[:passwd]', 'host[:port]'.""" - user, delim, host = host.rpartition('@') - return (user if delim else None), host - - -# adding a timeout to avoid freezing package_index -open_with_auth = socket_timeout(_SOCKET_TIMEOUT)(open_with_auth) - - -def fix_sf_url(url): - return url # backward compatibility - - -def local_open(url): - """Read a local path, with special support for directories""" - _scheme, _server, path, _param, _query, _frag = urllib.parse.urlparse(url) - filename = urllib.request.url2pathname(path) - if os.path.isfile(filename): - return urllib.request.urlopen(url) - elif path.endswith('/') and os.path.isdir(filename): - files = [] - for f in os.listdir(filename): - filepath = os.path.join(filename, f) - if f == 'index.html': - body = _read_utf8_with_fallback(filepath) - break - elif os.path.isdir(filepath): - f += '/' - files.append(f'{f}') - else: - tmpl = "{url}{files}" - body = tmpl.format(url=url, files='\n'.join(files)) - status, message = 200, "OK" - else: - status, message, body = 404, "Path not found", "Not found" - - headers = {'content-type': 'text/html'} - body_stream = io.StringIO(body) - return urllib.error.HTTPError(url, status, message, headers, body_stream) diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index a5472984b5..ac9ce74047 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -1,13 +1,19 @@ import contextlib +import io import os import subprocess import sys +import tarfile +import time from pathlib import Path import path import pytest +from setuptools._normalization import safer_name + from . import contexts, environment +from .textwrap import DALS @pytest.fixture @@ -155,3 +161,183 @@ def bare_venv(tmp_path): env.create_opts = ['--no-setuptools', '--no-pip', '--no-wheel', '--no-seed'] env.ensure_env() return env + + +def make_sdist(dist_path, files): + """ + Create a simple sdist tarball at dist_path, containing the files + listed in ``files`` as ``(filename, content)`` tuples. + """ + + # Distributions with only one file don't play well with pip. + assert len(files) > 1 + with tarfile.open(dist_path, 'w:gz') as dist: + for filename, content in files: + file_bytes = io.BytesIO(content.encode('utf-8')) + file_info = tarfile.TarInfo(name=filename) + file_info.size = len(file_bytes.getvalue()) + file_info.mtime = int(time.time()) + dist.addfile(file_info, fileobj=file_bytes) + + +def make_trivial_sdist(dist_path, distname, version): + """ + Create a simple sdist tarball at dist_path, containing just a simple + setup.py. + """ + + make_sdist( + dist_path, + [ + ( + 'setup.py', + DALS( + f"""\ + import setuptools + setuptools.setup( + name={distname!r}, + version={version!r} + ) + """ + ), + ), + ('setup.cfg', ''), + ], + ) + + +def make_nspkg_sdist(dist_path, distname, version): + """ + Make an sdist tarball with distname and version which also contains one + package with the same name as distname. The top-level package is + designated a namespace package). + """ + # Assert that the distname contains at least one period + assert '.' in distname + + parts = distname.split('.') + nspackage = parts[0] + + packages = ['.'.join(parts[:idx]) for idx in range(1, len(parts) + 1)] + + setup_py = DALS( + f"""\ + import setuptools + setuptools.setup( + name={distname!r}, + version={version!r}, + packages={packages!r}, + namespace_packages=[{nspackage!r}] + ) + """ + ) + + init = "__import__('pkg_resources').declare_namespace(__name__)" + + files = [('setup.py', setup_py), (os.path.join(nspackage, '__init__.py'), init)] + for package in packages[1:]: + filename = os.path.join(*(package.split('.') + ['__init__.py'])) + files.append((filename, '')) + + make_sdist(dist_path, files) + + +def make_python_requires_sdist(dist_path, distname, version, python_requires): + make_sdist( + dist_path, + [ + ( + 'setup.py', + DALS( + """\ + import setuptools + setuptools.setup( + name={name!r}, + version={version!r}, + python_requires={python_requires!r}, + ) + """ + ).format( + name=distname, version=version, python_requires=python_requires + ), + ), + ('setup.cfg', ''), + ], + ) + + +def create_setup_requires_package( + path, + distname='foobar', + version='0.1', + make_package=make_trivial_sdist, + setup_py_template=None, + setup_attrs=None, + use_setup_cfg=(), +): + """Creates a source tree under path for a trivial test package that has a + single requirement in setup_requires--a tarball for that requirement is + also created and added to the dependency_links argument. + + ``distname`` and ``version`` refer to the name/version of the package that + the test package requires via ``setup_requires``. The name of the test + package itself is just 'test_pkg'. + """ + + normalized_distname = safer_name(distname) + test_setup_attrs = { + 'name': 'test_pkg', + 'version': '0.0', + 'setup_requires': [f'{normalized_distname}=={version}'], + 'dependency_links': [os.path.abspath(path)], + } + if setup_attrs: + test_setup_attrs.update(setup_attrs) + + test_pkg = os.path.join(path, 'test_pkg') + os.mkdir(test_pkg) + + # setup.cfg + if use_setup_cfg: + options = [] + metadata = [] + for name in use_setup_cfg: + value = test_setup_attrs.pop(name) + if name in 'name version'.split(): + section = metadata + else: + section = options + if isinstance(value, (tuple, list)): + value = ';'.join(value) + section.append(f'{name}: {value}') + test_setup_cfg_contents = DALS( + """ + [metadata] + {metadata} + [options] + {options} + """ + ).format( + options='\n'.join(options), + metadata='\n'.join(metadata), + ) + else: + test_setup_cfg_contents = '' + with open(os.path.join(test_pkg, 'setup.cfg'), 'w', encoding="utf-8") as f: + f.write(test_setup_cfg_contents) + + # setup.py + if setup_py_template is None: + setup_py_template = DALS( + """\ + import setuptools + setuptools.setup(**%r) + """ + ) + with open(os.path.join(test_pkg, 'setup.py'), 'w', encoding="utf-8") as f: + f.write(setup_py_template % test_setup_attrs) + + foobar_path = os.path.join(path, f'{normalized_distname}-{version}.tar.gz') + make_package(foobar_path, distname, version) + + return test_pkg diff --git a/setuptools/tests/test_dist.py b/setuptools/tests/test_dist.py index e65ab310e7..011351f57e 100644 --- a/setuptools/tests/test_dist.py +++ b/setuptools/tests/test_dist.py @@ -8,7 +8,7 @@ from setuptools import Distribution from setuptools.dist import check_package_data, check_specifier -from .test_easy_install import make_trivial_sdist +from .fixtures import make_trivial_sdist from .test_find_packages import ensure_files from .textwrap import DALS diff --git a/setuptools/tests/test_easy_install.py b/setuptools/tests/test_easy_install.py deleted file mode 100644 index e5fb3276f5..0000000000 --- a/setuptools/tests/test_easy_install.py +++ /dev/null @@ -1,703 +0,0 @@ -"""Easy install Tests""" - -import io -import itertools -import logging -import os -import re -import site -import sys -import tarfile -import tempfile -import time -import warnings -import zipfile -from typing import NamedTuple -from unittest import mock - -import pytest - -import pkg_resources -import setuptools.command.easy_install as ei -from pkg_resources import Distribution as PRDistribution, normalize_path -from setuptools._normalization import safer_name -from setuptools.command.easy_install import PthDistributions -from setuptools.dist import Distribution - -from .textwrap import DALS - -import distutils.errors - - -@pytest.fixture(autouse=True) -def pip_disable_index(monkeypatch): - """ - Important: Disable the default index for pip to avoid - querying packages in the index and potentially resolving - and installing packages there. - """ - monkeypatch.setenv('PIP_NO_INDEX', 'true') - - -class FakeDist: - def get_entry_map(self, group): - if group != 'console_scripts': - return {} - return {'name': 'ep'} - - def as_requirement(self): - return 'spec' - - -SETUP_PY = DALS( - """ - from setuptools import setup - - setup() - """ -) - - -class TestEasyInstallTest: - def test_get_script_args(self): - header = ei.CommandSpec.best().from_environment().as_header() - dist = FakeDist() - args = next(ei.ScriptWriter.get_args(dist)) - _name, script = itertools.islice(args, 2) - assert script.startswith(header) - assert "'spec'" in script - assert "'console_scripts'" in script - assert "'name'" in script - assert re.search('^# EASY-INSTALL-ENTRY-SCRIPT', script, flags=re.MULTILINE) - - def test_no_find_links(self): - # new option '--no-find-links', that blocks find-links added at - # the project level - dist = Distribution() - cmd = ei.easy_install(dist) - cmd.check_pth_processing = lambda: True - cmd.no_find_links = True - cmd.find_links = ['link1', 'link2'] - cmd.install_dir = os.path.join(tempfile.mkdtemp(), 'ok') - cmd.args = ['ok'] - cmd.ensure_finalized() - assert cmd.package_index.scanned_urls == {} - - # let's try without it (default behavior) - cmd = ei.easy_install(dist) - cmd.check_pth_processing = lambda: True - cmd.find_links = ['link1', 'link2'] - cmd.install_dir = os.path.join(tempfile.mkdtemp(), 'ok') - cmd.args = ['ok'] - cmd.ensure_finalized() - keys = sorted(cmd.package_index.scanned_urls.keys()) - assert keys == ['link1', 'link2'] - - def test_write_exception(self): - """ - Test that `cant_write_to_target` is rendered as a DistutilsError. - """ - dist = Distribution() - cmd = ei.easy_install(dist) - cmd.install_dir = os.getcwd() - with pytest.raises(distutils.errors.DistutilsError): - cmd.cant_write_to_target() - - def test_all_site_dirs(self, monkeypatch): - """ - get_site_dirs should always return site dirs reported by - site.getsitepackages. - """ - path = normalize_path('/setuptools/test/site-packages') - - def mock_gsp(): - return [path] - - monkeypatch.setattr(site, 'getsitepackages', mock_gsp, raising=False) - assert path in ei.get_site_dirs() - - def test_all_site_dirs_works_without_getsitepackages(self, monkeypatch): - monkeypatch.delattr(site, 'getsitepackages', raising=False) - assert ei.get_site_dirs() - - @pytest.fixture - def sdist_unicode(self, tmpdir): - files = [ - ( - 'setup.py', - DALS( - """ - import setuptools - setuptools.setup( - name="setuptools-test-unicode", - version="1.0", - packages=["mypkg"], - include_package_data=True, - ) - """ - ), - ), - ( - 'mypkg/__init__.py', - "", - ), - ( - 'mypkg/☃.txt', - "", - ), - ] - sdist_name = 'setuptools-test-unicode-1.0.zip' - sdist = tmpdir / sdist_name - # can't use make_sdist, because the issue only occurs - # with zip sdists. - sdist_zip = zipfile.ZipFile(str(sdist), 'w') - for filename, content in files: - sdist_zip.writestr(filename, content) - sdist_zip.close() - return str(sdist) - - @pytest.fixture - def sdist_unicode_in_script(self, tmpdir): - files = [ - ( - "setup.py", - DALS( - """ - import setuptools - setuptools.setup( - name="setuptools-test-unicode", - version="1.0", - packages=["mypkg"], - include_package_data=True, - scripts=['mypkg/unicode_in_script'], - ) - """ - ), - ), - ("mypkg/__init__.py", ""), - ( - "mypkg/unicode_in_script", - DALS( - """ - #!/bin/sh - # á - - non_python_fn() { - } - """ - ), - ), - ] - sdist_name = "setuptools-test-unicode-script-1.0.zip" - sdist = tmpdir / sdist_name - # can't use make_sdist, because the issue only occurs - # with zip sdists. - sdist_zip = zipfile.ZipFile(str(sdist), "w") - for filename, content in files: - sdist_zip.writestr(filename, content.encode('utf-8')) - sdist_zip.close() - return str(sdist) - - @pytest.fixture - def sdist_script(self, tmpdir): - files = [ - ( - 'setup.py', - DALS( - """ - import setuptools - setuptools.setup( - name="setuptools-test-script", - version="1.0", - scripts=["mypkg_script"], - ) - """ - ), - ), - ( - 'mypkg_script', - DALS( - """ - #/usr/bin/python - print('mypkg_script') - """ - ), - ), - ] - sdist_name = 'setuptools-test-script-1.0.zip' - sdist = str(tmpdir / sdist_name) - make_sdist(sdist, files) - return sdist - - -@pytest.mark.filterwarnings('ignore:Unbuilt egg') -class TestPTHFileWriter: - def test_add_from_cwd_site_sets_dirty(self): - """a pth file manager should set dirty - if a distribution is in site but also the cwd - """ - pth = PthDistributions('does-not_exist', [os.getcwd()]) - assert not pth.dirty - pth.add(PRDistribution(os.getcwd())) - assert pth.dirty - - def test_add_from_site_is_ignored(self): - location = '/test/location/does-not-have-to-exist' - # PthDistributions expects all locations to be normalized - location = pkg_resources.normalize_path(location) - pth = PthDistributions( - 'does-not_exist', - [ - location, - ], - ) - assert not pth.dirty - pth.add(PRDistribution(location)) - assert not pth.dirty - - def test_many_pth_distributions_merge_together(self, tmpdir): - """ - If the pth file is modified under the hood, then PthDistribution - will refresh its content before saving, merging contents when - necessary. - """ - # putting the pth file in a dedicated sub-folder, - pth_subdir = tmpdir.join("pth_subdir") - pth_subdir.mkdir() - pth_path = str(pth_subdir.join("file1.pth")) - pth1 = PthDistributions(pth_path) - pth2 = PthDistributions(pth_path) - assert pth1.paths == pth2.paths == [], ( - "unless there would be some default added at some point" - ) - # and so putting the src_subdir in folder distinct than the pth one, - # so to keep it absolute by PthDistributions - new_src_path = tmpdir.join("src_subdir") - new_src_path.mkdir() # must exist to be accounted - new_src_path_str = str(new_src_path) - pth1.paths.append(new_src_path_str) - pth1.save() - assert pth1.paths, ( - "the new_src_path added must still be present/valid in pth1 after save" - ) - # now, - assert new_src_path_str not in pth2.paths, ( - "right before we save the entry should still not be present" - ) - pth2.save() - assert new_src_path_str in pth2.paths, ( - "the new_src_path entry should have been added by pth2 with its save() call" - ) - assert pth2.paths[-1] == new_src_path, ( - "and it should match exactly on the last entry actually " - "given we append to it in save()" - ) - # finally, - assert PthDistributions(pth_path).paths == pth2.paths, ( - "and we should have the exact same list at the end " - "with a fresh PthDistributions instance" - ) - - -@pytest.fixture -def setup_context(tmpdir): - with (tmpdir / 'setup.py').open('w', encoding="utf-8") as f: - f.write(SETUP_PY) - with tmpdir.as_cwd(): - yield tmpdir - - -@pytest.mark.usefixtures("user_override") -@pytest.mark.usefixtures("setup_context") -class TestUserInstallTest: - # prevent check that site-packages is writable. easy_install - # shouldn't be writing to system site-packages during finalize - # options, but while it does, bypass the behavior. - prev_sp_write = mock.patch( - 'setuptools.command.easy_install.easy_install.check_site_dir', - mock.Mock(), - ) - - # simulate setuptools installed in user site packages - @mock.patch('setuptools.command.easy_install.__file__', site.USER_SITE) - @mock.patch('site.ENABLE_USER_SITE', True) - @prev_sp_write - def test_user_install_not_implied_user_site_enabled(self): - self.assert_not_user_site() - - @mock.patch('site.ENABLE_USER_SITE', False) - @prev_sp_write - def test_user_install_not_implied_user_site_disabled(self): - self.assert_not_user_site() - - @staticmethod - def assert_not_user_site(): - # create a finalized easy_install command - dist = Distribution() - dist.script_name = 'setup.py' - cmd = ei.easy_install(dist) - cmd.args = ['py'] - cmd.ensure_finalized() - assert not cmd.user, 'user should not be implied' - - def test_multiproc_atexit(self): - pytest.importorskip('multiprocessing') - - log = logging.getLogger('test_easy_install') - logging.basicConfig(level=logging.INFO, stream=sys.stderr) - log.info('this should not break') - - @pytest.fixture - def foo_package(self, tmpdir): - egg_file = tmpdir / 'foo-1.0.egg-info' - with egg_file.open('w') as f: - f.write('Name: foo\n') - return str(tmpdir) - - @pytest.fixture - def install_target(self, tmpdir): - target = str(tmpdir) - with mock.patch('sys.path', sys.path + [target]): - python_path = os.path.pathsep.join(sys.path) - with mock.patch.dict(os.environ, PYTHONPATH=python_path): - yield target - - def test_local_index(self, foo_package, install_target): - """ - The local index must be used when easy_install locates installed - packages. - """ - dist = Distribution() - dist.script_name = 'setup.py' - cmd = ei.easy_install(dist) - cmd.install_dir = install_target - cmd.args = ['foo'] - cmd.ensure_finalized() - cmd.local_index.scan([foo_package]) - res = cmd.easy_install('foo') - actual = os.path.normcase(os.path.realpath(res.location)) - expected = os.path.normcase(os.path.realpath(foo_package)) - assert actual == expected - - -def make_trivial_sdist(dist_path, distname, version): - """ - Create a simple sdist tarball at dist_path, containing just a simple - setup.py. - """ - - make_sdist( - dist_path, - [ - ( - 'setup.py', - DALS( - f"""\ - import setuptools - setuptools.setup( - name={distname!r}, - version={version!r} - ) - """ - ), - ), - ('setup.cfg', ''), - ], - ) - - -def make_nspkg_sdist(dist_path, distname, version): - """ - Make an sdist tarball with distname and version which also contains one - package with the same name as distname. The top-level package is - designated a namespace package). - """ - # Assert that the distname contains at least one period - assert '.' in distname - - parts = distname.split('.') - nspackage = parts[0] - - packages = ['.'.join(parts[:idx]) for idx in range(1, len(parts) + 1)] - - setup_py = DALS( - f"""\ - import setuptools - setuptools.setup( - name={distname!r}, - version={version!r}, - packages={packages!r}, - namespace_packages=[{nspackage!r}] - ) - """ - ) - - init = "__import__('pkg_resources').declare_namespace(__name__)" - - files = [('setup.py', setup_py), (os.path.join(nspackage, '__init__.py'), init)] - for package in packages[1:]: - filename = os.path.join(*(package.split('.') + ['__init__.py'])) - files.append((filename, '')) - - make_sdist(dist_path, files) - - -def make_python_requires_sdist(dist_path, distname, version, python_requires): - make_sdist( - dist_path, - [ - ( - 'setup.py', - DALS( - """\ - import setuptools - setuptools.setup( - name={name!r}, - version={version!r}, - python_requires={python_requires!r}, - ) - """ - ).format( - name=distname, version=version, python_requires=python_requires - ), - ), - ('setup.cfg', ''), - ], - ) - - -def make_sdist(dist_path, files): - """ - Create a simple sdist tarball at dist_path, containing the files - listed in ``files`` as ``(filename, content)`` tuples. - """ - - # Distributions with only one file don't play well with pip. - assert len(files) > 1 - with tarfile.open(dist_path, 'w:gz') as dist: - for filename, content in files: - file_bytes = io.BytesIO(content.encode('utf-8')) - file_info = tarfile.TarInfo(name=filename) - file_info.size = len(file_bytes.getvalue()) - file_info.mtime = int(time.time()) - dist.addfile(file_info, fileobj=file_bytes) - - -def create_setup_requires_package( - path, - distname='foobar', - version='0.1', - make_package=make_trivial_sdist, - setup_py_template=None, - setup_attrs=None, - use_setup_cfg=(), -): - """Creates a source tree under path for a trivial test package that has a - single requirement in setup_requires--a tarball for that requirement is - also created and added to the dependency_links argument. - - ``distname`` and ``version`` refer to the name/version of the package that - the test package requires via ``setup_requires``. The name of the test - package itself is just 'test_pkg'. - """ - - normalized_distname = safer_name(distname) - test_setup_attrs = { - 'name': 'test_pkg', - 'version': '0.0', - 'setup_requires': [f'{normalized_distname}=={version}'], - 'dependency_links': [os.path.abspath(path)], - } - if setup_attrs: - test_setup_attrs.update(setup_attrs) - - test_pkg = os.path.join(path, 'test_pkg') - os.mkdir(test_pkg) - - # setup.cfg - if use_setup_cfg: - options = [] - metadata = [] - for name in use_setup_cfg: - value = test_setup_attrs.pop(name) - if name in 'name version'.split(): - section = metadata - else: - section = options - if isinstance(value, (tuple, list)): - value = ';'.join(value) - section.append(f'{name}: {value}') - test_setup_cfg_contents = DALS( - """ - [metadata] - {metadata} - [options] - {options} - """ - ).format( - options='\n'.join(options), - metadata='\n'.join(metadata), - ) - else: - test_setup_cfg_contents = '' - with open(os.path.join(test_pkg, 'setup.cfg'), 'w', encoding="utf-8") as f: - f.write(test_setup_cfg_contents) - - # setup.py - if setup_py_template is None: - setup_py_template = DALS( - """\ - import setuptools - setuptools.setup(**%r) - """ - ) - with open(os.path.join(test_pkg, 'setup.py'), 'w', encoding="utf-8") as f: - f.write(setup_py_template % test_setup_attrs) - - foobar_path = os.path.join(path, f'{normalized_distname}-{version}.tar.gz') - make_package(foobar_path, distname, version) - - return test_pkg - - -@pytest.mark.skipif( - sys.platform.startswith('java') and ei.is_sh(sys.executable), - reason="Test cannot run under java when executable is sh", -) -class TestScriptHeader: - non_ascii_exe = '/Users/José/bin/python' - exe_with_spaces = r'C:\Program Files\Python36\python.exe' - - def test_get_script_header(self): - expected = f'#!{ei.nt_quote_arg(os.path.normpath(sys.executable))}\n' - actual = ei.ScriptWriter.get_header('#!/usr/local/bin/python') - assert actual == expected - - def test_get_script_header_args(self): - expected = f'#!{ei.nt_quote_arg(os.path.normpath(sys.executable))} -x\n' - actual = ei.ScriptWriter.get_header('#!/usr/bin/python -x') - assert actual == expected - - def test_get_script_header_non_ascii_exe(self): - actual = ei.ScriptWriter.get_header( - '#!/usr/bin/python', executable=self.non_ascii_exe - ) - expected = f'#!{self.non_ascii_exe} -x\n' - assert actual == expected - - def test_get_script_header_exe_with_spaces(self): - actual = ei.ScriptWriter.get_header( - '#!/usr/bin/python', executable='"' + self.exe_with_spaces + '"' - ) - expected = f'#!"{self.exe_with_spaces}"\n' - assert actual == expected - - -class TestCommandSpec: - def test_custom_launch_command(self): - """ - Show how a custom CommandSpec could be used to specify a #! executable - which takes parameters. - """ - cmd = ei.CommandSpec(['/usr/bin/env', 'python3']) - assert cmd.as_header() == '#!/usr/bin/env python3\n' - - def test_from_param_for_CommandSpec_is_passthrough(self): - """ - from_param should return an instance of a CommandSpec - """ - cmd = ei.CommandSpec(['python']) - cmd_new = ei.CommandSpec.from_param(cmd) - assert cmd is cmd_new - - @mock.patch('sys.executable', TestScriptHeader.exe_with_spaces) - @mock.patch.dict(os.environ) - def test_from_environment_with_spaces_in_executable(self): - os.environ.pop('__PYVENV_LAUNCHER__', None) - cmd = ei.CommandSpec.from_environment() - assert len(cmd) == 1 - assert cmd.as_header().startswith('#!"') - - def test_from_simple_string_uses_shlex(self): - """ - In order to support `executable = /usr/bin/env my-python`, make sure - from_param invokes shlex on that input. - """ - cmd = ei.CommandSpec.from_param('/usr/bin/env my-python') - assert len(cmd) == 2 - assert '"' not in cmd.as_header() - - def test_from_param_raises_expected_error(self) -> None: - """ - from_param should raise its own TypeError when the argument's type is unsupported - """ - with pytest.raises(TypeError) as exc_info: - ei.CommandSpec.from_param(object()) # type: ignore[arg-type] # We want a type error here - assert ( - str(exc_info.value) == "Argument has an unsupported type " - ), exc_info.value - - -class TestWindowsScriptWriter: - def test_header(self): - hdr = ei.WindowsScriptWriter.get_header('') - assert hdr.startswith('#!') - assert hdr.endswith('\n') - hdr = hdr.lstrip('#!') - hdr = hdr.rstrip('\n') - # header should not start with an escaped quote - assert not hdr.startswith('\\"') - - -class VersionStub(NamedTuple): - major: int - minor: int - micro: int - releaselevel: str - serial: int - - -def test_use_correct_python_version_string(tmpdir, tmpdir_cwd, monkeypatch): - # In issue #3001, easy_install wrongly uses the `python3.1` directory - # when the interpreter is `python3.10` and the `--user` option is given. - # See pypa/setuptools#3001. - dist = Distribution() - cmd = dist.get_command_obj('easy_install') - cmd.args = ['ok'] - cmd.optimize = 0 - cmd.user = True - cmd.install_userbase = str(tmpdir) - cmd.install_usersite = None - install_cmd = dist.get_command_obj('install') - install_cmd.install_userbase = str(tmpdir) - install_cmd.install_usersite = None - - with monkeypatch.context() as patch, warnings.catch_warnings(): - warnings.simplefilter("ignore") - version = '3.10.1 (main, Dec 21 2021, 09:17:12) [GCC 10.2.1 20210110]' - info = VersionStub(3, 10, 1, "final", 0) - patch.setattr('site.ENABLE_USER_SITE', True) - patch.setattr('sys.version', version) - patch.setattr('sys.version_info', info) - patch.setattr(cmd, 'create_home_path', mock.Mock()) - cmd.finalize_options() - - name = "pypy" if hasattr(sys, 'pypy_version_info') else "python" - install_dir = cmd.install_dir.lower() - - # In some platforms (e.g. Windows), install_dir is mostly determined - # via `sysconfig`, which define constants eagerly at module creation. - # This means that monkeypatching `sys.version` to emulate 3.10 for testing - # may have no effect. - # The safest test here is to rely on the fact that 3.1 is no longer - # supported/tested, and make sure that if 'python3.1' ever appears in the string - # it is followed by another digit (e.g. 'python3.10'). - if re.search(name + r'3\.?1', install_dir): - assert re.search(name + r'3\.?1\d', install_dir) - - # The following "variables" are used for interpolation in distutils - # installation schemes, so it should be fair to treat them as "semi-public", - # or at least public enough so we can have a test to make sure they are correct - assert cmd.config_vars['py_version'] == '3.10.1' - assert cmd.config_vars['py_version_short'] == '3.10' - assert cmd.config_vars['py_version_nodot'] == '310' diff --git a/setuptools/tests/test_packageindex.py b/setuptools/tests/test_packageindex.py deleted file mode 100644 index 2a6e5917a8..0000000000 --- a/setuptools/tests/test_packageindex.py +++ /dev/null @@ -1,267 +0,0 @@ -import http.client -import re -import urllib.error -import urllib.request -from inspect import cleandoc - -import pytest - -import setuptools.package_index - -import distutils.errors - - -class TestPackageIndex: - def test_regex(self): - hash_url = 'http://other_url?:action=show_md5&' - hash_url += 'digest=0123456789abcdef0123456789abcdef' - doc = """ - Name - (md5) - """.lstrip().format(**locals()) - assert setuptools.package_index.PYPI_MD5.match(doc) - - def test_bad_url_bad_port(self): - index = setuptools.package_index.PackageIndex() - url = 'http://127.0.0.1:0/nonesuch/test_package_index' - with pytest.raises(Exception, match=re.escape(url)): - v = index.open_url(url) - assert isinstance(v, urllib.error.HTTPError) - - def test_bad_url_typo(self): - # issue 16 - # easy_install inquant.contentmirror.plone breaks because of a typo - # in its home URL - index = setuptools.package_index.PackageIndex(hosts=('www.example.com',)) - - url = 'url:%20https://svn.plone.org/svn/collective/inquant.contentmirror.plone/trunk' - - with pytest.raises(Exception, match=re.escape(url)): - v = index.open_url(url) - assert isinstance(v, urllib.error.HTTPError) - - def test_bad_url_bad_status_line(self): - index = setuptools.package_index.PackageIndex(hosts=('www.example.com',)) - - def _urlopen(*args): - raise http.client.BadStatusLine('line') - - index.opener = _urlopen - url = 'http://example.com' - with pytest.raises(Exception, match=r'line'): - index.open_url(url) - - def test_bad_url_double_scheme(self): - """ - A bad URL with a double scheme should raise a DistutilsError. - """ - index = setuptools.package_index.PackageIndex(hosts=('www.example.com',)) - - # issue 20 - url = 'http://http://svn.pythonpaste.org/Paste/wphp/trunk' - try: - index.open_url(url) - except distutils.errors.DistutilsError as error: - msg = str(error) - assert ( - 'nonnumeric port' in msg - or 'getaddrinfo failed' in msg - or 'Name or service not known' in msg - ) - return - raise RuntimeError("Did not raise") - - def test_url_ok(self): - index = setuptools.package_index.PackageIndex(hosts=('www.example.com',)) - url = 'file:///tmp/test_package_index' - assert index.url_ok(url, True) - - def test_parse_bdist_wininst(self): - parse = setuptools.package_index.parse_bdist_wininst - - actual = parse('reportlab-2.5.win32-py2.4.exe') - expected = 'reportlab-2.5', '2.4', 'win32' - assert actual == expected - - actual = parse('reportlab-2.5.win32.exe') - expected = 'reportlab-2.5', None, 'win32' - assert actual == expected - - actual = parse('reportlab-2.5.win-amd64-py2.7.exe') - expected = 'reportlab-2.5', '2.7', 'win-amd64' - assert actual == expected - - actual = parse('reportlab-2.5.win-amd64.exe') - expected = 'reportlab-2.5', None, 'win-amd64' - assert actual == expected - - def test__vcs_split_rev_from_url(self): - """ - Test the basic usage of _vcs_split_rev_from_url - """ - vsrfu = setuptools.package_index.PackageIndex._vcs_split_rev_from_url - url, rev = vsrfu('https://example.com/bar@2995') - assert url == 'https://example.com/bar' - assert rev == '2995' - - def test_local_index(self, tmpdir): - """ - local_open should be able to read an index from the file system. - """ - index_file = tmpdir / 'index.html' - with index_file.open('w') as f: - f.write('
content
') - url = 'file:' + urllib.request.pathname2url(str(tmpdir)) + '/' - res = setuptools.package_index.local_open(url) - assert 'content' in res.read() - - def test_egg_fragment(self): - """ - EGG fragments must comply to PEP 440 - """ - epoch = [ - '', - '1!', - ] - releases = [ - '0', - '0.0', - '0.0.0', - ] - pre = [ - 'a0', - 'b0', - 'rc0', - ] - post = ['.post0'] - dev = [ - '.dev0', - ] - local = [ - ('', ''), - ('+ubuntu.0', '+ubuntu.0'), - ('+ubuntu-0', '+ubuntu.0'), - ('+ubuntu_0', '+ubuntu.0'), - ] - versions = [ - [''.join([e, r, p, loc]) for loc in locs] - for e in epoch - for r in releases - for p in sum([pre, post, dev], ['']) - for locs in local - ] - for v, vc in versions: - dists = list( - setuptools.package_index.distros_for_url( - 'http://example.com/example-foo.zip#egg=example-foo-' + v - ) - ) - assert dists[0].version == '' - assert dists[1].version == vc - - def test_download_git_with_rev(self, tmp_path, fp): - url = 'git+https://github.example/group/project@master#egg=foo' - index = setuptools.package_index.PackageIndex() - - expected_dir = tmp_path / 'project@master' - fp.register([ - 'git', - 'clone', - '--quiet', - 'https://github.example/group/project', - expected_dir, - ]) - fp.register(['git', '-C', expected_dir, 'checkout', '--quiet', 'master']) - - result = index.download(url, tmp_path) - - assert result == str(expected_dir) - assert len(fp.calls) == 2 - - def test_download_git_no_rev(self, tmp_path, fp): - url = 'git+https://github.example/group/project#egg=foo' - index = setuptools.package_index.PackageIndex() - - expected_dir = tmp_path / 'project' - fp.register([ - 'git', - 'clone', - '--quiet', - 'https://github.example/group/project', - expected_dir, - ]) - index.download(url, tmp_path) - - def test_download_svn(self, tmp_path): - url = 'svn+https://svn.example/project#egg=foo' - index = setuptools.package_index.PackageIndex() - - msg = r".*SVN download is not supported.*" - with pytest.raises(distutils.errors.DistutilsError, match=msg): - index.download(url, tmp_path) - - -class TestContentCheckers: - def test_md5(self): - checker = setuptools.package_index.HashChecker.from_url( - 'http://foo/bar#md5=f12895fdffbd45007040d2e44df98478' - ) - checker.feed('You should probably not be using MD5'.encode('ascii')) - assert checker.hash.hexdigest() == 'f12895fdffbd45007040d2e44df98478' - assert checker.is_valid() - - def test_other_fragment(self): - "Content checks should succeed silently if no hash is present" - checker = setuptools.package_index.HashChecker.from_url( - 'http://foo/bar#something%20completely%20different' - ) - checker.feed('anything'.encode('ascii')) - assert checker.is_valid() - - def test_blank_md5(self): - "Content checks should succeed if a hash is empty" - checker = setuptools.package_index.HashChecker.from_url('http://foo/bar#md5=') - checker.feed('anything'.encode('ascii')) - assert checker.is_valid() - - def test_get_hash_name_md5(self): - checker = setuptools.package_index.HashChecker.from_url( - 'http://foo/bar#md5=f12895fdffbd45007040d2e44df98478' - ) - assert checker.hash_name == 'md5' - - def test_report(self): - checker = setuptools.package_index.HashChecker.from_url( - 'http://foo/bar#md5=f12895fdffbd45007040d2e44df98478' - ) - rep = checker.report(lambda x: x, 'My message about %s') - assert rep == 'My message about md5' - - -class TestPyPIConfig: - def test_percent_in_password(self, tmp_home_dir): - pypirc = tmp_home_dir / '.pypirc' - pypirc.write_text( - cleandoc( - """ - [pypi] - repository=https://pypi.org - username=jaraco - password=pity% - """ - ), - encoding="utf-8", - ) - cfg = setuptools.package_index.PyPIConfig() - cred = cfg.creds_by_repository['https://pypi.org'] - assert cred.username == 'jaraco' - assert cred.password == 'pity%' - - -@pytest.mark.timeout(1) -def test_REL_DoS(): - """ - REL should not hang on a contrived attack string. - """ - setuptools.package_index.REL.search('< rel=' + ' ' * 2**12) diff --git a/setuptools/tests/test_scripts.py b/setuptools/tests/test_scripts.py new file mode 100644 index 0000000000..8641f7b639 --- /dev/null +++ b/setuptools/tests/test_scripts.py @@ -0,0 +1,12 @@ +from setuptools import _scripts + + +class TestWindowsScriptWriter: + def test_header(self): + hdr = _scripts.WindowsScriptWriter.get_header('') + assert hdr.startswith('#!') + assert hdr.endswith('\n') + hdr = hdr.lstrip('#!') + hdr = hdr.rstrip('\n') + # header should not start with an escaped quote + assert not hdr.startswith('\\"') diff --git a/setuptools/tests/test_windows_wrappers.py b/setuptools/tests/test_windows_wrappers.py index f895485387..f14338404d 100644 --- a/setuptools/tests/test_windows_wrappers.py +++ b/setuptools/tests/test_windows_wrappers.py @@ -21,7 +21,6 @@ import pytest import pkg_resources -from setuptools.command.easy_install import nt_quote_arg pytestmark = pytest.mark.skipif(sys.platform != 'win32', reason="Windows only") @@ -29,7 +28,7 @@ class WrapperTester: @classmethod def prep_script(cls, template): - python_exe = nt_quote_arg(sys.executable) + python_exe = subprocess.list2cmdline([sys.executable]) return template % locals() @classmethod