Aluode/PerceptionLabPortable
0
1from __future__ import annotations2 3import os4import sys5from itertools import product, starmap6 7from .._path import StrPath8from ..dist import Distribution9 10import distutils.command.install_lib as orig11 12 13class install_lib(orig.install_lib):14 """Don't add compiled flags to filenames of non-Python files"""15 16 distribution: Distribution # override distutils.dist.Distribution with setuptools.dist.Distribution17 18 def run(self) -> None:19 self.build()20 outfiles = self.install()21 if outfiles is not None:22 # always compile, in case we have any extension stubs to deal with23 self.byte_compile(outfiles)24 25 def get_exclusions(self):26 """27 Return a collections.Sized collections.Container of paths to be28 excluded for single_version_externally_managed installations.29 """30 all_packages = (31 pkg32 for ns_pkg in self._get_SVEM_NSPs()33 for pkg in self._all_packages(ns_pkg)34 )35 36 excl_specs = product(all_packages, self._gen_exclusion_paths())37 return set(starmap(self._exclude_pkg_path, excl_specs))38 39 def _exclude_pkg_path(self, pkg, exclusion_path):40 """41 Given a package name and exclusion path within that package,42 compute the full exclusion path.43 """44 parts = pkg.split('.') + [exclusion_path]45 return os.path.join(self.install_dir, *parts)46 47 @staticmethod48 def _all_packages(pkg_name):49 """50 >>> list(install_lib._all_packages('foo.bar.baz'))51 ['foo.bar.baz', 'foo.bar', 'foo']52 """53 while pkg_name:54 yield pkg_name55 pkg_name, _sep, _child = pkg_name.rpartition('.')56 57 def _get_SVEM_NSPs(self):58 """59 Get namespace packages (list) but only for60 single_version_externally_managed installations and empty otherwise.61 """62 # TODO: is it necessary to short-circuit here? i.e. what's the cost63 # if get_finalized_command is called even when namespace_packages is64 # False?65 if not self.distribution.namespace_packages:66 return []67 68 install_cmd = self.get_finalized_command('install')69 svem = install_cmd.single_version_externally_managed70 71 return self.distribution.namespace_packages if svem else []72 73 @staticmethod74 def _gen_exclusion_paths():75 """76 Generate file paths to be excluded for namespace packages (bytecode77 cache files).78 """79 # always exclude the package module itself80 yield '__init__.py'81 82 yield '__init__.pyc'83 yield '__init__.pyo'84 85 if not hasattr(sys, 'implementation'):86 return87 88 base = os.path.join('__pycache__', '__init__.' + sys.implementation.cache_tag)89 yield base + '.pyc'90 yield base + '.pyo'91 yield base + '.opt-1.pyc'92 yield base + '.opt-2.pyc'93 94 def copy_tree(95 self,96 infile: StrPath,97 outfile: str,98 # override: Using actual booleans99 preserve_mode: bool = True, # type: ignore[override]100 preserve_times: bool = True, # type: ignore[override]101 preserve_symlinks: bool = False, # type: ignore[override]102 level: object = 1,103 ) -> list[str]:104 assert preserve_mode105 assert preserve_times106 assert not preserve_symlinks107 exclude = self.get_exclusions()108 109 if not exclude:110 return orig.install_lib.copy_tree(self, infile, outfile)111 112 # Exclude namespace package __init__.py* files from the output113 114 from setuptools.archive_util import unpack_directory115 116 from distutils import log117 118 outfiles: list[str] = []119 120 def pf(src: str, dst: str):121 if dst in exclude:122 log.warn("Skipping installation of %s (namespace package)", dst)123 return False124 125 log.info("copying %s -> %s", src, os.path.dirname(dst))126 outfiles.append(dst)127 return dst128 129 unpack_directory(infile, outfile, pf)130 return outfiles131 132 def get_outputs(self):133 outputs = orig.install_lib.get_outputs(self)134 exclude = self.get_exclusions()135 if exclude:136 return [f for f in outputs if f not in exclude]137 return outputs138 