Path: blob/main/test/lib/python3.9/site-packages/setuptools/command/install_lib.py
4799 views
import os1import sys2from itertools import product, starmap3import distutils.command.install_lib as orig456class install_lib(orig.install_lib):7"""Don't add compiled flags to filenames of non-Python files"""89def run(self):10self.build()11outfiles = self.install()12if outfiles is not None:13# always compile, in case we have any extension stubs to deal with14self.byte_compile(outfiles)1516def get_exclusions(self):17"""18Return a collections.Sized collections.Container of paths to be19excluded for single_version_externally_managed installations.20"""21all_packages = (22pkg23for ns_pkg in self._get_SVEM_NSPs()24for pkg in self._all_packages(ns_pkg)25)2627excl_specs = product(all_packages, self._gen_exclusion_paths())28return set(starmap(self._exclude_pkg_path, excl_specs))2930def _exclude_pkg_path(self, pkg, exclusion_path):31"""32Given a package name and exclusion path within that package,33compute the full exclusion path.34"""35parts = pkg.split('.') + [exclusion_path]36return os.path.join(self.install_dir, *parts)3738@staticmethod39def _all_packages(pkg_name):40"""41>>> list(install_lib._all_packages('foo.bar.baz'))42['foo.bar.baz', 'foo.bar', 'foo']43"""44while pkg_name:45yield pkg_name46pkg_name, sep, child = pkg_name.rpartition('.')4748def _get_SVEM_NSPs(self):49"""50Get namespace packages (list) but only for51single_version_externally_managed installations and empty otherwise.52"""53# TODO: is it necessary to short-circuit here? i.e. what's the cost54# if get_finalized_command is called even when namespace_packages is55# False?56if not self.distribution.namespace_packages:57return []5859install_cmd = self.get_finalized_command('install')60svem = install_cmd.single_version_externally_managed6162return self.distribution.namespace_packages if svem else []6364@staticmethod65def _gen_exclusion_paths():66"""67Generate file paths to be excluded for namespace packages (bytecode68cache files).69"""70# always exclude the package module itself71yield '__init__.py'7273yield '__init__.pyc'74yield '__init__.pyo'7576if not hasattr(sys, 'implementation'):77return7879base = os.path.join(80'__pycache__', '__init__.' + sys.implementation.cache_tag)81yield base + '.pyc'82yield base + '.pyo'83yield base + '.opt-1.pyc'84yield base + '.opt-2.pyc'8586def copy_tree(87self, infile, outfile,88preserve_mode=1, preserve_times=1, preserve_symlinks=0, level=189):90assert preserve_mode and preserve_times and not preserve_symlinks91exclude = self.get_exclusions()9293if not exclude:94return orig.install_lib.copy_tree(self, infile, outfile)9596# Exclude namespace package __init__.py* files from the output9798from setuptools.archive_util import unpack_directory99from distutils import log100101outfiles = []102103def pf(src, dst):104if dst in exclude:105log.warn("Skipping installation of %s (namespace package)",106dst)107return False108109log.info("copying %s -> %s", src, os.path.dirname(dst))110outfiles.append(dst)111return dst112113unpack_directory(infile, outfile, pf)114return outfiles115116def get_outputs(self):117outputs = orig.install_lib.get_outputs(self)118exclude = self.get_exclusions()119if exclude:120return [f for f in outputs if f not in exclude]121return outputs122123124